bpp-core3  3.0.0
PowellMultiDimensions.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 #include "../NumTools.h"
6 #include "BrentOneDimension.h"
9 
10 using namespace bpp;
11 
12 /******************************************************************************/
13 
15 {
16  callCount_++;
17  if (callCount_ <= burnin_)
18  return false;
20 }
21 
23 {
24  // NRC Test for done:
25  const PowellMultiDimensions* pmd = dynamic_cast<const PowellMultiDimensions*>(optimizer_);
26  double fp = pmd->fp_;
27  double fret = pmd->fret_;
28  return 2.0 * NumTools::abs(fp - fret) / (NumTools::abs(fp) + NumTools::abs(fret));
29 }
30 
31 /******************************************************************************/
32 
33 PowellMultiDimensions::PowellMultiDimensions(std::shared_ptr<FunctionInterface> function) :
35 {
36  setDefaultStopCondition_(make_shared<PMDStopCondition>(this));
38 }
39 
40 /******************************************************************************/
41 
43 {
44  // Build the initial matrix:
45  size_t n = params.size();
46  xi_.resize(n);
47  for (size_t i = 0; i < n; ++i)
48  {
49  // Copy the parameter list:
50  xi_[i].resize(n);
51  for (unsigned int j = 0; j < n; ++j)
52  {
53  // Set the directions to unit vectors:
54  xi_[i][j] = (j == i) ? 1 : 0;
55  }
56  }
57 
58  // Starting point:
60  pt_ = getParameters();
61 }
62 
63 /******************************************************************************/
64 
66 {
67  size_t n = getParameters().size();
68  fp_ = fret_;
69  size_t ibig = 0;
70  double del = 0.0; // Will be the biggest function decrease
71  Vdouble xit(n);
72 
73  // In each iteration, loop over all directions in the set.
74  double fptt;
75  for (size_t i = 0; i < n; i++)
76  {
77  // Copy the direction:
78  for (size_t j = 0; j < n; j++)
79  {
80  xit[j] = xi_[j][i];
81  }
82  fptt = fret_;
84  f1dim_, getParameters_(), xit, getStopCondition()->getTolerance(),
85  0, getMessageHandler(), getVerbose() > 0 ? getVerbose() - 1 : 0);
86  fret_ = function().f(getParameters());
87  if (getVerbose() > 2)
89  if (fret_ > fp_)
90  throw Exception("DEBUG: PowellMultiDimensions::doStep(). Line minimization failed!");
91  if (fptt - fret_ > del)
92  {
93  del = fptt - fret_;
94  ibig = i;
95  }
96  }
97 
99  for (size_t j = 0; j < n; ++j)
100  {
101  ptt[j].setValue(2.0 * getParameters()[j].getValue() - pt_[j].getValue());
102  xit[j] = getParameters()[j].getValue() - pt_[j].getValue();
103  pt_[j].setValue(getParameters()[j].getValue());
104  }
105  fptt = getFunction()->f(ptt);
106  if (fptt < fp_)
107  {
108  double t = 2.0 * (fp_ - 2.0 * fret_ + fptt) * NumTools::sqr(fp_ - fret_ - del) - del * NumTools::sqr(fp_ - fptt);
109  if (t < 0.0)
110  {
111  // cout << endl << "New direction: drection " << ibig << " removed." << endl;
113  getParameters_(), xit, getStopCondition()->getTolerance(),
114  0, getMessageHandler(), getVerbose() > 0 ? getVerbose() - 1 : 0);
115  fret_ = getFunction()->f(getParameters());
116  if (fret_ > fp_)
117  throw Exception("DEBUG: PowellMultiDimensions::doStep(). Line minimization failed!");
118  for (size_t j = 0; j < n; ++j)
119  {
120  xi_[j][ibig] = xi_[j][n - 1];
121  xi_[j][n - 1] = xit[j];
122  }
123  }
124  }
125  else
126  getFunction()->setParameters(getParameters());
127 
128  return fret_;
129 }
130 
131 /******************************************************************************/
132 
134 {
136  // Apply best parameter:
137  return getFunction()->f(getParameters());
138 }
139 
140 /******************************************************************************/
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
double optimize() override
Basic implementation.
Powell&#39;s multi-dimensions optimization algorithm for one parameter.
double optimize()
Basic implementation.
std::shared_ptr< OptimizationStopCondition > getStopCondition() override
Get the stop condition of the optimization algorithm.
PowellMultiDimensions(std::shared_ptr< FunctionInterface > function)
size_t size() const
Definition: ParameterList.h:56
void setDefaultStopCondition_(std::shared_ptr< OptimizationStopCondition > osc)
ParameterList & getParameters_()
const ParameterList & getParameters() const override
The parameter list object.
Definition: ParameterList.h:27
unsigned int getVerbose() const override
Get the verbose level.
double doStep()
This function is called by the step() method and contains all calculations.
std::vector< double > Vdouble
Definition: VectorTools.h:34
void printPoint(const ParameterList &params, double value)
Print parameters and corresponding function evaluation to profiler.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
double callCount_
Count the number of times the isToleranceReached() function has been called.
unsigned int nbEval_
The current number of function evaluations achieved.
Partial implementation of the Optimizer interface.
double getCurrentTolerance() const
Get the current tolerance.
static unsigned int lineMinimization(std::shared_ptr< DirectionFunction > f1dim, ParameterList &parameters, std::vector< double > &xi, double tolerance, std::shared_ptr< OutputStream > profiler=nullptr, std::shared_ptr< OutputStream > messenger=nullptr, unsigned int verbose=2)
const FunctionInterface & function() const override
Get the current function being optimized.
std::shared_ptr< OutputStream > getMessageHandler() const override
std::shared_ptr< const FunctionInterface > getFunction() const override
Get the current function being optimized.
std::shared_ptr< OptimizationStopCondition > getDefaultStopCondition() override
Get the default stop condition of the optimization algorithm.
std::shared_ptr< DirectionFunction > f1dim_
static T sqr(T a)
Get the square of a number.
Definition: NumTools.h:81
void setStopCondition(std::shared_ptr< OptimizationStopCondition > stopCondition) override
Set the stop condition of the optimization algorithm.
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:31