bpp-core  2.2.0
DownhillSimplexMethod.cpp
Go to the documentation of this file.
1 //
2 // File: DownhillSimplexMethod.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue Nov 4 17:10:05 2003
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (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 "DownhillSimplexMethod.h"
41 #include "../NumTools.h"
42 
43 using namespace bpp;
44 using namespace std;
45 
46 /******************************************************************************/
47 
49 {
50  const DownhillSimplexMethod* dsm = dynamic_cast<const DownhillSimplexMethod *>(optimizer_);
51  double rTol = 2.0 * NumTools::abs(dsm->y_[dsm->iHighest_] - dsm->y_[dsm->iLowest_]) /
52  (NumTools::abs(dsm->y_[dsm->iHighest_]) + NumTools::abs(dsm->y_[dsm->iLowest_]));
53  return rTol;
54 }
55 
56 /******************************************************************************/
57 
59  AbstractOptimizer(function), simplex_(), y_(), pSum_(), iHighest_(0), iNextHighest_(0), iLowest_(0)
60 {
61  // Default values:
62  nbEvalMax_ = 5000;
65 }
66 
67 /******************************************************************************/
68 
70 {
71  size_t nDim = getParameters().size();
72  nbEval_ = 0;
73 
74  // Initialize the simplex:
75  simplex_.resize(nDim + 1);
76  y_.resize(nDim + 1);
77  double lambda = 0.2; //20% of the parameter value.
78  for(unsigned int i = 1; i < nDim + 1; i++)
79  {
80  // Copy the vector...
81  simplex_[i] = getParameters();
82  // ... and set the initial values.
83  for(unsigned int j = 0; j < nDim; j++)
84  {
85  //Hummm... that does not work when the first point is 0!!! where does this come from???
86  //simplex_[i][j].setValue(getParameters()[j].getValue() * (1. + (j == i - 1 ? lambda : 0.)));
87  simplex_[i][j].setValue(getParameters()[j].getValue() + (j == i - 1 ? lambda : 0.));
88  }
89  //Compute the corresponding f value:
90  y_[i] = getFunction()->f(simplex_[i]);
91  nbEval_++;
92  }
93  //Last function evaluation, setting current value:
94  simplex_[0] = getParameters();
95  y_[0] = getFunction()->f(simplex_[0]);
96  nbEval_++;
97 
98  pSum_ = getPSum();
99 }
100 
101 /******************************************************************************/
102 
104 {
105  // The number of dimensions of the parameter space:
106  size_t nDim = simplex_.getDimension();
107  size_t mpts = nDim + 1;
108 
109  iLowest_ = 0;
110  // First we must determine which point is the highest (worst),
111  // next-highest, and lowest (best), by looping over the points
112  // in the simplex.
113  if(y_[0] > y_[1])
114  {
115  iHighest_ = 0;
116  iNextHighest_ = 1;
117  }
118  else
119  {
120  iHighest_ = 1;
121  iNextHighest_ = 0;
122  }
123 
124  for(unsigned int i = 0; i < mpts; i++)
125  {
126  if (y_[i] <= y_[iLowest_]) iLowest_ = i;
127  if (y_[i] > y_[iHighest_])
128  {
130  iHighest_ = i;
131  }
132  else if(y_[i] > y_[iNextHighest_] && i != iHighest_) iNextHighest_ = i;
133  }
134 
135  // Set current best point:
137 
138  // Begin a new iteration.
139  // First extrapolate by a factor -1 through the face of the simplex
140  // across from high point, i.e., reflect the simplex from the high point.</p>
141 
142  double yTry = tryExtrapolation(-1.0);
143  if (yTry <= y_[iLowest_])
144  {
145  // Expansion.
146  yTry = tryExtrapolation(2.0);
147  }
148  else if (yTry >= y_[iNextHighest_])
149  {
150  // Contraction.
151  double ySave = y_[iHighest_];
152  yTry = tryExtrapolation(0.5);
153  if (yTry >= ySave)
154  {
155  for (size_t i = 0; i < mpts; i++)
156  {
157  if (i != iLowest_)
158  {
159  for (size_t j = 0; j < nDim; j++)
160  {
161  pSum_[j].setValue(0.5 * (simplex_[i][j].getValue() + simplex_[iLowest_][j].getValue()));
162  simplex_[i][j].setValue(pSum_[j].getValue());
163  }
164  y_[i] = getFunction()->f(pSum_);
165  nbEval_++;
166  }
167  }
168  nbEval_ += static_cast<unsigned int>(nDim);
169  pSum_ = getPSum();
170  }
171  }
172 
173  return y_[iLowest_];
174 }
175 
176 /******************************************************************************/
177 
179 {
181 
182  // set best shot:
183  return getFunction()->f(simplex_[iLowest_]);
184 }
185 
186 /******************************************************************************/
187 
189 {
190  size_t ndim = simplex_.getDimension();
191  size_t mpts = ndim + 1;
192 
193  // Get a copy...
194  ParameterList pSum = getParameters();
195  // ... and initializes it.
196  for (size_t j = 0; j < ndim; j++)
197  {
198  double sum = 0.;
199  for (size_t i = 0; i < mpts; i++)
200  {
201  sum += simplex_[i][j].getValue();
202  }
203  pSum[j].setValue(sum);
204  }
205  return pSum;
206 }
207 
208 /******************************************************************************/
209 
211 {
212  size_t ndim = simplex_.getDimension();
213  double fac1, fac2, yTry;
214 
215  fac1 = (1.0 - fac) / static_cast<double>(ndim);
216  fac2 = fac1 - fac;
217 
218  // Get a copy...
219  ParameterList pTry = getParameters();
220  // and initialize it:
221  for (size_t j = 0; j < ndim; j++)
222  {
223  pTry[j].setValue(pSum_[j].getValue() * fac1 - simplex_[iHighest_][j].getValue() * fac2);
224  }
225  // Now compute the function for this new set of parameters:
226  yTry = getFunction()->f(pTry);
227  nbEval_++;
228 
229  // Then test this new point:
230  if (yTry < y_[iHighest_])
231  {
232  y_[iHighest_] = yTry;
233  for (size_t j = 0; j < ndim; j++)
234  {
235  pSum_[j].setValue(pSum_[j].getValue() + pTry[j].getValue() - simplex_[iHighest_][j].getValue());
236  simplex_[iHighest_][j].setValue(pTry[j].getValue());
237  }
238  }
239  return yTry;
240 }
241 
242 /******************************************************************************/
243 
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:117
double optimize()
Multidimensional minimization of the function function_ by the downhill simplex method of Nelder and ...
This class allows to perform a correspondence analysis.
double optimize()
Basic implementation.
ParameterList getPSum()
Update the pSum_ variable.
This is the function abstract class.
Definition: Functions.h:86
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
STL namespace.
ParameterList & getParameters_()
double getCurrentTolerance() const
Get the current tolerance.
The parameter list object.
Definition: ParameterList.h:61
double tryExtrapolation(double fac)
Extrapolates by a factor fac through the face of the simplex from the high point. Try the new point a...
void setDefaultStopCondition_(OptimizationStopCondition *osc)
double doStep()
This function is called by the step() method and contains all calculations.
const Function * getFunction() const
Get the current function being optimized.
const ParameterList & getParameters() const
DownhillSimplexMethod(Function *function)
Build a new Downhill Simplex optimizer.
Exception base class.
Definition: Exceptions.h:57
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:66
This implements the Downhill Simplex method in multidimensions.