24 #ifndef REDUCEDNEWTON__ALGORITHM_H_
25 #define REDUCEDNEWTON__ALGORITHM_H_
49 template <
typename PROBLEM,
typename VECTOR>
145 {
return gradient*gradient_transposed;}
147 unsigned int nonlinear_maxiter_, linear_maxiter_, line_maxiter_;
148 double nonlinear_tol_, nonlinear_global_tol_, linear_tol_, linear_global_tol_, lineasearch_rho_, linesearch_c_;
149 bool compute_functionals_in_every_step_;
150 std::string postindex_;
156 using namespace dealii;
160 template <
typename PROBLEM,
typename VECTOR>
163 param_reader.
SetSubsection(
"reducednewtonalgorithm parameters");
164 param_reader.
declare_entry(
"nonlinear_maxiter",
"10",Patterns::Integer(0));
165 param_reader.
declare_entry(
"nonlinear_tol",
"1.e-7",Patterns::Double(0));
166 param_reader.
declare_entry(
"nonlinear_global_tol",
"1.e-11",Patterns::Double(0));
168 param_reader.
declare_entry(
"linear_maxiter",
"40",Patterns::Integer(0));
169 param_reader.
declare_entry(
"linear_tol",
"1.e-10",Patterns::Double(0));
170 param_reader.
declare_entry(
"linear_global_tol",
"1.e-12",Patterns::Double(0));
172 param_reader.
declare_entry(
"line_maxiter",
"4",Patterns::Integer(0));
173 param_reader.
declare_entry(
"linesearch_rho",
"0.9",Patterns::Double(0));
174 param_reader.
declare_entry(
"linesearch_c",
"0.1",Patterns::Double(0));
176 param_reader.
declare_entry(
"compute_functionals_in_every_step",
"false",Patterns::Bool());
182 template <
typename PROBLEM,
typename VECTOR>
189 :
ReducedAlgorithm<PROBLEM, VECTOR>(OP,S,param_reader,Except,Output,base_priority)
192 param_reader.
SetSubsection(
"reducednewtonalgorithm parameters");
193 nonlinear_maxiter_ = param_reader.
get_integer (
"nonlinear_maxiter");
194 nonlinear_tol_ = param_reader.
get_double (
"nonlinear_tol");
195 nonlinear_global_tol_ = param_reader.
get_double (
"nonlinear_global_tol");
197 linear_maxiter_ = param_reader.
get_integer (
"linear_maxiter");
198 linear_tol_ = param_reader.
get_double (
"linear_tol");
199 linear_global_tol_ = param_reader.
get_double (
"linear_global_tol");
201 line_maxiter_ = param_reader.
get_integer (
"line_maxiter");
202 lineasearch_rho_ = param_reader.
get_double (
"linesearch_rho");
203 linesearch_c_ = param_reader.
get_double (
"linesearch_c");
205 compute_functionals_in_every_step_ = param_reader.
get_bool (
"compute_functionals_in_every_step");
207 postindex_ =
"_"+this->
GetProblem()->GetName();
212 template <
typename PROBLEM,
typename VECTOR>
220 template <
typename PROBLEM,
typename VECTOR>
228 this->GetReducedProblem()->ComputeReducedCostFunctional(q);
232 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::NewtonResidual");
237 this->GetReducedProblem()->ComputeReducedGradient(q,gradient,gradient_transposed);
241 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::NewtonResidual");
244 return sqrt(Residual(gradient,gradient_transposed));
249 template <
typename PROBLEM,
typename VECTOR>
259 std::stringstream out;
260 this->GetOutputHandler()->InitNewtonOut(out);
262 out <<
"**************************************************\n";
263 out <<
"* Starting Reduced Newton Algorithm *\n";
264 out <<
"* Solving : "<<this->GetProblem()->GetName()<<
"\t*\n";
268 this->GetReducedProblem()->StateSizeInfo(out);
269 out <<
"**************************************************";
270 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
272 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewton"+postindex_);
274 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
278 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
282 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::Solve");
285 out<<
"CostFunctional: " << cost;
286 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
288 if (compute_functionals_in_every_step_ ==
true)
292 this->GetReducedProblem()->ComputeReducedFunctionals(q);
296 this->GetExceptionHandler()->HandleCriticalException(e);
302 this->GetReducedProblem()->ComputeReducedGradient(q,gradient,gradient_transposed);
306 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::Solve");
309 double res = Residual(gradient,gradient_transposed);
310 double firstres = res;
314 this->GetOutputHandler()->Write(gradient,
"NewtonResidual"+postindex_,
"control");
315 out<<
"\t Newton step: " <<iter<<
"\t Residual (abs.): "<<sqrt(res)<<
"\n";
316 out<<
"\t Newton step: " <<iter<<
"\t Residual (rel.): "<<std::scientific<<sqrt(res)/sqrt(res)<<
"\n";
317 this->GetOutputHandler()->Write(out,3+this->GetBasePriority());
320 unsigned int miniter = 0;
324 global_tol = std::max(nonlinear_global_tol_,global_tol);
325 while(( (res >= global_tol*global_tol) && (res >= nonlinear_tol_*nonlinear_tol_*firstres) ) || iter < miniter )
328 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewton"+postindex_);
330 if(iter > nonlinear_maxiter_)
338 liniter = SolveReducedLinearSystem(q,gradient,gradient_transposed, dq);
344 this->GetExceptionHandler()->HandleException(e,
"ReducedNewtonAlgorithm::Solve");
349 this->GetExceptionHandler()->HandleException(e,
"ReducedNewtonAlgorithm::Solve");
354 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::Solve");
359 lineiter = ReducedNewtonLineSearch(dq,gradient,cost,q);
365 this->GetExceptionHandler()->HandleException(e,
"ReducedNewtonAlgorithm::Solve");
373 out<<
"CostFunctional: " << cost;
374 this->GetOutputHandler()->Write(out,3+this->GetBasePriority());
376 if (compute_functionals_in_every_step_ ==
true)
380 this->GetReducedProblem()->ComputeReducedFunctionals(q);
384 this->GetExceptionHandler()->HandleCriticalException(e);
392 this->GetReducedProblem()->ComputeReducedGradient(q,gradient,gradient_transposed);
396 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::Solve");
399 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
400 this->GetOutputHandler()->Write(gradient,
"NewtonResidual"+postindex_,
"control");
402 res = Residual(gradient,gradient_transposed);
404 out<<
"\t Newton step: " <<iter<<
"\t Residual (rel.): "<<this->GetOutputHandler()->ZeroTolerance(sqrt(res)/sqrt(firstres),1.0)<<
"\t LinearIters ["<<liniter<<
"]\t LineSearch {"<<lineiter<<
"} ";
405 this->GetOutputHandler()->Write(out,3+this->GetBasePriority());
409 out<<
"CostFunctional: " << cost;
410 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
413 this->GetReducedProblem()->ComputeReducedFunctionals(q);
417 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::Solve");
420 out <<
"**************************************************\n";
421 out <<
"* Stopping Reduced Newton Algorithm *\n";
422 out <<
"* after "<<std::setw(6)<<iter<<
" Iterations *\n";
424 out <<
"* with rel. Residual "<<std::scientific << std::setw(11) << this->GetOutputHandler()->ZeroTolerance(sqrt(res)/sqrt(firstres),1.0)<<
" *\n";
426 out <<
"**************************************************";
427 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
433 template <
typename PROBLEM,
typename VECTOR>
439 std::stringstream out;
444 r_transposed = gradient_transposed;
445 d = gradient_transposed;
447 double res = Residual(r,r_transposed);
448 double firstres = res;
452 out <<
"Starting Reduced Linear Solver with Residual: "<<sqrt(res);
453 this->GetOutputHandler()->Write(out,4+this->GetBasePriority());
455 unsigned int iter = 0;
456 double cgalpha, cgbeta, oldres;
458 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewtonCg"+postindex_);
462 while(res>= std::min(0.25,sqrt(firstres))*firstres && res>=linear_global_tol_*linear_global_tol_)
465 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewtonCg"+postindex_);
466 if(iter > linear_maxiter_)
468 throw DOpEIterationException(
"Iteration count exceeded bounds!",
"ReducedNewtonAlgorithm::SolveReducedLinearSystem");
473 this->GetReducedProblem()->ComputeReducedHessianVector(q,d,Hd,Hd_transposed);
477 this->GetExceptionHandler()->HandleCriticalException(e,
"ReducedNewtonAlgorithm::SolveReducedLinearSystem");
480 cgalpha = res / (Hd*d);
493 r_transposed.add(cgalpha,Hd_transposed);
496 res = Residual(r,r_transposed);
503 this->GetReducedProblem()->ComputeReducedHessianVector(q,dq,Hd,Hd_transposed);
507 this->GetExceptionHandler()->HandleCriticalException(e);
510 r_transposed = gradient_transposed;
512 r_transposed.add(1.,Hd_transposed);
513 res = Residual(r,r_transposed);
515 if(res < 0. && ( res > -1. *linear_global_tol_*linear_global_tol_ || res > -1.*std::min(0.25,sqrt(firstres))*firstres) )
520 out<<
"\t There seem to be cancellation errors accumulating in the Residual,"<<std::endl;
521 out<<
"\t and its norm gets negative. Since it is below the tolerance, we stop the iteration.";
522 this->GetOutputHandler()->Write(out,4+this->GetBasePriority());
525 out<<
"\t Cg step: " <<iter<<
"\t Residual: "<<sqrt(res);
526 this->GetOutputHandler()->Write(out,4+this->GetBasePriority());
528 cgbeta = res / oldres;
530 d.equ(-1,r_transposed);
538 template <
typename PROBLEM,
typename VECTOR>
544 double rho = lineasearch_rho_;
545 double c = linesearch_c_;
548 bool force_linesearch =
false;
553 costnew = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
558 force_linesearch =
true;
559 this->GetOutputHandler()->Write(
"Computing Cost Failed",4+this->GetBasePriority());
563 unsigned int iter =0;
565 double reduction = gradient*dq;
568 this->GetOutputHandler()->WriteError(
"Waring: computed direction doesn't seem to be a descend direction!");
572 if(line_maxiter_ > 0)
574 if(fabs(reduction) < 1.e-10*cost)
576 if(std::isinf(costnew) || std::isnan(costnew) || (costnew >= cost + c*alpha*reduction) || force_linesearch)
578 this->GetOutputHandler()->Write(
"\t linesearch ",4+this->GetBasePriority());
579 while(std::isinf(costnew) || std::isnan(costnew) || (costnew >= cost + c*alpha*reduction) || force_linesearch)
582 if(iter > line_maxiter_)
586 throw DOpEException(
"Iteration count exceeded bounds while unable to compute the CostFunctional!",
"ReducedNewtonAlgorithm::ReducedNewtonLineSearch");
591 throw DOpEIterationException(
"Iteration count exceeded bounds!",
"ReducedNewtonAlgorithm::ReducedNewtonLineSearch");
594 force_linesearch =
false;
595 q.
add(alpha*(rho-1.),dq);
600 costnew = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
605 force_linesearch =
true;
606 this->GetOutputHandler()->Write(
"Computing Cost Failed",4+this->GetBasePriority());
Definition: dopeexception.h:76
void PrintInfos(std::stringstream &out)
Definition: controlvector.cc:896
ReducedNewtonAlgorithm(PROBLEM *OP, ReducedProblemInterface< PROBLEM, VECTOR > *S, ParameterReader ¶m_reader, DOpEExceptionHandler< VECTOR > *Except=NULL, DOpEOutputHandler< VECTOR > *Output=NULL, int base_priority=0)
Definition: reducednewtonalgorithm.h:183
double get_double(const std::string &entry_name)
Definition: parameterreader.h:115
Definition: dopeexception.h:90
bool get_bool(const std::string &entry_name)
Definition: parameterreader.h:148
Definition: parameterreader.h:36
Definition: optproblemcontainer.h:70
virtual ~ReducedNewtonAlgorithm()
Definition: reducednewtonalgorithm.h:213
virtual double Residual(const ControlVector< VECTOR > &gradient, const ControlVector< VECTOR > &gradient_transposed)
Definition: reducednewtonalgorithm.h:143
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Definition: parameterreader.h:98
Definition: controlvector.h:49
Definition: reducednewtonalgorithm.h:50
PROBLEM * GetProblem()
Definition: reducedalgorithm.h:187
virtual int Solve(ControlVector< VECTOR > &q, double global_tol=-1.)
Definition: reducednewtonalgorithm.h:250
virtual int SolveReducedLinearSystem(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &gradient, const ControlVector< VECTOR > &gradient_transposed, ControlVector< VECTOR > &dq)
Definition: reducednewtonalgorithm.h:434
Definition: reducedalgorithm.h:54
void add(double s, const ControlVector &dq)
Definition: controlvector.cc:629
void ReInit()
Definition: controlvector.cc:96
static void declare_params(ParameterReader ¶m_reader)
Definition: reducedalgorithm.h:241
virtual int ReducedNewtonLineSearch(const ControlVector< VECTOR > &dq, const ControlVector< VECTOR > &gradient, double &cost, ControlVector< VECTOR > &q)
Definition: reducednewtonalgorithm.h:539
int get_integer(const std::string &entry_name)
Definition: parameterreader.h:126
static void declare_params(ParameterReader ¶m_reader)
Definition: reducednewtonalgorithm.h:161
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93
Definition: reducedprobleminterface.h:335
Definition: dopeexception.h:35
double NewtonResidual(const ControlVector< VECTOR > &q)
Definition: reducednewtonalgorithm.h:221
Definition: optproblemcontainer.h:72