Gaia
frozenlinearcombinationdistance.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_FROZENLINEARCOMBINATIONDISTANCE_H
21 #define GAIA_FROZENLINEARCOMBINATIONDISTANCE_H
22 
23 #include "frozendistance.h"
24 
25 namespace gaia2 {
26 
27 
29 public:
30  FrozenLinearCombinationDistance(const FrozenDataSet& dataset, const ParameterMap& params)
31  : FrozenDistance(dataset) {
32 
33  validParams << "NO_PARAMS_CHECK";
34 
35  if (params.empty()) throw GaiaException("Cannot create a linear combination distance with no params...");
36 
37  ParameterMap::const_iterator it = params.constBegin();
38  for (; it != params.constEnd(); ++it) {
39  ParameterMap dparams = it.value().toParameterMap();
40  if (dparams.size() != 3 || !dparams.contains("distance") ||
41  !dparams.contains("params") || !dparams.contains("weight")) {
42  throw GaiaException("Params map should contain the following keys (and only those): distance, params, weight");
43  }
44 
45  QString dname = dparams["distance"].toString().toLower();
46  ParameterMap params = dparams["params"].toParameterMap();
47  FrozenDistance* dist = FrozenMetricFactory::create(dname, dataset, params);
48 
49  _dists.push_back(qMakePair((Real)dparams["weight"].toDouble(), dist));
50  }
51  }
52 
54  for (uint i=0; i<_dists.size(); i++) delete _dists[i].second;
55  }
56 
57  void prepare(const FrozenPoint& query) {
58  for (uint i=0; i<_dists.size(); i++) {
59  _dists[i].second->prepare(query);
60  }
61  }
62 
63  Real operator()(int i, const FrozenPoint& query) const {
64  Real result = _dists[0].first * (*_dists[0].second)(i, query);
65  for (uint j=1; j<_dists.size(); j++) {
66  result += _dists[j].first * (*_dists[j].second)(i, query);
67  }
68  return result;
69  }
70 
71 protected:
72  std::vector<QPair<Real, FrozenDistance*> > _dists;
73 };
74 
75 
76 
77 } // namespace gaia2
78 
79 #endif // GAIA_FROZENLINEARCOMBINATIONDISTANCE_H
Definition: frozendistance.h:33
Definition: frozenlinearcombinationdistance.h:28
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Definition: parameter.h:34
A FrozenDataSet is a dataset that has been flagged as immutable.
Definition: frozendataset.h:49
Exception class that can take up to 3 arguments of any type, which will be serialized into a QString ...
Definition: gaiaexception.h:46