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