bpp-core3  3.0.0
NewtonBacktrackOneDimension.cpp
Go to the documentation of this file.
1 //
2 // File: NewtonBacktrackOneDimension.cpp
3 // Authors:
4 // Laurent Guéguen
5 // Created: jeudi 16 décembre 2010, à 15h 45
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 "../NumTools.h"
45 
46 using namespace bpp;
47 
48 /******************************************************************************/
49 
51  AbstractOptimizer(function),
52  fold_(0), f_(0), a_(0), alam_(0), alamin_(0), alam2_(0), b_(0), disc_(0), f2_(0), rhs1_(0), rhs2_(0), slope_(slope), test_(test), tmplam_(0)
53 
54 {
58 }
59 
60 /******************************************************************************/
61 
63 {
64  // Set the initial value (no use here! Use setInitialValues() instead).
65  if (params.size() != 1)
66  throw Exception("NewtonBacktrackOneDimension::init(). This optimizer only deals with one parameter.");
68  getStopCondition()->setTolerance(getStopCondition()->getTolerance() / test_);
70  alam_ = 1;
71 }
72 
73 /******************************************************************************/
74 
76 {
77  if (alam_ < alamin_)
78  {
79  getParameter_(0).setValue(0);
80  tolIsReached_ = true;
81  return fold_;
82  }
83 
85  f_ = getFunction()->f(getParameters());
86 
87  if (f_ <= fold_ + alam_ * 0.0001 * slope_)
88  {
89  tolIsReached_ = true;
90  return f_;
91  }
92 
93  if (alam_ == 1)
94  {
95  tmplam_ = -slope_ / (2.0 * (f_ - fold_ - slope_));
96  f2_ = f_;
97  alam_ = tmplam_ > 0.1 ? tmplam_ : 0.1;
98  return f_;
99  }
100 
101  rhs1_ = f_ - fold_ - alam_ * slope_;
102  rhs2_ = f2_ - fold_ - alam2_ * slope_;
103 
104  a_ = (rhs1_ / (alam_ * alam_) - rhs2_ / (alam2_ * alam2_)) / (alam_ - alam2_);
105  b_ = (-alam2_ * rhs1_ / (alam_ * alam_) + alam_ * rhs2_ / (alam2_ * alam2_)) / (alam_ - alam2_);
106 
107  if (a_ == 0.0)
108  tmplam_ = -slope_ / (2.0 * b_);
109  else
110  {
111  disc_ = b_ * b_ - 3.0 * a_ * slope_;
112  if (disc_ < 0.0)
113  tmplam_ = 0.5 * alam_;
114  else if (b_ <= 0)
115  tmplam_ = (-b_ + sqrt(disc_)) / (3.0 * a_);
116  else
117  tmplam_ = -slope_ / (b_ + sqrt(disc_));
118  }
119  if (tmplam_ > 0.5 * alam_)
120  tmplam_ = 0.5 * alam_;
121 
122  alam2_ = alam_;
123  f2_ = f_;
124  alam_ = tmplam_ > 0.1 * alam_ ? tmplam_ : 0.1 * alam_;
125 
126  return f_;
127 }
128 
129 /******************************************************************************/
Partial implementation of the Optimizer interface.
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.
bool tolIsReached_
Tell if the tolerance level has been reached.
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
double doStep()
This function is called by the step() method and contains all calculations.
NewtonBacktrackOneDimension(Function *function, double slope, double test)
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
virtual void setTolerance(double tolerance)=0
Set the tolerance parameter.
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