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

Optimization algorithm: BarzilaiBorwein. More...

#include <optimizerBarzilaiBorwein.h>

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

Public Member Functions

 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={})
 
void checkDerivativeZero ()
 
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::OptimizerBarzilaiBorwein< S, C >

Optimization algorithm: BarzilaiBorwein.

OptimizerBarzilaiBorwein optimizes an optimization problem which is given in OptiCase. OptiCase provides therefore methods to evaluate an object functional and compute derivatives. BarzilaiBorwein searches along the direction of the derivative to find a lower value of the evaluated object functional. Thereby the steplength is adjusted to be an approximation of the Hessian. Barzilai-Borwein is therefore a "quasi" quasi Newton method.

This class is not intended to be derived from.

Definition at line 56 of file optimizerBarzilaiBorwein.h.

Constructor & Destructor Documentation

◆ OptimizerBarzilaiBorwein()

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

Definition at line 71 of file optimizerBarzilaiBorwein.h.

76 {})
77 : OptimizerLineSearch<S,C>(dim, eps, maxIt, /*lambda*/ 1., maxStepAttempts, stepCondition,
78 verboseOn, fname, logFileName, withUpperBound, upperBound, withLowerBound,
79 lowerBound, vectorBounds, controlEps, true, gplotAnalysis),
80 clout(std::cout,"OptimizerBarzilaiBorwein")
81 {
82
83 // Starting lambda for iT==0: steepest descent
84 // Line Search will be initialised with the natural lambda = 1
85 _startLambda = lambda;
86
87 _lastControl = util::ContainerCreator<C>::create(this->_dimCtrl);
88 _lastDerivative = util::ContainerCreator<C>::create(this->_dimCtrl);
89
90 _sStore = util::ContainerCreator<C>::create(this->_dimCtrl);
91 _yStore = util::ContainerCreator<C>::create(this->_dimCtrl);
92
93 };
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

Member Function Documentation

◆ checkDerivativeZero()

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

Definition at line 95 of file optimizerBarzilaiBorwein.h.

96 {
97 S normDir = util::euklidN(this->_direction.data(), this->_dimCtrl);
98 if (std::isnan(normDir)) {
99 clout << "Warning: Derivative is null at first iteration. Check derivative calculations.\nProgram terminated" << std::endl;
100 exit(1);
101 }
102 }
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::OptimizerBarzilaiBorwein< S, C >::computeDirection ( )
inlinevirtual

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

Definition at line 104 of file optimizerBarzilaiBorwein.h.

105 {
106 for (int i=0; i<this->_dimCtrl; i++) {
107 _sStore[i] = 0.;
108 _yStore[i] = 0.;
109 }
110 S sTy = 0.;
111 S sTs = 0.;
112 S yTy = 0.;
113 // Alternating between lambda_1 and lambda_2, see:
114 // http://www.math.ucla.edu/~wotaoyin/math273a/slides/Lec4a_Baizilai_Borwein_method_273a_2015_f.pdf, p.6
115 bool alternate = false;
116
117
118 // Compute Derivatives
119 // computeDerivative only if not already done by wolfeCondition()
120 if (!this->_nextDerivFlag) {
121 this->computeDerivatives(this->_control, this->_derivative);
122 }
123 else {
124 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
125 this->_derivative[iDim] = this->_nextDerivative[iDim];
126 }
127 }
128
129
130 if (this->_it == 0) {
131 // On first step do normalised steepest descent
132 S normDerivative = util::euklidN(this->_derivative.data(), this->_dimCtrl);
133 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
134 this->_direction[iDim] = _startLambda * this->_derivative[iDim] / normDerivative;
135 }
136 // terminate early if we recieve nan controls
138 // Store control and derivative
139 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
140 _lastControl[iDim] = this->_control[iDim];
141 _lastDerivative[iDim] = this->_derivative[iDim];
142 }
143
144 }
145 else {
146 // Calculate lambda as quasi Newton
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];
152 if (alternate) {
153 yTy += _yStore[iDim]*_yStore[iDim];
154 }
155 }
156 // lambda_1 is minimizing || sD - y ||
157 this->_lambda = sTs/sTy; //lambda_1
158 // lambda_2 is minimizing || s - yD^-1 ||
159 //this->_lambda = sTy/yTy; //lambda_2
160
161 //Alternate lambda_1 and lambda_2
162 if (alternate && this->_it%2 != 0) {
163 this->_lambda = sTy/yTy;
164 }
165
166 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
167 _lastControl[iDim] = this->_control[iDim];
168 _lastDerivative[iDim] = this->_derivative[iDim];
169 }
170
171 for (int iDim=0; iDim<this->_dimCtrl; iDim++) {
172 this->_direction[iDim] = this->_derivative[iDim];
173 }
174 }
175 };
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 >::_lambda, olb::opti::OptimizerLineSearch< S, C >::_nextDerivative, olb::opti::OptimizerLineSearch< S, C >::_nextDerivFlag, olb::opti::OptimizerBarzilaiBorwein< S, C >::checkDerivativeZero(), olb::opti::Optimizer< S, C >::computeDerivatives(), and olb::util::euklidN().

+ Here is the call graph for this function:

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