14 static unsigned const default_iterations = 30;
15 static unsigned const default_L = 100;
16 static unsigned const default_K = 25;
17 static unsigned const default_P = 100;
18 static unsigned const default_M = 0;
19 static unsigned const default_T = 1;
20 static unsigned const default_S = 10;
21 static unsigned const default_R = 100;
22 static unsigned const default_controls = 100;
23 static unsigned const default_seed = 1998;
24 static float const default_delta = 0.002;
25 static float const default_recall = 0.99;
26 static float const default_epsilon = 1e30;
27 static unsigned const default_verbosity = 1;
36 static unsigned const default_prune = 0;
37 static int const default_reverse = REVERSE_NONE;
42 extern unsigned verbosity;
52 virtual unsigned size ()
const = 0;
58 virtual float operator () (
unsigned i,
unsigned j)
const = 0;
69 virtual unsigned size ()
const = 0;
85 unsigned search (
unsigned K,
float epsilon,
unsigned *ids,
float *dists =
nullptr)
const;
108 IndexParams (): iterations(default_iterations), L(default_L), K(default_K), S(default_S), R(default_R), controls(default_controls), seed(default_seed), delta(default_delta), recall(default_recall), prune(default_prune), reverse(default_reverse) {
124 SearchParams (): K(default_K), M(default_M), P(default_P), S(default_S), T(default_T), epsilon(default_epsilon), seed(1998), init(0) {
160 virtual void load (
char const *path) = 0;
165 virtual void save (
char const *path,
int format = FORMAT_DEFAULT)
const = 0;
167 virtual void build (
IndexOracle const &oracle, IndexParams
const ¶ms, IndexInfo *info = 0) = 0;
187 return search(oracle, params, ids,
nullptr, info);
196 virtual unsigned search (
SearchOracle const &oracle, SearchParams
const ¶ms,
unsigned *ids,
float *dists, SearchInfo *info)
const = 0;
206 virtual void get_nn (
unsigned id,
unsigned *nns,
unsigned *M,
unsigned *L)
const {
207 get_nn(
id, nns,
nullptr, M, L);
224 virtual void get_nn (
unsigned id,
unsigned *nns,
float *dists,
unsigned *M,
unsigned *L)
const = 0;
226 virtual void reverse (
int) = 0;
230 #if __cplusplus > 199711L
231 #include <functional>
244 template <
typename CONTAINER_TYPE,
typename OBJECT_TYPE>
245 class VectorOracle:
public IndexOracle {
247 typedef std::function<float(OBJECT_TYPE const &, OBJECT_TYPE const &)> METRIC_TYPE;
249 CONTAINER_TYPE
const &data;
252 class VectorSearchOracle:
public SearchOracle {
253 CONTAINER_TYPE
const &data;
254 OBJECT_TYPE
const query;
257 VectorSearchOracle (CONTAINER_TYPE
const &p, OBJECT_TYPE
const &q, METRIC_TYPE m): data(p), query(q), dist(m) {
259 virtual unsigned size ()
const {
262 virtual float operator () (
unsigned i)
const {
263 return dist(data[i], query);
272 VectorOracle (CONTAINER_TYPE
const &d, METRIC_TYPE m): data(d), dist(m) {
274 virtual unsigned size ()
const {
277 virtual float operator () (
unsigned i,
unsigned j)
const {
278 return dist(data[i], data[j]);
281 VectorSearchOracle query (OBJECT_TYPE
const &q)
const {
282 return VectorSearchOracle(data, q, dist);
286 class invalid_argument:
public std::invalid_argument {
288 using std::invalid_argument::invalid_argument;
291 class runtime_error:
public std::runtime_error {
293 using std::runtime_error::runtime_error;
296 class io_error:
public runtime_error {
298 using runtime_error::runtime_error;
virtual float operator()(unsigned i, unsigned j) const =0
Computes similarity.
unsigned search(unsigned K, float epsilon, unsigned *ids, float *dists=nullptr) const
Search with brutal force.
static KGraph * create()
Constructor.
Information and statistics of the search algorithm.
Definition: kgraph.h:149
Search oracle.
Definition: kgraph.h:66
virtual void prune(IndexOracle const &oracle, unsigned level)=0
Prune the index.
virtual void get_nn(unsigned id, unsigned *nns, unsigned *M, unsigned *L) const
Get offline computed k-NNs of a given object.
Definition: kgraph.h:206
Index oracle.
Definition: kgraph.h:49
virtual unsigned size() const =0
Returns the size of the dataset.
SearchParams()
Construct with default values.
Definition: kgraph.h:124
IndexParams()
Construct with default values.
Definition: kgraph.h:108
virtual void load(char const *path)=0
Load index from file.
static char const * version()
Returns version string.
Search parameters.
Definition: kgraph.h:113
virtual unsigned size() const =0
Returns the size of the dataset.
Information and statistics of the indexing algorithm.
Definition: kgraph.h:134
virtual void build(IndexOracle const &oracle, IndexParams const ¶ms, IndexInfo *info=0)=0
Build the index.
The KGraph index.
Definition: kgraph.h:91
Indexing parameters.
Definition: kgraph.h:94
unsigned search(SearchOracle const &oracle, SearchParams const ¶ms, unsigned *ids, SearchInfo *info=0) const
Online k-NN search.
Definition: kgraph.h:186
virtual void save(char const *path, int format=FORMAT_DEFAULT) const =0
Save index to file.
virtual float operator()(unsigned i) const =0
Computes similarity.