KASKADE 7 development version
homotopy_base.hh
Go to the documentation of this file.
1#ifndef HOMOTOPY_BASE_HH
2#define HOMOTOPY_BASE_HH
3
4#include <vector>
5#include <memory> // unique_ptr
6#include <cmath>
7#include <fstream>
8
9#include "dune/grid/config.h"
10
11#include "abstract_interface.hh"
12#include "algorithm_base.hh"
13#include "newton_damped.hh"
14#include "modelfunction.hh"
15#include <boost/timer/timer.hpp>
16
17namespace Kaskade
18{
19
20class NewtonsMethod;
21
24class InteriorPointSqrtModel;
25template<class IPF, class DomainVector> class InteriorPointTangentialPredictor;
27
28
30{
31public:
32 InteriorPointParameters(double mu_, double desiredAccuracy_, double desiredContraction_, int maxSteps_, double muFinal_, double relDist, double accfinal_,
33 double reductionStart_,double reductionFactorBest_, double redWorst_=1-1e-4)
34 : IterationParameters(desiredAccuracy_, maxSteps_),
35 desiredContraction(desiredContraction_),
36 muFinal(muFinal_),
37 reductionFactorWorst(redWorst_),
39 reductionStart(reductionStart_),
40 relDistanceToPath(relDist),
41 accfinal(accfinal_),
42 reductionFactorBest(reductionFactorBest_),
44 {
45 reset();
46 mu=mu_;
47 }
48
49//Parameters with values that must be supplied by client
52
54
56
57 void reset()
58 {
60 stepSuccessful=true;
62 }
63
65
66protected:
67
69
71
72
74
75 friend class HomotopyBase;
76 friend class InteriorPointSimple;
78 template<class A, class B> friend class InteriorPointTangentialPredictor;
80
81 virtual void doForAll(LQAction::ToDo td)
82 {
84 mu.doAction(td,"mu");
85 muTrial.doAction(td,"muTrial");
86 j.doAction(td,"jp");
87 jp.doAction(td,"j");
88 sigma.doAction(td,"sigma");
89 deltaMu.doAction(td,"deltaMu");
90 }
91
92
93};
94
96class HomotopyBase : public Algorithm
97 {
98 public:
100 corrector(n_), p(p_)
101 {}
103 { }
104
106 protected:
107
109 virtual void initialize() {};
110
112 virtual void finalize() {};
113
114 virtual void logQuantities();
115
117 virtual void computePredictor(int step);
118
120 virtual double muOnSuccess(int step) = 0;
121
123 virtual double muOnFailure() = 0;
124
126 virtual double muFinal() = 0;
127
129 virtual void updateModelOfHomotopy(int step) = 0;
130
132 virtual void initializeCorrector() = 0;
133
135 virtual void finalizeCorrector() = 0;
136
138 virtual void updateIterate() = 0;
139
141 virtual void recoverIterate() = 0;
142
144 virtual double lengthOfPath() = 0;
145
147 virtual int convergenceTest();
148
150 virtual void finalizeHomotopy(){};
151
153 virtual void terminationMessage(int errorFlag);
154
158
159 std::unique_ptr<AbstractFunctionSpaceElement> trialIterate;
160 int step;
161
162 private:
163
164 bool stepSuccessful();
165
166 void computeCorrector();
167 void computeGapParameter();
168 int runAlgorithm();
169 };
170
173{
174public:
176 HomotopyBase(n_, p_) {}
177
178 virtual double muOnSuccess(int step) { return p.mu*p.reductionStart;};
179 virtual double muOnFailure() { return p.mu*p.reductionStart;};
180 virtual double muFinal() { return 1e-7;};
181
182 virtual void updateModelOfHomotopy(int step) {};
183
184 virtual void initializeCorrector() {};
185 virtual void finalizeCorrector() {};
186
187 virtual void updateIterate() {};
188 virtual void recoverIterate() {};
189
190 virtual double lengthOfPath() { return std::sqrt(p.mu); };
191};
192
193
195{
196public:
198 : IterationParameters(0.0,0)
199 {
200 reset();
201 }
203 template<class A, class B> friend class InteriorPointTangentialPredictor;
205
207
208protected:
209
218
219 virtual void doForAll(LQAction::ToDo td)
220 {
221 omega.doAction(td,"omega");
222 eta.doAction(td,"eta");
223 slope.doAction(td,"slope");
224 curvature.doAction(td,"curvature");
225 jpl.doAction(td,"jpl");
226 accuracyCorrector.doAction(td,"accuracyCorrector");
227 newtonSum.doAction(td,"newtonSum");
228 timemeasured.doAction(td,"timemeasured");
229 }
230};
231
232
235{
236public:
239 AbstractNorm const* normPlain_=0) :
240 HomotopyBase(n_, p_),
241 norm(norm_),
242 solver(solver_),
243 normPlain(normPlain_)
244 {
245 if(!normPlain) normPlain = &norm;
246 }
247
248 virtual void initialize() { iterate=trialIterate->clone(); nNewton=0;};
249 virtual double muOnSuccess(int step);
250 virtual double muOnFailure();
251 virtual double muFinal() { return p.muFinal;};
252 virtual void updateModelOfHomotopy(int step);
253 virtual void initializeCorrector();
254 virtual void finalizeCorrector() { nNewton += corrector.stepsPerformed(); pp.newtonSum=nNewton;};
255 virtual void updateIterate();
256 virtual void recoverIterate() { *trialIterate=*iterate; }
257 virtual double lengthOfPath() {if(pp.eta.isValid()) return 2*p.mu*pp.slope; else return 1e300;};
259
260 virtual void logQuantities()
261 {
263 pp.logStep();
265 }
266
267private:
268 std::unique_ptr<AbstractFunctionSpaceElement> iterate;
269 AbstractNorm const& norm;
272 JModel jModel;
273 JModelLin jModelL;
274 AbstractNorm const* normPlain;
275 int nNewton;
276 boost::timer::cpu_timer overalltime;
277};
278
280/**************************************************
281 * Assumptions
282 *
283 * a < b
284 *
285 * Equation f with has double operator(double ) monotonically INCREASING
286 *
287 ************************************************************/
288
289template<class Equation>
290double bisection(double a, double b, Equation const& f, double accuracy, int& iterations)
291{
292 iterations = 0;
293 double u;
294 do {
295 ++iterations;
296 u=(a+b)/2;
297 if(u == a) return b;
298 if(f(u) > 0) b=u; else a=u;
299 }
300 while(b-a >= accuracy && (a+b)/2 != a && (a+b)/2 != b && iterations<50);
301 if(iterations > 48) std::cout << "Warning: bisection algorithm performed > 48 iterations" << std::endl;
302 return u;
303}
304
306{
307
308public:
310 AbstractNewtonDirection& solver_, AbstractNorm const* normPlain_=0,
311 NewtonsMethod* finalsolver_=0) :
312 HomotopyBase(n_, p_),
313 norm(norm_),
314 solver(solver_),
315 stp(0),
316 normPlain(normPlain_),
317 finalsolver(finalsolver_)
318 {
319 if(!normPlain_) normPlain=&norm;
320 }
321
322 virtual void initialize() {
323 iterate=trialIterate->clone();
324 tangent=trialIterate->clone();
325 nNewton=0;
326 };
327 virtual void finalize() {};
328
329 virtual double muOnSuccess(int step);
330
331 virtual double muOnFailure() {
332 p.sigma=0.5+p.sigma/2.0;
333 return p.mu*p.sigma;
334 };
335
336 virtual double muFinal() { return p.muFinal;};
337
338 virtual void updateModelOfHomotopy(int step);
339
340 virtual void computePredictor(int step){}
341
342
343 virtual void initializeCorrector();
344 virtual void finalizeCorrector() { nNewton += corrector.stepsPerformed(); pp.newtonSum=nNewton;};
345
346 virtual void finalizeHomotopy();
347
348 virtual void updateIterate();
349
350 virtual void recoverIterate() { *trialIterate=*iterate; }
351
352
353 virtual double lengthOfPath()
354 {
355 if(pp.eta.isValid())
356 return 2*p.mu*pp.slope;
357 else
358 return 1e300;
359 };
360
361 virtual void logQuantities()
362 {
364 pp.logStep();
366 }
367
369
370private:
371 std::unique_ptr<AbstractFunctionSpaceElement> iterate, tangent;
372
373 AbstractNorm const& norm;
376 JModelLin jModelL;
377 int stp;
378 AbstractNorm const* normPlain;
379 NewtonsMethod* finalsolver;
380 int nNewton;
381 boost::timer::cpu_timer overalltime;
382};
383
384} // namespace Kaskade
385
386#endif
Interfaces for function space oriented algorithms.
Abstract Vector for function space algorithms.
Class that models the functionality of a (possibly inexact) linear solver.
Creates a functional from a homotopy of functionals by inserting a parameter.
Base class for algorithms. Provides a unified interface and some simple wrapper routines,...
Base class for homotopy methods. Here, the main algorithm is programmed.
virtual double lengthOfPath()=0
estimate of length of homopoty path
std::unique_ptr< AbstractFunctionSpaceElement > trialIterate
AbstractParameterFunctional * functional
virtual void finalizeCorrector()=0
what should be done after corrector
InteriorPointParameters & p
virtual int convergenceTest()
convergence test. returns true if method converged and solution is found.
virtual void finalizeHomotopy()
to be performed, if convergence of path has occured
void solve(AbstractParameterFunctional *f, AbstractFunctionSpaceElement &x)
virtual void terminationMessage(int errorFlag)
output of a termination message
virtual void finalize()
Optional overload by derived class.
virtual void updateModelOfHomotopy(int step)=0
update of path parameters, e.g. eta and omega
virtual void recoverIterate()=0
make old iterate to current iterate (on failed corrector)
NewtonsMethod & corrector
virtual double muOnSuccess(int step)=0
New mu if corrector was successful.
virtual void computePredictor(int step)
Default: classical predictor.
HomotopyBase(NewtonsMethod &n_, InteriorPointParameters &p_)
virtual void updateIterate()=0
make trial iterate to current iterate (on successful corrector)
virtual double muOnFailure()=0
New mu if corrector failed.
virtual void logQuantities()
virtual void initialize()
Optional overload by derived class.
virtual double muFinal()=0
mu for successful termination
virtual void initializeCorrector()=0
what should be done before corrector
LoggedQuantity< double > muTrial
LoggedQuantity< double > mu
LoggedQuantity< double > sigma
LoggedQuantity< double > j
LoggedQuantity< double > jp
virtual void doForAll(LQAction::ToDo td)
To be overloaded by derived class.
void reset()
Reset all quantities in this class.
LoggedQuantity< double > deltaMu
InteriorPointParameters(double mu_, double desiredAccuracy_, double desiredContraction_, int maxSteps_, double muFinal_, double relDist, double accfinal_, double reductionStart_, double reductionFactorBest_, double redWorst_=1-1e-4)
LoggedQuantity< double > omega
virtual void doForAll(LQAction::ToDo td)
To be overloaded by derived class.
LoggedQuantity< double > curvature
LoggedQuantity< double > slope
LoggedQuantity< double > accuracyCorrector
LoggedQuantity< double > timemeasured
Very simple implementation of homotopy method: fixed stepsize.
virtual double muFinal()
mu for successful termination
virtual void finalizeCorrector()
what should be done after corrector
virtual void initializeCorrector()
what should be done before corrector
InteriorPointSimple(NewtonsMethod &n_, InteriorPointParameters &p_)
virtual double lengthOfPath()
estimate of length of homopoty path
virtual void updateModelOfHomotopy(int step)
update of path parameters, e.g. eta and omega
virtual double muOnFailure()
New mu if corrector failed.
virtual double muOnSuccess(int step)
New mu if corrector was successful.
virtual void updateIterate()
make trial iterate to current iterate (on successful corrector)
virtual void recoverIterate()
make old iterate to current iterate (on failed corrector)
virtual void updateIterate()
make trial iterate to current iterate (on successful corrector)
virtual void finalize()
Optional overload by derived class.
virtual void updateModelOfHomotopy(int step)
update of path parameters, e.g. eta and omega
virtual void finalizeHomotopy()
to be performed, if convergence of path has occured
virtual void initialize()
Optional overload by derived class.
virtual double muFinal()
mu for successful termination
virtual double muOnSuccess(int step)
New mu if corrector was successful.
virtual void recoverIterate()
make old iterate to current iterate (on failed corrector)
virtual double muOnFailure()
New mu if corrector failed.
InteriorPointSlopeEstimate(NewtonsMethod &n_, AbstractNorm const &norm_, InteriorPointParameters &p_, AbstractNewtonDirection &solver_, AbstractNorm const *normPlain_=0, NewtonsMethod *finalsolver_=0)
virtual void initializeCorrector()
what should be done before corrector
virtual void finalizeCorrector()
what should be done after corrector
virtual double lengthOfPath()
estimate of length of homopoty path
virtual void computePredictor(int step)
Default: classical predictor.
InteriorPointMethod with a sqrt-model of the path: eta ~ mu^{-1/2}, omega ~mu^{-1/2}.
InteriorPointSqrtModel(NewtonsMethod &n_, AbstractNorm const &norm_, InteriorPointParameters &p_, AbstractNewtonDirection &solver_, AbstractNorm const *normPlain_=0)
virtual void recoverIterate()
make old iterate to current iterate (on failed corrector)
virtual void initialize()
Optional overload by derived class.
virtual void initializeCorrector()
what should be done before corrector
virtual double lengthOfPath()
estimate of length of homopoty path
virtual double muFinal()
mu for successful termination
virtual void finalizeCorrector()
what should be done after corrector
virtual void updateIterate()
make trial iterate to current iterate (on successful corrector)
virtual double muOnSuccess(int step)
New mu if corrector was successful.
virtual double muOnFailure()
New mu if corrector failed.
virtual void updateModelOfHomotopy(int step)
update of path parameters, e.g. eta and omega
Base class for algorithmic parameters.
virtual void reset()
Reset all quantities in this class.
virtual void doForAll(LQAction::ToDo td)
To be overloaded by derived class.
void doAction(LQAction::ToDo td, std::string const &name_="noName")
bool isValid()
Returns true, iff there is a valid current value.
Base class for Newton's method. Defines the main algorithm, potentially using damping.
Definition: newton_base.hh:89
Some model functions for various purposes.
double bisection(double a, double b, Equation const &f, double accuracy, int &iterations)
Performs bisection algorithm to find a zero of a.
enum toDo ToDo