29#ifndef OPTIMIZER_LINE_SEARCH_H
30#define OPTIMIZER_LINE_SEARCH_H
50template<
typename S,
typename C>
77 OptimizerLineSearch(
int dimCtrl, S eps,
int maxIt, S lambda,
int maxStepAttempts, std::string stepCondition,
78 bool verboseOn=
true,
const std::string fname=
"",
const std::string logFileName=
"",
79 bool withUpperBound=
false, S upperBound=S(),
80 bool withLowerBound=
false, S lowerBound=S(),
bool vectorBounds=
false,
81 S controlEps=S(std::numeric_limits<double>::epsilon() ),
bool failOnMaxIter =
true,
82 std::vector<OptimizerLogType> gplotAnalysis = {})
83 :
Optimizer<S,C>(dimCtrl, eps, maxIt, verboseOn, fname, logFileName, withUpperBound, upperBound, withLowerBound,
84 lowerBound, vectorBounds, controlEps, failOnMaxIter, gplotAnalysis),
85 clout(std::cout,
"OptimizerLineSearch")
113 clout <<
"No step condition chosen! Exiting..." << std::endl;
127 for (
int iDim=0; iDim<this->
_dimCtrl; ++iDim) {
133 clout <<
"Control stuck at upper bound: " << std::string(this->
_upperBound.begin(), this->_upperBound.end()) << std::endl;
144 for (
int iDim=0; iDim<this->
_dimCtrl; ++iDim) {
150 clout <<
"Control stuck at lower bound: " << std::string(this->
_lowerBound.begin(), this->_lowerBound.end()) << std::endl;
163 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
170 && !std::signbit(this->
_control[iDim])) ) {
178 && std::signbit(this->
_control[iDim])) ) {
187 if (!(tempValue > this->
_value || std::isnan(tempValue))) {
211 for (
int i=0; i<this->
_dimCtrl; i++) {
216 if (!(tempValue <= this->
_value + c1*this->_lambda*dir)) {
218 clout <<
"Armijo failed!" << std::endl;
232 for (
int i=0; i<this->
_dimCtrl; i++) {
235 bool curvature = ( dirNext <= c2*dir );
242 clout <<
"Curvature failed!" << std::endl;
245 this->_lambda *= 2.1;
257 if (std::isnan(tempValue) ) {
258 newLambda = this->_lambda/2.;
261 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
262 dir += this->
_derivative[iDim]*this->_direction[iDim];
264 newLambda = dir*this->_lambda*this->_lambda / (2.*(tempValue - this->
_value + dir*this->
_lambda));
266 if (newLambda < this->
_lambda*.1 ) {
267 newLambda = this->_lambda*.1;
269 if (newLambda > this->_lambda*.5 ) {
270 newLambda = this->_lambda*.5;
273 this->_lambda = newLambda;
281 int refinementStep = 0;
284 while ( !(this->*condition)(tempValue) ) {
285 if (
util::abs(lambda/this->
_value) < std::numeric_limits<double>::epsilon() ) {
286 clout <<
"Excessive refinement steps (too small step size).\nProgram terminated." <<std::endl;
293 clout <<
"Excessive refinement steps (maxStepAttempts exeeded).\nProgram terminated." <<std::endl;
298 bool notSensible =
true;
299 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
302 S tmp = (newLambda-lambda)*this->_direction[iDim];
311 clout <<
"Excessive refinement steps (not sensible).\nProgram terminated." <<std::endl;
320 clout <<
"[Step " << this->
_it <<
"][Ref " << refinementStep <<
"] <<<<<<<<<< lambda=" << newLambda <<
" <<<<<<<<<<" << std::endl;
331 this->_lambda = startLambda;
340 clout <<
"Computing directions..." << std::endl;
352 const C initialControl = this->
_control;
355 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
365 clout <<
"[Step " << this->
_it <<
"] <<<<<<<<<< lambda=" << this->_lambda <<
" <<<<<<<<<<" << std::endl;
381 clout <<
"Bounding control" << std::endl;
383 for (
int iDim=0; iDim<this->
_dimCtrl; iDim++) {
395 for (
int iDim=0; iDim<this->
_dimCtrl; ++iDim) {
396 S ave = 0.5 * (initialControl[iDim] + this->
_control[iDim]);
class for marking output with some text
Optimization algorithm: LineSearch.
virtual void computeDirection()=0
C _direction
Search direction.
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.
void backtrackingLineSearch(S &tempValue, S lambda, bool(OptimizerLineSearch::*condition)(const S &))
bool(OptimizerLineSearch::* _stepConditionFunction)(const S &)
bool armijoWolfeConditions(const S &tempValue)
int _maxStepAttempts
Maximal number of step attempts for conditioned line search.
bool noCondition(const S &tempValue)
void quadraticInterpolationStep(const S &tempValue)
S _lambda
Lambda start value.
void(OptimizerLineSearch::* _stepLengthFunction)(const S &)
virtual ~OptimizerLineSearch()
virtual void optimizationStep()
Optimization step: line search.
std::string _stepCondition
bool smallerValue(const S &tempValue)
Interface for the use of various optimization algorithms.
bool _withUpperBound
Bounded versions.
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)
S _value
Value of the objective functional evaluated for controlled variables saved in _control.
bool _controlsConverged
For setting tolerance of controls.
void computeDerivatives(const C &control, C &derivatives)
void evaluateObjective(const C &control, S &result)
ADf< T, DIM > abs(const ADf< T, DIM > &a)
bool nearZero(const ADf< T, DIM > &a)
Top level namespace for all of OpenLB.
The description of optimization algorithms – header file.
Creates a container of type C.