bpp-core3  3.0.0
PowellMultiDimensions.cpp
Go to the documentation of this file.
1 //
2 // File: PowellMultiDimensions.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-17 15:16:45
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 
42 #include "../NumTools.h"
43 #include "BrentOneDimension.h"
45 #include "PowellMultiDimensions.h"
46 
47 using namespace bpp;
48 
49 /******************************************************************************/
50 
52 {
53  callCount_++;
54  if (callCount_ <= burnin_)
55  return false;
57 }
58 
60 {
61  // NRC Test for done:
62  const PowellMultiDimensions* pmd = dynamic_cast<const PowellMultiDimensions*>(optimizer_);
63  double fp = pmd->fp_;
64  double fret = pmd->fret_;
65  return 2.0 * NumTools::abs(fp - fret) / (NumTools::abs(fp) + NumTools::abs(fret));
66 }
67 
68 /******************************************************************************/
69 
71  AbstractOptimizer(function), fp_(0), fret_(0), pt_(), xi_(), ncom_(0), pcom_(), xicom_(), f1dim_(function)
72 {
75 }
76 
77 /******************************************************************************/
78 
80 {
81  // Build the initial matrix:
82  size_t n = params.size();
83  xi_.resize(n);
84  for (size_t i = 0; i < n; i++)
85  {
86  // Copy the parameter list:
87  xi_[i].resize(n);
88  for (unsigned int j = 0; j < n; j++)
89  {
90  // Set the directions to unit vectors:
91  xi_[i][j] = (j == i) ? 1 : 0;
92  }
93  }
94 
95  // Starting point:
97  pt_ = getParameters();
98 }
99 
100 /******************************************************************************/
101 
103 {
104  size_t n = getParameters().size();
105  fp_ = fret_;
106  size_t ibig = 0;
107  double del = 0.0; // Will be the biggest function decrease
108  Vdouble xit(n);
109 
110  // In each iteration, loop over all directions in the set.
111  double fptt;
112  for (size_t i = 0; i < n; i++)
113  {
114  // Copy the direction:
115  for (size_t j = 0; j < n; j++)
116  {
117  xit[j] = xi_[j][i];
118  }
119  fptt = fret_;
121  getParameters_(), xit, getStopCondition()->getTolerance(),
122  0, getMessageHandler(), getVerbose() > 0 ? getVerbose() - 1 : 0);
124  if (getVerbose() > 2)
126  if (fret_ > fp_)
127  throw Exception("DEBUG: PowellMultiDimensions::doStep(). Line minimization failed!");
128  if (fptt - fret_ > del)
129  {
130  del = fptt - fret_;
131  ibig = i;
132  }
133  }
134 
136  for (size_t j = 0; j < n; j++)
137  {
138  ptt[j].setValue(2.0 * getParameters()[j].getValue() - pt_[j].getValue());
139  xit[j] = getParameters()[j].getValue() - pt_[j].getValue();
140  pt_[j].setValue(getParameters()[j].getValue());
141  }
142  fptt = getFunction()->f(ptt);
143  if (fptt < fp_)
144  {
145  double t = 2.0 * (fp_ - 2.0 * fret_ + fptt) * NumTools::sqr(fp_ - fret_ - del) - del * NumTools::sqr(fp_ - fptt);
146  if (t < 0.0)
147  {
148  // cout << endl << "New direction: drection " << ibig << " removed." << endl;
150  getParameters_(), xit, getStopCondition()->getTolerance(),
151  0, getMessageHandler(), getVerbose() > 0 ? getVerbose() - 1 : 0);
153  if (fret_ > fp_)
154  throw Exception("DEBUG: PowellMultiDimensions::doStep(). Line minimization failed!");
155  for (size_t j = 0; j < n; j++)
156  {
157  xi_[j][ibig] = xi_[j][n - 1];
158  xi_[j][n - 1] = xit[j];
159  }
160  }
161  }
162  else
164 
165  return fret_;
166 }
167 
168 /******************************************************************************/
169 
171 {
173  // Apply best parameter:
174  return getFunction()->f(getParameters());
175 }
176 
177 /******************************************************************************/
double callCount_
Count the number of times the isToleranceReached() function has been called.
Partial implementation of the Optimizer interface.
OutputStream * getMessageHandler() const
void printPoint(const ParameterList &params, double value)
Print parameters and corresponding function evaluation to profiler.
double optimize()
Basic implementation.
OptimizationStopCondition * getDefaultStopCondition()
Get the default stop condition of the optimization algorithm.
unsigned int nbEval_
The current number of function evaluations achieved.
const ParameterList & getParameters() const
void setStopCondition(const OptimizationStopCondition &stopCondition)
Set the stop condition of the optimization algorithm.
ParameterList & getParameters_()
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
const Function * getFunction() const
Get the current function being optimized.
unsigned int getVerbose() const
Get the verbose level.
void setDefaultStopCondition_(OptimizationStopCondition *osc)
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
This is the function abstract class.
Definition: Functions.h:89
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:117
static T sqr(T a)
Get the square of a number.
Definition: NumTools.h:117
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:67
static unsigned int lineMinimization(DirectionFunction &f1dim, ParameterList &parameters, std::vector< double > &xi, double tolerance, OutputStream *profiler=0, OutputStream *messenger=0, unsigned int verbose=2)
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
bool isToleranceReached() const
Tell if the we reached the desired tolerance with a given new set of estimates.
double getCurrentTolerance() const
Get the current tolerance.
Powell's multi-dimensions optimization algorithm for one parameter.
double doStep()
This function is called by the step() method and contains all calculations.
PowellMultiDimensions(Function *function)
double optimize()
Basic implementation.
void doInit(const ParameterList &params)
This function is called by the init() method and contains all calculations.
std::vector< double > Vdouble
Definition: VectorTools.h:70