42 #include "../NumConstants.h"
43 #include "../NumTools.h"
74 parameters[0].setValue(bracket.
a.
x); bracket.
a.
f =
function->f(parameters);
76 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function->f(parameters);
78 while (std::isnan(bracket.
b.
f) || std::isinf(bracket.
b.
f))
81 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function->f(parameters);
84 if (bracket.
b.
f > bracket.
a.
f)
88 NumTools::swap<double>(bracket.
a.
x, bracket.
b.
x);
89 NumTools::swap<double>(bracket.
a.
f, bracket.
b.
f);
94 parameters[0].setValue(bracket.
c.
x); bracket.
c.
f =
function->f(parameters);
98 while (bracket.
b.
f > bracket.
c.
f)
102 double r = (bracket.
b.
x - bracket.
a.
x) * (bracket.
b.
f - bracket.
c.
f);
103 double q = (bracket.
b.
x - bracket.
c.
x) * (bracket.
b.
f - bracket.
a.
f);
105 double xu = bracket.
b.
x - ((bracket.
b.
x - bracket.
c.
x) * q - (bracket.
b.
x - bracket.
a.
x) * r) /
107 double xulim = (bracket.
b.
x) + GLIMIT * (bracket.
c.
x - bracket.
b.
x);
112 if ((bracket.
b.
x - xu) * (xu - bracket.
c.
x) > 0.0)
114 parameters[0].setValue(xu); fu =
function->f(parameters);
115 if (fu < bracket.
c.
f)
117 bracket.
setA(bracket.
b.
x, bracket.
b.
f);
118 bracket.
setB(xu, fu);
121 else if (fu > bracket.
b.
f)
123 bracket.
setC(xu, fu);
129 parameters[0].setValue(xu); fu =
function->f(parameters);
131 else if ((bracket.
c.
x - xu) * (xu - xulim) > 0.0)
134 parameters[0].setValue(xu); fu =
function->f(parameters);
135 if (fu < bracket.
c.
f)
138 parameters[0].setValue(xu);
139 NumTools::shift<double>(bracket.
b.
f, bracket.
c.
f, fu, function->f(parameters));
142 else if ((xu - xulim) * (xulim - bracket.
c.
x) >= 0.0)
146 parameters[0].setValue(xu); fu =
function->f(parameters);
152 parameters[0].setValue(xu); fu =
function->f(parameters);
155 NumTools::shift<double>(bracket.
a.
x, bracket.
b.
x, bracket.
c.
x, xu);
156 NumTools::shift<double>(bracket.
a.
f, bracket.
b.
f, bracket.
c.
f, fu);
173 parameters[0].setValue(bracket.
a.
x); bracket.
a.
f =
function->f(parameters);
175 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function->f(parameters);
177 while (std::isnan(bracket.
b.
f) || std::isinf(bracket.
b.
f))
180 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function->f(parameters);
183 double bestMiddleX, bestMiddleF;
184 double curr, fcurr, jump;
189 bestMiddleX = (bracket.
a.
f < bracket.
b.
f ? bracket.
a.
x : bracket.
b.
x);
190 bestMiddleF = (bracket.
a.
f < bracket.
b.
f ? bracket.
a.
f : bracket.
b.
f);
191 jump = (b - a) /
static_cast<double>(intervalsNum);
192 for (
size_t i = 1; i <= intervalsNum; i++)
195 parameters[0].setValue(curr); fcurr =
function->f(parameters);
197 if (fcurr < bestMiddleF)
203 bracket.
c.
x = bestMiddleX;
204 parameters[0].setValue(bracket.
c.
x); bracket.
c.
f =
function->f(parameters);
213 std::vector<double>& xi,
217 unsigned int verbose)
225 f1dim.
init(parameters, xi);
236 bod.
init(singleParameter);
242 for (
size_t j = 0; j < parameters.
size(); j++)
245 parameters[j].setValue(parameters[j].getValue() + xi[j]);
254 std::vector<double>& xi,
255 std::vector<double>& gradient,
258 unsigned int verbose)
260 size_t size = xi.size();
264 f1dim.
init(parameters, xi);
267 for (
size_t i = 0; i < size; i++)
269 slope += xi[i] * gradient[i];
275 double x, temp, test = 0;
276 for (
size_t i = 0; i < size; ++i)
278 x = abs(parameters[i].getValue());
297 nbod.
init(singleParameter);
303 for (
unsigned int j = 0; j < parameters.
size(); j++)
306 parameters[j].setValue(parameters[j].getValue() + xi[j]);
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
void setProfiler(OutputStream *profiler)
Set the profiler for this optimizer.
void setConstraintPolicy(const std::string &constraintPolicy)
Set the constraint policy for this optimizer.
void setMessageHandler(OutputStream *mh)
Set the message handler for this optimizer.
double optimize()
Basic implementation.
void init(const ParameterList ¶ms)
Basic implementation.
void setVerbose(unsigned int v)
Set the verbose level.
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 std::string CONSTRAINTS_AUTO
static std::string CONSTRAINTS_KEEP
void set(double x, double f)
void setA(double xa, double fa)
void setB(double xb, double fb)
void setC(double xc, double fc)
Brent's optimization for one parameter.
double optimize()
Initialize optimizer.
void setInitialInterval(double inf, double sup)
Set intial search interval.
void setConstraintPolicy(const std::string &constraintPolicy)
void init(const ParameterList &p, const std::vector< double > &xi)
void setMessageHandler(OutputStream *messenger)
const ParameterList & getParameters() const
Get all parameters available.
This is the function abstract class.
Newton's backtrack nearly optimization for one parameter.
static double VERY_TINY()
static double GOLDEN_RATIO_PHI()
virtual void setTolerance(double tolerance)=0
Set the tolerance parameter.
The parameter list object.
virtual void addParameter(const Parameter ¶m)
Add a new parameter at the end of the list.
This class is designed to facilitate the manipulation of parameters.