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