bpp-core  2.2.0
AbstractOptimizer.h
Go to the documentation of this file.
1 //
2 // File: AbstractOptimizer.h
3 // Created by: Julien Dutheil
4 // Created on: Mon Dec 22 12:18:09 2003
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 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 
40 #ifndef _ABSTRACTOPTIMIZER_H_
41 #define _ABSTRACTOPTIMIZER_H_
42 
43 #include "Optimizer.h"
44 
45 namespace bpp
46 {
47 
60  public virtual Optimizer
61 {
62  private:
63 
68 
73 
78 
83 
94  std::string constraintPolicy_;
95 
100 
105 
111  unsigned int verbose_;
112 
117 
118  time_t startTime_;
119 
120  std::vector<OptimizationListener*> listeners_;
121 
123 
124  std::string stepChar_;
125 
126  protected:
127 
131  unsigned int nbEvalMax_;
132 
136  unsigned int nbEval_;
137 
142 
150 
151  public:
152  AbstractOptimizer(Function* function = 0);
153 
155 
157 
159  {
160  delete stopCondition_;
161  delete defaultStopCondition_;
162  }
163 
164  public:
165 
176  void init(const ParameterList& params) throw (Exception);
182  double step() throw (Exception);
188  double optimize() throw (Exception);
189  bool isInitialized() const { return isInitialized_; }
190  const ParameterList& getParameters() const { return parameters_; }
191  double getParameterValue(const std::string& name) const { return parameters_.getParameterValue(name); }
192  void setFunction(Function* function)
193  {
194  function_ = function;
195  if (function) stopCondition_->init();
196  }
197  const Function* getFunction() const { return function_; }
199  bool hasFunction() const { return function_ != 0; }
200  double getFunctionValue() const throw (NullPointerException)
201  {
202  if (!function_) throw NullPointerException("AbstractOptimizer::getFunctionValue. No function associated to this optimizer.");
203  return currentValue_;
204  }
205 
208  void setProfiler(OutputStream* profiler) { profiler_ = profiler; }
209  OutputStream* getProfiler() const { return profiler_; }
210 
211  unsigned int getNumberOfEvaluations() const { return nbEval_; }
212  void setStopCondition(const OptimizationStopCondition& stopCondition)
213  {
214  stopCondition_ = dynamic_cast<OptimizationStopCondition*>(stopCondition.clone());
215  }
220  bool isToleranceReached() const { return tolIsReached_; }
222  void setMaximumNumberOfEvaluations(unsigned int max) { nbEvalMax_ = max; }
223  void setVerbose(unsigned int v) { verbose_ = v; }
224  unsigned int getVerbose() const { return verbose_; }
225  void setConstraintPolicy(const std::string& constraintPolicy) { constraintPolicy_ = constraintPolicy; }
226  std::string getConstraintPolicy() const { return constraintPolicy_; }
228  {
229  if (listener)
230  listeners_.push_back(listener);
231  }
243  void updateParameters(bool yn) { updateParameters_ = yn; }
244 
254  bool updateParameters() const { return updateParameters_; }
255 
261  void setOptimizationProgressCharacter(const std::string& c) { stepChar_ = c; }
265  const std::string& getOptimizationProgressCharacter() const { return stepChar_; }
266 
267  protected:
268 
274  virtual void doInit(const ParameterList& params) throw (Exception) = 0;
275 
281  virtual double doStep() throw (Exception) = 0;
282 
292  void autoParameter();
293 
297  void ignoreConstraints();
298 
304  void profile(double v);
305 
311  void profile(unsigned int v);
312 
318  void profile(const std::string& s);
319 
325  void profileln(double v);
326 
332  void profileln(unsigned int v);
333 
339  void profileln(const std::string& s);
340 
347  void printPoint(const ParameterList& params, double value);
348 
354  void printMessage(const std::string& message);
355 
364 
373 
374  bool listenerModifiesParameters() const;
377  protected:
379  Parameter& getParameter_(size_t i) { return parameters_[i]; }
382  {
383  defaultStopCondition_ = osc;
384  }
385 
386 };
387 
388 } //end of namespace bpp.
389 
390 #endif //_ABSTRACTOPTIMIZER_H_
391 
bool isInitialized_
Check if the optimizer have been feeded with initial parameters values.
virtual void init()=0
Initialize the condition.
OptimizationStopCondition * clone() const =0
Create a copy of this object and send a pointer to it.
unsigned int getNumberOfEvaluations() const
Get the number of function evaluations performed since the call of the init function.
AbstractOptimizer & operator=(const AbstractOptimizer &opt)
virtual void doInit(const ParameterList &params)=0
This function is called by the init() method and contains all calculations.
OptimizationStopCondition * defaultStopCondition_
The default stoping condition to use while optimizing.
std::vector< OptimizationListener * > listeners_
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
virtual double getParameterValue(const std::string &name) const
Get the value of the parameter with name name.
unsigned int getVerbose() const
Get the verbose level.
This class allows to perform a correspondence analysis.
double optimize()
Basic implementation.
Function * function_
The function to optimize.
OutputStream * getProfiler() const
bool updateParameters() const
Tell if we shall update all parameters after one optimization step.
This is the function abstract class.
Definition: Functions.h:86
Interface for otimization stop condition objet.
void profileln(double v)
Print to the profile if there is one and end line.
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
ParameterList & getParameters_()
const OptimizationStopCondition * getDefaultStopCondition() const
Get the default stop condition of the optimization algorithm.
void ignoreConstraints()
Remove the constraints of all the arguments.
void setMaximumNumberOfEvaluations(unsigned int max)
Set the maximum number of function evaluation to perform during optimization.
The parameter list object.
Definition: ParameterList.h:61
bool hasFunction() const
Tell if a funciton is associatied to this optimizer.
double getFunctionValue() const
Get the current function value.
bool tolIsReached_
Tell if the tolerance level has been reached.
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
OutputStream * profiler_
The profiler.
void updateParameters(bool yn)
Tell if we shall update all parameters after one optimization step.
The listener interface for receiving optimization events.
Definition: Optimizer.h:100
void addOptimizationListener(OptimizationListener *listener)
Register a listener to this class.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
double step()
Basic implementation.
Parameter & getParameter_(size_t i)
const Function * getFunction() const
Get the current function being optimized.
const ParameterList & getParameters() const
bool listenerModifiesParameters() const
double getParameterValue(const std::string &name) const
unsigned int verbose_
State of the verbose mode: > 0 = enabled.
The base class exception for NULL pointer error.
Definition: Exceptions.h:123
OutputStream interface.
Definition: OutputStream.h:64
void printPoint(const ParameterList &params, double value)
Print parameters and corresponding function evaluation to profiler.
Function * getFunction()
Get the current function being optimized.
An event object which indicates that an optimization event occured.
Definition: Optimizer.h:55
const OptimizationStopCondition * getStopCondition() const
Get the stop condition of the optimization algorithm.
void setFunction(Function *function)
Set the function to optimize.
OptimizationStopCondition * stopCondition_
The stoping condition to use while optimizing.
Exception base class.
Definition: Exceptions.h:57
void setProfiler(OutputStream *profiler)
Set the profiler for this optimizer.
void setVerbose(unsigned int v)
Set the verbose level.
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
bool isMaximumNumberOfEvaluationsReached() const
Tell if the maximum number of function evaluations is reached.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
void fireOptimizationInitializationPerformed(const OptimizationEvent &event)
Notify all listeners that optimizer initialization was performed.
double currentValue_
The current value of the function.
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
OutputStream * getMessageHandler() const
void setConstraintPolicy(const std::string &constraintPolicy)
Set the constraint policy for this optimizer.
std::string constraintPolicy_
The constraint policy.
std::string getConstraintPolicy() const
Get the constraint policy for this optimizer.
void setMessageHandler(OutputStream *mh)
Set the message handler for this optimizer.
AbstractOptimizer(Function *function=0)
virtual double doStep()=0
This function is called by the step() method and contains all calculations.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:125
OutputStream * messageHandler_
The message handler.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
void profile(double v)
Print to the profile if there is one.
const std::string & getOptimizationProgressCharacter() const
bool isToleranceReached() const
Tell if the tolerance level is reached.
void fireOptimizationStepPerformed(const OptimizationEvent &event)
Notify all listeners that an optimization step was performed.
void printMessage(const std::string &message)
Give a message to print to the message handler.
ParameterList parameters_
The parameters that will be optimized.
void autoParameter()
Build a list of AutoParameter instead of Parameter.
void init(const ParameterList &params)
Basic implementation.