bpp-core3  3.0.0
GoldenSectionSearch.cpp
Go to the documentation of this file.
1 //
2 // File: GoldenSectionSearch.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-10 10:42:17
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 "../../Text/TextTools.h"
43 #include "../NumConstants.h"
44 #include "../NumTools.h"
45 #include "GoldenSectionSearch.h"
47 
48 using namespace bpp;
49 
50 /******************************************************************************/
51 
53 {
54  callCount_++;
55  if (callCount_ <= burnin_)
56  return false;
57  return getTolerance() <= tolerance_;
58 }
59 
60 /******************************************************************************/
61 
63 {
64  // NRC Test for done:
65  const GoldenSectionSearch* gss = dynamic_cast<const GoldenSectionSearch*>(optimizer_);
66  return NumTools::abs(gss->x3 - gss->x0) / (NumTools::abs(gss->x1) + NumTools::abs(gss->x2));
67 }
68 
69 /******************************************************************************/
70 
72  AbstractOptimizer(function),
73  f1(0), f2(0), x0(0), x1(0), x2(0), x3(0), xinf_(0), xsup_(0), isInitialIntervalSet_(false)
74 {
75  nbEvalMax_ = 10000;
78 }
79 
80 /******************************************************************************/
81 
83 {
84  // Set the initial value (no use here! Use setInitialValues() instead).
85  if (params.size() != 1)
86  throw Exception("GoldenSectionSearch::init(). This optimizer only deals with one parameter.");
87 
88  // Bracket the minimum.
90  if (getVerbose() > 0)
91  {
92  printMessage("Initial bracketing:");
93  printMessage("A: x = " + TextTools::toString(bracket.a.x) + ", f = " + TextTools::toString(bracket.a.f));
94  printMessage("B: x = " + TextTools::toString(bracket.b.x) + ", f = " + TextTools::toString(bracket.b.f));
95  printMessage("C: x = " + TextTools::toString(bracket.c.x) + ", f = " + TextTools::toString(bracket.c.f));
96  }
97 
98  // At any given time we will keep track of four points, x0, x1, x2 and x3.
99  x0 = bracket.a.x;
100  x3 = bracket.c.x;
101  if (NumTools::abs(bracket.c.x - bracket.b.x)
102  > NumTools::abs(bracket.b.x - bracket.a.x))
103  {
104  // Make x0 to x1 the smaller segment,
105  x1 = bracket.b.x;
106  // and fill in the new point to be tried.
107  x2 = bracket.b.x + NumConstants::GOLDEN_RATIO_C() * (bracket.c.x - bracket.b.x);
108  }
109  else
110  {
111  x2 = bracket.b.x;
112  x1 = bracket.b.x - NumConstants::GOLDEN_RATIO_C() * (bracket.b.x - bracket.a.x);
113  }
114  // The initial function evaluations.
115  // Note that we never need to evaluate the function at the original endpoints.
118 }
119 
120 /******************************************************************************/
121 
122 void GoldenSectionSearch::setInitialInterval(double inf, double sup)
123 {
124  if (sup > inf)
125  {
126  xinf_ = inf; xsup_ = sup;
127  }
128  else
129  {
130  xinf_ = sup; xsup_ = inf;
131  }
132  isInitialIntervalSet_ = true;
133 }
134 
135 /******************************************************************************/
136 
138 {
140  throw Exception("GoldenSectionSearch::step. Initial interval not set: call the 'setInitialInterval' method first!");
141 
142  nbEval_++;
143 
144  if (f2 < f1)
145  {
146  // One possible outcome, its housekeeping,
147  NumTools::shift<double>(x0, x1, x2);
149  // and a new function evaluation.
152  NumTools::shift<double>(f1, f2, getFunction()->f(getParameters()));
153  return f2;
154  }
155  else
156  {
157  // The other outcome,
158  NumTools::shift<double>(x3, x2, x1);
160  // and its new function evaluation.
163  NumTools::shift<double>(f2, f1, getFunction()->f(getParameters()));
164  return f1;
165  }
166 }
167 
168 /******************************************************************************/
169 
171 {
172  if (!hasFunction())
173  throw NullPointerException("GoldenSectionSearch::getFunctionValue. No function associated to this optimizer.");
174  // return NumTools::min(f1, f2);
175  return currentValue_;
176 }
177 
178 /******************************************************************************/
double callCount_
Count the number of times the isToleranceReached() function has been called.
double getTolerance() const
Get the tolerance parameter.
Partial implementation of the Optimizer interface.
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)
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.
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
const Function * getFunction() const
Get the current function being optimized.
double currentValue_
The current value of the function.
bool tolIsReached_
Tell if the tolerance level has been reached.
unsigned int getVerbose() const
Get the verbose level.
bool hasFunction() const
Tell if a funciton is associatied to this optimizer.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
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
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
double getCurrentTolerance() const
Get the current tolerance.
Golden Section Search optimization algorithm for one parameter.
void setInitialInterval(double inf, double sup)
Set intial search interval.
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.
GoldenSectionSearch(Function *function)
double getFunctionValue() const
Initialize optimizer.
The base class exception for NULL pointer error. This exception may be thrown when an unexpected NULL...
Definition: Exceptions.h:95
static double GOLDEN_RATIO_R()
Definition: NumConstants.h:67
static double GOLDEN_RATIO_C()
Definition: NumConstants.h:68
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:67
static Bracket bracketMinimum(double a, double b, Function *function, ParameterList parameters)
Bracket a minimum.
virtual bool isToleranceReached() const =0
Tell if the we reached the desired tolerance with a given new set of estimates.
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