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

Interface for the use of various optimization algorithms. More...

#include <optimizer.h>

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

Public Member Functions

 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 ()
 
virtual void optimizationStep ()=0
 
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)
 

Public Attributes

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

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::Optimizer< S, C >

Interface for the use of various optimization algorithms.

This class is intended to be derived from. S is the underlying arithmetic data type C is the container type that is used to store the vectors (e.g. std::vector<S>) C is expected to allow construction via ContainerCreator class

Definition at line 56 of file optimizer.h.

Constructor & Destructor Documentation

◆ Optimizer()

template<typename S , typename C >
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 = {} )

Definition at line 41 of file optimizer.hh.

50: clout(std::cout,"Optimizer"), _dimCtrl(dimCtrl),
51 _it(0), _maxIt(maxIt), _failOnMaxIter(failOnMaxIter), _eps(eps),
52 _verboseOn(verboseOn),
53 _withUpperBound(withUpperBound), _withLowerBound(withLowerBound),
54 _vectorBounds(vectorBounds), _controlEps(controlEps),
55 gplot(logFileName, Gnuplot<S>::LINEAR, Gnuplot<S>::OFF),
56 _gplotAnalysis(gplotAnalysis)
57{
58 _controlsConverged = false;
59
60 _control = util::ContainerCreator<C>::create(_dimCtrl);
61 _derivative = util::ContainerCreator<C>::create(_dimCtrl);
62
64 _boundedControl = util::ContainerCreator<C>::create(_dimCtrl);
65 if (_withLowerBound) {
66 _lowerBound = util::ContainerCreator<C>::create(_dimCtrl);
67 if (! _vectorBounds) {
68 _lowerBound[0] = lowerBound; // only first component is used...
69 }
70 }
71 if (_withUpperBound) {
72 _upperBound = util::ContainerCreator<C>::create(_dimCtrl);
73 if (! _vectorBounds) {
74 _upperBound[0] = upperBound; // only first component is used...
75 }
76 }
77 }
78
80}
std::vector< OptimizerLogType > _gplotAnalysis
For defining what kind of gnuplot analysis is wanted, if empty vector - no analysis,...
Definition optimizer.h:106
bool _withUpperBound
Bounded versions.
Definition optimizer.h:84
int _maxIt
Maximal number of iteration.
Definition optimizer.h:75
S _eps
Optimizer stops if |_derivatives| < _eps.
Definition optimizer.h:79
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
int _dimCtrl
Number of controlled variables.
Definition optimizer.h:63
bool _verboseOn
Verbose.
Definition optimizer.h:81
C _control
Vector of controlled variables (size _dimCtrl)
Definition optimizer.h:65
void readControlFromFile(const std::string fname="control.dat")
Reads the latest control variables from file fname.
Definition optimizer.hh:155
Gnuplot< S > gplot
Definition optimizer.h:103
bool _controlsConverged
For setting tolerance of controls.
Definition optimizer.h:92
bool _failOnMaxIter
Fail when max number of iteration reached.
Definition optimizer.h:77

References olb::opti::Optimizer< S, C >::_boundedControl, olb::opti::Optimizer< S, C >::_control, olb::opti::Optimizer< S, C >::_controlsConverged, olb::opti::Optimizer< S, C >::_derivative, olb::opti::Optimizer< S, C >::_dimCtrl, olb::opti::Optimizer< S, C >::_lowerBound, olb::opti::Optimizer< S, C >::_upperBound, olb::opti::Optimizer< S, C >::_vectorBounds, olb::opti::Optimizer< S, C >::_withLowerBound, olb::opti::Optimizer< S, C >::_withUpperBound, and olb::opti::Optimizer< S, C >::readControlFromFile().

+ Here is the call graph for this function:

◆ ~Optimizer()

template<typename S , typename C >
virtual olb::opti::Optimizer< S, C >::~Optimizer ( )
inlinevirtual

Definition at line 118 of file optimizer.h.

118{ };

Member Function Documentation

◆ computeDerivatives()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::computeDerivatives ( const C & control,
C & derivatives )
inline

Definition at line 144 of file optimizer.h.

144 {
145
146 _optiCase->computeDerivatives(control, derivatives, _it);
147 }
OptiCase< S, C > * _optiCase
Provides the Optimizer with methods to evaluate the value of an object functional and compute derivat...
Definition optimizer.h:97

References olb::opti::Optimizer< S, C >::_it, olb::opti::Optimizer< S, C >::_optiCase, and olb::opti::control.

+ Here is the caller graph for this function:

◆ evaluateObjective()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::evaluateObjective ( const C & control,
S & result )
inline

Definition at line 140 of file optimizer.h.

140 {
141 result = _optiCase->evaluateObjective(control, _it);
142 }

References olb::opti::Optimizer< S, C >::_it, olb::opti::Optimizer< S, C >::_optiCase, and olb::opti::control.

+ Here is the caller graph for this function:

◆ getControl()

template<typename S , typename C >
const C & olb::opti::Optimizer< S, C >::getControl ( ) const
inline

Definition at line 156 of file optimizer.h.

156 {
157 return _control;
158 }

References olb::opti::Optimizer< S, C >::_control.

◆ getDerivative()

template<typename S , typename C >
const C & olb::opti::Optimizer< S, C >::getDerivative ( ) const
inline

Definition at line 160 of file optimizer.h.

160 {
161 return _derivative;
162 }

References olb::opti::Optimizer< S, C >::_derivative.

◆ getIteration()

template<typename S , typename C >
int olb::opti::Optimizer< S, C >::getIteration ( ) const
inline

Definition at line 168 of file optimizer.h.

168 {
169 return _it;
170 }

References olb::opti::Optimizer< S, C >::_it.

◆ getObjective()

template<typename S , typename C >
const S & olb::opti::Optimizer< S, C >::getObjective ( ) const
inline

Definition at line 164 of file optimizer.h.

164 {
165 return _value;
166 }
S _value
Value of the objective functional evaluated for controlled variables saved in _control.
Definition optimizer.h:68

References olb::opti::Optimizer< S, C >::_value.

◆ getOptiCase()

template<typename S , typename C >
OptiCase< S, C > * olb::opti::Optimizer< S, C >::getOptiCase ( )
inline

Definition at line 180 of file optimizer.h.

180{ return _optiCase; }

References olb::opti::Optimizer< S, C >::_optiCase.

◆ maxIterationReached()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::maxIterationReached ( )

Definition at line 83 of file optimizer.hh.

84{
85 if (_failOnMaxIter) {
86 clout << "Warning: Optimization problem failed to converge within specified iteration limit of "
87 << _maxIt << " iterations with tolerance of " << _eps << ".\nProgram terminated" << std::endl;
88 exit(1);
89 }
90 else {
91 clout << "Maximum iteration reached." << std::endl;
92 }
93}
void exit(int exitcode)
Definition singleton.h:165

◆ optimizationStep()

template<typename S , typename C >
virtual void olb::opti::Optimizer< S, C >::optimizationStep ( )
pure virtual

◆ optimize() [1/2]

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::optimize ( )
virtual

Definition at line 96 of file optimizer.hh.

97{
98 // Evaluate objective function
100 _optiCase->postEvaluation();
101
102 // Optimization step (update of _control, _value, _derivatives and _it)
103 do {
104 if (_it >= _maxIt) {
106 return;
107 }
108
109 // Call optimisation algorithm: evaluate objective function and compute derivatives
111
112 // Print info
113 _optiCase->postEvaluation();
114 if (_verboseOn) {
115 print(_it);
116 }
117 if (_gplotAnalysis.size() > 0) {
119 }
120
121 // Optimize as long as |derivative| > eps or a maximum of iterations is reached
122 }
124
125 if(_gplotAnalysis.size() > 0){
126 gplot.writePNG();
127 }
128}
void writePNG(int iT=-1, double xRange=-1, std::string plotName="")
writes PNGs usage: first argument: numbering of png file (optional), second argument: range for the x...
void print(int it)
Prints information of the current optimization step it.
Definition optimizer.hh:131
virtual void optimizationStep()=0
void evaluateObjective(const C &control, S &result)
Definition optimizer.h:140
T euklidN2(const T x[], int dim)
Squared Euclidean norm of an array.
Definition norm.h:37

References olb::util::euklidN2().

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

◆ optimize() [2/2]

template<typename S , typename C >
virtual void olb::opti::Optimizer< S, C >::optimize ( OptiCase< S, C > & optiCase)
inlinevirtual

Definition at line 126 of file optimizer.h.

126 {
127 _optiCase = &optiCase;
128 optimize();
129 }
virtual void optimize()
Definition optimizer.hh:96

References olb::opti::Optimizer< S, C >::_optiCase, and olb::opti::Optimizer< S, C >::optimize().

+ Here is the call graph for this function:

◆ print()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::print ( int it)

Prints information of the current optimization step it.

Definition at line 131 of file optimizer.hh.

132{
133 clout << "=======================================" << std::endl;
134 clout << ">>>>>>>>>> Optimizer: step " << it << " <<<<<<<<<<" << std::endl;
135 clout << " Value objective = " << std::setprecision(12) << _value << std::endl;
136 clout << " Norm derivative = " << std::setprecision(12) << util::euklidN(&_derivative[0], _dimCtrl) << std::endl;
137 clout << "=======================================" << std::endl;
138}
T euklidN(const T x[], int dim)
Euclidean norm of an array.
Definition norm.h:60

References olb::util::euklidN().

+ Here is the call graph for this function:

◆ readControlFromFile()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::readControlFromFile ( const std::string fname = "control.dat")

Reads the latest control variables from file fname.

Definition at line 155 of file optimizer.hh.

156{
157 std::ifstream file(fname.c_str());
158 if ( file.is_open() ) {
159 int dimCtrl;
160 file >> dimCtrl;
161 if (dimCtrl!=_dimCtrl) {
162 clout << "Error: dimensions do not match! dim_controller=" << _dimCtrl << "; dim_file=" << dimCtrl << std::endl;
163 assert(false);
164 }
165 else {
166 for (int i=0; i<_dimCtrl; i++) {
167 double tmpVal;
168 file >> tmpVal;
169 _control[i] = tmpVal;
170 }
172 for (int i=0; i<_dimCtrl; i++) {
173 double tmpVal;
174 file >> tmpVal;
175 _lowerBound[i] = tmpVal;
176 }
177 }
179 for (int i=0; i<_dimCtrl; i++) {
180 double tmpVal;
181 file >> tmpVal;
182 _upperBound[i] = tmpVal;
183 }
184 }
185 }
186 file.close();
187 }
188}
+ Here is the caller graph for this function:

◆ setControl()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::setControl ( C & control)
inline

Definition at line 152 of file optimizer.h.

152 {
153 _control = std::move(control);
154 }

References olb::opti::Optimizer< S, C >::_control, and olb::opti::control.

◆ setGnuplotData()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::setGnuplotData ( )

Definition at line 198 of file optimizer.hh.

199{
200 std::vector<S> yValues;
201 std::vector<std::string> labels;
202 std::vector<char> plotTypes;
203
204 for(unsigned i = 0; i < _gplotAnalysis.size(); i++){
205 if(_gplotAnalysis[i] == value){
206 yValues.push_back(_value);
207 labels.push_back("value");
208 plotTypes.push_back('p');
209 } else if(_gplotAnalysis[i] == control){
210 // get all entries of control
211 for(unsigned j = 0; j< _control.size(); j++){
212 yValues.push_back(_control[j]);
213 labels.push_back("control[" + std::to_string(j) + "]");
214 plotTypes.push_back('p');
215 }
216 } else if(_gplotAnalysis[i] == derivative){
217
218 for(unsigned j = 0; j< _derivative.size(); j++){
219 yValues.push_back(_derivative[j]);
220 labels.push_back("derivative[" + std::to_string(j) + "]");
221 plotTypes.push_back('p');
222 }
223 } else if(_gplotAnalysis[i] == error){
225 labels.push_back("control_error");
226 plotTypes.push_back('p');
227 } else if(_gplotAnalysis[i] == norm_derivative){
228 yValues.push_back(util::euklidN(&_derivative[0], _dimCtrl));
229 labels.push_back("derivative_norm");
230 plotTypes.push_back('p');
231 }
232 }
233 gplot.setData(S(_it), yValues, labels, "top right", plotTypes); // _it is the iterationstep
234}
void setData(T xValue, T yValue, std::string name="", std::string key="", char plotType='l')
sets the data and plot file for two doubles (x and y) the plotType indicates whether the user want to...
C _referenceControl
control vector to compare with (for numerical evaluation)
Definition optimizer.h:100
@ norm_derivative
Definition optimizer.h:46
T euklidDistance(const T x[], const T y[], int dim)
Euclidean distance between two arrays.
Definition norm.h:67

References olb::opti::control, olb::opti::derivative, olb::opti::error, olb::util::euklidDistance(), olb::util::euklidN(), olb::opti::norm_derivative, and olb::opti::value.

+ Here is the call graph for this function:

◆ setOptiCase()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::setOptiCase ( OptiCase< S, C > * optiCase)
inline

Definition at line 182 of file optimizer.h.

182{ _optiCase = optiCase; }

References olb::opti::Optimizer< S, C >::_optiCase.

◆ setReferenceControl()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::setReferenceControl ( C result)
inline

set the reference value for the control vector (exact solution)

Definition at line 187 of file optimizer.h.

187 {
188 _referenceControl = result;
189 }

References olb::opti::Optimizer< S, C >::_referenceControl.

◆ setStartValue()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::setStartValue ( S startValue)

Definition at line 191 of file optimizer.hh.

192{
193 // update control variables
194 std::fill(_control.begin(), _control.end(), startValue);
195}

◆ simulate() [1/2]

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::simulate ( )
inline

Definition at line 131 of file optimizer.h.

131 {
133 }

References olb::opti::Optimizer< S, C >::_control, olb::opti::Optimizer< S, C >::_value, and olb::opti::Optimizer< S, C >::evaluateObjective().

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

◆ simulate() [2/2]

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::simulate ( OptiCase< S, C > & optiCase)
inline

Definition at line 135 of file optimizer.h.

135 {
136 _optiCase = &optiCase;
137 simulate();
138 }

References olb::opti::Optimizer< S, C >::_optiCase, and olb::opti::Optimizer< S, C >::simulate().

+ Here is the call graph for this function:

◆ writeControlToFile()

template<typename S , typename C >
void olb::opti::Optimizer< S, C >::writeControlToFile ( const std::string fname = "control.dat")

Writes the current control variables linewise into file fname.

Definition at line 141 of file optimizer.hh.

142{
143 if (singleton::mpi().isMainProcessor() ) {
144 std::stringstream stream;
145 stream << _dimCtrl << std::endl;
146 std::for_each(_control.begin(), _control.end(), [&stream](auto c){ stream << c << '\n'; });
147 std::ofstream file;
148 file.open(singleton::directories().getLogOutDir() + fname.c_str());
149 file << stream.str();
150 file.close();
151 }
152}
MpiManager & mpi()
Directories & directories()
Definition singleton.h:150

References olb::singleton::directories(), and olb::singleton::mpi().

+ Here is the call graph for this function:

Member Data Documentation

◆ _boundedControl

template<typename S , typename C >
C olb::opti::Optimizer< S, C >::_boundedControl
protected

Definition at line 87 of file optimizer.h.

◆ _control

template<typename S , typename C >
C olb::opti::Optimizer< S, C >::_control
protected

Vector of controlled variables (size _dimCtrl)

Definition at line 65 of file optimizer.h.

◆ _controlEps

template<typename S , typename C >
S olb::opti::Optimizer< S, C >::_controlEps
protected

Definition at line 93 of file optimizer.h.

◆ _controlsConverged

template<typename S , typename C >
bool olb::opti::Optimizer< S, C >::_controlsConverged
protected

For setting tolerance of controls.

Definition at line 92 of file optimizer.h.

◆ _derivative

template<typename S , typename C >
C olb::opti::Optimizer< S, C >::_derivative
protected

Vector of derivatives of the object functional with respect to the controlled variables.

Definition at line 71 of file optimizer.h.

◆ _dimCtrl

template<typename S , typename C >
int olb::opti::Optimizer< S, C >::_dimCtrl
protected

Number of controlled variables.

Definition at line 63 of file optimizer.h.

◆ _eps

template<typename S , typename C >
S olb::opti::Optimizer< S, C >::_eps
protected

Optimizer stops if |_derivatives| < _eps.

Definition at line 79 of file optimizer.h.

◆ _failOnMaxIter

template<typename S , typename C >
bool olb::opti::Optimizer< S, C >::_failOnMaxIter
protected

Fail when max number of iteration reached.

Definition at line 77 of file optimizer.h.

◆ _gplotAnalysis

template<typename S , typename C >
std::vector<OptimizerLogType> olb::opti::Optimizer< S, C >::_gplotAnalysis

For defining what kind of gnuplot analysis is wanted, if empty vector - no analysis, value, control and derivative are the possible options.

Definition at line 106 of file optimizer.h.

◆ _it

template<typename S , typename C >
int olb::opti::Optimizer< S, C >::_it
protected

Current iteration no.

Definition at line 73 of file optimizer.h.

◆ _lowerBound

template<typename S , typename C >
C olb::opti::Optimizer< S, C >::_lowerBound
protected

Definition at line 89 of file optimizer.h.

◆ _maxIt

template<typename S , typename C >
int olb::opti::Optimizer< S, C >::_maxIt
protected

Maximal number of iteration.

Definition at line 75 of file optimizer.h.

◆ _optiCase

template<typename S , typename C >
OptiCase<S,C>* olb::opti::Optimizer< S, C >::_optiCase
protected

Provides the Optimizer with methods to evaluate the value of an object functional and compute derivatives.

Definition at line 97 of file optimizer.h.

◆ _referenceControl

template<typename S , typename C >
C olb::opti::Optimizer< S, C >::_referenceControl
protected

control vector to compare with (for numerical evaluation)

Definition at line 100 of file optimizer.h.

◆ _upperBound

template<typename S , typename C >
C olb::opti::Optimizer< S, C >::_upperBound
protected

Definition at line 88 of file optimizer.h.

◆ _value

template<typename S , typename C >
S olb::opti::Optimizer< S, C >::_value
protected

Value of the objective functional evaluated for controlled variables saved in _control.

Definition at line 68 of file optimizer.h.

◆ _vectorBounds

template<typename S , typename C >
bool olb::opti::Optimizer< S, C >::_vectorBounds
protected

Definition at line 86 of file optimizer.h.

◆ _verboseOn

template<typename S , typename C >
bool olb::opti::Optimizer< S, C >::_verboseOn
protected

Verbose.

Definition at line 81 of file optimizer.h.

◆ _withLowerBound

template<typename S , typename C >
bool olb::opti::Optimizer< S, C >::_withLowerBound
protected

Definition at line 85 of file optimizer.h.

◆ _withUpperBound

template<typename S , typename C >
bool olb::opti::Optimizer< S, C >::_withUpperBound
protected

Bounded versions.

Definition at line 84 of file optimizer.h.

◆ gplot

template<typename S , typename C >
Gnuplot<S> olb::opti::Optimizer< S, C >::gplot

Definition at line 103 of file optimizer.h.


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