bpp-core3  3.0.0
MetaOptimizer.cpp
Go to the documentation of this file.
1 //
2 // File: MetaOptimizer.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2004-11-17 17:22:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
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 
43 /**************************************************************************/
44 
45 #include "MetaOptimizer.h"
46 #include "../../App/ApplicationTools.h"
47 
48 using namespace bpp;
49 using namespace std;
50 
51 /**************************************************************************/
52 
53 string MetaOptimizerInfos::IT_TYPE_STEP = "step";
54 string MetaOptimizerInfos::IT_TYPE_FULL = "full";
55 
56 /**************************************************************************/
57 
59  Function* function,
60  MetaOptimizerInfos* desc,
61  unsigned int n) :
62  AbstractOptimizer(function),
63  optDesc_(desc), optParameters_(desc->getNumberOfOptimizers()),
64  nbParameters_(desc->getNumberOfOptimizers()), n_(n),
65  precisionStep_(-1.), stepCount_(0), initialValue_(-1.)
66 {
69  precisionStep_ = log10(getStopCondition()->getTolerance()) / n_;
71 }
72 
73 /**************************************************************************/
74 
76  const MetaOptimizer& opt) :
77  AbstractOptimizer(opt),
78  optDesc_(dynamic_cast<MetaOptimizerInfos*>(opt.optDesc_->clone())),
79  optParameters_(opt.optParameters_),
80  nbParameters_(opt.nbParameters_),
81  n_(opt.n_),
82  precisionStep_(opt.precisionStep_),
83  stepCount_(opt.stepCount_),
84  initialValue_(opt.initialValue_)
85 {}
86 
87 /**************************************************************************/
88 
90  const MetaOptimizer& opt)
91 {
93  optDesc_ = dynamic_cast<MetaOptimizerInfos*>(opt.optDesc_->clone());
96  n_ = opt.n_;
98  stepCount_ = opt.stepCount_;
100  return *this;
101 }
102 
103 /**************************************************************************/
104 
106 {
107  // Delete all optimizers:
108  delete optDesc_;
109 }
110 
111 /**************************************************************************/
112 
113 void MetaOptimizer::doInit(const ParameterList& parameters)
114 {
116  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
117  {
118  optParameters_[i].reset();
119  for (size_t j = 0; j < optDesc_->getParameterNames(i).size(); j++)
120  {
121  string pname = optDesc_->getParameterNames(i)[j];
122  if (parameters.hasParameter(pname))
123  {
124  optParameters_[i].addParameter(parameters.getParameter(pname));
125  }
126  }
127  nbParameters_[i] = optParameters_[i].size();
128  }
129 
130  // Initialize optimizers:
131  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
132  {
133  if (nbParameters_[i] > 0)
134  {
135  Optimizer* opt = optDesc_->getOptimizer(i);
136  dynamic_cast<AbstractOptimizer*>(opt)->updateParameters(updateParameters());
137  opt->setProfiler(getProfiler());
140  opt->setVerbose(getVerbose() > 0 ? getVerbose() - 1 : 0);
141  }
142  }
143 
144  // Actualize parameters:
146 
149  // Reset counter:
150  stepCount_ = 1;
151  // Recompute step if precision has changed:
152  precisionStep_ = (log10(getStopCondition()->getTolerance()) - log10(initialValue_)) / n_;
153 }
154 
155 /**************************************************************************/
156 
158 {
159  stepCount_++;
160 
161  int tolTest = 0;
162  double tol = getStopCondition()->getTolerance();
163  if (stepCount_ <= n_)
164  {
165  tol = initialValue_ * pow(10, stepCount_ * precisionStep_);
166  }
167 
168  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
169  {
170  if (nbParameters_[i] > 0)
171  {
173  {
174  (ApplicationTools::message->endLine() << optDesc_->getName(i)).endLine();
175  ApplicationTools::message->flush();
176  }
178  dynamic_cast<DerivableFirstOrder*>(getFunction())->enableFirstOrderDerivatives(true);
180  dynamic_cast<DerivableSecondOrder*>(getFunction())->enableSecondOrderDerivatives(true);
181 
182  optParameters_[i].matchParametersValues(getParameters());
183  Optimizer* opt = optDesc_->getOptimizer(i);
184  opt->getStopCondition()->setTolerance(tol);
185  opt->init(optParameters_[i]);
187  opt->step();
189  opt->optimize();
190  else
191  throw Exception("MetaOptimizer::step. Unknown iteration type specified.");
194  dynamic_cast<DerivableFirstOrder*>(getFunction())->enableFirstOrderDerivatives(false);
196  dynamic_cast<DerivableSecondOrder*>(getFunction())->enableSecondOrderDerivatives(false);
197  if (getVerbose() > 1)
198  cout << endl;
200  }
201  tolTest += nbParameters_[i] > 0 ? 1 : 0;
202  }
203  tolIsReached_ = (tolTest == 1);
204 
205  return getFunction()->getValue();
206 }
207 
208 /**************************************************************************/
Partial implementation of the Optimizer interface.
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
OutputStream * getMessageHandler() const
OutputStream * getProfiler() const
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
unsigned int nbEval_
The current number of function evaluations achieved.
const ParameterList & getParameters() const
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
ParameterList & getParameters_()
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
const Function * getFunction() const
Get the current function being optimized.
std::string getConstraintPolicy() const
Get the constraint policy for this optimizer.
bool tolIsReached_
Tell if the tolerance level has been reached.
unsigned int getVerbose() const
Get the verbose level.
bool updateParameters() const
Tell if we shall update all parameters after one optimization step.
AbstractOptimizer & operator=(const AbstractOptimizer &opt)
void setDefaultStopCondition_(OptimizationStopCondition *osc)
static std::shared_ptr< OutputStream > message
The output stream where messages have to be displayed.
This is the abstract class for first order derivable functions.
Definition: Functions.h:133
This is the abstract class for second order derivable functions.
Definition: Functions.h:188
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Stop condition on function value.
This is the function abstract class.
Definition: Functions.h:89
virtual double getValue() const =0
Get the value of the function at the current point.
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
Provide a list of optimizer and corresponding options to be used with the MetaOptimizer class.
Definition: MetaOptimizer.h:57
virtual const std::string & getName(size_t i) const
virtual Optimizer * getOptimizer(size_t i)
virtual bool requiresSecondOrderDerivatives(size_t i) const
virtual size_t getNumberOfOptimizers() const
static std::string IT_TYPE_STEP
Definition: MetaOptimizer.h:59
virtual std::string & getIterationType(size_t i)
MetaOptimizerInfos * clone() const
Create a copy of this object and send a pointer to it.
virtual bool requiresFirstOrderDerivatives(size_t i) const
virtual std::vector< std::string > & getParameterNames(size_t i)
static std::string IT_TYPE_FULL
Definition: MetaOptimizer.h:60
Meta-optimizer.
double doStep()
This function is called by the step() method and contains all calculations.
MetaOptimizer & operator=(const MetaOptimizer &opt)
std::vector< size_t > nbParameters_
MetaOptimizerInfos * optDesc_
MetaOptimizer(Function *function, MetaOptimizerInfos *desc, unsigned int n=1)
Build a new MetaOptimizer object.
void doInit(const ParameterList &parameters)
This function is called by the init() method and contains all calculations.
std::vector< ParameterList > optParameters_
unsigned int stepCount_
virtual void setTolerance(double tolerance)=0
Set the tolerance parameter.
virtual double getTolerance() const =0
Get the tolerance parameter.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:119
virtual double step()=0
Perform an optimization step.
virtual void setConstraintPolicy(const std::string &constraintPolicy)=0
Set the constraint policy for this optimizer.
virtual void init(const ParameterList &params)=0
Set the initial values of the parameters.
virtual void setVerbose(unsigned int v)=0
Set the verbose level.
virtual void setMessageHandler(OutputStream *mh)=0
Set the message handler for this optimizer.
virtual double optimize()=0
Perform as many optimization steps untill the stop condition is met.
virtual unsigned int getNumberOfEvaluations() const =0
Get the number of function evaluations performed since the call of the init function.
virtual OptimizationStopCondition * getStopCondition()=0
Get the stop condition of the optimization algorithm.
virtual const ParameterList & getParameters() const =0
virtual void setProfiler(OutputStream *profiler)=0
Set the profiler for this optimizer.
The parameter list object.
Definition: ParameterList.h:65
virtual bool hasParameter(const std::string &name) const
virtual const Parameter & getParameter(const std::string &name) const
Get the parameter with name name.
virtual bool matchParametersValues(const ParameterList &params, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.