29#ifndef OPTIMIZER_BARZILAI_BORWEIN_H
30#define OPTIMIZER_BARZILAI_BORWEIN_H
55template<
typename S,
typename C>
72 int dim, S eps,
int maxIt, S lambda,
int maxStepAttempts, std::string stepCondition,
73 bool verboseOn=
true,
const std::string fname=
"",
const std::string logFileName=
"",
74 bool withUpperBound=
false, S upperBound=S(),
bool withLowerBound=
false, S lowerBound=S(),
75 bool vectorBounds=
false, S controlEps=S(std::numeric_limits<double>::epsilon() ),
76 std::vector<OptimizerLogType> gplotAnalysis = {})
78 verboseOn, fname, logFileName, withUpperBound, upperBound, withLowerBound,
79 lowerBound, vectorBounds, controlEps, true, gplotAnalysis),
80 clout(std::cout,
"OptimizerBarzilaiBorwein")
85 _startLambda = lambda;
98 if (std::isnan(normDir)) {
99 clout <<
"Warning: Derivative is null at first iteration. Check derivative calculations.\nProgram terminated" << std::endl;
106 for (
int i=0; i<this->
_dimCtrl; i++) {
115 bool alternate =
false;
124 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
130 if (this->
_it == 0) {
133 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
139 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
140 _lastControl[iDim] = this->
_control[iDim];
147 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
148 _sStore[iDim] = this->
_control[iDim] - _lastControl[iDim];
149 _yStore[iDim] = this->
_derivative[iDim] - _lastDerivative[iDim];
150 sTs += _sStore[iDim]*_sStore[iDim];
151 sTy += _sStore[iDim]*_yStore[iDim];
153 yTy += _yStore[iDim]*_yStore[iDim];
162 if (alternate && this->
_it%2 != 0) {
166 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
167 _lastControl[iDim] = this->
_control[iDim];
171 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
180template<
typename S,
typename C = std::vector<S>>
192 int maxStepAttempts = 100;
193 std::string stepCondition =
"None";
196 std::string fname =
"control.dat";
197 std::string logFileName =
"log.txt";
199 bool vectorBounds =
false;
200 bool withUpperBound =
false;
202 bool withLowerBound =
false;
205 std::vector<OptimizerLogType> gplotAnalysis = {};
206 std::string gplotAnalysisString =
"";
209 params.
readOrWarn<
int>(
"Optimization",
"MaxIter",
"", maxIt);
211 params.
readOrWarn<S>(
"Optimization",
"Tolerance",
"", eps);
212 params.
readOrWarn<S>(
"Optimization",
"ControlTolerance",
"", controlEps);
213 params.
readOrWarn<S>(
"Optimization",
"Lambda",
"", lambda);
214 params.
readOrWarn<
int>(
"Optimization",
"MaxStepAttempts",
"", maxStepAttempts);
215 params.
readOrWarn<std::string>(
"Optimization",
"StepCondition",
"", stepCondition);
217 params.
readOrWarn<
bool>(
"Optimization",
"Verbose",
"", verboseOn);
218 params.
readOrWarn<std::string>(
"Optimization",
"InputFileName",
"", fname);
219 params.
readOrWarn<std::string>(
"Optimization",
"LogFileName",
"", logFileName);
221 params.
readOrWarn<
bool>(
"Optimization",
"VectorBounds",
"", vectorBounds);
222 if ( params.
readOrWarn<S>(
"Optimization",
"UpperBound",
"", upperBound,
false,
false) ) {
223 withUpperBound =
true;
225 if ( params.
readOrWarn<S>(
"Optimization",
"LowerBound",
"", lowerBound,
false,
false) ) {
226 withLowerBound =
true;
230 params.
readOrWarn<std::string>(
"Optimization",
"VisualizationGnuplot",
"VisualizedParameters",
"", gplotAnalysisString,
false,
false);
236 verboseOn, fname, logFileName, withUpperBound, upperBound, withLowerBound, lowerBound,
237 vectorBounds, controlEps, gplotAnalysis);
class for marking output with some text
bool readOrWarn(std::string name_parameter_1, std::string name_parameter_2, std::string name_parameter_3, ParameterType &var, bool defaultAvailable=true, bool exitIfMissing=false, bool showWarning=true) const
This wrapper function reads the given parameter from the "type_parameter" and "name_parameter_1" or "...
Optimization algorithm: BarzilaiBorwein.
virtual void computeDirection()
void checkDerivativeZero()
OptimizerBarzilaiBorwein(int dim, S eps, int maxIt, S lambda, int maxStepAttempts, std::string stepCondition, bool verboseOn=true, const std::string fname="", const std::string logFileName="", bool withUpperBound=false, S upperBound=S(), bool withLowerBound=false, S lowerBound=S(), bool vectorBounds=false, S controlEps=S(std::numeric_limits< double >::epsilon()), std::vector< OptimizerLogType > gplotAnalysis={})
Optimization algorithm: LineSearch.
C _direction
Search direction.
S _lambda
Lambda start value.
int _it
Current iteration no.
C _derivative
Vector of derivatives of the object functional with respect to the controlled variables.
int _dimCtrl
Number of controlled variables.
C _control
Vector of controlled variables (size _dimCtrl)
void computeDerivatives(const C &control, C &derivatives)
OptimizerBarzilaiBorwein< S, C > * createOptimizerBarzilaiBorwein(XMLreader const ¶ms, std::size_t dimCtrl)
Creator Function for Barzilai-Borwein.
void getGnuplotTagsFromString(std::string gplotAnalysisString, std::vector< OptimizerLogType > &gplotAnalysis)
the gplotAnalysisString is gained from the xml file and the function than separates and prepares it t...
T euklidN(const T x[], int dim)
Euclidean norm of an array.
Top level namespace for all of OpenLB.
Implements Euclidean norm for arrays.
The description of line search optimization algorithm – header file.
Creates a container of type C.