29#ifndef OPTIMIZER_LBFGS_H
30#define OPTIMIZER_LBFGS_H
53template<
typename S,
typename C>
78 int dimCtrl, S eps,
int maxIt, S lambda,
int maxStepAttempts,
79 std::string stepCondition,
int l, S startCoefH,
bool verboseOn=
true,
80 const std::string fname=
"",
const std::string logFileName=
"",
81 bool withUpperBound=
false, S upperBound=S(),
82 bool withLowerBound=
false, S lowerBound=S(),
bool vectorBounds=
false,
83 S controlEps=S(std::numeric_limits<double>::epsilon() ),
bool failOnMaxIter =
true,
84 std::vector<OptimizerLogType> gplotAnalysis = {})
86 dimCtrl, eps, maxIt, 1., maxStepAttempts, stepCondition, verboseOn,
87 fname, logFileName, withUpperBound, upperBound,
88 withLowerBound, lowerBound, vectorBounds, controlEps, failOnMaxIter, gplotAnalysis),
89 clout(std::cout,
"OptimizerLBFGS")
93 _startLambda = lambda;
96 _startCoefH = startCoefH;
103 _sStore =
new S* [_l];
104 _yStore =
new S* [_l];
106 _rhoStore =
new S [_l];
109 for (
int i=0; i<_l; i++) {
114 for (
int i=0; i<_l; i++) {
115 _sStore[i] =
new S [this->
_dimCtrl];
116 _yStore[i] =
new S [this->
_dimCtrl];
126 for (
int i=0; i<_l; i++) {
144 _startCoefH = startCoefH;
150 if (std::isnan(normDir)) {
151 clout <<
"Warning: Derivative is null at first iteration. Check derivative calculations.\nProgram terminated" << std::endl;
160 _rhoStore[(this->
_it)%_l] = 0;
163 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
164 _sStore[(this->
_it)%_l][iDim] = this->
_control[iDim]-_lastControl[iDim];
165 _yStore[(this->
_it)%_l][iDim] = this->
_derivative[iDim]-_lastDerivative[iDim];
166 _rhoStore[(this->
_it)%_l] += _yStore[(this->
_it)%_l][iDim]*_sStore[(this->
_it)%_l][iDim];
167 temp1 += _sStore[(this->
_it)%_l][iDim]*_yStore[(this->
_it)%_l][iDim];
168 temp2 += _yStore[(this->
_it)%_l][iDim]*_yStore[(this->
_it)%_l][iDim];
170 _startCoefH = temp1/temp2;
172 _rhoStore[(this->
_it)%_l] = 1./_rhoStore[(this->
_it)%_l];
174 _firstStore = (_firstStore+1)%_l;
176 _lastStore = (this->
_it)%_l;
179 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
180 _lastControl[iDim] = this->
_control[iDim];
193 else for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
200 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
213 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
224 for (
int i=0; i<iMax; i++) {
225 iStore = (_lastStore + _l - i) % _l;
228 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
229 _alpha[iStore] += _sStore[iStore][iDim]*this->
_direction[iDim];
231 _alpha[iStore] *= _rhoStore[iStore];
233 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
234 this->
_direction[iDim] -= _alpha[iStore]*_yStore[iStore][iDim];
237 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
242 for (
int i=0; i<iMax; i++) {
243 iStore = (_firstStore + _l + i) % _l;
246 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
247 beta += _yStore[iStore][iDim]*this->
_direction[iDim];
249 beta *= _rhoStore[iStore];
251 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
252 this->
_direction[iDim] += _sStore[iStore][iDim]*(_alpha[iStore]-beta);
262template<
typename S,
typename C = std::vector<S>>
271 int maxStepAttempts = 100;
272 std::string stepCondition =
"StrongWolfe";
277 S startCoefH = S(1e-4);
280 std::string fname =
"control.dat";
281 std::string logFileName =
"log.txt";
282 bool vectorBounds =
false;
283 bool withUpperBound =
false;
285 bool withLowerBound =
false;
287 bool failOnMaxIter =
true;
289 std::vector<OptimizerLogType> gplotAnalysis = {};
290 std::string gplotAnalysisString =
"";
293 params.
readOrWarn<
int>(
"Optimization",
"MaxIter",
"", maxIt);
294 params.
readOrWarn<
int>(
"Optimization",
"L",
"", l);
295 params.
readOrWarn<S>(
"Optimization",
"Lambda",
"", lambda);
296 params.
readOrWarn<
int>(
"Optimization",
"MaxStepAttempts",
"", maxStepAttempts);
297 params.
readOrWarn<std::string>(
"Optimization",
"StepCondition",
"", stepCondition);
299 params.
readOrWarn<S>(
"Optimization",
"Tolerance",
"", eps);
300 params.
readOrWarn<S>(
"Optimization",
"ControlTolerance",
"", controlEps);
301 params.
readOrWarn<S>(
"Optimization",
"StartCoefH",
"", startCoefH);
302 params.
readOrWarn<
bool>(
"Optimization",
"FailOnMaxIter",
"", failOnMaxIter);
303 params.
readOrWarn<
bool>(
"Optimization",
"Verbose",
"", verboseOn);
304 params.
readOrWarn<std::string>(
"Optimization",
"InputFileName",
"", fname);
305 params.
readOrWarn<std::string>(
"Optimization",
"LogFileName",
"", logFileName);
307 params.
readOrWarn<
bool>(
"Optimization",
"VectorBounds",
"", vectorBounds);
308 if ( params.
readOrWarn<S>(
"Optimization",
"UpperBound",
"", upperBound,
false,
false) ) {
309 withUpperBound =
true;
312 if ( params.
readOrWarn<S>(
"Optimization",
"LowerBound",
"", lowerBound,
false,
false) ) {
313 withLowerBound =
true;
317 params.
readOrWarn<std::string>(
"Optimization",
318 "VisualizationGnuplot",
319 "VisualizedParameters", gplotAnalysisString,
false,
false);
323 clout <<
"Creating optimizer ..." << std::endl;
325 return new OptimizerLBFGS<S,C>(dimCtrl, eps, maxIt, lambda, maxStepAttempts, stepCondition, l, startCoefH,
326 verboseOn, fname, logFileName, withUpperBound, upperBound, withLowerBound, lowerBound, vectorBounds,
327 controlEps, failOnMaxIter, 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: LBFGS.
void checkDerivativeZero()
OptimizerLBFGS(int dimCtrl, S eps, int maxIt, S lambda, int maxStepAttempts, std::string stepCondition, int l, S startCoefH, 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()), bool failOnMaxIter=true, std::vector< OptimizerLogType > gplotAnalysis={})
virtual void computeDirection()
void setStartCoefH(S startCoefH)
Optimization algorithm: LineSearch.
C _direction
Search direction.
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)
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...
OptimizerLBFGS< S, C > * createOptimizerLBFGS(XMLreader const ¶ms, std::size_t dimCtrl)
Creator Function for LBFGS.
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.