bpp-core  2.2.0
NewtonOneDimension.cpp
Go to the documentation of this file.
1 //
2 // File: NewtonOneDimension.cpp
3 // Created by: Julien Dutheil
4 // Created on: Thu Apr 26 14:16 2007
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 "NewtonOneDimension.h"
41 #include "../NumTools.h"
42 #include "../../Text/TextTools.h"
43 
44 using namespace bpp;
45 
46 /******************************************************************************/
47 
49  AbstractOptimizer(function),
50  _param(),
51  _maxCorrection(10)
52 {
55  nbEvalMax_ = 10000;
56 }
57 
58 /******************************************************************************/
59 
61 {
62  // Set the initial value (no use here! Use setInitialValues() instead).
63  if (params.size() != 1)
64  throw Exception("NewtonOneDimension::init(). This optimizer only deals with one parameter.");
65  _param = params[0].getName();
66  currentValue_ = getFunction()->f(getParameters());
67  getStopCondition()->init();
68 }
69 
70 /******************************************************************************/
71 
73 {
74  double movement;
75  ParameterList newPoint = getParameters();
76  ParameterList bckPoint = getFunction()->getParameters();
77  double newValue;
78  double firstOrderDerivative = getFunction()->getFirstOrderDerivative(_param);
79  double secondOrderDerivative = getFunction()->getSecondOrderDerivative(_param);
80  if (secondOrderDerivative <= 0)
81  {
82  printMessage("!!! Second order derivative is negative (" + TextTools::toString(getParameters()[0].getValue()) + "). No move performed.");
83  //movements[i] = 0; // We want to reach a minimum, not a maximum!
84  // My personnal improvement:
85  movement = -firstOrderDerivative / secondOrderDerivative;
86  }
87  else movement = firstOrderDerivative / secondOrderDerivative;
88  if (std::isnan(movement))
89  {
90  printMessage("!!! Non derivable point. No move performed. (f=" + TextTools::toString(currentValue_) + ", d1=" + TextTools::toString(firstOrderDerivative) + ", d2=" + TextTools::toString(secondOrderDerivative) + ").");
91  movement = 0; // Either first or second order derivative is infinity. This may happen when the function == inf at this point.
92  }
93  newPoint[0].setValue(getParameters()[0].getValue() - movement);
94  newValue = getFunction()->f(newPoint);
95 
96  // Check newValue:
97  unsigned int count = 0;
98  while (newValue > currentValue_)
99  {
100  //Restore previous point (all parameters in case of global constraint):
101  getFunction()->setParameters(bckPoint);
102 
103  count++;
104  if (count >= _maxCorrection)
105  {
106  printMessage("!!! Felsenstein-Churchill correction applied too much time. Stopping here. Convergence probably not reached.");
107  tolIsReached_ = true;
108  return currentValue_;
109  //throw Exception("NewtonOneDimension::step(). Felsenstein-Churchill correction applied more than 10000 times.");
110  }
111  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()));
112  movement = movement / 2;
113  newPoint[0].setValue(getParameters()[0].getValue() - movement);
114  newValue = getFunction()->f(newPoint);
115  }
116 
117  getParameters_() = newPoint; // Function as been set to newPoint by the call of f(newPoint).
118  return newValue;
119 }
120 
121 /******************************************************************************/
122 
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
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
This class allows to perform a correspondence analysis.
virtual const ParameterList & getParameters() const =0
Get all parameters available.
ParameterList & getParameters_()
NewtonOneDimension(DerivableSecondOrder *function=0)
Stop condition on function value.
virtual double getSecondOrderDerivative(const std::string &variable) const =0
Get the second order derivative of the function at the current point.
virtual double getFirstOrderDerivative(const std::string &variable) const =0
Get the derivative of the function at the current point.
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 tolIsReached_
Tell if the tolerance level has been reached.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
const ParameterList & getParameters() const
const DerivableSecondOrder * getFunction() const
Get the current function being optimized.
Exception base class.
Definition: Exceptions.h:57
double doStep()
This function is called by the step() method and contains all calculations.
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
This is the abstract class for second order derivable functions.
Definition: Functions.h:189
Partial implementation of the Optimizer interface.
double currentValue_
The current value of the function.
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
void printMessage(const std::string &message)
Give a message to print to the message handler.