bpp-core  2.2.0
MetaOptimizer.cpp
Go to the documentation of this file.
1 //
2 // File: MetaOptimizer.cpp
3 // Created by: Julien Dutheil
4 // Created on: Fri Oct 12 16:05 2007
5 // From file: NewtonBrentMetaOptimizer.cpp
6 // Created on: Tue Nov 17 17:22 2004
7 //
8 //
9 
10 /*
11 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
12 
13 This software is a computer program whose purpose is to provide classes
14 for phylogenetic data analysis.
15 
16 This software is governed by the CeCILL license under French law and
17 abiding by the rules of distribution of free software. You can use,
18 modify and/ or redistribute the software under the terms of the CeCILL
19 license as circulated by CEA, CNRS and INRIA at the following URL
20 "http://www.cecill.info".
21 
22 As a counterpart to the access to the source code and rights to copy,
23 modify and redistribute granted by the license, users are provided only
24 with a limited warranty and the software's author, the holder of the
25 economic rights, and the successive licensors have only limited
26 liability.
27 
28 In this respect, the user's attention is drawn to the risks associated
29 with loading, using, modifying and/or developing or reproducing the
30 software by the user in light of its specific status of free software,
31 that may mean that it is complicated to manipulate, and that also
32 therefore means that it is reserved for developers and experienced
33 professionals having in-depth computer knowledge. Users are therefore
34 encouraged to load and test the software's suitability as regards their
35 requirements in conditions enabling the security of their systems and/or
36 data to be ensured and, more generally, to use and operate it in the
37 same conditions as regards security.
38 
39 The fact that you are presently reading this means that you have had
40 knowledge of the CeCILL license and that you accept its terms.
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  throw (Exception)
115 {
116  optParameters_.resize(optDesc_->getNumberOfOptimizers());
117  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
118  {
119  optParameters_[i].reset();
120  for (size_t j = 0; j < optDesc_->getParameterNames(i).size(); j++)
121  {
122  string pname = optDesc_->getParameterNames(i)[j];
123  if (parameters.hasParameter(pname))
124  {
125  optParameters_[i].addParameter(parameters.getParameter(pname));
126  }
127  }
128  nbParameters_[i] = optParameters_[i].size();
129  }
130 
131  // Initialize optimizers:
132  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
133  {
134  if (nbParameters_[i] > 0)
135  {
136  Optimizer * opt = optDesc_->getOptimizer(i);
137  dynamic_cast<AbstractOptimizer*>(opt)->updateParameters(updateParameters());
138  opt->setProfiler(getProfiler());
139  opt->setMessageHandler(getMessageHandler());
140  opt->setConstraintPolicy(getConstraintPolicy());
141  opt->setVerbose(getVerbose() > 0 ? getVerbose() - 1 : 0);
142  }
143  }
144 
145  // Actualize parameters:
146  getParameters_().matchParametersValues(getFunction()->getParameters());
147 
148  getFunction()->setParameters(getParameters());
149  initialValue_ = getFunction()->getValue();
150  // Reset counter:
151  stepCount_ = 1;
152  // Recompute step if precision has changed:
153  precisionStep_ = (log10(getStopCondition()->getTolerance()) - log10(initialValue_)) / n_;
154 }
155 
156 /**************************************************************************/
157 
159 {
160  stepCount_++;
161 
162  int tolTest = 0;
163  double tol = getStopCondition()->getTolerance();
164  if (stepCount_ <= n_)
165  {
166  tol = initialValue_ * pow(10, stepCount_ * precisionStep_);
167  }
168 
169  for (unsigned int i = 0; i < optDesc_->getNumberOfOptimizers(); i++)
170  {
171  if (nbParameters_[i] > 0)
172  {
174  {
175  (ApplicationTools::message->endLine() << optDesc_->getName(i)).endLine();
177  }
179  dynamic_cast<DerivableFirstOrder*>(getFunction())->enableFirstOrderDerivatives(true);
181  dynamic_cast<DerivableSecondOrder*>(getFunction())->enableSecondOrderDerivatives(true);
182 
183  optParameters_[i].matchParametersValues(getParameters());
184  Optimizer * opt = optDesc_->getOptimizer(i);
185  opt->getStopCondition()->setTolerance(tol);
186  opt->init(optParameters_[i]);
188  opt->step();
190  opt->optimize();
191  else 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) cout << endl;
199  }
200  tolTest += nbParameters_[i] > 0 ? 1 : 0;
201  }
202  tolIsReached_ = (tolTest == 1);
203 
204  return getFunction()->getValue();
205 }
206 
207 /**************************************************************************/
208 
virtual Optimizer * getOptimizer(size_t i)
virtual void setMessageHandler(OutputStream *mh)=0
Set the message handler for this optimizer.
AbstractOptimizer & operator=(const AbstractOptimizer &opt)
virtual size_t getNumberOfOptimizers() const
MetaOptimizer & operator=(const MetaOptimizer &opt)
virtual OutputStream & flush()=0
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
unsigned int getVerbose() const
Get the verbose level.
virtual const ParameterList & getParameters() const =0
This class allows to perform a correspondence analysis.
void doInit(const ParameterList &parameters)
This function is called by the init() method and contains all calculations.
virtual bool matchParametersValues(const ParameterList &params, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.
This is the function abstract class.
Definition: Functions.h:86
virtual double optimize()=0
Perform as many optimization steps untill the stop condition is met.
double doStep()
This function is called by the step() method and contains all calculations.
MetaOptimizerInfos * optDesc_
static std::string IT_TYPE_STEP
Definition: MetaOptimizer.h:61
STL namespace.
ParameterList & getParameters_()
virtual bool requiresFirstOrderDerivatives(size_t i) const
Stop condition on function value.
virtual void setTolerance(double tolerance)=0
Set the tolerance parameter.
static std::string IT_TYPE_FULL
Definition: MetaOptimizer.h:62
The parameter list object.
Definition: ParameterList.h:61
virtual bool requiresSecondOrderDerivatives(size_t i) const
bool tolIsReached_
Tell if the tolerance level has been reached.
std::vector< ParameterList > optParameters_
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
MetaOptimizer(Function *function, MetaOptimizerInfos *desc, unsigned int n=1)
Build a new MetaOptimizer object.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
static OutputStream * message
The output stream where messages have to be displayed.
virtual std::string & getIterationType(size_t i)
const Function * getFunction() const
Get the current function being optimized.
const ParameterList & getParameters() const
virtual double step()=0
Perform an optimization step.
virtual unsigned int getNumberOfEvaluations() const =0
Get the number of function evaluations performed since the call of the init function.
virtual OutputStream & endLine()=0
Meta-optimizer.
virtual void setVerbose(unsigned int v)=0
Set the verbose level.
Exception base class.
Definition: Exceptions.h:57
Provide a list of optimizer and corresponding options to be used with the MetaOptimizer class...
Definition: MetaOptimizer.h:57
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
virtual double getTolerance() const =0
Get the tolerance parameter.
virtual void setProfiler(OutputStream *profiler)=0
Set the profiler for this optimizer.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:125
virtual const std::string & getName(size_t i) const
virtual void init(const ParameterList &params)=0
Set the initial values of the parameters.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
MetaOptimizerInfos * clone() const
Create a copy of this object and send a pointer to it.
unsigned int stepCount_
std::vector< size_t > nbParameters_
virtual double getValue() const =0
Get the value of the function at the current point.
virtual void setConstraintPolicy(const std::string &constraintPolicy)=0
Set the constraint policy for this optimizer.
virtual OptimizationStopCondition * getStopCondition()=0
Get the stop condition of the optimization algorithm.