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