bpp-core3  3.0.0
SimpleNewtonMultiDimensions.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 SimpleNewtonMultiDimensions::SimpleNewtonMultiDimensions(std::shared_ptr<SecondOrderDerivable> function) :
15  AbstractOptimizer(function), nbParams_(0), optimizer_(function)
16 {
17  setDefaultStopCondition_(make_shared<FunctionStopCondition>(this));
20 }
21 
22 /******************************************************************************/
23 
24 void SimpleNewtonMultiDimensions::setFunction(std::shared_ptr<FunctionInterface> function)
25 {
27  optimizer_.setFunction(function);
28 }
29 
30 /******************************************************************************/
31 
33 {
34  nbParams_ = params.size();
35  if (nbParams_ == 0)
36  return;
37 
38  // Initialize optimizers:
39  unsigned int nbEvalMax = nbEvalMax_ / static_cast<unsigned int>(nbParams_);
43  optimizer_.getStopCondition()->setTolerance(getStopCondition()->getTolerance());
45  optimizer_.setVerbose(getVerbose() > 0 ? getVerbose() - 1 : 0);
47  getFunction()->setParameters(getParameters());
48 }
49 
50 /******************************************************************************/
51 
53 {
54  double f = getFunction()->getValue();
55  for (unsigned int i = 0; i < nbParams_; i++)
56  {
57  if (getVerbose() > 0)
58  {
59  cout << getParameters()[i].getName() << ":";
60  cout.flush();
61  }
62  // Re-init optimizer according to new values:
63  optimizer_.init(getParameters().createSubList(i));
64 
65  // Optimize through this dimension:
66  f = optimizer_.optimize();
67  if (getVerbose() > 0)
68  cout << endl;
71  }
72  tolIsReached_ = nbParams_ <= 1;
73  return f;
74 }
75 
76 /******************************************************************************/
SimpleNewtonMultiDimensions(std::shared_ptr< SecondOrderDerivable > function)
void doInit(const ParameterList &params) override
This function is called by the init() method and contains all calculations.
double optimize() override
Basic implementation.
void setMaximumNumberOfCorrections(unsigned int mx)
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.
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.
unsigned int nbEvalMax_
The maximum number of function evaluations allowed.
void setFunction(std::shared_ptr< FunctionInterface > function) override
Set the function to optimize.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
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.