bpp-core3  3.0.0
AbstractOptimizer.h
Go to the documentation of this file.
1 //
2 // File: AbstractOptimizer.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-12-22 12:18:09
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 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 #ifndef BPP_NUMERIC_FUNCTION_ABSTRACTOPTIMIZER_H
42 #define BPP_NUMERIC_FUNCTION_ABSTRACTOPTIMIZER_H
43 
44 
45 #include "Optimizer.h"
46 
47 namespace bpp
48 {
61  public virtual Optimizer
62 {
63 private:
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:
130  unsigned int nbEvalMax_;
131 
135  unsigned int nbEval_;
136 
141 
149 
150 public:
151  AbstractOptimizer(Function* function = 0);
152 
154 
156 
158  {
159  delete stopCondition_;
160  delete defaultStopCondition_;
161  }
162 
163 public:
174  void init(const ParameterList& params);
180  double step();
186  double optimize();
187  bool isInitialized() const { return isInitialized_; }
188  const ParameterList& getParameters() const { return parameters_; }
189  double getParameterValue(const std::string& name) const { return parameters_.getParameterValue(name); }
190  void setFunction(Function* function)
191  {
192  function_ = function;
193  if (function) stopCondition_->init();
194  }
195  const Function* getFunction() const { return function_; }
197  bool hasFunction() const { return function_ != 0; }
198  double getFunctionValue() const
199  {
200  if (!function_) throw NullPointerException("AbstractOptimizer::getFunctionValue. No function associated to this optimizer.");
201  return currentValue_;
202  }
203 
206  void setProfiler(OutputStream* profiler) { profiler_ = profiler; }
207  OutputStream* getProfiler() const { return profiler_; }
208 
209  unsigned int getNumberOfEvaluations() const { return nbEval_; }
210  void setStopCondition(const OptimizationStopCondition& stopCondition)
211  {
212  stopCondition_ = dynamic_cast<OptimizationStopCondition*>(stopCondition.clone());
213  }
218  bool isToleranceReached() const { return tolIsReached_; }
220  void setMaximumNumberOfEvaluations(unsigned int max) { nbEvalMax_ = max; }
221  void setVerbose(unsigned int v) { verbose_ = v; }
222  unsigned int getVerbose() const { return verbose_; }
223  void setConstraintPolicy(const std::string& constraintPolicy) { constraintPolicy_ = constraintPolicy; }
224  std::string getConstraintPolicy() const { return constraintPolicy_; }
226  {
227  if (listener)
228  listeners_.push_back(listener);
229  }
241  void updateParameters(bool yn) { updateParameters_ = yn; }
242 
252  bool updateParameters() const { return updateParameters_; }
253 
259  void setOptimizationProgressCharacter(const std::string& c) { stepChar_ = c; }
263  const std::string& getOptimizationProgressCharacter() const { return stepChar_; }
264 
265 protected:
271  virtual void doInit(const ParameterList& params) = 0;
272 
278  virtual double doStep() = 0;
279 
289  void autoParameter();
290 
294  void ignoreConstraints();
295 
301  void profile(double v);
302 
308  void profile(unsigned int v);
309 
315  void profile(const std::string& s);
316 
322  void profileln(double v);
323 
329  void profileln(unsigned int v);
330 
336  void profileln(const std::string& s);
337 
344  void printPoint(const ParameterList& params, double value);
345 
351  void printMessage(const std::string& message);
352 
361 
370 
371  bool listenerModifiesParameters() const;
374 protected:
376  Parameter& getParameter_(size_t i) { return parameters_[i]; }
379  {
380  defaultStopCondition_ = osc;
381  }
382 };
383 } // end of namespace bpp.
384 #endif // BPP_NUMERIC_FUNCTION_ABSTRACTOPTIMIZER_H
Partial implementation of the Optimizer interface.
const std::string & getOptimizationProgressCharacter() const
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
void setFunction(Function *function)
Set the function to optimize.
void setProfiler(OutputStream *profiler)
Set the profiler for this optimizer.
void ignoreConstraints()
Remove the constraints of all the arguments.
Function * function_
The function to optimize.
void setConstraintPolicy(const std::string &constraintPolicy)
Set the constraint policy for this optimizer.
const OptimizationStopCondition * getDefaultStopCondition() const
Get the default stop condition of the optimization algorithm.
OutputStream * getMessageHandler() const
std::vector< OptimizationListener * > listeners_
OutputStream * getProfiler() const
void setMessageHandler(OutputStream *mh)
Set the message handler for this optimizer.
OptimizationStopCondition * stopCondition_
The stoping condition to use while optimizing.
void fireOptimizationInitializationPerformed(const OptimizationEvent &event)
Notify all listeners that optimizer initialization was performed.
void printPoint(const ParameterList &params, double value)
Print parameters and corresponding function evaluation to profiler.
double step()
Basic implementation.
double optimize()
Basic implementation.
void printMessage(const std::string &message)
Give a message to print to the message handler.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
Parameter & getParameter_(size_t i)
bool listenerModifiesParameters() const
void profileln(double v)
Print to the profile if there is one and end line.
Function * getFunction()
Get the current function being optimized.
double getParameterValue(const std::string &name) const
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
void fireOptimizationStepPerformed(const OptimizationEvent &event)
Notify all listeners that an optimization step was performed.
OutputStream * profiler_
The profiler.
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.
double getFunctionValue() const
Get the current function value.
OptimizationStopCondition * defaultStopCondition_
The default stoping condition to use while optimizing.
AbstractOptimizer(Function *function=0)
void init(const ParameterList &params)
Basic implementation.
bool isToleranceReached() const
Tell if the tolerance level is reached.
const OptimizationStopCondition * getStopCondition() const
Get the stop condition of the optimization algorithm.
void setVerbose(unsigned int v)
Set the verbose level.
unsigned int getNumberOfEvaluations() const
Get the number of function evaluations performed since the call of the init function.
OutputStream * messageHandler_
The message handler.
ParameterList & getParameters_()
unsigned int verbose_
State of the verbose mode: > 0 = enabled.
std::string constraintPolicy_
The constraint policy.
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
const Function * getFunction() const
Get the current function being optimized.
void setMaximumNumberOfEvaluations(unsigned int max)
Set the maximum number of function evaluation to perform during optimization.
bool isInitialized_
Check if the optimizer have been feeded with initial parameters values.
std::string getConstraintPolicy() const
Get the constraint policy for this optimizer.
double currentValue_
The current value of the function.
bool isMaximumNumberOfEvaluationsReached() const
Tell if the maximum number of function evaluations is reached.
bool tolIsReached_
Tell if the tolerance level has been reached.
unsigned int getVerbose() const
Get the verbose level.
virtual void doInit(const ParameterList &params)=0
This function is called by the init() method and contains all calculations.
void profile(double v)
Print to the profile if there is one.
void addOptimizationListener(OptimizationListener *listener)
Register a listener to this class.
bool updateParameters() const
Tell if we shall update all parameters after one optimization step.
void updateParameters(bool yn)
Tell if we shall update all parameters after one optimization step.
void autoParameter()
Build a list of AutoParameter instead of Parameter.
bool hasFunction() const
Tell if a funciton is associatied to this optimizer.
AbstractOptimizer & operator=(const AbstractOptimizer &opt)
ParameterList parameters_
The parameters that will be optimized.
virtual double doStep()=0
This function is called by the step() method and contains all calculations.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
This is the function abstract class.
Definition: Functions.h:89
The base class exception for NULL pointer error. This exception may be thrown when an unexpected NULL...
Definition: Exceptions.h:95
An event object which indicates that an optimization event occured.
Definition: Optimizer.h:57
The listener interface for receiving optimization events.
Definition: Optimizer.h:96
Interface for otimization stop condition objet.
OptimizationStopCondition * clone() const =0
Create a copy of this object and send a pointer to it.
virtual void init()=0
Initialize the condition.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:119
OutputStream interface.
Definition: OutputStream.h:67
The parameter list object.
Definition: ParameterList.h:65
virtual double getParameterValue(const std::string &name) const
Get the value of the parameter with name name.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135