Skip to content

Commit

Permalink
fix minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Sep 4, 2018
1 parent 9acca3d commit 788fd75
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -4,7 +4,7 @@ project(ngt)

set(ngt_VERSION_MAJOR 1)
set(ngt_VERSION_MINOR 4)
set(ngt_VERSION_PATCH 2)
set(ngt_VERSION_PATCH 3)

set(ngt_VERSION ${ngt_VERSION_MAJOR}.${ngt_VERSION_MINOR}.${ngt_VERSION_PATCH})
set(ngt_SOVERSION ${ngt_VERSION_MAJOR})
Expand Down
22 changes: 14 additions & 8 deletions lib/NGT/Command.cpp
Expand Up @@ -736,15 +736,21 @@
float intervalTo = 0.8;
size_t querySize = 100;
double gtEpsilon = 0.1;
double mergin = 0.2;

try {
size_t baseEdgeSize = NGT::Optimizer::adjustBaseSearchEdgeSize(outIndex, pair<float, float>(intervalFrom, intervalTo), querySize, gtEpsilon, mergin);
NeighborhoodGraph::Property &prop = outGraph.getGraphProperty();
prop.dynamicEdgeSizeBase = baseEdgeSize;
cerr << "Reconstruct Graph: adjust the base search edge size. " << baseEdgeSize << endl;
} catch(NGT::Exception &err) {
cerr << "Warning: Cannot adjust the base edge size. " << err.what() << endl;
for (double mergin = 0.2; ; mergin += 0.05) {
try {
size_t baseEdgeSize = NGT::Optimizer::adjustBaseSearchEdgeSize(outIndex, pair<float, float>(intervalFrom, intervalTo), querySize, gtEpsilon, mergin);
NeighborhoodGraph::Property &prop = outGraph.getGraphProperty();
prop.dynamicEdgeSizeBase = baseEdgeSize;
cerr << "Reconstruct Graph: adjust the base search edge size. " << baseEdgeSize << endl;
break;
} catch(NGT::Exception &err) {
cerr << "Warning: Cannot adjust the base edge size. " << err.what() << endl;
if (mergin > 0.4) {
break;
}
cerr << "Warning: However, try again with increased mergin " << mergin << "." << endl;
}
}

outGraph.saveIndex(outIndexName);
Expand Down
57 changes: 25 additions & 32 deletions lib/NGT/Graph.cpp
Expand Up @@ -173,14 +173,6 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds,

std::sort(tmp.begin(), tmp.end());

for (ObjectDistances::iterator ri = tmp.begin(); ri != tmp.end(); ri++) {
#if !defined(NGT_GRAPH_CHECK_VECTOR) || defined(NGT_GRAPH_CHECK_BOOLEANSET)
distanceChecked.insert((*ri).id);
#else
distanceChecked[(*ri).id] = 1;
#endif
unchecked.push(*ri);
}
for (ObjectDistances::iterator ri = tmp.begin(); ri != tmp.end(); ri++) {
if ((results.size() < (unsigned int)sc.size) && ((*ri).distance <= sc.radius)) {
results.push((*ri));
Expand All @@ -189,6 +181,18 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds,
}
}

if (results.size() >= sc.size) {
sc.radius = results.top().distance;
}

for (ObjectDistances::iterator ri = tmp.begin(); ri != tmp.end(); ri++) {
#if !defined(NGT_GRAPH_CHECK_VECTOR) || defined(NGT_GRAPH_CHECK_BOOLEANSET)
distanceChecked.insert((*ri).id);
#else
distanceChecked[(*ri).id] = 1;
#endif
unchecked.push(*ri);
}
}

#ifdef NGT_GRAPH_READ_ONLY_GRAPH
Expand Down Expand Up @@ -259,20 +263,14 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds,
result.set(neighbor.first, distance);
unchecked.push(result);
if (distance <= sc.radius) {
results.push(result);
if (results.size() >= sc.size) {
if (results.top().distance > distance) {
results.pop();
results.push(result);
sc.radius = results.top().distance;
explorationRadius = sc.explorationCoefficient * sc.radius;
if (results.size() > sc.size) {
results.pop();
}
} else {
results.push(result);
if (results.size() == sc.size) {
sc.radius = results.top().distance;
explorationRadius = sc.explorationCoefficient * sc.radius;
}
}
sc.radius = results.top().distance;
explorationRadius = sc.explorationCoefficient * sc.radius;
}
}
}
}
Expand All @@ -289,7 +287,6 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds,

#endif


void
NeighborhoodGraph::search(NGT::SearchContainer &sc, ObjectDistances &seeds)
{
Expand Down Expand Up @@ -408,18 +405,14 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds,
result.set(neighbor.id, distance);
unchecked.push(result);
if (distance <= sc.radius) {
results.push(result);
if (results.size() >= sc.size) {
if (results.top().distance > distance) {
results.push(result);
results.pop();
sc.radius = results.top().distance;
explorationRadius = sc.explorationCoefficient * sc.radius;
}
} else {
results.push(result);
if (results.size() == sc.size) {
sc.radius = results.top().distance;
explorationRadius = sc.explorationCoefficient * sc.radius;
if (results.top().distance >= distance) {
if (results.size() > sc.size) {
results.pop();
}
sc.radius = results.top().distance;
explorationRadius = sc.explorationCoefficient * sc.radius;
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/NGT/Optimizer.h
Expand Up @@ -39,7 +39,7 @@ namespace NGT {
ifstream is(sp.query);
if (!is) {
stringstream msg;
msg << "Cannot open the specified file. " << sp.query << endl;
msg << "Cannot open the specified file. " << sp.query;
NGTThrowException(msg);
}

Expand Down Expand Up @@ -300,7 +300,7 @@ namespace NGT {
} else {
stringstream msg;
msg << "check: inner error! " << epsilon;
cerr << "Cannot find epsilon." << endl;
cerr << "Cannot find epsilon.";
NGTThrowException(msg);
}
{
Expand Down Expand Up @@ -402,7 +402,7 @@ namespace NGT {
if (epsilon > 0.2) {
stringstream msg;
msg << "exploreEpsilonForAccuracy:" << endl;
msg << "Error!! Epsilon is too large. " << epsilon << endl;
msg << "Error!! Epsilon is too large. " << epsilon;
NGTThrowException(msg);
}
acc.clear();
Expand All @@ -417,7 +417,7 @@ namespace NGT {
if (fromOverEpsilon == startEpsilon) {
stringstream msg;
msg << "exploreEpsilonForAccuracy:" << endl;
msg << "Error! startEpsilon should be reduced for the specified range." << endl;
msg << "Error! startEpsilon should be reduced for the specified range.";
NGTThrowException(msg);
}
fromOver = acc[0].averageAccuracy;
Expand All @@ -430,7 +430,7 @@ namespace NGT {
if (epsilon > 0.2) {
stringstream msg;
msg << "exploreEpsilonForAccuracy:" << endl;
msg << "Error!! Epsilon is too large. " << epsilon << endl;
msg << "Error!! Epsilon is too large. " << epsilon;
NGTThrowException(msg);
}
acc.clear();
Expand Down Expand Up @@ -480,7 +480,7 @@ namespace NGT {
stringstream msg;
msg << "exploreEpsilonForAccuracy:" << endl;
msg << "Error!! Not found proper under epsilon for mergin=" << mergin << " and the number of queries." << endl;
msg << " Should increase mergin or the number of queries to get the proper epsilon." << endl;
msg << " Should increase mergin or the number of queries to get the proper epsilon.";
NGTThrowException(msg);
} else {
sp.beginOfEpsilon = sp.endOfEpsilon = (fromUnderEpsilon + fromOverEpsilon) / 2.0;
Expand All @@ -490,7 +490,7 @@ namespace NGT {
stringstream msg;
msg << "exploreEpsilonForAccuracy:" << endl;
msg << "Error!! Not found proper over epsilon for mergin=" << mergin << " and the number of queries." << endl;
msg << " Should increase mergin or the number of queries to get the proper epsilon." << endl;
msg << " Should increase mergin or the number of queries to get the proper epsilon.";
NGTThrowException(msg);
} else {
sp.beginOfEpsilon = sp.endOfEpsilon = (toUnderEpsilon + toOverEpsilon) / 2.0;
Expand All @@ -499,7 +499,7 @@ namespace NGT {
if (fromUnderEpsilon == toOverEpsilon) {
stringstream msg;
msg << "exploreEpsilonForAccuracy:" << endl;
msg << "Error!! From and to epsilons are the same. Cannot continue." << endl;
msg << "Error!! From and to epsilons are the same. Cannot continue.";
NGTThrowException(msg);
}
sp.beginOfEpsilon = fromUnderEpsilon;
Expand All @@ -508,7 +508,7 @@ namespace NGT {
}
}
stringstream msg;
msg << "Something wrong!" << endl;
msg << "Something wrong!";
NGTThrowException(msg);
}

Expand Down

0 comments on commit 788fd75

Please sign in to comment.