bpp-core  2.2.0
FunctionTools.cpp
Go to the documentation of this file.
1 //
2 // File: FunctionTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Apr 13 10:47 2009
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for numerical calculus.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "FunctionTools.h"
41 #include "../../App/ApplicationTools.h"
42 
43 using namespace bpp;
44 
45 //From the STL;
46 #include <algorithm>
47 using namespace std;
48 
49 void ParameterGrid::addDimension(const std::string& name, const Vdouble& values) throw (Exception)
50 {
51  if (find(names_.begin(), names_.end(), name) != names_.end()) throw Exception("ParameterGrid::addDimension(). A dimension with name '" + name + "' already exists in the grid.");
52  if (values.size() == 0) throw Exception("ParameterGrid::addDimension(). Empty vector given! The dimension should at least contain one point.");
53  names_.push_back(name);
54  grid_.push_back(values);
55 }
56 
57 const Vdouble& ParameterGrid::getPointsForDimension(const std::string& name) const throw (Exception)
58 {
59  for(unsigned int i = 0; i < names_.size(); i++)
60  if (names_[i] == name)
61  return grid_[i];
62  throw Exception("ParameterGrid::getPointsForDimension(). No dimension with name '" + name + "' was found in the grid.");
63 }
64 
66 {
67  if (i >= names_.size()) throw IndexOutOfBoundsException("ParameterGrid::getPointsForDimension().", i, 0, names_.size() - 1);
68  return grid_[i];
69 }
70 
72 {
73  if (grid_.size() == 0) return 0;
74  size_t n = 1;
75  for (size_t i = 0; i < grid_.size(); i++)
76  n *= grid_[i].size();
77  return n;
78 }
79 
81  Function& function,
82  const ParameterGrid& grid) throw (Exception)
83 {
84  //Init stuff...
85  size_t n = grid.getNumberOfDimensions();
86  VVdouble* data = new VVdouble();
87  if(n == 0) return data; //Empty data table returned.
88 
89  VVdouble points = grid.getPoints();
90 
91  //Get the parameter list. this may throw an exception if the grid does not
92  //match the function parameters...
93  ParameterList pl = function.getParameters().subList(grid.getDimensionNames());
94  for(unsigned int i = 0; i < n; i++)
95  pl.setParameterValue(grid.getDimensionName(i), grid.getPointsForDimension(i)[0]);
96 
97  //Iterate over all dimensions:
98  unsigned int currentDimension = 0;
99  vector<unsigned int> currentPointInDimension(n);
100  vector<double> row(n + 1);
101  size_t nbPoints = grid.getTotalNumberOfPoints();
102  ApplicationTools::displayMessage("Computing likelihood profile...");
103  for (unsigned int i = 0; true ; i++)
104  {
105  ApplicationTools::displayGauge(i, nbPoints - 1, '=');
106  //We start by adding the current point to the table:
107  for (unsigned int j = 0; j < n; j++)
108  row[j] = pl[j].getValue();
109  row[n] = function.f(pl);
110  data->push_back(row);
111 
112  //Now increment iterator:
113  bool dimensionChanged = false;
114  while (currentDimension < n && currentPointInDimension[currentDimension] == points[currentDimension].size() - 1)
115  {
116  currentDimension++;
117  dimensionChanged = true;
118  }
119  //Stopping condition:
120  if (currentDimension == n) break;
121 
122  currentPointInDimension[currentDimension]++;
123  if (dimensionChanged)
124  {
125  for (unsigned int j = 0; j < currentDimension; j++)
126  currentPointInDimension[j] = 0;
127  currentDimension = 0;
128  }
129 
130  //Set the new parameter value:
131  for (unsigned int j = 0; j < points.size(); j++)
132  {
133  pl.setParameterValue(grid.getDimensionName(j), points[j][currentPointInDimension[j]]);
134  }
135  }
137  //and we are done:
138  return data;
139 }
140 
virtual ParameterList subList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
This class allows to perform a correspondence analysis.
This is the function abstract class.
Definition: Functions.h:86
STL namespace.
virtual void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
The parameter list object.
Definition: ParameterList.h:61
static VVdouble * computeGrid(Function &function, const ParameterGrid &grid)
Evaluates a function on all points in a given grid.
This class is a data structure to specify a set of parameter values (most likely for evaluation by a ...
Definition: FunctionTools.h:54
size_t getTotalNumberOfPoints() const
std::vector< double > Vdouble
Definition: VectorTools.h:67
static void displayMessage(const std::string &text)
Print a message.
Exception base class.
Definition: Exceptions.h:57
static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string &mes="")
Display a gauge.
Index out of bounds exception class.
Definition: Exceptions.h:298
const Vdouble & getPointsForDimension(unsigned int i) const
void addDimension(const std::string &name, const Vdouble &values)
Add a new dimension (parameter name + corresponding values).
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:68