OpenLB 1.7
Loading...
Searching...
No Matches
optimizer.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2012-2016 Mathias J. Krause, Benjamin Förster
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
29#ifndef OPTIMIZER_H
30#define OPTIMIZER_H
31
32#include "optiCase.h"
34#include "io/gnuplotWriter.h"
35#include "io/xmlReader.h"
36#include "core/singleton.h"
37
38namespace olb {
39
41namespace opti {
42
43
44template<typename, typename> class OptiCase;
45
47
49
55template<typename S, typename C>
56class Optimizer {
57
58private:
59 mutable OstreamManager clout;
60
61protected:
73 int _it;
75 int _maxIt;
82
90
94
98
101
102public:
106 std::vector<OptimizerLogType> _gplotAnalysis;
107
108 Optimizer(int dimCtrl, S eps, int maxIt,
109 bool verboseOn=true, const std::string fname="",
110 const std::string logFileName="",
111 bool withUpperBound=false, S upperBound=S(),
112 bool withLowerBound=false, S lowerBound=S(),
113 bool vectorBounds=false,
114 S controlEps=S(std::numeric_limits<double>::epsilon() ),
115 bool failOnMaxIter = true,
116 std::vector<OptimizerLogType> gplotAnalysis = {});
117
118 virtual ~Optimizer() { };
119
120 virtual void optimizationStep() = 0;
121
122 void maxIterationReached();
123
124 virtual void optimize();
125
126 virtual void optimize(OptiCase<S,C>& optiCase) {
127 _optiCase = &optiCase;
128 optimize();
129 }
130
134
135 void simulate(OptiCase<S,C>& optiCase) {
136 _optiCase = &optiCase;
137 simulate();
138 }
139
140 void evaluateObjective(const C& control, S& result) {
141 result = _optiCase->evaluateObjective(control, _it);
142 }
143
144 void computeDerivatives(const C& control, C& derivatives) {
145
146 _optiCase->computeDerivatives(control, derivatives, _it);
147 }
148
150 void print(int it);
151
153 _control = std::move(control);
154 }
155
156 const C& getControl() const {
157 return _control;
158 }
159
160 const C& getDerivative() const {
161 return _derivative;
162 }
163
164 const S& getObjective() const {
165 return _value;
166 }
167
168 int getIteration() const {
169 return _it;
170 }
171
173 void writeControlToFile(const std::string fname="control.dat");
174
176 void readControlFromFile(const std::string fname="control.dat");
177
178 void setStartValue(S startValue);
179
181
182 void setOptiCase(OptiCase<S,C>* optiCase) { _optiCase = optiCase; }
183
184 void setGnuplotData();
185
187 void setReferenceControl(C result) {
188 _referenceControl = result;
189 }
190};
191
193void getGnuplotTagsFromString(std::string gplotAnalysisString, std::vector<OptimizerLogType>& gplotAnalysis );
194
195
196
197
198
199// Work in progress: Attempt to refactor the optimizer classes in the
200// style of the solver architecture: separate parameter structs and algorithms.
201/*
202template <typename S>
203struct OptimizationParameters
204{
205 private:
206 OstreamManager clout {std::cout, "OptimizationParameters"};
207
208 public:
210 int _maxIt;
212 bool _failOnMaxIter {true};
214 S _eps;
216 bool _verboseOn;
217
219 bool _withUpperBound;
220 bool _withLowerBound;
221 bool _vectorBounds;
222 S* _boundedControl;
223 S* _upperBound;
224 S* _lowerBound;
225
226 S _controlEps;
227
228 explicit OptimizationParameters(XMLreader *xml);
229};
230
231template <typename S>
232OptimizationParameters<S>::OptimizationParameters(XMLreader *xml)
233{
234
235}
236
237
238template<typename T>
239class OptimizerBase
240{
241 private:
242 mutable OstreamManager clout;
243
244 protected:
245 OptimizationParameters<T> _optiParam;
246
247 std::shared_ptr<Controller<T>> _controller;
248
249 std::unique_ptr<OptiCase<T>> _optiCase;
250
253 T objectiveValue;
256 std::vector<T> _derivativeObjective;
257
259 int _it {0};
260
261 bool _controlsConverged {false};
262
263
266 OptiCase<T>* _optiCase;
267
268public:
270 NewOptimizer();
271
273 virtual void optimizationStep() = 0;
274
275 void maxIterationReached();
276
278 virtual void optimize(OptiCase<T>* optiCase);
279
281 void evaluate(OptiCase<T>* optiCase)
282 {
283 optiCase->evaluateObjective(_control);
284 }
285};
286*/
287
288
289} // namespace opti
290
291} // namespace olb
292
293#endif
class for marking output with some text
Abstract base class for optimization tasks.
Definition optiCase.h:40
Interface for the use of various optimization algorithms.
Definition optimizer.h:56
const S & getObjective() const
Definition optimizer.h:164
void setControl(C &control)
Definition optimizer.h:152
void print(int it)
Prints information of the current optimization step it.
Definition optimizer.hh:131
void setReferenceControl(C result)
set the reference value for the control vector (exact solution)
Definition optimizer.h:187
virtual void optimize(OptiCase< S, C > &optiCase)
Definition optimizer.h:126
std::vector< OptimizerLogType > _gplotAnalysis
For defining what kind of gnuplot analysis is wanted, if empty vector - no analysis,...
Definition optimizer.h:106
OptiCase< S, C > * getOptiCase()
Definition optimizer.h:180
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
const C & getDerivative() const
Definition optimizer.h:160
int _it
Current iteration no.
Definition optimizer.h:73
virtual void optimizationStep()=0
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
OptiCase< S, C > * _optiCase
Provides the Optimizer with methods to evaluate the value of an object functional and compute derivat...
Definition optimizer.h:97
bool _verboseOn
Verbose.
Definition optimizer.h:81
void setOptiCase(OptiCase< S, C > *optiCase)
Definition optimizer.h:182
C _control
Vector of controlled variables (size _dimCtrl)
Definition optimizer.h:65
void setStartValue(S startValue)
Definition optimizer.hh:191
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
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 optimizer.hh:41
S _value
Value of the objective functional evaluated for controlled variables saved in _control.
Definition optimizer.h:68
bool _controlsConverged
For setting tolerance of controls.
Definition optimizer.h:92
void computeDerivatives(const C &control, C &derivatives)
Definition optimizer.h:144
int getIteration() const
Definition optimizer.h:168
bool _failOnMaxIter
Fail when max number of iteration reached.
Definition optimizer.h:77
void simulate(OptiCase< S, C > &optiCase)
Definition optimizer.h:135
C _referenceControl
control vector to compare with (for numerical evaluation)
Definition optimizer.h:100
void writeControlToFile(const std::string fname="control.dat")
Writes the current control variables linewise into file fname.
Definition optimizer.hh:141
const C & getControl() const
Definition optimizer.h:156
virtual void optimize()
Definition optimizer.hh:96
void evaluateObjective(const C &control, S &result)
Definition optimizer.h:140
Wrapper functions that simplify the use of MPI.
@ norm_derivative
Definition optimizer.h:46
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...
Definition optimizer.hh:236
Top level namespace for all of OpenLB.
Optimization Code.
Definition of singletons: global, publicly available information.
Input/Output in XML format – header file.