bpp-core  2.2.0
GoldenSectionSearch.cpp
Go to the documentation of this file.
1 //
2 // File: GoldenSectionSearch.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Nov 10 10:42:17 2003
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (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 "GoldenSectionSearch.h"
42 #include "../NumTools.h"
43 #include "../NumConstants.h"
44 #include "../../Text/TextTools.h"
45 
46 using namespace bpp;
47 
48 /******************************************************************************/
49 
51 {
52  callCount_++;
53  if (callCount_ <= burnin_) return false;
54  return getTolerance() <= tolerance_;
55 }
56 
57 /******************************************************************************/
58 
60 {
61  // NRC Test for done:
62  const GoldenSectionSearch* gss = dynamic_cast<const GoldenSectionSearch*>(optimizer_);
63  return NumTools::abs(gss->x3 - gss->x0) / (NumTools::abs(gss->x1) + NumTools::abs(gss->x2));
64 }
65 
66 /******************************************************************************/
67 
69  AbstractOptimizer(function),
70  f1(0), f2(0), x0(0), x1(0), x2(0), x3(0), xinf_(0), xsup_(0), isInitialIntervalSet_(false)
71 {
72  nbEvalMax_ = 10000;
75 }
76 
77 /******************************************************************************/
78 
80 {
81  // Set the initial value (no use here! Use setInitialValues() instead).
82  if(params.size() != 1) throw Exception("GoldenSectionSearch::init(). This optimizer only deals with one parameter.");
83 
84  // Bracket the minimum.
85  Bracket bracket = OneDimensionOptimizationTools::bracketMinimum(xinf_, xsup_, getFunction(), getParameters());
86  if (getVerbose() > 0)
87  {
88  printMessage("Initial bracketing:");
89  printMessage("A: x = " + TextTools::toString(bracket.a.x) + ", f = " + TextTools::toString(bracket.a.f));
90  printMessage("B: x = " + TextTools::toString(bracket.b.x) + ", f = " + TextTools::toString(bracket.b.f));
91  printMessage("C: x = " + TextTools::toString(bracket.c.x) + ", f = " + TextTools::toString(bracket.c.f));
92  }
93 
94  // At any given time we will keep track of four points, x0, x1, x2 and x3.
95  x0 = bracket.a.x;
96  x3 = bracket.c.x;
97  if (NumTools::abs(bracket.c.x - bracket.b.x)
98  > NumTools::abs(bracket.b.x - bracket.a.x))
99  {
100  // Make x0 to x1 the smaller segment,
101  x1 = bracket.b.x;
102  // and fill in the new point to be tried.
103  x2 = bracket.b.x + NumConstants::GOLDEN_RATIO_C() * (bracket.c.x - bracket.b.x);
104  }
105  else
106  {
107  x2 = bracket.b.x;
108  x1 = bracket.b.x - NumConstants::GOLDEN_RATIO_C() * (bracket.b.x - bracket.a.x);
109  }
110  // The initial function evaluations.
111  // Note that we never need to evaluate the function at the original endpoints.
112  getParameter_(0).setValue(x1); f1 = getFunction()->f(getParameters());
113  getParameter_(0).setValue(x2); f2 = getFunction()->f(getParameters());
114 }
115 
116 /******************************************************************************/
117 
118 void GoldenSectionSearch::setInitialInterval(double inf, double sup)
119 {
120  if(sup > inf)
121  {
122  xinf_ = inf; xsup_ = sup;
123  }
124  else
125  {
126  xinf_ = sup; xsup_ = inf;
127  }
128  isInitialIntervalSet_ = true;
129 }
130 
131 /******************************************************************************/
132 
134 {
135  if (!isInitialIntervalSet_) throw Exception("GoldenSectionSearch::step. Initial interval not set: call the 'setInitialInterval' method first!");
136 
137  nbEval_++;
138 
139  if (f2 < f1)
140  {
141  // One possible outcome, its housekeeping,
142  NumTools::shift<double>(x0, x1, x2);
144  // and a new function evaluation.
147  NumTools::shift<double>(f1, f2, getFunction()->f(getParameters()));
148  return f2;
149  }
150  else
151  {
152  // The other outcome,
153  NumTools::shift<double>(x3, x2, x1);
155  // and its new function evaluation.
158  NumTools::shift<double>(f2, f1, getFunction()->f(getParameters()));
159  return f1;
160  }
161 }
162 
163 /******************************************************************************/
164 
166 {
167  if (!hasFunction())
168  throw NullPointerException("GoldenSectionSearch::getFunctionValue. No function associated to this optimizer.");
169  //return NumTools::min(f1, f2);
170  return currentValue_;
171 }
172 
173 /******************************************************************************/
174 
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:117
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
double getTolerance() const
Get the tolerance parameter.
This class allows to perform a correspondence analysis.
This is the function abstract class.
Definition: Functions.h:86
virtual bool isToleranceReached() const =0
Tell if the we reached the desired tolerance with a given new set of estimates.
static double GOLDEN_RATIO_C()
Definition: NumConstants.h:67
double getCurrentTolerance() const
Get the current tolerance.
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
The parameter list object.
Definition: ParameterList.h:61
bool hasFunction() const
Tell if a funciton is associatied to this optimizer.
bool tolIsReached_
Tell if the tolerance level has been reached.
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:123
Golden Section Search optimization algorithm for one parameter.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
Parameter & getParameter_(size_t i)
const Function * getFunction() const
Get the current function being optimized.
static Bracket bracketMinimum(double a, double b, Function *function, ParameterList parameters)
Bracket a minimum.
const ParameterList & getParameters() const
The base class exception for NULL pointer error.
Definition: Exceptions.h:123
Exception base class.
Definition: Exceptions.h:57
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
double callCount_
Count the number of times the isToleranceReached() function has been called.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
double currentValue_
The current value of the function.
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
void setInitialInterval(double inf, double sup)
Set intial search interval.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
double doStep()
This function is called by the step() method and contains all calculations.
GoldenSectionSearch(Function *function)
static double GOLDEN_RATIO_R()
Definition: NumConstants.h:66
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:66
double getFunctionValue() const
Initialize optimizer.