bpp-core  2.2.0
OneDimensionOptimizationTools.cpp
Go to the documentation of this file.
1 //
2 // File: OneDimensionOptimizationTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Mon Nov 17 11:15:22 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. This file is part of the Bio++ project.
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 
41 #include "BrentOneDimension.h"
43 #include "../NumTools.h"
44 #include "../NumConstants.h"
45 
46 using namespace bpp;
47 using namespace std;
48 
49 /******************************************************************************
50 * The Point class *
51 ******************************************************************************/
52 inline void BracketPoint::set(double xval, double fval) { this->x = xval; this->f = fval; }
53 
54 /******************************************************************************
55 * The Bracket class *
56 ******************************************************************************/
57 inline void Bracket::setA(double xa, double fa) { a.set(xa, fa); }
58 inline void Bracket::setB(double xb, double fb) { b.set(xb, fb); }
59 inline void Bracket::setC(double xc, double fc) { c.set(xc, fc); }
60 
61 /******************************************************************************/
62 
64  double a,
65  double b,
66  Function* function,
67  ParameterList parameters)
68 {
69  Bracket bracket;
70  // Copy the parameter to use.
71  bracket.a.x = a;
72  parameters[0].setValue(bracket.a.x); bracket.a.f = function->f(parameters);
73  bracket.b.x = b;
74  parameters[0].setValue(bracket.b.x); bracket.b.f = function->f(parameters);
75  if (bracket.b.f > bracket.a.f)
76  {
77  // Switch roles of first and second point so that we can go downhill
78  // in the direction from a to b.
79  NumTools::swap<double>(bracket.a.x, bracket.b.x);
80  NumTools::swap<double>(bracket.a.f, bracket.b.f);
81  }
82 
83  // First guess for third point:
84  bracket.c.x = bracket.b.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.b.x - bracket.a.x);
85  parameters[0].setValue(bracket.c.x); bracket.c.f = function->f(parameters);
86 
87  // Keep returning here until we bracket:
88  while (bracket.b.f > bracket.c.f)
89  {
90  // Compute xu by parabolic extrapolation from a, b, c. TINY is used to prevent
91  // any possible division by 0.
92  double r = (bracket.b.x - bracket.a.x) * (bracket.b.f - bracket.c.f);
93  double q = (bracket.b.x - bracket.c.x) * (bracket.b.f - bracket.a.f);
94 
95  double xu = bracket.b.x - ((bracket.b.x - bracket.c.x) * q - (bracket.b.x - bracket.a.x) * r) /
97  double xulim = (bracket.b.x) + GLIMIT * (bracket.c.x - bracket.b.x);
98  double fu;
99 
100  // We don't go farther than this.
101  // Test various possibilities:
102  if ((bracket.b.x - xu) * (xu - bracket.c.x) > 0.0)
103  {
104  parameters[0].setValue(xu); fu = function->f(parameters);
105  if (fu < bracket.c.f)
106  {
107  bracket.setA(bracket.b.x, bracket.b.f);
108  bracket.setB(xu, fu);
109  return bracket;
110  }
111  else if (fu > bracket.b.f)
112  {
113  bracket.setC(xu, fu);
114  return bracket;
115  }
116  // Parabolic fit was no use.
117  // Use default magnification.
118  xu = bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x);
119  parameters[0].setValue(xu); fu = function->f(parameters);
120  }
121  else if ((bracket.c.x - xu) * (xu - xulim) > 0.0)
122  {
123  // Parabolic fit is between point 3 and its allowed limit.
124  parameters[0].setValue(xu); fu = function->f(parameters);
125  if (fu < bracket.c.f)
126  {
127  NumTools::shift<double>(bracket.b.x, bracket.c.x, xu, bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x));
128  parameters[0].setValue(xu);
129  NumTools::shift<double>(bracket.b.f, bracket.c.f, fu, function->f(parameters));
130  }
131  }
132  else if ((xu - xulim) * (xulim - bracket.c.x) >= 0.0)
133  {
134  // Limit parabolic xu to maximum allowed value.
135  xu = xulim;
136  parameters[0].setValue(xu); fu = function->f(parameters);
137  }
138  else
139  {
140  // Reject parabolic xu, use default magnification.
141  xu = bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x);
142  parameters[0].setValue(xu); fu = function->f(parameters);
143  }
144  // Eliminate oldest point and continue.
145  NumTools::shift<double>(bracket.a.x, bracket.b.x, bracket.c.x, xu);
146  NumTools::shift<double>(bracket.a.f, bracket.b.f, bracket.c.f, fu);
147  }
148  return bracket;
149 }
150 
151 /******************************************************************************/
152 
154  DirectionFunction& f1dim,
155  ParameterList& parameters,
156  std::vector<double>& xi,
157  double tolerance,
158  OutputStream* profiler,
159  OutputStream* messenger,
160  unsigned int verbose)
161 {
162  // Initial guess for brackets:
163  double ax = 0.;
164  double xx = 0.01;
165 
167  f1dim.setMessageHandler(messenger);
168  f1dim.init(parameters, xi);
169  BrentOneDimension bod(&f1dim);
170  bod.setMessageHandler(messenger);
171  bod.setProfiler(profiler);
172  bod.setVerbose(verbose >= 1 ? 1 : 0);
174  bod.getStopCondition()->setTolerance(0.01);
175  bod.setInitialInterval(ax, xx);
177  ParameterList singleParameter;
178  singleParameter.addParameter(Parameter("x", 0.0));
179  bod.init(singleParameter);
180  bod.optimize();
181  // Update parameters:
182  // parameters.matchParametersValues(f1dim.getFunction()->getParameters());
183 
184  double xmin = f1dim.getParameters()[0].getValue();
185  for (size_t j = 0; j < parameters.size(); j++)
186  {
187  xi[j] *= xmin;
188  parameters[j].setValue(parameters[j].getValue() + xi[j]);
189  }
190  return bod.getNumberOfEvaluations();
191 }
192 
193 /******************************************************************************/
194 
196  ParameterList& parameters,
197  std::vector<double>& xi,
198  std::vector<double>& gradient,
199  OutputStream* profiler,
200  OutputStream* messenger,
201  unsigned int verbose)
202 {
203  size_t size = xi.size();
204 
206  f1dim.setMessageHandler(messenger);
207  f1dim.init(parameters, xi);
208 
209  double slope = 0;
210  for (size_t i = 0; i < size; i++)
211  {
212  slope += xi[i] * gradient[i];
213  }
214 
215  // if (slope>=0)
216  // throw Exception("Slope problem in OneDimensionOptimizationTools::lineSearch. Slope="+TextTools::toString(slope));
217 
218  double x, temp, test = 0;
219  for (size_t i = 0; i < size; ++i)
220  {
221  x = abs(parameters[i].getValue());
222  temp = abs(xi[i]);
223  if (x > 1.0)
224  temp /= x;
225  if (temp > test)
226  test = temp;
227  }
228 
229  NewtonBacktrackOneDimension nbod(&f1dim, slope, test);
230 
231  nbod.setMessageHandler(messenger);
232  nbod.setProfiler(profiler);
233  nbod.setVerbose(verbose >= 1 ? 1 : 0);
235  nbod.getStopCondition()->setTolerance(0.0001);
236  // nbod.setInitialInterval(ax, xx);
238  ParameterList singleParameter;
239  singleParameter.addParameter(Parameter("x", 0.0));
240  nbod.init(singleParameter);
241  nbod.optimize();
242  // Update parameters:
243  // parameters.matchParametersValues(f1dim.getFunction()->getParameters());
244 
245  double xmin = f1dim.getParameters()[0].getValue();
246  for (unsigned int j = 0; j < parameters.size(); j++)
247  {
248  xi[j] *= xmin;
249  parameters[j].setValue(parameters[j].getValue() + xi[j]);
250  }
251 
252  return nbod.getNumberOfEvaluations();
253 }
254 
255 /******************************************************************************/
256 
258 
259 /******************************************************************************/
static unsigned int lineSearch(DirectionFunction &f1dim, ParameterList &parameters, std::vector< double > &xi, std::vector< double > &gradient, OutputStream *profiler=0, OutputStream *messenger=0, unsigned int verbose=2)
Search a &#39;sufficiently low&#39; value for a function in a given direction.
void setConstraintPolicy(const std::string &constraintPolicy)
void init(const ParameterList &p, const std::vector< double > &xi)
unsigned int getNumberOfEvaluations() const
Get the number of function evaluations performed since the call of the init function.
OptimizationStopCondition * getStopCondition()
Get the stop condition of the optimization algorithm.
static T sign(T a)
Get the sign of a value.
Definition: NumTools.h:77
This class allows to perform a correspondence analysis.
double optimize()
Basic implementation.
static std::string CONSTRAINTS_KEEP
This is the function abstract class.
Definition: Functions.h:86
static double GOLDEN_RATIO_PHI()
Definition: NumConstants.h:65
size_t size() const
Definition: ParameterList.h:90
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
void setMessageHandler(OutputStream *messenger)
static double VERY_TINY()
Definition: NumConstants.h:82
void setB(double xb, double fb)
void setA(double xa, double fa)
void setC(double xc, double fc)
virtual void setTolerance(double tolerance)=0
Set the tolerance parameter.
The parameter list object.
Definition: ParameterList.h:61
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
static T max(T a, T b)
Get the max between 2 values.
Definition: NumTools.h:88
static Bracket bracketMinimum(double a, double b, Function *function, ParameterList parameters)
Bracket a minimum.
OutputStream interface.
Definition: OutputStream.h:64
Newton&#39;s backtrack nearly optimization for one parameter.
double optimize()
Initialize optimizer.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
void setProfiler(OutputStream *profiler)
Set the profiler for this optimizer.
Brent&#39;s optimization for one parameter.
void setVerbose(unsigned int v)
Set the verbose level.
static std::string CONSTRAINTS_AUTO
const ParameterList & getParameters() const
Get all parameters available.
void set(double x, double f)
void setConstraintPolicy(const std::string &constraintPolicy)
Set the constraint policy for this optimizer.
void setMessageHandler(OutputStream *mh)
Set the message handler for this optimizer.
void setInitialInterval(double inf, double sup)
Set intial search interval.
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 double GLIMIT
Maximum magnification allowed for a parabolic-fit step.
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:66
void init(const ParameterList &params)
Basic implementation.