knnbucketsize.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         for (unsigned bucketSize = 2; bucketSize < 40; ++bucketSize)
00065         {
00066                 Parameters additionalParameters("bucketSize", unsigned(bucketSize));
00067                 NNS* nns = NNS::createKDTreeLinearHeap(d, std::numeric_limits<typename NNS::Index>::max(), 0, additionalParameters);
00068         
00069                 double duration(0);
00070                 for (int s = 0; s < searchCount; ++s)
00071                 {
00072                         boost::timer t;
00073                         nns->knn(q, indexes_bf, dists2_bf, K, 0, NNS::ALLOW_SELF_MATCH);
00074                         duration += t.elapsed();
00075                 }
00076                 cout << bucketSize << " " << duration/double(searchCount) << endl;
00077                 
00078                 delete nns;
00079         }
00080 }
00081 
00082 
00083 int main(int argc, char* argv[])
00084 {
00085         if (argc != 5)
00086         {
00087                 cerr << "Usage " << argv[0] << " DATA K METHOD SEARCH_COUNT" << endl;
00088                 return 1;
00089         }
00090         
00091         const int K(atoi(argv[2]));
00092         const int method(atoi(argv[3]));
00093         const int searchCount(atoi(argv[4]));
00094         
00095         cout << "bucketSize average_duration\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