OpenLB 1.7
Loading...
Searching...
No Matches
Public Member Functions | List of all members
olb::opti::OptimizerLBFGS< S, C > Class Template Reference

Optimization algorithm: LBFGS. More...

#include <optimizerLBFGS.h>

+ Inheritance diagram for olb::opti::OptimizerLBFGS< S, C >:
+ Collaboration diagram for olb::opti::OptimizerLBFGS< S, C >:

Public Member Functions

 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={})
 
 ~OptimizerLBFGS ()
 
void setL (int l)
 
void setStartCoefH (S startCoefH)
 
void checkDerivativeZero ()
 
void storeHelpers ()
 
virtual void computeDirection ()
 
- Public Member Functions inherited from olb::opti::OptimizerLineSearch< S, C >
 OptimizerLineSearch (int dimCtrl, 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()), bool failOnMaxIter=true, std::vector< OptimizerLogType > gplotAnalysis={})
 Construction of an OptimizerLineSearch.
 
virtual ~OptimizerLineSearch ()
 
void checkBound ()
 
void boundControl ()
 
bool smallerValue (const S &tempValue)
 
bool noCondition (const S &tempValue)
 
bool armijoWolfeConditions (const S &tempValue)
 
void quadraticInterpolationStep (const S &tempValue)
 
void backtrackingLineSearch (S &tempValue, S lambda, bool(OptimizerLineSearch::*condition)(const S &))
 
virtual void optimizationStep ()
 Optimization step: line search.
 
- Public Member Functions inherited from olb::opti::Optimizer< S, C >
 Optimizer (int dimCtrl, S eps, int maxIt, 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 ~Optimizer ()
 
void maxIterationReached ()
 
virtual void optimize ()
 
virtual void optimize (OptiCase< S, C > &optiCase)
 
void simulate ()
 
void simulate (OptiCase< S, C > &optiCase)
 
void evaluateObjective (const C &control, S &result)
 
void computeDerivatives (const C &control, C &derivatives)
 
void print (int it)
 Prints information of the current optimization step it.
 
void setControl (C &control)
 
const C & getControl () const
 
const C & getDerivative () const
 
const S & getObjective () const
 
int getIteration () const
 
void writeControlToFile (const std::string fname="control.dat")
 Writes the current control variables linewise into file fname.
 
void readControlFromFile (const std::string fname="control.dat")
 Reads the latest control variables from file fname.
 
void setStartValue (S startValue)
 
OptiCase< S, C > * getOptiCase ()
 
void setOptiCase (OptiCase< S, C > *optiCase)
 
void setGnuplotData ()
 
void setReferenceControl (C result)
 set the reference value for the control vector (exact solution)
 

Additional Inherited Members

- Public Attributes inherited from olb::opti::Optimizer< S, C >
Gnuplot< S > gplot
 
std::vector< OptimizerLogType_gplotAnalysis
 For defining what kind of gnuplot analysis is wanted, if empty vector - no analysis, value, control and derivative are the possible options.
 
- Protected Attributes inherited from olb::opti::OptimizerLineSearch< S, C >
_lambda
 Lambda start value.
 
_direction
 Search direction.
 
bool _lowerBoundFlag
 
bool _upperBoundFlag
 
int _maxStepAttempts
 Maximal number of step attempts for conditioned line search.
 
_nextDerivative
 
bool _nextDerivFlag
 
std::string _stepCondition
 
bool(OptimizerLineSearch::* _stepConditionFunction )(const S &)
 
void(OptimizerLineSearch::* _stepLengthFunction )(const S &)
 
- Protected Attributes inherited from olb::opti::Optimizer< S, C >
int _dimCtrl
 Number of controlled variables.
 
_control
 Vector of controlled variables (size _dimCtrl)
 
_value
 Value of the objective functional evaluated for controlled variables saved in _control.
 
_derivative
 Vector of derivatives of the object functional with respect to the controlled variables.
 
int _it
 Current iteration no.
 
int _maxIt
 Maximal number of iteration.
 
bool _failOnMaxIter
 Fail when max number of iteration reached.
 
_eps
 Optimizer stops if |_derivatives| < _eps.
 
bool _verboseOn
 Verbose.
 
bool _withUpperBound
 Bounded versions.
 
bool _withLowerBound
 
bool _vectorBounds
 
_boundedControl
 
_upperBound
 
_lowerBound
 
bool _controlsConverged
 For setting tolerance of controls.
 
_controlEps
 
OptiCase< S, C > * _optiCase
 Provides the Optimizer with methods to evaluate the value of an object functional and compute derivatives.
 
_referenceControl
 control vector to compare with (for numerical evaluation)
 

Detailed Description

template<typename S, typename C>
class olb::opti::OptimizerLBFGS< S, C >

Optimization algorithm: LBFGS.

OptimizerSteepestDescent optimizes an optimization problem which is given in OptiCase. OptiCase provides therefore methods to evaluate an object functional and compute derivatives. LBFGS searches along the direction of the derivative to find a lower value of the evaluated object functional.

This class is not intended to be derived from.

Definition at line 54 of file optimizerLBFGS.h.

Constructor & Destructor Documentation

◆ OptimizerLBFGS()

template<typename S , typename C >
olb::opti::OptimizerLBFGS< S, C >::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 = {} )
inline

Definition at line 77 of file optimizerLBFGS.h.

84 {})
86 dimCtrl, eps, maxIt, /*lambda*/ 1., maxStepAttempts, stepCondition, verboseOn,
87 fname, logFileName, withUpperBound, upperBound,
88 withLowerBound, lowerBound, vectorBounds, controlEps, failOnMaxIter, gplotAnalysis),
89 clout(std::cout,"OptimizerLBFGS")
90 {
91 // Starting lambda for iT==0: steepest descent
92 // Line Search will be initialised with the natural lambda = 1
93 _startLambda = lambda;
94
95 _l=l;
96 _startCoefH = startCoefH;
97 _firstStore = 1;
98 _lastStore = 1;
99
100 _lastControl = util::ContainerCreator<C>::create(this->_dimCtrl);
101 _lastDerivative = util::ContainerCreator<C>::create(this->_dimCtrl);
102
103 _sStore = new S* [_l];
104 _yStore = new S* [_l];
105
106 _rhoStore = new S [_l];
107 _alpha = new S [_l];
108
109 for (int i=0; i<_l; i++) {
110 _rhoStore[i] = S(0);
111 _alpha[i] = S(0);
112 }
113
114 for (int i=0; i<_l; i++) {
115 _sStore[i] = new S [this->_dimCtrl];
116 _yStore[i] = new S [this->_dimCtrl];
117 //for (int iDim=0; iDim<this->_dimCtrl; ++iDim) {
118 //_sStore[i][iDim] = S(0);
119 //_yStore[i][iDim] = S(0);
120 //};
121 }
122 };
OptimizerLineSearch(int dimCtrl, 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()), bool failOnMaxIter=true, std::vector< OptimizerLogType > gplotAnalysis={})
Construction of an OptimizerLineSearch.
int _dimCtrl
Number of controlled variables.
Definition optimizer.h:63

◆ ~OptimizerLBFGS()

template<typename S , typename C >
olb::opti::OptimizerLBFGS< S, C >::~OptimizerLBFGS ( )
inline

Definition at line 124 of file optimizerLBFGS.h.

125 {
126 for (int i=0; i<_l; i++) {
127 delete[] _sStore[i];
128 delete[] _yStore[i];
129 }
130 delete[] _sStore;
131 delete[] _yStore;
132 delete[] _rhoStore;
133 delete[] _alpha;
134 }

Member Function Documentation

◆ checkDerivativeZero()

template<typename S , typename C >
void olb::opti::OptimizerLBFGS< S, C >::checkDerivativeZero ( )
inline

Definition at line 147 of file optimizerLBFGS.h.

148 {
149 S normDir = util::euklidN(this->_direction.data(), this->_dimCtrl);
150 if (std::isnan(normDir)) {
151 clout << "Warning: Derivative is null at first iteration. Check derivative calculations.\nProgram terminated" << std::endl;
152 exit(1);
153 }
154 }
void exit(int exitcode)
Definition singleton.h:165
T euklidN(const T x[], int dim)
Euclidean norm of an array.
Definition norm.h:60

References olb::opti::OptimizerLineSearch< S, C >::_direction, and olb::util::euklidN().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ computeDirection()

template<typename S , typename C >
virtual void olb::opti::OptimizerLBFGS< S, C >::computeDirection ( )
inlinevirtual

Implements olb::opti::OptimizerLineSearch< S, C >.

Definition at line 185 of file optimizerLBFGS.h.

186 {
187
188 // Update _derivative
189 // computeDerivative only if not already done by wolfeCondition()
190 if (!this->_nextDerivFlag) {
191 this->computeDerivatives(this->_control, this->_derivative);
192 }
193 else for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
194 this->_derivative[iDim] = this->_nextDerivative[iDim];
195 }
196
197 if (this->_it==0) {
198 // On first step do normalised steepest descent
199 S normDerivative = util::euklidN(this->_derivative.data(), this->_dimCtrl);
200 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
201 this->_direction[iDim] = _startLambda * this->_derivative[iDim] / normDerivative;
202 }
203 // terminate early if we recieve nan controls
205
206 // Store Helpers!
207 storeHelpers();
208
209 }
210 else {
211 storeHelpers();
212
213 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
214 this->_direction[iDim] = this->_derivative[iDim];
215 }
216
217 int iMax = _l;
218 if (this->_it<_l) {
219 iMax = this->_it;
220 }
221
222 int iStore;
223 // first for loop
224 for (int i=0; i<iMax; i++) {
225 iStore = (_lastStore + _l - i) % _l;
226
227 _alpha[iStore] = 0;
228 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
229 _alpha[iStore] += _sStore[iStore][iDim]*this->_direction[iDim];
230 }
231 _alpha[iStore] *= _rhoStore[iStore];
232
233 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
234 this->_direction[iDim] -= _alpha[iStore]*_yStore[iStore][iDim];
235 }
236 } // end first for loop
237 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
238 this->_direction[iDim] *= _startCoefH;
239 }
240
241 // second for loop
242 for (int i=0; i<iMax; i++) {
243 iStore = (_firstStore + _l + i) % _l;
244
245 S beta = 0;
246 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
247 beta += _yStore[iStore][iDim]*this->_direction[iDim];
248 }
249 beta *= _rhoStore[iStore];
250
251 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
252 this->_direction[iDim] += _sStore[iStore][iDim]*(_alpha[iStore]-beta);
253 }
254 } // end second for loop
255 }
256 };
int _it
Current iteration no.
Definition optimizer.h:73
C _derivative
Vector of derivatives of the object functional with respect to the controlled variables.
Definition optimizer.h:71
C _control
Vector of controlled variables (size _dimCtrl)
Definition optimizer.h:65
void computeDerivatives(const C &control, C &derivatives)
Definition optimizer.h:144

References olb::opti::Optimizer< S, C >::_control, olb::opti::Optimizer< S, C >::_derivative, olb::opti::Optimizer< S, C >::_dimCtrl, olb::opti::OptimizerLineSearch< S, C >::_direction, olb::opti::Optimizer< S, C >::_it, olb::opti::OptimizerLineSearch< S, C >::_nextDerivative, olb::opti::OptimizerLineSearch< S, C >::_nextDerivFlag, olb::opti::OptimizerLBFGS< S, C >::checkDerivativeZero(), olb::opti::Optimizer< S, C >::computeDerivatives(), olb::util::euklidN(), and olb::opti::OptimizerLBFGS< S, C >::storeHelpers().

+ Here is the call graph for this function:

◆ setL()

template<typename S , typename C >
void olb::opti::OptimizerLBFGS< S, C >::setL ( int l)
inline

Definition at line 137 of file optimizerLBFGS.h.

138 {
139 _l=l;
140 };

◆ setStartCoefH()

template<typename S , typename C >
void olb::opti::OptimizerLBFGS< S, C >::setStartCoefH ( S startCoefH)
inline

Definition at line 142 of file optimizerLBFGS.h.

143 {
144 _startCoefH = startCoefH;
145 };

◆ storeHelpers()

template<typename S , typename C >
void olb::opti::OptimizerLBFGS< S, C >::storeHelpers ( )
inline

Definition at line 156 of file optimizerLBFGS.h.

157 {
158
159 if (this->_it!=0) {
160 _rhoStore[(this->_it)%_l] = 0;
161 S temp1 = S();
162 S temp2 = S();
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];
169 }
170 _startCoefH = temp1/temp2;
171
172 _rhoStore[(this->_it)%_l] = 1./_rhoStore[(this->_it)%_l];
173 if (this->_it>_l) {
174 _firstStore = (_firstStore+1)%_l;
175 }
176 _lastStore = (this->_it)%_l;
177 }
178
179 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
180 _lastControl[iDim] = this->_control[iDim];
181 _lastDerivative[iDim] = this->_derivative[iDim];
182 }
183 }

References olb::opti::Optimizer< S, C >::_control, olb::opti::Optimizer< S, C >::_derivative, olb::opti::Optimizer< S, C >::_dimCtrl, and olb::opti::Optimizer< S, C >::_it.

+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: