24 #ifndef REDUCEDTRUSTREGION_NEWTON__ALGORITHM_H_
25 #define REDUCEDTRUSTREGION_NEWTON__ALGORITHM_H_
49 template <
typename PROBLEM,
typename VECTOR>
167 {
return gradient*gradient_transposed;}
169 unsigned int nonlinear_maxiter_;
170 double nonlinear_tol_, nonlinear_global_tol_, tr_delta_max_, tr_delta_null_, tr_delta_eta_;
171 unsigned int linear_max_iter_;
172 double linear_tol_, linear_global_tol_;
173 std::string postindex_;
174 std::string tr_method_;
180 using namespace dealii;
184 template <
typename PROBLEM,
typename VECTOR>
187 param_reader.
SetSubsection(
"reducedtrustregionnewtonalgorithm parameters");
188 param_reader.
declare_entry(
"nonlinear_maxiter",
"10",Patterns::Integer(0));
189 param_reader.
declare_entry(
"nonlinear_tol",
"1.e-7",Patterns::Double(0));
190 param_reader.
declare_entry(
"nonlinear_global_tol",
"1.e-11",Patterns::Double(0));
191 param_reader.
declare_entry(
"tr_delta_max",
"1.",Patterns::Double(0));
192 param_reader.
declare_entry(
"tr_delta_null",
"0.25",Patterns::Double(0));
193 param_reader.
declare_entry(
"tr_delta_eta",
"0.01",Patterns::Double(0,0.25));
195 param_reader.
declare_entry(
"linear_maxiter",
"40",Patterns::Integer(0));
196 param_reader.
declare_entry(
"linear_tol",
"1.e-10",Patterns::Double(0));
197 param_reader.
declare_entry(
"linear_global_tol",
"1.e-12",Patterns::Double(0));
199 param_reader.
declare_entry(
"tr_method",
"dogleg",Patterns::Selection(
"dogleg|exact|steinhaug"));
205 template <
typename PROBLEM,
typename VECTOR>
213 :
ReducedAlgorithm<PROBLEM,VECTOR>(OP,S,param_reader,Except,Output,base_priority)
215 param_reader.
SetSubsection(
"reducedtrustregionnewtonalgorithm parameters");
216 nonlinear_maxiter_ = param_reader.
get_integer (
"nonlinear_maxiter");
217 nonlinear_tol_ = param_reader.
get_double (
"nonlinear_tol");
218 nonlinear_global_tol_ = param_reader.
get_double (
"nonlinear_global_tol");
219 tr_delta_max_ = param_reader.
get_double(
"tr_delta_max");
220 tr_delta_null_ = param_reader.
get_double(
"tr_delta_null");
221 tr_delta_eta_ = param_reader.
get_double(
"tr_delta_eta");
223 assert(tr_delta_eta_ < 0.25);
224 assert(tr_delta_null_<tr_delta_max_);
226 linear_max_iter_ = param_reader.
get_integer (
"linear_maxiter");
227 linear_tol_ = param_reader.
get_double (
"linear_tol");
228 linear_global_tol_ = param_reader.
get_double (
"linear_global_tol");
230 tr_method_ = param_reader.
get_string(
"tr_method");
232 postindex_ =
"_"+this->GetProblem()->GetName();
237 template <
typename PROBLEM,
typename VECTOR>
244 template <
typename PROBLEM,
typename VECTOR>
252 this->GetReducedProblem()->ComputeReducedCostFunctional(q);
256 this->GetExceptionHandler()->HandleCriticalException(e);
261 this->GetReducedProblem()->ComputeReducedGradient(q,gradient,gradient_transposed);
265 this->GetExceptionHandler()->HandleCriticalException(e);
268 return sqrt(Residual(gradient,gradient_transposed));
274 template <
typename PROBLEM,
typename VECTOR>
280 ControlVector<VECTOR> p_u(q), p_b(q), gradient(q), gradient_transposed(q), hessian(q), hessian_transposed(q),dq(q);
284 std::stringstream out;
286 unsigned int n_good =0;
287 unsigned int n_bad =0;
288 this->GetOutputHandler()->InitNewtonOut(out);
290 out <<
"**************************************************************\n";
291 out <<
"* Starting Reduced Trustregion_Newton Algorithm *\n";
292 out <<
"* Solving : "<<this->GetProblem()->GetName()<<
"\t\t\t*\n";
296 this->GetReducedProblem()->StateSizeInfo(out);
297 out <<
"**************************************************";
298 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
300 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewton"+postindex_);
302 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
306 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
310 this->GetExceptionHandler()->HandleCriticalException(e);
313 this->GetOutputHandler()->InitOut(out);
314 out<<
"CostFunctional: " << cost;
315 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
316 this->GetOutputHandler()->InitNewtonOut(out);
329 this->GetReducedProblem()->ComputeReducedGradient(q,gradient,gradient_transposed);
333 this->GetExceptionHandler()->HandleCriticalException(e);
336 double res = Residual(gradient,gradient_transposed);
337 double firstres = res;
339 double tr_delta_max = tr_delta_max_;
340 double tr_delta = tr_delta_null_;
341 double tr_eta = 0.01;
343 double tr_model = 0.;
344 double point_norm = sqrt(q*q);
348 this->GetOutputHandler()->Write(gradient,
"NewtonResidual"+postindex_,
"control");
349 out<<
"\t Newton step: " <<iter<<
"\t Residual (abs.): "<<sqrt(res)<<
"\n";
350 out<<
"\t Newton step: " <<iter<<
"\t Residual (rel.): "<<std::scientific<<sqrt(res)/sqrt(res)<<
"\n";
351 this->GetOutputHandler()->Write(out,3+this->GetBasePriority());
353 global_tol = std::max(nonlinear_global_tol_,global_tol);
355 while( iter==0 || iter ==1 || ((res >= global_tol*global_tol) && (res >= nonlinear_tol_*nonlinear_tol_*firstres) ))
357 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewton"+postindex_);
359 if(iter > nonlinear_maxiter_)
361 out <<
"**************************************************\n";
362 out <<
"* Aborting Reduced Trustregion_Newton Algorithm *\n";
363 out <<
"* after "<<std::setw(6)<<iter<<
" Iterations *\n";
365 out <<
"* with Residual "<<std::scientific << std::setw(11) << sqrt(res)<<
" *\n";
366 out <<
"* with Cost "<<std::scientific << std::setw(11) << cost<<
" *\n";
367 out <<
"* n_good: "<<n_good<<
" n_bad: "<<n_bad<<
" *\n";
369 out <<
"**************************************************";
370 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
371 throw DOpEIterationException(
"Iteration count exceeded bounds!",
"ReducedTrustregion_NewtonAlgorithm::Solve");
373 if(tr_delta <= 1.e-8*point_norm)
375 out <<
"**************************************************\n";
376 out <<
"* Aborting Reduced Trustregion_Newton Algorithm *\n";
377 out <<
"* after "<<std::setw(6)<<iter<<
" Iterations *\n";
379 out <<
"* with Residual "<<std::scientific << std::setw(11) << sqrt(res)<<
" *\n";
380 out <<
"* with Cost "<<std::scientific << std::setw(11) << cost<<
" *\n";
381 out <<
"* n_good: "<<n_good<<
" n_bad: "<<n_bad<<
" *\n";
383 out <<
"**************************************************";
384 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
385 throw DOpEIterationException(
"Iteration aborted due to too small Trustregion radius!",
"ReducedTrustregion_NewtonAlgorithm::Solve");
391 double last_cost = cost;
393 bool good = ComputeTRModelMinimizer(q,gradient,gradient_transposed,hessian,hessian_transposed,p_u,p_b,dq,tr_delta,cost,tr_model,expand,liniter);
397 double loc_delta = 1.e-8*std::max(1.,fabs(last_cost));
398 if(std::max(fabs(last_cost-cost),fabs(tr_model)) < loc_delta)
403 tr_rho = (last_cost-cost-loc_delta)/(0.-tr_model-loc_delta);
408 double norm = sqrt(dq*dq);
409 if(norm <= 1.e-8*point_norm && good)
411 out <<
"**************************************************\n";
412 out <<
"* Aborting Reduced Trustregion_Newton Algorithm *\n";
413 out <<
"* after "<<std::setw(6)<<iter<<
" Iterations *\n";
415 out <<
"* with Residual "<<std::scientific << std::setw(11) << sqrt(res)<<
" *\n";
416 out <<
"* with Cost "<<std::scientific << std::setw(11) << cost<<
" *\n";
417 out <<
"* n_good: "<<n_good<<
" n_bad: "<<n_bad<<
" *\n";
419 out <<
"**************************************************";
420 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
421 throw DOpEIterationException(
"Iteration aborted due to too small update!",
"ReducedTrustregion_NewtonAlgorithm::Solve");
424 out<<
"TR-Newton Predicted Reduction: "<<-tr_model<<
" Actual Reduction: "<<last_cost-cost<<
" rho: "<<tr_rho<<
" where TR-Minimizer is "<<good<<
" with lenght: "<<norm;
425 this->GetOutputHandler()->Write(out,4+this->GetBasePriority());
426 out<<
"\t TR-Newton step: " <<iter<<
"\t";
427 out<<
"delta: "<<tr_delta<<
"->";
428 if((tr_rho < 0.01) || !good)
430 tr_delta=std::max(0.5*norm,0.125*tr_delta);
436 tr_delta = std::min(expand*tr_delta,tr_delta_max);
445 out<<
" accepting step!";
447 point_norm = sqrt(q*q);
452 out<<
" rejecting step!";
458 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
462 this->GetExceptionHandler()->HandleCriticalException(e);
466 this->GetReducedProblem()->ComputeReducedGradient(q,gradient,gradient_transposed);
470 this->GetExceptionHandler()->HandleCriticalException(e);
473 res = Residual(gradient,gradient_transposed);
475 out<<
"\t Residual: (rel.):"<<this->GetOutputHandler()->ZeroTolerance(sqrt(res)/sqrt(firstres),1.0)<<
"\t LinearIters ["<<liniter<<
"]";
476 this->GetOutputHandler()->Write(out,3+this->GetBasePriority());
478 out<<
"CostFunctional: " << cost;
479 this->GetOutputHandler()->Write(out,3+this->GetBasePriority());
481 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
482 this->GetOutputHandler()->Write(gradient,
"NewtonResidual"+postindex_,
"control");
486 out<<
"CostFunctional: " << cost;
487 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
490 this->GetReducedProblem()->ComputeReducedFunctionals(q);
494 this->GetExceptionHandler()->HandleCriticalException(e);
497 out <<
"**************************************************\n";
498 out <<
"* Stopping Reduced Trustregion_Newton Algorithm *\n";
499 out <<
"* after "<<std::setw(6)<<iter<<
" Iterations *\n";
501 out <<
"* with rel. Residual "<<std::scientific << std::setw(11) << this->GetOutputHandler()->ZeroTolerance(sqrt(res)/sqrt(firstres),1.0)<<
" *\n";
502 out <<
"* with Cost "<<std::scientific << std::setw(11) << cost<<
" *\n";
503 out <<
"* n_good: "<<n_good<<
" n_bad: "<<n_bad<<
" *\n";
505 out <<
"**************************************************";
506 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
512 template <
typename PROBLEM,
typename VECTOR>
530 if(
"dogleg" == tr_method_)
535 liniter = SolveReducedLinearSystem(q,gradient,gradient_transposed,p_b);
541 this->GetExceptionHandler()->HandleException(e);
546 this->GetExceptionHandler()->HandleCriticalException(e);
550 this->GetReducedProblem()->ComputeReducedHessianVector(q,gradient,hessian,hessian_transposed);
554 this->GetExceptionHandler()->HandleCriticalException(e);
557 double scale = (gradient*gradient_transposed)/(gradient*hessian_transposed);
558 p_u.
equ(-1.*scale,gradient);
564 double n_b = sqrt(p_b*p_b);
575 expand = std::max(n_b/tr_delta,2.);
577 expand = std::min(expand, 10.);
580 double n_u = sqrt(p_u*p_u);
590 double d = (c-a)/(a+b-2.*c);
591 double e = (tr_delta*tr_delta-a)/(a+b-2.*c);
593 double l = sqrt(e+d*d) - d;
602 min.
equ(1./n_u*tr_delta,p_u);
611 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(tmp1);
620 this->GetReducedProblem()->ComputeReducedCostFunctional(q);
624 model = gradient*min;
627 this->GetReducedProblem()->ComputeReducedHessianVector(q,min,tmp1,tmp2);
631 this->GetExceptionHandler()->HandleCriticalException(e);
633 model += 0.5*(tmp1*min);
638 else if(
"exact" == tr_method_)
640 throw DOpEException(
"Method not yet implemented: "+tr_method_,
"ReducedTrustregion_NewtonAlgorithm::ComputeTRModelMinimizer");
642 else if(
"steinhaug" == tr_method_)
644 throw DOpEException(
"Method not yet implemented: "+tr_method_,
"ReducedTrustregion_NewtonAlgorithm::ComputeTRModelMinimizer");
648 throw DOpEException(
"Unknown Method: "+tr_method_,
"ReducedTrustregion_NewtonAlgorithm::ComputeTRModelMinimizer");
656 template <
typename PROBLEM,
typename VECTOR>
663 std::stringstream out;
668 r_transposed = gradient_transposed;
669 d = gradient_transposed;
671 double res = Residual(r,r_transposed);
672 double firstres = res;
676 out <<
"Starting Reduced Linear Solver with Residual: "<<sqrt(res);
677 this->GetOutputHandler()->Write(out,5+this->GetBasePriority());
679 unsigned int iter = 0;
680 double cgalpha, cgbeta, oldres;
682 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewtonCg"+postindex_);
686 while(res>= std::min(0.25,sqrt(firstres))*firstres && res>=linear_global_tol_*linear_global_tol_)
689 this->GetOutputHandler()->SetIterationNumber(iter,
"OptNewtonCg"+postindex_);
690 if(iter > linear_max_iter_)
692 throw DOpEIterationException(
"Iteration count exceeded bounds!",
"ReducedNewtonAlgorithm::SolveReducedLinearSystem");
697 this->GetReducedProblem()->ComputeReducedHessianVector(q,d,Hd,Hd_transposed);
701 this->GetExceptionHandler()->HandleCriticalException(e);
704 cgalpha = res / (Hd*d);
717 r_transposed.add(cgalpha,Hd_transposed);
720 res = Residual(r,r_transposed);
727 this->GetReducedProblem()->ComputeReducedHessianVector(q,dq,Hd,Hd_transposed);
731 this->GetExceptionHandler()->HandleCriticalException(e);
734 r_transposed = gradient_transposed;
736 r_transposed.add(1.,Hd_transposed);
737 res = Residual(r,r_transposed);
740 out<<
"\t Cg step: " <<iter<<
"\t Residual: "<<sqrt(res);
741 this->GetOutputHandler()->Write(out,5+this->GetBasePriority());
743 cgbeta = res / oldres;
745 d.equ(-1,r_transposed);
Definition: dopeexception.h:76
ReducedTrustregion_NewtonAlgorithm(PROBLEM *OP, ReducedProblemInterface< PROBLEM, VECTOR > *S, ParameterReader ¶m_reader, DOpEExceptionHandler< VECTOR > *Except=NULL, DOpEOutputHandler< VECTOR > *Output=NULL, int base_priority=0)
Definition: reducedtrustregionnewton.h:207
void PrintInfos(std::stringstream &out)
Definition: controlvector.cc:896
double get_double(const std::string &entry_name)
Definition: parameterreader.h:115
Definition: dopeexception.h:90
Definition: parameterreader.h:36
virtual double Residual(const ControlVector< VECTOR > &gradient, const ControlVector< VECTOR > &gradient_transposed)
Definition: reducedtrustregionnewton.h:165
Definition: optproblemcontainer.h:70
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: reducedtrustregionnewton.h:50
Definition: reducedalgorithm.h:54
void add(double s, const ControlVector &dq)
Definition: controlvector.cc:629
void ReInit()
Definition: controlvector.cc:96
virtual int Solve(ControlVector< VECTOR > &q, double global_tol=-1.)
Definition: reducedtrustregionnewton.h:275
std::string get_string(const std::string &entry_name)
Definition: parameterreader.h:137
bool ComputeTRModelMinimizer(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &gradient, const ControlVector< VECTOR > &gradient_transposed, ControlVector< VECTOR > &hessian, ControlVector< VECTOR > &hessian_transposed, ControlVector< VECTOR > &p_u, ControlVector< VECTOR > &p_b, ControlVector< VECTOR > &min, double tr_delta, double &cost, double &model, double &expand, int &liniter)
Definition: reducedtrustregionnewton.h:514
static void declare_params(ParameterReader ¶m_reader)
Definition: reducedalgorithm.h:241
int get_integer(const std::string &entry_name)
Definition: parameterreader.h:126
virtual int SolveReducedLinearSystem(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &gradient, const ControlVector< VECTOR > &gradient_transposed, ControlVector< VECTOR > &dq)
Definition: reducedtrustregionnewton.h:658
~ReducedTrustregion_NewtonAlgorithm()
Definition: reducedtrustregionnewton.h:238
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93
Definition: reducedprobleminterface.h:335
void equ(double s, const ControlVector &dq)
Definition: controlvector.cc:665
static void declare_params(ParameterReader ¶m_reader)
Definition: reducedtrustregionnewton.h:185
Definition: dopeexception.h:35
double NewtonResidual(const ControlVector< VECTOR > &q)
Definition: reducedtrustregionnewton.h:245
Definition: optproblemcontainer.h:72