From 788fd7501ff8330c29f5e863288d30ee3de7f52a Mon Sep 17 00:00:00 2001 From: masajiro Date: Tue, 4 Sep 2018 15:58:30 +0900 Subject: [PATCH] fix minor bugs --- CMakeLists.txt | 2 +- lib/NGT/Command.cpp | 22 ++++++++++------- lib/NGT/Graph.cpp | 57 ++++++++++++++++++++------------------------- lib/NGT/Optimizer.h | 18 +++++++------- 4 files changed, 49 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9577f2..01cc753 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/lib/NGT/Command.cpp b/lib/NGT/Command.cpp index 890a2dc..3f8a21c 100644 --- a/lib/NGT/Command.cpp +++ b/lib/NGT/Command.cpp @@ -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(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(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); diff --git a/lib/NGT/Graph.cpp b/lib/NGT/Graph.cpp index c1c18e8..6df3958 100644 --- a/lib/NGT/Graph.cpp +++ b/lib/NGT/Graph.cpp @@ -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)); @@ -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 @@ -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; + } } } } @@ -289,7 +287,6 @@ NeighborhoodGraph::setupSeeds(NGT::SearchContainer &sc, ObjectDistances &seeds, #endif - void NeighborhoodGraph::search(NGT::SearchContainer &sc, ObjectDistances &seeds) { @@ -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; } } } diff --git a/lib/NGT/Optimizer.h b/lib/NGT/Optimizer.h index f9079c2..96c60d8 100644 --- a/lib/NGT/Optimizer.h +++ b/lib/NGT/Optimizer.h @@ -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); } @@ -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); } { @@ -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(); @@ -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; @@ -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(); @@ -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; @@ -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; @@ -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; @@ -508,7 +508,7 @@ namespace NGT { } } stringstream msg; - msg << "Something wrong!" << endl; + msg << "Something wrong!"; NGTThrowException(msg); }