bpp-core3  3.0.0
Optimizer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_FUNCTION_OPTIMIZER_H
6 #define BPP_NUMERIC_FUNCTION_OPTIMIZER_H
7 
8 
9 #include "../../Clonable.h"
10 #include "../../Io/OutputStream.h"
11 #include "../ParameterList.h"
12 #include "Functions.h"
14 
15 namespace bpp
16 {
17 class OptimizerInterface;
18 
23 {
24 private:
31 
32 public:
36  OptimizationEvent(OptimizerInterface* optimizer) : optimizer_(optimizer) {}
37  OptimizationEvent(const OptimizationEvent& oe) : optimizer_(oe.optimizer_) {}
39  {
40  optimizer_ = oe.optimizer_;
41  return *this;
42  }
43  virtual ~OptimizationEvent() {}
44 
45 public:
50 
54  const OptimizerInterface* getOptimizer() const { return optimizer_; }
55 };
56 
57 
67 {
68 public:
70  virtual ~OptimizationListener() {}
71 
72 public:
73  virtual void optimizationInitializationPerformed(const OptimizationEvent& event) = 0;
74  virtual void optimizationStepPerformed(const OptimizationEvent& event) = 0;
78  virtual bool listenerModifiesParameters() const = 0;
79 };
80 
81 
89  public virtual Clonable
90 {
91 public:
93  virtual ~OptimizerInterface() {}
94 
95  OptimizerInterface* clone() const = 0;
96 
97 public:
104  virtual void init(const ParameterList& params) = 0;
105 
109  virtual bool isInitialized() const = 0;
110 
117  virtual double step() = 0;
118 
122  virtual const ParameterList& getParameters() const = 0;
123 
128  virtual double getParameterValue(const std::string& name) const = 0;
135  virtual double getFunctionValue() const = 0;
136 
143  virtual double optimize() = 0;
144 
150  virtual void setFunction(std::shared_ptr<FunctionInterface> function) = 0;
151 
158  virtual const FunctionInterface& function() const = 0;
159 
166  virtual FunctionInterface& function() = 0;
167 
173  virtual std::shared_ptr<const FunctionInterface> getFunction() const = 0;
174 
180  virtual std::shared_ptr<FunctionInterface> getFunction() = 0;
181 
187  virtual bool hasFunction() const = 0;
188 
200  virtual void setMessageHandler(std::shared_ptr<OutputStream> mh) = 0;
201 
205  virtual std::shared_ptr<OutputStream> getMessageHandler() const = 0;
206 
218  virtual void setProfiler(std::shared_ptr<OutputStream> profiler) = 0;
219 
223  virtual std::shared_ptr<OutputStream> getProfiler() const = 0;
224 
231  virtual unsigned int getNumberOfEvaluations() const = 0;
232 
239  virtual void setStopCondition(std::shared_ptr<OptimizationStopCondition> stopCondition) = 0;
240 
246  virtual std::shared_ptr<OptimizationStopCondition> getStopCondition() = 0;
247 
253  virtual std::shared_ptr<const OptimizationStopCondition> getStopCondition() const = 0;
254 
260  virtual std::shared_ptr<OptimizationStopCondition> getDefaultStopCondition() = 0;
261 
267  virtual std::shared_ptr<const OptimizationStopCondition> getDefaultStopCondition() const = 0;
268 
275  virtual bool isToleranceReached() const = 0;
276 
282  virtual bool isMaximumNumberOfEvaluationsReached() const = 0;
283 
289  virtual void setMaximumNumberOfEvaluations(unsigned int max) = 0;
290 
303  virtual void setVerbose(unsigned int v) = 0;
304 
310  virtual unsigned int getVerbose() const = 0;
311 
317  virtual void setConstraintPolicy(const std::string& constraintPolicy) = 0;
318 
324  virtual std::string getConstraintPolicy() const = 0;
325 
334  virtual void addOptimizationListener(std::shared_ptr<OptimizationListener> listener) = 0;
335 
345  virtual void updateParameters(bool yn) = 0;
346 
356  virtual bool updateParameters() const = 0;
357 };
358 
359 
364  public OptimizationListener
365 {
366 private:
367  std::string backupFile_;
368 
369 public:
370  BackupListener(const string& backupFile) :
371  backupFile_(backupFile) {}
372 
373  virtual ~BackupListener() {}
374 
375 public:
377 
379  {
380  std::ofstream bck(backupFile_.c_str(), std::ios::out);
381  bck << "f(x)=" << setprecision(20) << event.getOptimizer()->getFunction()->getValue() << endl;
382  ParameterList pl = event.getOptimizer()->getFunction()->getParameters();
383  for (unsigned int i = 0; i < pl.size(); ++i)
384  {
385  bck << pl[i].getName() << "=" << setprecision(20) << pl[i].getValue() << std::endl;
386  }
387  bck.close();
388  }
389 
390  bool listenerModifiesParameters() const { return false; }
391 };
392 } // end of namespace bpp.
393 #endif // BPP_NUMERIC_FUNCTION_OPTIMIZER_H
std::string backupFile_
Definition: Optimizer.h:367
bool listenerModifiesParameters() const
Definition: Optimizer.h:390
OptimizationEvent(const OptimizationEvent &oe)
Definition: Optimizer.h:37
OptimizerInterface * getOptimizer()
Definition: Optimizer.h:49
OptimizationEvent & operator=(const OptimizationEvent &oe)
Definition: Optimizer.h:38
OptimizationEvent(OptimizerInterface *optimizer)
Definition: Optimizer.h:36
size_t size() const
Definition: ParameterList.h:56
This is the function abstract class.
Definition: Functions.h:52
void optimizationStepPerformed(const OptimizationEvent &event)
Definition: Optimizer.h:378
virtual ~OptimizationListener()
Definition: Optimizer.h:70
The parameter list object.
Definition: ParameterList.h:27
The listener interface for receiving optimization events.
Definition: Optimizer.h:66
virtual ~BackupListener()
Definition: Optimizer.h:373
virtual ~OptimizationEvent()
Definition: Optimizer.h:43
Save intermediate optimization results to file.
Definition: Optimizer.h:363
An event object which indicates that an optimization event occured.
Definition: Optimizer.h:22
const OptimizerInterface * getOptimizer() const
Definition: Optimizer.h:54
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
This is the basal interface for all optimization methods.
Definition: Optimizer.h:88
virtual ~OptimizerInterface()
Definition: Optimizer.h:93
OptimizerInterface * optimizer_
Stores a pointer toward the optimizer that cast the event.
Definition: Optimizer.h:30
void optimizationInitializationPerformed(const OptimizationEvent &event)
Definition: Optimizer.h:376
BackupListener(const string &backupFile)
Definition: Optimizer.h:370