knnepsilon.cpp
Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2010--2011, Stephane Magnenat, ASL, ETHZ, Switzerland
00004 You can contact the author at <stephane at magnenat dot net>
00005 
00006 All rights reserved.
00007 
00008 Redistribution and use in source and binary forms, with or without
00009 modification, are permitted provided that the following conditions are met:
00010     * Redistributions of source code must retain the above copyright
00011       notice, this list of conditions and the following disclaimer.
00012     * Redistributions in binary form must reproduce the above copyright
00013       notice, this list of conditions and the following disclaimer in the
00014       documentation and/or other materials provided with the distribution.
00015     * Neither the name of the <organization> nor the
00016       names of its contributors may be used to endorse or promote products
00017       derived from this software without specific prior written permission.
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
00023 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 */
00031 
00032 #include "nabo/nabo.h"
00033 #include "helpers.h"
00034 #include <iostream>
00035 #include <fstream>
00036 #include <stdexcept>
00037 
00038 using namespace std;
00039 using namespace Nabo;
00040 
00041 template<typename T>
00042 void doTestEpsilon(const char *fileName, const int K, const int method, const int searchCount)
00043 {
00044         typedef Nabo::NearestNeighbourSearch<T> NNS;
00045         typedef typename NNS::Matrix Matrix;
00046         typedef typename NNS::IndexMatrix IndexMatrix;
00047         
00048         // check if file is ok
00049         const Matrix d(load<T>(fileName));
00050         if (K >= d.cols())
00051         {
00052                 cerr << "Requested more nearest neighbour than points in the data set" << endl;
00053                 exit(2);
00054         }
00055         
00056         // check methods together
00057         const int itCount(method >= 0 ? method : d.cols() * 2);
00058         
00059         // create big query
00060         const Matrix q(createQuery<T>(d, itCount, method));
00061         IndexMatrix indexes_bf(K, q.cols());
00062         Matrix dists2_bf(K, q.cols());
00063         
00064         NNS* nns = NNS::createKDTreeLinearHeap(d, std::numeric_limits<typename NNS::Index>::max(), NNS::TOUCH_STATISTICS);
00065         for (T epsilon = 0; epsilon < 2; epsilon += 0.01)
00066         {
00067                 double duration(0);
00068                 double touchStats(0);
00069                 for (int s = 0; s < searchCount; ++s)
00070                 {
00071                         boost::timer t;
00072                         touchStats += nns->knn(q, indexes_bf, dists2_bf, K, epsilon, NNS::ALLOW_SELF_MATCH);
00073                         duration += t.elapsed();
00074                 }
00075                 cout << epsilon << " " << duration/double(searchCount) << " " << touchStats/double(searchCount) << endl;
00076         }
00077         delete nns;
00078 }
00079 
00080 
00081 int main(int argc, char* argv[])
00082 {
00083         if (argc != 5)
00084         {
00085                 cerr << "Usage " << argv[0] << " DATA K METHOD SEARCH_COUNT" << endl;
00086                 return 1;
00087         }
00088         
00089         const int K(atoi(argv[2]));
00090         const int method(atoi(argv[3]));
00091         const int searchCount(atoi(argv[4]));
00092         
00093         //cout << "Float: (epsilon, average duration)\n";
00094         //doTestEpsilon<float>(argv[1], K, method, searchCount);
00095         cout << "epsilon average_duration search_count\n";
00096         doTestEpsilon<double>(argv[1], K, method, searchCount);
00097         
00098         return 0;
00099 }


libnabo
Author(s): Stéphane Magnenat
autogenerated on Thu Sep 10 2015 10:54:55