bpp-core3  3.0.0
AbstractOptimizer.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_ABSTRACTOPTIMIZER_H
6 #define BPP_NUMERIC_FUNCTION_ABSTRACTOPTIMIZER_H
7 
8 
9 #include "Optimizer.h"
10 
11 namespace bpp
12 {
23  public virtual OptimizerInterface
24 {
25 protected:
29  std::shared_ptr<FunctionInterface> function_;
30 
35 
39  std::shared_ptr<OutputStream> messageHandler_;
40 
44  std::shared_ptr<OutputStream> profiler_;
45 
56  std::string constraintPolicy_;
57 
61  std::shared_ptr<OptimizationStopCondition> stopCondition_;
62 
66  std::shared_ptr<OptimizationStopCondition> defaultStopCondition_;
67 
73  unsigned int verbose_;
74 
79 
80  time_t startTime_;
81 
82  std::vector< std::shared_ptr<OptimizationListener>> listeners_;
83 
85 
86  std::string stepChar_;
87 
88 protected:
92  unsigned int nbEvalMax_;
93 
97  unsigned int nbEval_;
98 
103 
111 
112 public:
113  AbstractOptimizer(std::shared_ptr<FunctionInterface> function = nullptr);
114 
116 
118 
119  virtual ~AbstractOptimizer() {}
120 
121 public:
132  void init(const ParameterList& params) override;
138  double step() override;
139 
145  double optimize() override;
146 
147  bool isInitialized() const override { return isInitialized_; }
148 
149  const ParameterList& getParameters() const override { return parameters_; }
150 
151  double getParameterValue(const std::string& name) const override
152  {
153  return parameters_.getParameterValue(name);
154  }
155 
156  void setFunction(std::shared_ptr<FunctionInterface> function) override
157  {
158  function_ = function;
159  if (function) stopCondition_->init();
160  }
161 
162  const FunctionInterface& function() const override
163  {
164  if (function_)
165  {
166  return *function_;
167  }
168  else
169  {
170  throw NullPointerException("AbstractOptimizer::function() : no function associated to this optimizer.");
171  }
172  }
173 
174  FunctionInterface& function() override
175  {
176  if (function_)
177  {
178  return *function_;
179  }
180  else
181  {
182  throw NullPointerException("AbstractOptimizer::function() : no function associated to this optimizer.");
183  }
184  }
185 
186  std::shared_ptr<const FunctionInterface> getFunction() const override { return function_; }
187 
188  std::shared_ptr<FunctionInterface> getFunction() override { return function_; }
189 
190  bool hasFunction() const override { return function_ != 0; }
191 
192  double getFunctionValue() const override
193  {
194  if (!function_) throw NullPointerException("AbstractOptimizer::getFunctionValue. No function associated to this optimizer.");
195  return currentValue_;
196  }
197 
198  void setMessageHandler(std::shared_ptr<OutputStream> mh) override { messageHandler_ = mh; }
199 
200  std::shared_ptr<OutputStream> getMessageHandler() const override { return messageHandler_; }
201 
202  void setProfiler(std::shared_ptr<OutputStream> profiler) override { profiler_ = profiler; }
203 
204  std::shared_ptr<OutputStream> getProfiler() const override { return profiler_; }
205 
206  unsigned int getNumberOfEvaluations() const override { return nbEval_; }
207 
208  void setStopCondition(std::shared_ptr<OptimizationStopCondition> stopCondition) override
209  {
210  stopCondition_ = stopCondition;
211  }
212 
213  std::shared_ptr<OptimizationStopCondition> getStopCondition() override { return stopCondition_; }
214 
215  std::shared_ptr<const OptimizationStopCondition> getStopCondition() const override { return stopCondition_; }
216 
217  std::shared_ptr<OptimizationStopCondition> getDefaultStopCondition() override { return defaultStopCondition_; }
218 
219  std::shared_ptr<const OptimizationStopCondition> getDefaultStopCondition() const override { return defaultStopCondition_; }
220 
221  bool isToleranceReached() const override { return tolIsReached_; }
222 
223  bool isMaximumNumberOfEvaluationsReached() const override { return nbEval_ >= nbEvalMax_; }
224 
225  void setMaximumNumberOfEvaluations(unsigned int max) override { nbEvalMax_ = max; }
226 
227  void setVerbose(unsigned int v) override { verbose_ = v; }
228 
229  unsigned int getVerbose() const override { return verbose_; }
230 
231  void setConstraintPolicy(const std::string& constraintPolicy) override { constraintPolicy_ = constraintPolicy; }
232 
233  std::string getConstraintPolicy() const override { return constraintPolicy_; }
234 
235  void addOptimizationListener(std::shared_ptr<OptimizationListener> listener) override
236  {
237  if (listener)
238  listeners_.push_back(listener);
239  }
251  void updateParameters(bool yn) override { updateParameters_ = yn; }
252 
262  bool updateParameters() const override { return updateParameters_; }
263 
269  void setOptimizationProgressCharacter(const std::string& c) { stepChar_ = c; }
273  const std::string& getOptimizationProgressCharacter() const { return stepChar_; }
274 
275 protected:
281  virtual void doInit(const ParameterList& params) = 0;
282 
288  virtual double doStep() = 0;
289 
299  void autoParameter();
300 
304  void ignoreConstraints();
305 
311  void profile(double v);
312 
318  void profile(unsigned int v);
319 
325  void profile(const std::string& s);
326 
332  void profileln(double v);
333 
339  void profileln(unsigned int v);
340 
346  void profileln(const std::string& s);
347 
354  void printPoint(const ParameterList& params, double value);
355 
361  void printMessage(const std::string& message);
362 
371 
380 
381  bool listenerModifiesParameters() const;
384 protected:
386 
387  Parameter& getParameter_(size_t i) { return parameters_[i]; }
388 
389  std::shared_ptr<FunctionInterface> getFunction_() { return function_; }
390 
391  void setDefaultStopCondition_(std::shared_ptr<OptimizationStopCondition> osc)
392  {
393  defaultStopCondition_ = osc;
394  }
395 };
396 } // end of namespace bpp.
397 #endif // BPP_NUMERIC_FUNCTION_ABSTRACTOPTIMIZER_H
bool isInitialized_
Check if the optimizer have been feeded with initial parameters values.
std::shared_ptr< const OptimizationStopCondition > getStopCondition() const override
Get the stop condition of the optimization algorithm.
void addOptimizationListener(std::shared_ptr< OptimizationListener > listener) override
Register a listener to this class.
AbstractOptimizer & operator=(const AbstractOptimizer &opt)
double step() override
Basic implementation.
virtual void doInit(const ParameterList &params)=0
This function is called by the init() method and contains all calculations.
void updateParameters(bool yn) override
Tell if we shall update all parameters after one optimization step.
double optimize() override
Basic implementation.
virtual double getParameterValue(const std::string &name) const
Get the value of the parameter with name name.
std::shared_ptr< OptimizationStopCondition > getStopCondition() override
Get the stop condition of the optimization algorithm.
bool isInitialized() const override
void setVerbose(unsigned int v) override
Set the verbose level.
bool isToleranceReached() const override
Tell if the tolerance level is reached.
void profileln(double v)
Print to the profile if there is one and end line.
std::shared_ptr< FunctionInterface > function_
The function to optimize.
void setConstraintPolicy(const std::string &constraintPolicy) override
Set the constraint policy for this optimizer.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
void setDefaultStopCondition_(std::shared_ptr< OptimizationStopCondition > osc)
ParameterList & getParameters_()
const ParameterList & getParameters() const override
unsigned int getNumberOfEvaluations() const override
Get the number of function evaluations performed since the call of the init function.
This is the function abstract class.
Definition: Functions.h:52
std::shared_ptr< OutputStream > getProfiler() const override
std::string getConstraintPolicy() const override
Get the constraint policy for this optimizer.
double getFunctionValue() const override
Get the current function value.
void ignoreConstraints()
Remove the constraints of all the arguments.
The parameter list object.
Definition: ParameterList.h:27
void setMessageHandler(std::shared_ptr< OutputStream > mh) override
Set the message handler for this optimizer.
bool tolIsReached_
Tell if the tolerance level has been reached.
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
unsigned int getVerbose() const override
Get the verbose level.
void setProfiler(std::shared_ptr< OutputStream > profiler) override
Set the profiler for this optimizer.
void setFunction(std::shared_ptr< FunctionInterface > function) override
Set the function to optimize.
std::shared_ptr< OutputStream > messageHandler_
The message handler.
std::shared_ptr< FunctionInterface > getFunction_()
Parameter & getParameter_(size_t i)
std::shared_ptr< OptimizationStopCondition > stopCondition_
The stoping condition to use while optimizing.
bool listenerModifiesParameters() const
unsigned int verbose_
State of the verbose mode: > 0 = enabled.
The base class exception for NULL pointer error. This exception may be thrown when an unexpected NULL...
Definition: Exceptions.h:56
void printPoint(const ParameterList &params, double value)
Print parameters and corresponding function evaluation to profiler.
std::shared_ptr< OutputStream > profiler_
The profiler.
An event object which indicates that an optimization event occured.
Definition: Optimizer.h:22
bool updateParameters() const override
Tell if we shall update all parameters after one optimization step.
This is the basal interface for all optimization methods.
Definition: Optimizer.h:88
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
std::vector< std::shared_ptr< OptimizationListener > > listeners_
std::shared_ptr< const OptimizationStopCondition > getDefaultStopCondition() const override
Get the default stop condition of the optimization algorithm.
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.
std::string constraintPolicy_
The constraint policy.
double getParameterValue(const std::string &name) const override
std::shared_ptr< OptimizationStopCondition > defaultStopCondition_
The default stoping condition to use while optimizing.
virtual double doStep()=0
This function is called by the step() method and contains all calculations.
std::shared_ptr< OutputStream > getMessageHandler() const override
bool hasFunction() const override
Tell if a funciton is associatied to this optimizer.
std::shared_ptr< FunctionInterface > getFunction() override
Get the current function being optimized.
std::shared_ptr< const FunctionInterface > getFunction() const override
Get the current function being optimized.
void profile(double v)
Print to the profile if there is one.
void init(const ParameterList &params) override
Basic implementation.
void setMaximumNumberOfEvaluations(unsigned int max) override
Set the maximum number of function evaluation to perform during optimization.
const std::string & getOptimizationProgressCharacter() const
std::shared_ptr< OptimizationStopCondition > getDefaultStopCondition() override
Get the default stop condition of the optimization algorithm.
void fireOptimizationStepPerformed(const OptimizationEvent &event)
Notify all listeners that an optimization step was performed.
bool isMaximumNumberOfEvaluationsReached() const override
Tell if the maximum number of function evaluations is reached.
void printMessage(const std::string &message)
Give a message to print to the message handler.
AbstractOptimizer(std::shared_ptr< FunctionInterface > function=nullptr)
void setStopCondition(std::shared_ptr< OptimizationStopCondition > stopCondition) override
Set the stop condition of the optimization algorithm.
ParameterList parameters_
The parameters that will be optimized.
void autoParameter()
Build a list of AutoParameter instead of Parameter.