Gaia
frozensearch.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_FROZENSEARCH_H
21 #define GAIA_FROZENSEARCH_H
22 
23 #include <iostream>
24 #include "dataset.h"
25 #include "frozendataset.h"
26 #include "frozendistance.h"
27 #include "frozeneuclideandistance.h"
28 #include "frozencosineangledistance.h"
29 #include "frozenlinearcombinationdistance.h"
30 #include "distancefunction.h"
31 #include "timer.h"
32 
33 // Some utility functions for use with the gaiabench tool
34 
35 namespace gaia2 {
36 
37 // return a right-aligned formatted string with fixed precision that goes up to 3 digits in front
38 // also converts from seconds to milliseconds
39 inline QString RTIME(double t) {
40  QString num = QString::number(t*1000, 'f', 4);
41  return QString(8-num.size(), ' ') + num;
42 }
43 
44 
45 void normalSearch(DataSet& dataset, const DistanceFunction* dist, const Point& query, int N);
46 
47 
48 class EuclideanDistance {
49  int dim;
50 public:
51  EuclideanDistance(int dimension) : dim(dimension) {}
52 
53  float operator()(const float* const p1, const float* const p2) const;
54 };
55 
57  int dim;
58 public:
59  CosineDistance(int dimension) : dim(dimension) {}
60 
61  float operator()(const float* const p1, const float* const p2) const;
62 };
63 
64 
65 
66 //typedef pair<float, qint32> fspoint;
67 typedef std::pair<float, int> fspoint;
68 
69 
70 template <typename DistanceFunction>
71 void frozenSearch(const DataSet& dataset, const float* frozenDataSet, int dimension,
72  const float* queryPoint, int N, const DistanceFunction& dist) {
73  int npoints = dataset.size();
74 
75  std::vector<fspoint> result(npoints);
76 
77  Timer chrono;
78  chrono.start();
79  for (int n=0; n<N; n++) {
80 
81  for (int i=0; i<npoints; i++) {
82  result[i].first = dist(&frozenDataSet[i*dimension], queryPoint);
83  result[i].second = i;
84  }
85 
86  partial_sort(result.begin(), result.begin() + 100, result.end());
87  }
88  chrono.stop();
89 
90  G_INFO("Average on " << N << " runs using naive frozen Gaia datasets and views: "
91  << RTIME(chrono.elapsed()/N) << "ms / query");
92 
93  for (int i=0; i<5; i++) {
94  G_DEBUG(GTools, i << " - " << result[i].first << " - " << dataset[result[i].second]->name());
95  }
96  G_DEBUG(GTools, "");
97 }
98 
99 
100 
101 
102 
103 void deepFreeze(FrozenDataSet& frozenDS, const FrozenDistance& dist, const Eigen::RowVectorXf& queryPoint, int N);
104 void deepFoldedFreeze(FrozenDataSet& frozenDS, const FrozenDistance& dist, const Eigen::RowVectorXf& queryPoint, int N);
105 void deepMultiFreeze(FrozenDataSet& frozenDS, const QList<FrozenDistance*>& dists,
106  const QList<Eigen::RowVectorXf> queries, int N);
107 
108 } // namespace gaia2
109 
110 #endif // GAIA_FROZENSEARCH_H
Definition: frozendistance.h:33
double elapsed() const
Returns the cumulative number of seconds elapsed between each call to the start()/stop() pair...
Definition: timer.cpp:51
This class represents a dataset and all related information.
Definition: dataset.h:91
void start()
Starts the timer.
Definition: timer.cpp:32
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Real operator()(const Point &p1, const Point &p2, int seg1, int seg2) const
This function computes the distance between the two given points.
Definition: euclideandistance.cpp:31
Definition: distancefunction.h:37
void stop()
Stops the timer.
Definition: timer.cpp:42
A FrozenDataSet is a dataset that has been flagged as immutable.
Definition: frozendataset.h:49
Definition: frozensearch.h:56
QString name
Name for the metric, usually the key that was used to instantiate it from the factory.
Definition: distancefunction.h:44
This class allows you to measure a certain amount of time, for instance if you want to know how long ...
Definition: timer.h:38