bpp-core3  3.0.0
NewtonBacktrackOneDimension.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "../../Text/TextTools.h"
6 #include "../NumTools.h"
8 
9 using namespace bpp;
10 
11 /******************************************************************************/
12 
13 NewtonBacktrackOneDimension::NewtonBacktrackOneDimension(std::shared_ptr<FunctionInterface> function, double slope, double test) :
14  AbstractOptimizer(function),
15  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)
16 
17 {
18  setDefaultStopCondition_(make_shared<NBODStopCondition>(this));
21 }
22 
23 /******************************************************************************/
24 
26 {
27  // Set the initial value (no use here! Use setInitialValues() instead).
28  if (params.size() != 1)
29  throw Exception("NewtonBacktrackOneDimension::init(). This optimizer only deals with one parameter.");
31  getStopCondition()->setTolerance(getStopCondition()->getTolerance() / test_);
32  alamin_ = getStopCondition()->getTolerance();
33  alam_ = 1;
34 }
35 
36 /******************************************************************************/
37 
39 {
40  if (alam_ < alamin_)
41  {
42  getParameter_(0).setValue(0);
43  tolIsReached_ = true;
44  return fold_;
45  }
46 
48  f_ = getFunction()->f(getParameters());
49 
50  if (f_ <= fold_ + alam_ * 0.0001 * slope_)
51  {
52  tolIsReached_ = true;
53  return f_;
54  }
55 
56  if (alam_ == 1)
57  {
58  tmplam_ = -slope_ / (2.0 * (f_ - fold_ - slope_));
59  f2_ = f_;
60  alam_ = tmplam_ > 0.1 ? tmplam_ : 0.1;
61  return f_;
62  }
63 
64  rhs1_ = f_ - fold_ - alam_ * slope_;
65  rhs2_ = f2_ - fold_ - alam2_ * slope_;
66 
67  a_ = (rhs1_ / (alam_ * alam_) - rhs2_ / (alam2_ * alam2_)) / (alam_ - alam2_);
68  b_ = (-alam2_ * rhs1_ / (alam_ * alam_) + alam_ * rhs2_ / (alam2_ * alam2_)) / (alam_ - alam2_);
69 
70  if (a_ == 0.0)
71  tmplam_ = -slope_ / (2.0 * b_);
72  else
73  {
74  disc_ = b_ * b_ - 3.0 * a_ * slope_;
75  if (disc_ < 0.0)
76  tmplam_ = 0.5 * alam_;
77  else if (b_ <= 0)
78  tmplam_ = (-b_ + sqrt(disc_)) / (3.0 * a_);
79  else
80  tmplam_ = -slope_ / (b_ + sqrt(disc_));
81  }
82  if (tmplam_ > 0.5 * alam_)
83  tmplam_ = 0.5 * alam_;
84 
85  alam2_ = alam_;
86  f2_ = f_;
87  alam_ = tmplam_ > 0.1 * alam_ ? tmplam_ : 0.1 * alam_;
88 
89  return f_;
90 }
91 
92 /******************************************************************************/
NewtonBacktrackOneDimension(std::shared_ptr< FunctionInterface > function, double slope, double test)
Constructor.
std::shared_ptr< OptimizationStopCondition > getStopCondition() override
Get the stop condition of the optimization algorithm.
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
size_t size() const
Definition: ParameterList.h:56
void setDefaultStopCondition_(std::shared_ptr< OptimizationStopCondition > osc)
const ParameterList & getParameters() const override
The parameter list object.
Definition: ParameterList.h:27
bool tolIsReached_
Tell if the tolerance level has been reached.
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:55
double doStep()
This function is called by the step() method and contains all calculations.
Parameter & getParameter_(size_t i)
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
Partial implementation of the Optimizer interface.
std::shared_ptr< const FunctionInterface > getFunction() const override
Get the current function being optimized.
void setMaximumNumberOfEvaluations(unsigned int max) override
Set the maximum number of function evaluation to perform during optimization.
std::shared_ptr< OptimizationStopCondition > getDefaultStopCondition() override
Get the default stop condition of the optimization algorithm.
void setStopCondition(std::shared_ptr< OptimizationStopCondition > stopCondition) override
Set the stop condition of the optimization algorithm.