bpp-core3  3.0.0
NewtonOneDimension.cpp
Go to the documentation of this file.
1 //
2 // File: NewtonOneDimension.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2007-04-26 14:16:00
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 "../NumTools.h"
44 #include "NewtonOneDimension.h"
45 
46 using namespace bpp;
47 
48 /******************************************************************************/
49 
51  AbstractOptimizer(function),
52  _param(),
53  _maxCorrection(10)
54 {
57  nbEvalMax_ = 10000;
58 }
59 
60 /******************************************************************************/
61 
63 {
64  // Set the initial value (no use here! Use setInitialValues() instead).
65  if (params.size() != 1)
66  throw Exception("NewtonOneDimension::init(). This optimizer only deals with one parameter.");
67  _param = params[0].getName();
70 }
71 
72 /******************************************************************************/
73 
75 {
76  double movement;
77  ParameterList newPoint = getParameters();
78  ParameterList bckPoint = getFunction()->getParameters();
79  double newValue;
80  double firstOrderDerivative = getFunction()->getFirstOrderDerivative(_param);
81  double secondOrderDerivative = getFunction()->getSecondOrderDerivative(_param);
82  if (secondOrderDerivative <= 0)
83  {
84  printMessage("!!! Second order derivative is negative (" + TextTools::toString(getParameters()[0].getValue()) + "). No move performed.");
85  // movements[i] = 0; // We want to reach a minimum, not a maximum!
86  // My personnal improvement:
87  movement = -firstOrderDerivative / secondOrderDerivative;
88  }
89  else
90  movement = firstOrderDerivative / secondOrderDerivative;
91  if (std::isnan(movement))
92  {
93  printMessage("!!! Non derivable point. No move performed. (f=" + TextTools::toString(currentValue_) + ", d1=" + TextTools::toString(firstOrderDerivative) + ", d2=" + TextTools::toString(secondOrderDerivative) + ").");
94  movement = 0; // Either first or second order derivative is infinity. This may happen when the function == inf at this point.
95  }
96  newPoint[0].setValue(getParameters()[0].getValue() - movement);
97  newValue = getFunction()->f(newPoint);
98 
99  // Check newValue:
100  unsigned int count = 0;
101  while (newValue > currentValue_)
102  {
103  // Restore previous point (all parameters in case of global constraint):
104  getFunction()->setParameters(bckPoint);
105 
106  count++;
107  if (count >= _maxCorrection)
108  {
109  printMessage("!!! Felsenstein-Churchill correction applied too much time. Stopping here. Convergence probably not reached.");
110  tolIsReached_ = true;
111  return currentValue_;
112  // throw Exception("NewtonOneDimension::step(). Felsenstein-Churchill correction applied more than 10000 times.");
113  }
114  printMessage("!!! Function at new point is greater than at current point: " + TextTools::toString(newValue) + ">" + TextTools::toString(currentValue_) + ". Applying Felsenstein-Churchill correction, value = " + TextTools::toString(newPoint[0].getValue()));
115  movement = movement / 2;
116  newPoint[0].setValue(getParameters()[0].getValue() - movement);
117  newValue = getFunction()->f(newPoint);
118  }
119 
120  getParameters_() = newPoint; // Function as been set to newPoint by the call of f(newPoint).
121  return newValue;
122 }
123 
124 /******************************************************************************/
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.
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
const ParameterList & getParameters() const
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
ParameterList & getParameters_()
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
double currentValue_
The current value of the function.
bool tolIsReached_
Tell if the tolerance level has been reached.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
virtual double getFirstOrderDerivative(const std::string &variable) const =0
Get the derivative of the function at the current point.
This is the abstract class for second order derivable functions.
Definition: Functions.h:188
virtual double getSecondOrderDerivative(const std::string &variable) const =0
Get the second order derivative of the function at the current point.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Stop condition on function value.
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:117
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.
NewtonOneDimension(DerivableSecondOrder *function=0)
const DerivableSecondOrder * getFunction() const
Get the current function being optimized.
virtual void init()=0
Initialize the condition.
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
virtual const ParameterList & getParameters() const =0
Get all parameters available.
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153
std::size_t count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.
Definition: TextTools.cpp:426