bpp-core3  3.0.0
BrentOneDimension.cpp
Go to the documentation of this file.
1 //
2 // File: BrentOneDimension.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-17 11:45:58
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 "../../Text/TextTools.h"
43 #include "../NumConstants.h"
44 #include "../NumTools.h"
45 #include "BrentOneDimension.h"
47 
48 using namespace bpp;
49 
50 /******************************************************************************/
51 
53 {
54  callCount_++;
55  if (callCount_ <= burnin_)
56  return false;
57  const BrentOneDimension* bod = dynamic_cast<const BrentOneDimension*>(optimizer_);
58  return getCurrentTolerance() <= (bod->tol2 - 0.5 * (bod->b - bod->a));
59 }
60 
61 /******************************************************************************/
62 
64 {
65  // NRC Test for done:
66  const BrentOneDimension* bod = dynamic_cast<const BrentOneDimension*>(optimizer_);
67  return NumTools::abs(bod->x - bod->xm);
68 }
69 
70 /******************************************************************************/
71 
73  AbstractOptimizer(function),
74  a(0), b(0), d(0), e(0), etemp(0), fu(0), fv(0), fw(0), fx(0), p(0), q(0), r(0), tol1(0), tol2(0),
75  u(0), v(0), w(0), x(0), xm(0), _xinf(0), _xsup(0), isInitialIntervalSet_(false), bracketing_(BrentOneDimension::BRACKET_OUTWARD)
76 {
80 }
81 
82 /******************************************************************************/
83 
84 double BrentOneDimension::ZEPS = 1.0e-10;
85 
86 /******************************************************************************/
87 
89 {
90  if (params.size() != 1)
91  throw Exception("BrentOneDimension::init(). This optimizer only deals with one parameter.");
92 
93  // Bracket the minimum.
94  Bracket bracket;
96  {
98  }
99  else
100  {
102  }
103 
104  if (getVerbose() > 0)
105  {
106  printMessage("Initial bracketing:");
107  printMessage("A: x = " + TextTools::toString(bracket.a.x) + ", f = " + TextTools::toString(bracket.a.f));
108  printMessage("B: x = " + TextTools::toString(bracket.b.x) + ", f = " + TextTools::toString(bracket.b.f));
109  printMessage("C: x = " + TextTools::toString(bracket.c.x) + ", f = " + TextTools::toString(bracket.c.f));
110  }
111 
112  // This will be the distance moved on the step before last.
113  e = 0.0;
114 
115  // a and b must be in ascending order, but input abscissa need not be.
116  a = (bracket.a.x < bracket.c.x ? bracket.a.x : bracket.c.x);
117  b = (bracket.a.x > bracket.c.x ? bracket.a.x : bracket.c.x);
118  // Initializations...
119  fw = fv = fx = getFunction()->f(getParameters());
120  if (fx < bracket.b.f)
121  {
122  // We don't want to lose our initial guess!
123  x = w = v = bracket.b.x = getParameters()[0].getValue();
124  }
125  else
126  {
127  x = w = v = bracket.b.x;
129  fw = fv = fx = getFunction()->f(getParameters());
130  }
131 }
132 
133 /******************************************************************************/
134 
135 void BrentOneDimension::setInitialInterval(double inf, double sup)
136 {
137  if (sup > inf)
138  {
139  _xinf = inf; _xsup = sup;
140  }
141  else
142  {
143  _xinf = sup; _xsup = inf;
144  }
145  isInitialIntervalSet_ = true;
146 }
147 
148 /******************************************************************************/
149 
151 {
152  xm = 0.5 * (a + b);
153  tol2 = 2.0 * (tol1 = getStopCondition()->getTolerance() * NumTools::abs(x) + ZEPS);
154 
155  if (NumTools::abs(e) > tol1)
156  {
157  r = (x - w) * (fx - fv);
158  q = (x - v) * (fx - fw);
159  p = (x - v) * q - (x - w) * r;
160  q = 2.0 * (q - r);
161  if (q > 0.0)
162  p = -p;
163  q = NumTools::abs(q);
164  etemp = e;
165  e = d;
166  if (NumTools::abs(p) >= NumTools::abs(0.5 * q * etemp) || p <= q * (a - x) || p >= q * (b - x))
167  d = NumConstants::GOLDEN_RATIO_C() * (e = (x >= xm ? a - x : b - x));
168  else
169  {
170  d = p / q;
171  u = x + d;
172  if (u - a < tol2 || b - u < tol2)
173  d = NumTools::sign(tol1, xm - x);
174  }
175  }
176  else
177  {
178  d = NumConstants::GOLDEN_RATIO_C() * (e = (x >= xm ? a - x : b - x));
179  }
180  u = (NumTools::abs(d) >= tol1 ? x + d : x + NumTools::sign(tol1, d));
181 
182  // Function evaluaton:
184  pl[0].setValue(u);
185 
186  fu = getFunction()->f(pl);
187 
188  if (fu <= fx)
189  {
190  if (u >= x)
191  a = x;
192  else
193  b = x;
194  NumTools::shift(v, w, x, u);
195  NumTools::shift(fv, fw, fx, fu);
196  }
197  else
198  {
199  if (u < x)
200  a = u;
201  else
202  b = u;
203  if (fu <= fw || w == x)
204  {
205  v = w;
206  w = u;
207  fv = fw;
208  fw = fu;
209  }
210  else if (fu <= fv || v == x || v == w)
211  {
212  v = u;
213  fv = fu;
214  }
215  }
216 
217  // Store results for this step:
219  return fx;
220 }
221 
222 /******************************************************************************/
223 
225 {
227  throw Exception("BrentOneDimension::optimize. Initial interval not set: call the 'setInitialInterval' method first!");
229  // Apply parameters and evaluate function at minimum point:
231  return currentValue_;
232 }
233 
234 /******************************************************************************/
double callCount_
Count the number of times the isToleranceReached() function has been called.
Partial implementation of the Optimizer interface.
double optimize()
Basic implementation.
void printMessage(const std::string &message)
Give a message to print to the message handler.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
Parameter & getParameter_(size_t i)
const ParameterList & getParameters() const
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
const Function * getFunction() const
Get the current function being optimized.
void setMaximumNumberOfEvaluations(unsigned int max)
Set the maximum number of function evaluation to perform during optimization.
double currentValue_
The current value of the function.
unsigned int getVerbose() const
Get the verbose level.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
double getCurrentTolerance() const
Get the current tolerance.
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
Brent's optimization for one parameter.
double doStep()
This function is called by the step() method and contains all calculations.
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
BrentOneDimension(Function *function=0)
double optimize()
Initialize optimizer.
void setInitialInterval(double inf, double sup)
Set intial search interval.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
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 double GOLDEN_RATIO_C()
Definition: NumConstants.h:68
static void shift(T &a, T &b, T c)
Definition: NumTools.h:148
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:67
static T sign(T a)
Get the sign of a value.
Definition: NumTools.h:78
static Bracket inwardBracketMinimum(double a, double b, Function *function, ParameterList parameters, uint intervalsNum=10)
Bracket a minimum by a search within thw parameter's bounds.
static Bracket bracketMinimum(double a, double b, Function *function, ParameterList parameters)
Bracket a minimum.
virtual double getTolerance() const =0
Get the tolerance parameter.
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:110
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153