5 #include "../NumConstants.h" 6 #include "../NumTools.h" 37 parameters[0].setValue(bracket.
a.
x); bracket.
a.
f =
function.f(parameters);
39 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function.f(parameters);
41 while (std::isnan(bracket.
b.
f) || std::isinf(bracket.
b.
f))
44 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function.f(parameters);
47 if (bracket.
b.
f > bracket.
a.
f)
51 NumTools::swap<double>(bracket.
a.
x, bracket.
b.
x);
52 NumTools::swap<double>(bracket.
a.
f, bracket.
b.
f);
57 parameters[0].setValue(bracket.
c.
x); bracket.
c.
f =
function.f(parameters);
61 while (bracket.
b.
f > bracket.
c.
f)
65 double r = (bracket.
b.
x - bracket.
a.
x) * (bracket.
b.
f - bracket.
c.
f);
66 double q = (bracket.
b.
x - bracket.
c.
x) * (bracket.
b.
f - bracket.
a.
f);
68 double xu = bracket.
b.
x - ((bracket.
b.
x - bracket.
c.
x) * q - (bracket.
b.
x - bracket.
a.
x) * r) /
70 double xulim = (bracket.
b.
x) + GLIMIT * (bracket.
c.
x - bracket.
b.
x);
75 if ((bracket.
b.
x - xu) * (xu - bracket.
c.
x) > 0.0)
77 parameters[0].setValue(xu); fu =
function.f(parameters);
80 bracket.
setA(bracket.
b.
x, bracket.
b.
f);
84 else if (fu > bracket.
b.
f)
92 parameters[0].setValue(xu); fu =
function.f(parameters);
94 else if ((bracket.
c.
x - xu) * (xu - xulim) > 0.0)
97 parameters[0].setValue(xu); fu =
function.f(parameters);
101 parameters[0].setValue(xu);
102 NumTools::shift<double>(bracket.
b.
f, bracket.
c.
f, fu,
function.f(parameters));
105 else if ((xu - xulim) * (xulim - bracket.
c.
x) >= 0.0)
109 parameters[0].setValue(xu); fu =
function.f(parameters);
115 parameters[0].setValue(xu); fu =
function.f(parameters);
118 NumTools::shift<double>(bracket.
a.
x, bracket.
b.
x, bracket.
c.
x, xu);
119 NumTools::shift<double>(bracket.
a.
f, bracket.
b.
f, bracket.
c.
f, fu);
131 unsigned int intervalsNum)
136 parameters[0].setValue(bracket.
a.
x); bracket.
a.
f =
function.f(parameters);
138 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function.f(parameters);
140 while (std::isnan(bracket.
b.
f) || std::isinf(bracket.
b.
f))
143 parameters[0].setValue(bracket.
b.
x); bracket.
b.
f =
function.f(parameters);
146 double bestMiddleX, bestMiddleF;
147 double curr, fcurr, jump;
152 bestMiddleX = (bracket.
a.
f < bracket.
b.
f ? bracket.
a.
x : bracket.
b.
x);
153 bestMiddleF = (bracket.
a.
f < bracket.
b.
f ? bracket.
a.
f : bracket.
b.
f);
154 jump = (b - a) / static_cast<double>(intervalsNum);
155 for (
size_t i = 1; i <= intervalsNum; i++)
158 parameters[0].setValue(curr); fcurr =
function.f(parameters);
160 if (fcurr < bestMiddleF)
166 bracket.
c.
x = bestMiddleX;
167 parameters[0].setValue(bracket.
c.
x); bracket.
c.
f =
function.f(parameters);
174 std::shared_ptr<DirectionFunction> f1dim,
176 std::vector<double>& xi,
178 std::shared_ptr<OutputStream> profiler,
179 std::shared_ptr<OutputStream> messenger,
180 unsigned int verbose)
187 f1dim->setMessageHandler(messenger);
188 f1dim->init(parameters, xi);
199 bod.
init(singleParameter);
204 double xmin = f1dim->getParameters()[0].getValue();
205 for (
size_t j = 0; j < parameters.
size(); ++j)
208 parameters[j].setValue(parameters[j].getValue() + xi[j]);
216 std::shared_ptr<DirectionFunction> f1dim,
218 std::vector<double>& xi,
219 std::vector<double>& gradient,
220 std::shared_ptr<OutputStream> profiler,
221 std::shared_ptr<OutputStream> messenger,
222 unsigned int verbose)
224 size_t size = xi.size();
227 f1dim->setMessageHandler(messenger);
228 f1dim->init(parameters, xi);
231 for (
size_t i = 0; i < size; i++)
233 slope += xi[i] * gradient[i];
239 double x, temp, test = 0;
240 for (
size_t i = 0; i < size; ++i)
242 x = abs(parameters[i].getValue());
261 nbod.
init(singleParameter);
266 double xmin = f1dim->getParameters()[0].getValue();
267 for (
unsigned int j = 0; j < parameters.
size(); ++j)
270 parameters[j].setValue(parameters[j].getValue() + xi[j]);
double optimize() override
Basic implementation.
std::shared_ptr< OptimizationStopCondition > getStopCondition() override
Get the stop condition of the optimization algorithm.
static std::string CONSTRAINTS_KEEP
void setVerbose(unsigned int v) override
Set the verbose level.
static double GOLDEN_RATIO_PHI()
void setConstraintPolicy(const std::string &constraintPolicy) override
Set the constraint policy for this optimizer.
This class is designed to facilitate the manipulation of parameters.
unsigned int getNumberOfEvaluations() const override
Get the number of function evaluations performed since the call of the init function.
This is the function abstract class.
static double VERY_TINY()
void setB(double xb, double fb)
void setA(double xa, double fa)
void setC(double xc, double fc)
The parameter list object.
void setMessageHandler(std::shared_ptr< OutputStream > mh) override
Set the message handler for this optimizer.
void setOptimizationProgressCharacter(const std::string &c)
Set the character to be displayed during optimization.
void setProfiler(std::shared_ptr< OutputStream > profiler) override
Set the profiler for this optimizer.
Newton's backtrack nearly optimization for one parameter.
double optimize()
Initialize optimizer.
virtual void addParameter(const Parameter ¶m)
Add a new parameter at the end of the list.
Brent's optimization for one parameter.
static std::string CONSTRAINTS_AUTO
void set(double x, double f)
void setInitialInterval(double inf, double sup)
Set intial search interval.
void init(const ParameterList ¶ms) override
Basic implementation.