bpp-core3  3.0.0
Optimizer.h
Go to the documentation of this file.
1 //
2 // File: Optimizer.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-04 16:01:27
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_OPTIMIZER_H
42 #define BPP_NUMERIC_FUNCTION_OPTIMIZER_H
43 
44 
45 #include "../../Clonable.h"
46 #include "../../Io/OutputStream.h"
47 #include "../ParameterList.h"
48 #include "Functions.h"
50 
51 namespace bpp
52 {
57 {
58 private:
60 
61 public:
65  OptimizationEvent(Optimizer* optimizer) : optimizer_(optimizer) {}
68  {
70  return *this;
71  }
72  virtual ~OptimizationEvent() {}
73 
74 public:
79 
83  const Optimizer* getOptimizer() const { return optimizer_; }
84 };
85 
86 
96 {
97 public:
99  virtual ~OptimizationListener() {}
100 
101 public:
103  virtual void optimizationStepPerformed(const OptimizationEvent& event) = 0;
107  virtual bool listenerModifiesParameters() const = 0;
108 };
109 
110 
117 class Optimizer :
118  public virtual Clonable
119 {
120 public:
122  virtual ~Optimizer() {}
123 
124  Optimizer* clone() const = 0;
125 
126 public:
133  virtual void init(const ParameterList& params) = 0;
134 
138  virtual bool isInitialized() const = 0;
139 
146  virtual double step() = 0;
147 
151  virtual const ParameterList& getParameters() const = 0;
152 
157  virtual double getParameterValue(const std::string& name) const = 0;
164  virtual double getFunctionValue() const = 0;
165 
172  virtual double optimize() = 0;
173 
179  virtual void setFunction(Function* function) = 0;
180 
186  virtual const Function* getFunction() const = 0;
187 
193  virtual Function* getFunction() = 0;
194 
200  virtual bool hasFunction() const = 0;
201 
213  virtual void setMessageHandler(OutputStream* mh) = 0;
214 
218  virtual OutputStream* getMessageHandler() const = 0;
219 
231  virtual void setProfiler(OutputStream* profiler) = 0;
232 
236  virtual OutputStream* getProfiler() const = 0;
237 
244  virtual unsigned int getNumberOfEvaluations() const = 0;
245 
252  virtual void setStopCondition(const OptimizationStopCondition& stopCondition) = 0;
253 
260 
266  virtual const OptimizationStopCondition* getStopCondition() const = 0;
267 
274 
281 
288  virtual bool isToleranceReached() const = 0;
289 
295  virtual bool isMaximumNumberOfEvaluationsReached() const = 0;
296 
302  virtual void setMaximumNumberOfEvaluations(unsigned int max) = 0;
303 
316  virtual void setVerbose(unsigned int v) = 0;
317 
323  virtual unsigned int getVerbose() const = 0;
324 
330  virtual void setConstraintPolicy(const std::string& constraintPolicy) = 0;
331 
337  virtual std::string getConstraintPolicy() const = 0;
338 
347  virtual void addOptimizationListener(OptimizationListener* listener) = 0;
348 };
349 
350 
355  public OptimizationListener
356 {
357 private:
358  std::string backupFile_;
359 
360 public:
361  BackupListener(const string& backupFile) :
362  backupFile_(backupFile) {}
363 
364  virtual ~BackupListener() {}
365 
366 public:
368 
370  {
371  std::ofstream bck(backupFile_.c_str(), std::ios::out);
372  bck << "f(x)=" << setprecision(20) << event.getOptimizer()->getFunction()->getValue() << endl;
373  ParameterList pl = event.getOptimizer()->getFunction()->getParameters();
374  for (unsigned int i = 0; i < pl.size(); ++i)
375  {
376  bck << pl[i].getName() << "=" << setprecision(20) << pl[i].getValue() << std::endl;
377  }
378  bck.close();
379  }
380 
381  bool listenerModifiesParameters() const { return false; }
382 };
383 } // end of namespace bpp.
384 #endif // BPP_NUMERIC_FUNCTION_OPTIMIZER_H
Save intermediate optimization results to file.
Definition: Optimizer.h:356
std::string backupFile_
Definition: Optimizer.h:358
bool listenerModifiesParameters() const
Definition: Optimizer.h:381
BackupListener(const string &backupFile)
Definition: Optimizer.h:361
virtual ~BackupListener()
Definition: Optimizer.h:364
void optimizationStepPerformed(const OptimizationEvent &event)
Definition: Optimizer.h:369
void optimizationInitializationPerformed(const OptimizationEvent &event)
Definition: Optimizer.h:367
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
This is the function abstract class.
Definition: Functions.h:89
An event object which indicates that an optimization event occured.
Definition: Optimizer.h:57
OptimizationEvent(Optimizer *optimizer)
Definition: Optimizer.h:65
Optimizer * getOptimizer()
Definition: Optimizer.h:78
const Optimizer * getOptimizer() const
Definition: Optimizer.h:83
OptimizationEvent(const OptimizationEvent &oe)
Definition: Optimizer.h:66
virtual ~OptimizationEvent()
Definition: Optimizer.h:72
OptimizationEvent & operator=(const OptimizationEvent &oe)
Definition: Optimizer.h:67
Optimizer * optimizer_
Definition: Optimizer.h:59
The listener interface for receiving optimization events.
Definition: Optimizer.h:96
virtual void optimizationStepPerformed(const OptimizationEvent &event)=0
virtual void optimizationInitializationPerformed(const OptimizationEvent &event)=0
virtual ~OptimizationListener()
Definition: Optimizer.h:99
virtual bool listenerModifiesParameters() const =0
Interface for otimization stop condition objet.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:119
virtual double getParameterValue(const std::string &name) const =0
virtual double step()=0
Perform an optimization step.
virtual OutputStream * getMessageHandler() const =0
virtual bool isToleranceReached() const =0
Tell if the tolerance level is reached.
virtual void setConstraintPolicy(const std::string &constraintPolicy)=0
Set the constraint policy for this optimizer.
virtual OutputStream * getProfiler() const =0
virtual bool isMaximumNumberOfEvaluationsReached() const =0
Tell if the maximum number of function evaluations is reached.
virtual std::string getConstraintPolicy() const =0
Get the constraint policy for this optimizer.
virtual OptimizationStopCondition * getDefaultStopCondition()=0
Get the default stop condition of the optimization algorithm.
virtual void setMaximumNumberOfEvaluations(unsigned int max)=0
Set the maximum number of function evaluation to perform during optimization.
virtual void init(const ParameterList &params)=0
Set the initial values of the parameters.
virtual double getFunctionValue() const =0
Get the current function value.
virtual unsigned int getVerbose() const =0
Get the verbose level.
virtual bool isInitialized() const =0
virtual ~Optimizer()
Definition: Optimizer.h:122
virtual const OptimizationStopCondition * getDefaultStopCondition() const =0
Get the default stop condition of the optimization algorithm.
virtual void setVerbose(unsigned int v)=0
Set the verbose level.
virtual const OptimizationStopCondition * getStopCondition() const =0
Get the stop condition of the optimization algorithm.
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 void setFunction(Function *function)=0
Set the function to optimize.
Optimizer * clone() const =0
Create a copy of this object and send a pointer to it.
virtual void setStopCondition(const OptimizationStopCondition &stopCondition)=0
Set the stop condition of the optimization algorithm.
virtual unsigned int getNumberOfEvaluations() const =0
Get the number of function evaluations performed since the call of the init function.
virtual bool hasFunction() const =0
Tell if a funciton is associatied to this optimizer.
virtual OptimizationStopCondition * getStopCondition()=0
Get the stop condition of the optimization algorithm.
virtual Function * getFunction()=0
Get the current function being optimized.
virtual const ParameterList & getParameters() const =0
virtual void setProfiler(OutputStream *profiler)=0
Set the profiler for this optimizer.
virtual const Function * getFunction() const =0
Get the current function being optimized.
virtual void addOptimizationListener(OptimizationListener *listener)=0
Register a listener to this class.
OutputStream interface.
Definition: OutputStream.h:67
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92