bpp-core3  3.0.0
SimpleMultiDimensions.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 /******************************************************************************/
6 
8 
9 using namespace bpp;
10 using namespace std;
11 
12 /******************************************************************************/
13 
14 SimpleMultiDimensions::SimpleMultiDimensions(std::shared_ptr<FunctionInterface> function) :
15  AbstractOptimizer(function), nbParams_(0), optimizer_(function)
16 {
17  setDefaultStopCondition_(make_shared<FunctionStopCondition>(this));
20 }
21 
22 /******************************************************************************/
23 
24 void SimpleMultiDimensions::setFunction(std::shared_ptr<FunctionInterface> function)
25 {
27  optimizer_.setFunction(function);
28  getStopCondition()->init();
29 }
30 
31 /******************************************************************************/
32 
34 {
35  nbParams_ = params.size();
36  if (nbParams_ == 0)
37  return;
38 
39  // Initialize optimizers:
40  unsigned int nbEvalMax = nbEvalMax_ / static_cast<unsigned int>(nbParams_);
44  optimizer_.getStopCondition()->setTolerance(getStopCondition()->getTolerance());
46  optimizer_.setVerbose(getVerbose() > 0 ? getVerbose() - 1 : 0);
48  getFunction()->setParameters(getParameters());
49 }
50 
51 /******************************************************************************/
52 
54 {
55  double f = getFunction()->getValue();
56  for (unsigned int i = 0; i < nbParams_; i++)
57  {
58  if (getVerbose() > 0)
59  {
60  cout << getParameters()[i].getName() << ":";
61  cout.flush();
62  }
63  // Re-init optimizer according to new values:
64  double v = getParameters()[i].getValue();
65  double t = std::max(0.000001, std::min(std::abs(v), getStopCondition()->getTolerance()));
66  optimizer_.setInitialInterval(v - t, v + t);
67  optimizer_.init(getParameters().createSubList(i));
68 
69  // Optimize through this dimension:
70  f = optimizer_.optimize();
71  if (getVerbose() > 0)
72  cout << endl;
75  }
76  tolIsReached_ = nbParams_ <= 1;
77  return f;
78 }
79 
80 /******************************************************************************/
virtual bool matchParametersValues(const ParameterList &params, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.
std::shared_ptr< OptimizationStopCondition > getStopCondition() override
Get the stop condition of the optimization algorithm.
void setVerbose(unsigned int v) override
Set the verbose level.
size_t size() const
Definition: ParameterList.h:56
void setConstraintPolicy(const std::string &constraintPolicy) override
Set the constraint policy for this optimizer.
STL namespace.
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.
void doInit(const ParameterList &params) override
This function is called by the init() method and contains all calculations.
std::shared_ptr< OutputStream > getProfiler() const override
std::string getConstraintPolicy() const override
Get the constraint policy for this optimizer.
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.
double doStep() override
This function is called by the step() method and contains all calculations.
double optimize()
Initialize optimizer.
void setFunction(std::shared_ptr< FunctionInterface > function) override
Set the function to optimize.
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
SimpleMultiDimensions(std::shared_ptr< FunctionInterface > function)
void setInitialInterval(double inf, double sup)
Set intial search interval.
std::shared_ptr< OutputStream > getMessageHandler() const override
std::shared_ptr< const FunctionInterface > getFunction() const override
Get the current function being optimized.
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.
std::shared_ptr< OptimizationStopCondition > getDefaultStopCondition() override
Get the default stop condition of the optimization algorithm.
void setStopCondition(std::shared_ptr< OptimizationStopCondition > stopCondition) override
Set the stop condition of the optimization algorithm.