bpp-core  2.2.0
Optimizer.h
Go to the documentation of this file.
1 //
2 // File: Optimizer.h
3 // Created by: Julien Dutheil
4 // Created on: Tue Nov 4 16:01:27 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 _OPTIMIZER_H_
41 #define _OPTIMIZER_H_
42 
43 #include "Functions.h"
44 #include "../ParameterList.h"
46 #include "../../Clonable.h"
47 #include "../../Io/OutputStream.h"
48 
49 namespace bpp
50 {
51 
56  {
57  private:
59 
60  public:
61 
65  OptimizationEvent(Optimizer* optimizer): optimizer_(optimizer) {}
68  {
70  return *this;
71  }
72  virtual ~OptimizationEvent() {}
73 
74  public:
75 
80 
84  const Optimizer* getOptimizer() const { return optimizer_; }
85  };
86 
87 
88 
89 
90 
91 
101  {
102  public:
105 
106  public:
107  virtual void optimizationInitializationPerformed(const OptimizationEvent& event) = 0;
108  virtual void optimizationStepPerformed(const OptimizationEvent& event) = 0;
112  virtual bool listenerModifiesParameters() const = 0;
113  };
114 
115 
116 
117 
118 
125  class Optimizer:
126  public virtual Clonable
127  {
128  public:
130  virtual ~Optimizer() {}
131 
132 #if defined(NO_VIRTUAL_COV)
133  Clonable* clone() const = 0;
134 #else
135  Optimizer* clone() const = 0;
136 #endif
137 
138  public:
139 
146  virtual void init(const ParameterList& params) throw (Exception) = 0;
147 
151  virtual bool isInitialized() const = 0;
152 
159  virtual double step() throw (Exception) = 0;
160 
164  virtual const ParameterList& getParameters() const = 0;
165 
170  virtual double getParameterValue(const std::string& name) const = 0;
177  virtual double getFunctionValue() const throw (NullPointerException) = 0;
178 
185  virtual double optimize() throw (Exception) = 0;
186 
192  virtual void setFunction(Function* function) = 0;
193 
199  virtual const Function* getFunction() const = 0;
200 
206  virtual Function* getFunction() = 0;
207 
213  virtual bool hasFunction() const = 0;
214 
226  virtual void setMessageHandler(OutputStream* mh) = 0;
227 
231  virtual OutputStream* getMessageHandler() const = 0;
232 
244  virtual void setProfiler(OutputStream* profiler) = 0;
245 
249  virtual OutputStream* getProfiler() const = 0;
250 
257  virtual unsigned int getNumberOfEvaluations() const = 0;
258 
265  virtual void setStopCondition(const OptimizationStopCondition& stopCondition) = 0;
266 
273 
279  virtual const OptimizationStopCondition* getStopCondition() const = 0;
280 
287 
293  virtual const OptimizationStopCondition* getDefaultStopCondition() const = 0;
294 
301  virtual bool isToleranceReached() const = 0;
302 
308  virtual bool isMaximumNumberOfEvaluationsReached() const = 0;
309 
315  virtual void setMaximumNumberOfEvaluations(unsigned int max) = 0;
316 
329  virtual void setVerbose(unsigned int v) = 0;
330 
336  virtual unsigned int getVerbose() const = 0;
337 
343  virtual void setConstraintPolicy(const std::string & constraintPolicy) = 0;
344 
350  virtual std::string getConstraintPolicy() const = 0;
351 
360  virtual void addOptimizationListener(OptimizationListener * listener) = 0;
361 
362  };
363 
364 
365 
366 
367 
372  public OptimizationListener
373  {
374  private:
375  std::string backupFile_;
376 
377  public:
378  BackupListener(const string& backupFile):
379  backupFile_(backupFile) {}
380 
381  virtual ~BackupListener() {}
382 
383  public:
385 
387  std::ofstream bck(backupFile_.c_str(), std::ios::out);
388  bck << "f(x)=" << setprecision(20) << event.getOptimizer()->getFunction()->getValue() << endl;
389  ParameterList pl = event.getOptimizer()->getFunction()->getParameters();
390  for (unsigned int i = 0; i < pl.size(); ++i) {
391  bck << pl[i].getName() << "=" << setprecision(20) << pl[i].getValue() << std::endl;
392  }
393  bck.close();
394  }
395 
396  bool listenerModifiesParameters() const { return false; };
397  };
398 
399 } //end of namespace bpp.
400 
401 #endif //_OPTIMIZER_H_
402 
virtual bool hasFunction() const =0
Tell if a funciton is associatied to this optimizer.
virtual void setMessageHandler(OutputStream *mh)=0
Set the message handler for this optimizer.
std::string backupFile_
Definition: Optimizer.h:375
const Optimizer * getOptimizer() const
Definition: Optimizer.h:84
virtual OutputStream * getMessageHandler() const =0
bool listenerModifiesParameters() const
Definition: Optimizer.h:396
virtual const ParameterList & getParameters() const =0
This class allows to perform a correspondence analysis.
virtual const Function * getFunction() const =0
Get the current function being optimized.
virtual bool isToleranceReached() const =0
Tell if the tolerance level is reached.
This is the function abstract class.
Definition: Functions.h:86
OptimizationEvent(Optimizer *optimizer)
Definition: Optimizer.h:65
virtual double optimize()=0
Perform as many optimization steps untill the stop condition is met.
OptimizationEvent(const OptimizationEvent &oe)
Definition: Optimizer.h:66
Interface for otimization stop condition objet.
OptimizationEvent & operator=(const OptimizationEvent &oe)
Definition: Optimizer.h:67
size_t size() const
Definition: ParameterList.h:90
STL namespace.
virtual std::string getConstraintPolicy() const =0
Get the constraint policy for this optimizer.
void optimizationStepPerformed(const OptimizationEvent &event)
Definition: Optimizer.h:386
virtual OptimizationStopCondition * getDefaultStopCondition()=0
Get the default stop condition of the optimization algorithm.
virtual ~OptimizationListener()
Definition: Optimizer.h:104
Optimizer * getOptimizer()
Definition: Optimizer.h:79
The parameter list object.
Definition: ParameterList.h:61
virtual bool listenerModifiesParameters() const =0
virtual double getFunctionValue() const =0
Get the current function value.
virtual OutputStream * getProfiler() const =0
The listener interface for receiving optimization events.
Definition: Optimizer.h:100
virtual void addOptimizationListener(OptimizationListener *listener)=0
Register a listener to this class.
virtual ~BackupListener()
Definition: Optimizer.h:381
virtual void setFunction(Function *function)=0
Set the function to optimize.
virtual double step()=0
Perform an optimization step.
virtual ~OptimizationEvent()
Definition: Optimizer.h:72
virtual void optimizationInitializationPerformed(const OptimizationEvent &event)=0
Save intermediate optimization results to file.
Definition: Optimizer.h:371
The base class exception for NULL pointer error.
Definition: Exceptions.h:123
virtual unsigned int getNumberOfEvaluations() const =0
Get the number of function evaluations performed since the call of the init function.
OutputStream interface.
Definition: OutputStream.h:64
virtual void setVerbose(unsigned int v)=0
Set the verbose level.
Optimizer * clone() const =0
Create a copy of this object and send a pointer to it.
virtual unsigned int getVerbose() const =0
Get the verbose level.
virtual void setMaximumNumberOfEvaluations(unsigned int max)=0
Set the maximum number of function evaluation to perform during optimization.
An event object which indicates that an optimization event occured.
Definition: Optimizer.h:55
virtual void setStopCondition(const OptimizationStopCondition &stopCondition)=0
Set the stop condition of the optimization algorithm.
Exception base class.
Definition: Exceptions.h:57
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
virtual void optimizationStepPerformed(const OptimizationEvent &event)=0
virtual ~Optimizer()
Definition: Optimizer.h:130
virtual bool isInitialized() const =0
virtual void setProfiler(OutputStream *profiler)=0
Set the profiler for this optimizer.
virtual bool isMaximumNumberOfEvaluationsReached() const =0
Tell if the maximum number of function evaluations is reached.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:125
Optimizer * optimizer_
Definition: Optimizer.h:58
virtual void init(const ParameterList &params)=0
Set the initial values of the parameters.
void optimizationInitializationPerformed(const OptimizationEvent &event)
Definition: Optimizer.h:384
BackupListener(const string &backupFile)
Definition: Optimizer.h:378
virtual double getParameterValue(const std::string &name) const =0
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.