bpp-core3  3.0.0
OptimizationStopCondition.cpp
Go to the documentation of this file.
1 //
2 // File: OptimizationStopCondition.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-12-23 11:51:31
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 19, 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 "../NumTools.h"
43 #include "../VectorTools.h"
45 #include "Optimizer.h"
46 
47 using namespace bpp;
48 using namespace std;
49 
50 /******************************************************************************/
51 
53  const Optimizer* optimizer) :
55  lastParametersEstimates_(),
56  newParametersEstimates_()
57 {
58  init();
59  if (newParametersEstimates_.size() == 0)
60  {
61  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
62  << "Be sure to have initialized the Optimizer first!" << endl;
63  }
64 }
65 
67  const Optimizer* optimizer,
68  double tolerance) :
69  AbstractOptimizationStopCondition(optimizer, tolerance),
70  lastParametersEstimates_(),
71  newParametersEstimates_()
72 {
73  init();
74  if (newParametersEstimates_.size() == 0)
75  {
76  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
77  << "Be sure to have initialized the Optimizer first!" << endl;
78  }
79 }
80 
82  const Optimizer* optimizer,
83  int burnin) :
84  AbstractOptimizationStopCondition(optimizer, burnin),
85  lastParametersEstimates_(),
86  newParametersEstimates_()
87 {
88  init();
89  if (newParametersEstimates_.size() == 0)
90  {
91  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
92  << "Be sure to have initialized the Optimizer first!" << endl;
93  }
94 }
95 
97  const Optimizer* optimizer,
98  double tolerance,
99  int burnin) :
100  AbstractOptimizationStopCondition(optimizer, tolerance, burnin),
101  lastParametersEstimates_(),
102  newParametersEstimates_()
103 {
104  init();
105  if (newParametersEstimates_.size() == 0)
106  {
107  cout << "DEBUG: WARNING!!! No parameter passed to ParametersStopCondition constructor. "
108  << "Be sure to have initialized the Optimizer first!" << endl;
109  }
110 }
111 
112 /******************************************************************************/
113 
115 {
117  if (optimizer_->getFunction() != 0)
119 }
120 
121 /******************************************************************************/
122 
124 {
125  callCount_++;
128  if (callCount_ <= burnin_)
129  return false;
130  for (unsigned int i = 0; i < newParametersEstimates_.size(); i++)
131  {
133  double lastEstimate = lastParametersEstimates_.getParameter(p.getName()).getValue();
134  double newEstimate = p.getValue();
135  double tol = NumTools::abs<double>(newEstimate - lastEstimate);
136  if (tol > tolerance_)
137  {
138  return false;
139  }
140  }
141  return true;
142 }
143 
144 /******************************************************************************/
145 
147 {
148  if (callCount_ > burnin_)
149  {
150  double maxTol = 0.;
151  for (unsigned int i = 0; i < newParametersEstimates_.size(); i++)
152  {
154  double lastEstimate = lastParametersEstimates_.getParameter(p.getName()).getValue();
155  double newEstimate = p.getValue();
156  double tol = NumTools::abs<double>(newEstimate - lastEstimate);
157  if (tol > maxTol)
158  maxTol = tol;
159  }
160  return maxTol;
161  }
162  else
163  {
164  return std::max(tolerance_, 1.);
165  }
166 }
167 
168 /******************************************************************************/
169 
171  const Optimizer* optimizer) :
173  lastFunctionValue_(-log(0.)),
174  newFunctionValue_(-log(0.))
175 {
176  init();
177 }
178 
180  const Optimizer* optimizer,
181  double tolerance) :
182  AbstractOptimizationStopCondition(optimizer, tolerance),
183  lastFunctionValue_(-log(0.)),
184  newFunctionValue_(-log(0.))
185 {
186  init();
187 }
188 
190  const Optimizer* optimizer,
191  int burnin) :
192  AbstractOptimizationStopCondition(optimizer, burnin),
193  lastFunctionValue_(-log(0.)),
194  newFunctionValue_(-log(0.))
195 {
196  init();
197 }
198 
200  const Optimizer* optimizer,
201  double tolerance,
202  int burnin) :
203  AbstractOptimizationStopCondition(optimizer, tolerance, burnin),
204  lastFunctionValue_(-log(0.)),
205  newFunctionValue_(-log(0.))
206 {
207  init();
208 }
209 
211 
212 /******************************************************************************/
213 
215 {
217  newFunctionValue_ = -log(0.);
218  if (optimizer_->getFunction() != 0)
219  {
221  }
222 }
223 
224 /******************************************************************************/
225 
227 {
228  callCount_++;
231  if (callCount_ <= burnin_)
232  return false;
233  double tol = NumTools::abs<double>(newFunctionValue_ - lastFunctionValue_);
234  return tol < tolerance_;
235 }
236 
237 /******************************************************************************/
238 
240 {
241  if (callCount_ > burnin_)
242  return NumTools::abs<double>(newFunctionValue_ - lastFunctionValue_);
243  else
244  return std::max(tolerance_, 1.);
245 }
246 
247 /******************************************************************************/
Partial implementation of the OptimizationStopCondition interface.
double callCount_
Count the number of times the isToleranceReached() function has been called.
double getCurrentTolerance() const
Get the current tolerance.
void init()
Initialize the condition.
double lastFunctionValue_
The last value of the function.
double newFunctionValue_
The new value of the function.
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
FunctionStopCondition(const Optimizer *optimizer)
This is the basal interface for all optimization methods.
Definition: Optimizer.h:119
virtual double getFunctionValue() const =0
Get the current function value.
virtual const ParameterList & getParameters() const =0
virtual const Function * getFunction() const =0
Get the current function being optimized.
virtual const Parameter & getParameter(const std::string &name) const
Get the parameter with name name.
size_t size() const
Definition: ParameterList.h:92
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:211
ParametersStopCondition(const Optimizer *optimizer)
ParameterList lastParametersEstimates_
The last estimates of the parameters.
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
void init()
Initialize the condition.
ParameterList newParametersEstimates_
The new estimates of the parameters.
double getCurrentTolerance() const
Get the current tolerance.