37template<
typename,
typename>
class OptiCase;
40template<
typename S,
typename C>
42 bool verboseOn,
const std::string fname,
43 const std::string logFileName,
44 bool withUpperBound, S upperBound,
45 bool withLowerBound, S lowerBound,
49 std::vector<OptimizerLogType> gplotAnalysis)
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),
56 _gplotAnalysis(gplotAnalysis)
82template<
typename S,
typename C>
86 clout <<
"Warning: Optimization problem failed to converge within specified iteration limit of "
87 << _maxIt <<
" iterations with tolerance of " << _eps <<
".\nProgram terminated" << std::endl;
91 clout <<
"Maximum iteration reached." << std::endl;
95template<
typename S,
typename C>
99 evaluateObjective(_control, _value);
100 _optiCase->postEvaluation();
105 maxIterationReached();
113 _optiCase->postEvaluation();
117 if (_gplotAnalysis.size() > 0) {
123 while (
util::euklidN2(_derivative) > (_eps*_eps) && !_controlsConverged);
125 if(_gplotAnalysis.size() > 0){
130template<
typename S,
typename C>
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;
140template<
typename S,
typename C>
144 std::stringstream stream;
145 stream << _dimCtrl << std::endl;
146 std::for_each(_control.begin(), _control.end(), [&stream](
auto c){ stream << c <<
'\n'; });
149 file << stream.str();
154template<
typename S,
typename C>
157 std::ifstream file(fname.c_str());
158 if ( file.is_open() ) {
161 if (dimCtrl!=_dimCtrl) {
162 clout <<
"Error: dimensions do not match! dim_controller=" << _dimCtrl <<
"; dim_file=" << dimCtrl << std::endl;
166 for (
int i=0; i<_dimCtrl; i++) {
169 _control[i] = tmpVal;
171 if (_withLowerBound && _vectorBounds) {
172 for (
int i=0; i<_dimCtrl; i++) {
175 _lowerBound[i] = tmpVal;
178 if (_withUpperBound && _vectorBounds) {
179 for (
int i=0; i<_dimCtrl; i++) {
182 _upperBound[i] = tmpVal;
190template<
typename S,
typename C>
194 std::fill(_control.begin(), _control.end(), startValue);
197template<
typename S,
typename C>
200 std::vector<S> yValues;
201 std::vector<std::string> labels;
202 std::vector<char> plotTypes;
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){
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');
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');
223 }
else if(_gplotAnalysis[i] ==
error){
225 labels.push_back(
"control_error");
226 plotTypes.push_back(
'p');
229 labels.push_back(
"derivative_norm");
230 plotTypes.push_back(
'p');
233 gplot.setData(S(_it), yValues, labels,
"top right", plotTypes);
237 std::vector<OptimizerLogType>& gplotAnalysis ){
239 std::vector<std::string> gplotAnalysisStringVector;
240 std::istringstream iss(gplotAnalysisString);
241 std::copy(std::istream_iterator<std::string>(iss),
242 std::istream_iterator<std::string>(),
243 std::back_inserter(gplotAnalysisStringVector));
245 for(
unsigned i = 0; i < gplotAnalysisStringVector.size(); i++){
246 if(gplotAnalysisStringVector[i] ==
"VALUE"){
248 }
else if(gplotAnalysisStringVector[i] ==
"CONTROL"){
250 }
else if(gplotAnalysisStringVector[i] ==
"DERIVATIVE"){
252 }
else if(gplotAnalysisStringVector[i] ==
"ERROR"){
254 }
else if(gplotAnalysisStringVector[i] ==
"NORM_DERIVATIVE"){
void print(int it)
Prints information of the current optimization step it.
bool _withUpperBound
Bounded versions.
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 maxIterationReached()
void setStartValue(S startValue)
void readControlFromFile(const std::string fname="control.dat")
Reads the latest control variables from file fname.
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={})
bool _controlsConverged
For setting tolerance of controls.
void writeControlToFile(const std::string fname="control.dat")
Writes the current control variables linewise into file fname.
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...
Directories & directories()
T euklidN(const T x[], int dim)
Euclidean norm of an array.
T euklidDistance(const T x[], const T y[], int dim)
Euclidean distance between two arrays.
T euklidN2(const T x[], int dim)
Squared Euclidean norm of an array.
Top level namespace for all of OpenLB.
Implements Euclidean norm for arrays.
The description of optimization algorithms – header file.
Creates a container of type C.