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