Skip to content

Commit

Permalink
v1.10.0 add expected accuracy parameter for search
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Mar 23, 2020
1 parent 8a7873d commit 5b69650
Show file tree
Hide file tree
Showing 15 changed files with 726 additions and 143 deletions.
18 changes: 12 additions & 6 deletions README-jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,22 @@ Neighborhood Graph and Tree for Indexing High-dimensional Data

- [Releases](https://github.com/yahoojapan/NGT/releases)

### ビルド済み

#### macOS

$ brew install ngt

### ビルド

#### Linux

$ unzip NGT-x.x.x.zip
$ cd NGT-x.x.x
$ mkdir build
$ cd build
$ cd build
$ cmake ..
$ make
$ make
$ make install
$ ldconfig /usr/local/lib

Expand All @@ -59,14 +65,14 @@ Neighborhood Graph and Tree for Indexing High-dimensional Data
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install cmake
$ brew install gcc@9
$ export CXX=/usr/local/bin/g++
$ export CC=/usr/local/bin/gcc
$ export CXX=/usr/local/bin/g++-9
$ export CC=/usr/local/bin/gcc-9
$ unzip NGT-x.x.x.zip
$ cd NGT-x.x.x
$ mkdir build
$ cd build
$ cd build
$ cmake ..
$ make
$ make
$ make install

#### 共有メモリの利用
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,4 @@ Publications
##### [ANNG](bin/ngt/README.md#anng)
- Iwasaki, M.: Proximity search in metric spaces using approximate k nearest neighbor graph (in Japanese). IPSJ Trans. on Database 3(1) (2010) 18-28. ([pdf](https://s.yimg.jp/i/docs/research_lab/articles/miwasaki-ipsj-tod-2010.pdf))

Copyright © 2015-2019 Yahoo Japan Corporation All Rights Reserved.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.1
1.10.0
105 changes: 98 additions & 7 deletions lib/NGT/Capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,18 @@ NGTObjectDistances ngt_create_empty_results(NGTError error) {
}
}

static bool ngt_search_index_(NGT::Index* pindex, NGT::Object *ngtquery, size_t size, float epsilon, float radius, NGTObjectDistances results) {
static bool ngt_search_index_(NGT::Index* pindex, NGT::Object *ngtquery, size_t size, float epsilon, float radius, NGTObjectDistances results, int edge_size = INT_MIN) {
// set search prameters.
NGT::SearchContainer sc(*ngtquery); // search parametera container.

sc.setResults(static_cast<NGT::ObjectDistances*>(results)); // set the result set.
sc.setSize(size); // the number of resultant objects.
sc.setRadius(radius); // search radius.
sc.setEpsilon(epsilon); // set exploration coefficient.

sc.setSize(size); // the number of resultant objects.
sc.setRadius(radius); // search radius.
sc.setEpsilon(epsilon); // set exploration coefficient.
if (edge_size != INT_MIN) {
sc.setEdgeSize(edge_size);// set # of edges for each node
}

pindex->search(sc);

// delete the query object.
Expand Down Expand Up @@ -402,6 +405,39 @@ bool ngt_search_index_as_float(NGTIndex index, float *query, int32_t query_dim,
return true;
}

bool ngt_search_index_with_query(NGTIndex index, NGTQuery query, NGTObjectDistances results, NGTError error) {
if(index == NULL || query.query == NULL || results == NULL){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: index = " << index << " query = " << query.query << " results = " << results;
operate_error_string_(ss, error);
return false;
}

NGT::Index* pindex = static_cast<NGT::Index*>(index);
int32_t dim = pindex->getObjectSpace().getDimension();

NGT::Object *ngtquery = NULL;

if(query.radius < 0.0){
query.radius = FLT_MAX;
}

try{
std::vector<float> vquery(&query.query[0], &query.query[dim]);
ngtquery = pindex->allocateObject(vquery);
ngt_search_index_(pindex, ngtquery, query.size, query.epsilon, query.radius, results, query.edge_size);
}catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
if(ngtquery != NULL){
pindex->deleteObject(ngtquery);
}
return false;
}
return true;
}


// * deprecated *
int32_t ngt_get_size(NGTObjectDistances results, NGTError error) {
Expand Down Expand Up @@ -768,10 +804,11 @@ bool ngt_optimizer_execute(NGTOptimizer optimizer, const char *inIndex, const ch
return true;
}

// obsolute because of a lack of a parameter
bool ngt_optimizer_set(NGTOptimizer optimizer, int outgoing, int incoming, int nofqs,
float baseAccuracyFrom, float baseAccuracyTo,
float rateAccuracyFrom, float rateAccuracyTo,
double qte, double m, NGTError error) {
double gte, double m, NGTError error) {
if(optimizer == NULL){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: optimizer = " << optimizer;
Expand All @@ -780,7 +817,48 @@ bool ngt_optimizer_set(NGTOptimizer optimizer, int outgoing, int incoming, int n
}
try{
(static_cast<NGT::GraphOptimizer*>(optimizer))->set(outgoing, incoming, nofqs, baseAccuracyFrom, baseAccuracyTo,
rateAccuracyFrom, rateAccuracyTo, qte, m);
rateAccuracyFrom, rateAccuracyTo, gte, m);
}catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
return false;
}
return true;
}

bool ngt_optimizer_set_minimum(NGTOptimizer optimizer, int outgoing, int incoming,
int nofqs, int nofrs, NGTError error) {
if(optimizer == NULL){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: optimizer = " << optimizer;
operate_error_string_(ss, error);
return false;
}
try{
(static_cast<NGT::GraphOptimizer*>(optimizer))->set(outgoing, incoming, nofqs, nofrs);
}catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
return false;
}
return true;
}

bool ngt_optimizer_set_extension(NGTOptimizer optimizer,
float baseAccuracyFrom, float baseAccuracyTo,
float rateAccuracyFrom, float rateAccuracyTo,
double gte, double m, NGTError error) {
if(optimizer == NULL){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: optimizer = " << optimizer;
operate_error_string_(ss, error);
return false;
}
try{
(static_cast<NGT::GraphOptimizer*>(optimizer))->setExtension(baseAccuracyFrom, baseAccuracyTo,
rateAccuracyFrom, rateAccuracyTo, gte, m);
}catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
Expand All @@ -796,3 +874,16 @@ void ngt_destroy_optimizer(NGTOptimizer optimizer)
delete(static_cast<NGT::GraphOptimizer*>(optimizer));
}

bool ngt_refine_anng(NGTIndex index, float epsilon, float accuracy, int edgeSize, size_t batchSize, NGTError error)
{
NGT::Index* pindex = static_cast<NGT::Index*>(index);
try {
NGT::GraphReconstructor::refineANNG(*pindex, epsilon, accuracy, edgeSize, batchSize);
} catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
return false;
}
return true;
}
29 changes: 27 additions & 2 deletions lib/NGT/Capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ typedef struct {
float distance;
} NGTObjectDistance;

typedef struct {
float *query;
size_t size; // # of returned objects
float epsilon;
float accuracy; // expected accuracy
float radius;
size_t edge_size; // # of edges to explore for each node
} NGTQuery;

NGTIndex ngt_open_index(const char *, NGTError);

NGTIndex ngt_create_graph_and_tree(const char *, NGTProperty, NGTError);
Expand Down Expand Up @@ -88,7 +97,9 @@ NGTObjectDistances ngt_create_empty_results(NGTError);
bool ngt_search_index(NGTIndex, double*, int32_t, size_t, float, float, NGTObjectDistances, NGTError);

bool ngt_search_index_as_float(NGTIndex, float*, int32_t, size_t, float, float, NGTObjectDistances, NGTError);


bool ngt_search_index_with_query(NGTIndex, NGTQuery, NGTObjectDistances, NGTError);

int32_t ngt_get_size(NGTObjectDistances, NGTError); // deprecated

uint32_t ngt_get_result_size(NGTObjectDistances, NGTError);
Expand Down Expand Up @@ -146,10 +157,24 @@ bool ngt_optimizer_execute(NGTOptimizer, const char *, const char *, NGTError);
bool ngt_optimizer_set(NGTOptimizer optimizer, int outgoing, int incoming, int nofqs,
float baseAccuracyFrom, float baseAccuracyTo,
float rateAccuracyFrom, float rateAccuracyTo,
double qte, double m, NGTError error);
double gte, double m, NGTError error);

bool ngt_optimizer_set_minimum(NGTOptimizer optimizer, int outgoing, int incoming,
int nofqs, int nofrs, NGTError error);

bool ngt_optimizer_set_extension(NGTOptimizer optimizer,
float baseAccuracyFrom, float baseAccuracyTo,
float rateAccuracyFrom, float rateAccuracyTo,
double gte, double m, NGTError error);

void ngt_destroy_optimizer(NGTOptimizer);

// refine the specified index by searching each node.
// epsilon, exepectedAccuracy and edgeSize are the same as the prameters for search
// batchSize is the degree of parallelizm.
bool ngt_refine_anng(NGTIndex index, float epsilon, float expectedAccuracy,
int edgeSize, size_t batchSize, NGTError error);

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 5b69650

Please sign in to comment.