bpp-core3  3.0.0
OptimizationStopCondition.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 "../NumTools.h"
6 #include "../VectorTools.h"
8 #include "Optimizer.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 /******************************************************************************/
14 
16  const OptimizerInterface* optimizer) :
18  lastParametersEstimates_(),
19  newParametersEstimates_()
20 {
21  init();
22  if (newParametersEstimates_.size() == 0)
23  {
24  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
25  << "Be sure to have initialized the Optimizer first!" << endl;
26  }
27 }
28 
30  const OptimizerInterface* optimizer,
31  double tolerance) :
32  AbstractOptimizationStopCondition(optimizer, tolerance),
35 {
36  init();
37  if (newParametersEstimates_.size() == 0)
38  {
39  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
40  << "Be sure to have initialized the Optimizer first!" << endl;
41  }
42 }
43 
45  const OptimizerInterface* optimizer,
46  int burnin) :
47  AbstractOptimizationStopCondition(optimizer, burnin),
50 {
51  init();
52  if (newParametersEstimates_.size() == 0)
53  {
54  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
55  << "Be sure to have initialized the Optimizer first!" << endl;
56  }
57 }
58 
60  const OptimizerInterface* optimizer,
61  double tolerance,
62  int burnin) :
63  AbstractOptimizationStopCondition(optimizer, tolerance, burnin),
66 {
67  init();
68  if (newParametersEstimates_.size() == 0)
69  {
70  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
71  << "Be sure to have initialized the Optimizer first!" << endl;
72  }
73 }
74 
75 /******************************************************************************/
76 
78 {
80  if (optimizer_->getFunction() != 0)
82 }
83 
84 /******************************************************************************/
85 
87 {
88  callCount_++;
91  if (callCount_ <= burnin_)
92  return false;
93  for (unsigned int i = 0; i < newParametersEstimates_.size(); i++)
94  {
96  double lastEstimate = lastParametersEstimates_.parameter(p.getName()).getValue();
97  double newEstimate = p.getValue();
98  double tol = NumTools::abs<double>(newEstimate - lastEstimate);
99  if (tol > tolerance_)
100  {
101  return false;
102  }
103  }
104  return true;
105 }
106 
107 /******************************************************************************/
108 
110 {
111  if (callCount_ > burnin_)
112  {
113  double maxTol = 0.;
114  for (unsigned int i = 0; i < newParametersEstimates_.size(); i++)
115  {
117  double lastEstimate = lastParametersEstimates_.parameter(p.getName()).getValue();
118  double newEstimate = p.getValue();
119  double tol = NumTools::abs<double>(newEstimate - lastEstimate);
120  if (tol > maxTol)
121  maxTol = tol;
122  }
123  return maxTol;
124  }
125  else
126  {
127  return std::max(tolerance_, 1.);
128  }
129 }
130 
131 /******************************************************************************/
132 
134  const OptimizerInterface* optimizer) :
136  lastFunctionValue_(-log(0.)),
137  newFunctionValue_(-log(0.))
138 {
139  init();
140 }
141 
143  const OptimizerInterface* optimizer,
144  double tolerance) :
145  AbstractOptimizationStopCondition(optimizer, tolerance),
146  lastFunctionValue_(-log(0.)),
147  newFunctionValue_(-log(0.))
148 {
149  init();
150 }
151 
153  const OptimizerInterface* optimizer,
154  int burnin) :
155  AbstractOptimizationStopCondition(optimizer, burnin),
156  lastFunctionValue_(-log(0.)),
157  newFunctionValue_(-log(0.))
158 {
159  init();
160 }
161 
163  const OptimizerInterface* optimizer,
164  double tolerance,
165  int burnin) :
166  AbstractOptimizationStopCondition(optimizer, tolerance, burnin),
167  lastFunctionValue_(-log(0.)),
168  newFunctionValue_(-log(0.))
169 {
170  init();
171 }
172 
174 
175 /******************************************************************************/
176 
178 {
180  newFunctionValue_ = -log(0.);
181  if (optimizer_->getFunction() != 0)
182  {
184  }
185 }
186 
187 /******************************************************************************/
188 
190 {
191  callCount_++;
194  if (callCount_ <= burnin_)
195  return false;
196  double tol = NumTools::abs<double>(newFunctionValue_ - lastFunctionValue_);
197  return tol < tolerance_;
198 }
199 
200 /******************************************************************************/
201 
203 {
204  if (callCount_ > burnin_)
205  return NumTools::abs<double>(newFunctionValue_ - lastFunctionValue_);
206  else
207  return std::max(tolerance_, 1.);
208 }
209 
210 /******************************************************************************/
ParameterList newParametersEstimates_
The new estimates of the parameters.
virtual std::shared_ptr< const FunctionInterface > getFunction() const =0
Get the current function being optimized.
double lastFunctionValue_
The last value of the function.
virtual const Parameter & parameter(const std::string &name) const
Get the parameter with name name.
ParameterList lastParametersEstimates_
The last estimates of the parameters.
size_t size() const
Definition: ParameterList.h:56
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
double getCurrentTolerance() const
Get the current tolerance.
void init()
Initialize the condition.
virtual double getFunctionValue() const =0
Get the current function value.
void init()
Initialize the condition.
double newFunctionValue_
The new value of the function.
virtual const ParameterList & getParameters() const =0
double getCurrentTolerance() const
Get the current tolerance.
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:174
ParametersStopCondition(const OptimizerInterface *optimizer)
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:88
double callCount_
Count the number of times the isToleranceReached() function has been called.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:181
Partial implementation of the OptimizationStopCondition interface.
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
FunctionStopCondition(const OptimizerInterface *optimizer)