8#include <kernel/solver/base.hpp>
9#include <kernel/solver/iterative.hpp>
10#include <kernel/solver/linesearch.hpp>
11#include <kernel/solver/nloptls.hpp>
12#include <kernel/solver/nlopt_precond.hpp>
41 case NLCGDirectionUpdate::undefined:
42 return os <<
"undefined";
43 case NLCGDirectionUpdate::DaiYuan:
44 return os <<
"DaiYuan";
45 case NLCGDirectionUpdate::DYHSHybrid:
46 return os <<
"DYHSHybrid";
47 case NLCGDirectionUpdate::FletcherReeves:
48 return os <<
"FletcherReeves";
49 case NLCGDirectionUpdate::HagerZhang:
50 return os <<
"HagerZhang";
51 case NLCGDirectionUpdate::HestenesStiefel:
52 return os <<
"HestenesStiefel";
53 case NLCGDirectionUpdate::PolakRibiere:
54 return os <<
"PolakRibiere";
56 return os <<
"-unknown-";
62 if(update_name ==
"undefined")
63 update = NLCGDirectionUpdate::undefined;
64 else if(update_name ==
"DaiYuan")
65 update = NLCGDirectionUpdate::DaiYuan;
66 else if(update_name ==
"DYHSHybrid")
67 update = NLCGDirectionUpdate::DYHSHybrid;
68 else if(update_name ==
"FletcherReeves")
69 update = NLCGDirectionUpdate::FletcherReeves;
70 else if(update_name ==
"HagerZhang")
71 update = NLCGDirectionUpdate::HagerZhang;
72 else if(update_name ==
"HestenesStiefel")
73 update = NLCGDirectionUpdate::HestenesStiefel;
74 else if(update_name ==
"PolakRibiere")
75 update = NLCGDirectionUpdate::PolakRibiere;
77 XABORTM(
"Unknown NLCGDirectionUpdate identifier string " +update_name);
96 template<
typename Functional_,
typename Filter_>
174 explicit NLCG(Functional_& functional, Filter_& filter, std::shared_ptr<LinesearchType> linesearch,
176 bool keep_iterates =
false, std::shared_ptr<PrecondType> precond =
nullptr) :
177 BaseClass(
"NLCG", functional, filter, precond),
195 iterates =
new std::deque<VectorType>;
222 std::shared_ptr<LinesearchType> linesearch, std::shared_ptr<PrecondType> precond =
nullptr) :
223 BaseClass(
"NLCG", section_name, section, functional, filter, precond),
240 auto direction_update_p = section->
query(
"direction_update");
241 if(direction_update_p.second)
244 my_update << direction_update_p.first;
249 auto keep_iterates_p = section->
query(
"keep_iterates");
250 if(keep_iterates_p.second && std::stoul(keep_iterates_p.first) == 1)
252 iterates =
new std::deque<VectorType>;
255 auto max_num_restarts_p = section->
query(
"max_num_restarts");
256 if(max_num_restarts_p.second)
292 this->_vec_p.clear();
293 this->_vec_r.clear();
294 this->_vec_y.clear();
295 this->_vec_z.clear();
318 this->_vec_r.copy(vec_def);
322 if(this->_precond !=
nullptr)
324 this->_precond->prepare(vec_cor, this->
_filter);
342 this->_vec_r.scale(this->_vec_r,
DataType(-1));
343 this->
_filter.filter_def(this->_vec_r);
346 if(this->_precond !=
nullptr)
348 this->_precond->prepare(vec_sol, this->
_filter);
369 iterates =
new std::deque<VectorType>;
424 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->
name()));
435 iterates->push_back(std::move(vec_sol.clone()));
442 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->
name(), status, this->
get_num_iter()));
455 this->_vec_p.copy(this->_vec_z);
466 eta = this->_vec_p.dot(this->_vec_r);
471 this->_vec_p.copy(this->_vec_r);
475 DataType gamma(this->_vec_z.dot(this->_vec_r));
478 if(this->_def_init <= this->
_tol_rel)
484 Index its_since_restart(0);
502 this->_vec_y.copy(this->_vec_r);
506 _linesearch->set_grad_from_defect(this->_vec_r);
508 if(this->_precond !=
nullptr)
517 this->
_fval = _linesearch->get_final_fval();
518 this->
_ls_its = _linesearch->get_num_iter();
521 this->_vec_y.scale(this->_vec_y, -
DataType(1));
522 this->_vec_y.axpy(this->_vec_r);
527 iterates->push_back(vec_sol.clone());
536 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->
name(), status, this->
get_num_iter()));
541 if(this->_precond !=
nullptr)
543 this->_precond->prepare(vec_sol, this->
_filter);
556 gamma = this->_vec_r.dot(this->_vec_z);
577 its_since_restart =
Index(0);
591 this->_vec_p.copy(this->_vec_z);
595 this->_vec_p.scale(this->_vec_p, beta);
596 this->_vec_p.axpy(this->_vec_z);
606 eta = this->_vec_p.dot(this->_vec_r);
612 this->_vec_p.copy(this->_vec_r);
640 case NLCGDirectionUpdate::DaiYuan:
642 case NLCGDirectionUpdate::DYHSHybrid:
644 case NLCGDirectionUpdate::FletcherReeves:
646 case NLCGDirectionUpdate::HagerZhang:
649 case NLCGDirectionUpdate::HestenesStiefel:
651 case NLCGDirectionUpdate::PolakRibiere:
716 DataType eta_dif = this->_vec_p.dot(this->_vec_y);
718 return gamma/(-eta_dif);
746 DataType gamma_dif = this->_vec_z.dot(this->_vec_y);
748 DataType eta_dif = this->_vec_p.dot(this->_vec_y);
779 return gamma/gamma_prev;
818 DataType eta_dif = this->_vec_p.dot(this->_vec_y);
820 DataType gamma_dif = this->_vec_y.dot(this->_vec_z);
822 DataType omega_mid = this->_vec_p.dot(this->_vec_z);
824 DataType norm_y = this->_vec_y.norm2();
854 DataType eta_dif = this->_vec_p.dot(this->_vec_y);
856 DataType gamma_dif = this->_vec_z.dot(this->_vec_y);
858 return gamma_dif/( -eta_dif);
879 DataType gamma_dif = this->_vec_z.dot(this->_vec_y);
910 template<
typename Functional_,
typename Filter_,
typename Linesearch_>
911 inline std::shared_ptr<NLCG<Functional_, Filter_>>
new_nlcg(
912 Functional_& functional, Filter_& filter, Linesearch_& linesearch,
914 bool keep_iterates =
false,
917 return std::make_shared<NLCG<Functional_, Filter_>>(functional, filter, linesearch, direction_update,
918 keep_iterates, precond);
945 template<
typename Functional_,
typename Filter_,
typename Linesearch_>
946 inline std::shared_ptr<NLCG<Functional_, Filter_>>
new_nlcg(
948 Functional_& functional, Filter_& filter, Linesearch_& linesearch,
951 return std::make_shared<NLCG<Functional_, Filter_>>(section_name, section, functional, filter, linesearch,
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
A class organizing a tree of key-value pairs.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
Helper class for iteration statistics collection.
void destroy()
destroy the objects contents (and generate Statistics::expression) before the actual destructor call
Index get_num_iter() const
Returns number of performed iterations.
Status _status
current status of the solver
Index _num_iter
number of performed iterations
DataType _tol_rel
relative tolerance parameter
virtual void plot_summary() const
Plot a summary of the last solver run.
Index _min_stag_iter
minimum number of stagnation iterations
Nonlinear Conjugate Gradient method for finding a minimum of an functional's gradient.
virtual void init_symbolic() override
Symbolic initialization method.
DataType fletcher_reeves(const DataType gamma, const DataType gamma_prev) const
Fletcher-Reeves update.
DataType dy_hs_hybrid(const DataType gamma) const
Dai-Yuan-Hestenes-Stiefel update.
Functional_ FunctionalType
The nonlinear functional type.
Solver::Linesearch< Functional_, Filter_ > LinesearchType
Our type of linesearch.
NLCG(Functional_ &functional, Filter_ &filter, std::shared_ptr< LinesearchType > linesearch, const NLCGDirectionUpdate du=direction_update_default, bool keep_iterates=false, std::shared_ptr< PrecondType > precond=nullptr)
Standard constructor.
DataType polak_ribiere(const DataType gamma_prev) const
Modified Polak-Ribiere-Polyak.
NLOptPrecond< typename Functional_::VectorTypeL, Filter_ > PrecondType
Generic preconditioner.
Filter_ FilterType
The filter type.
virtual Status apply(VectorType &vec_cor, const VectorType &vec_def) override
Solver application method.
void set_restart_freq(Index restart_freq)
Sets the restart frquency.
DataType hager_zhang(const DataType gamma) const
Hager-Zhang update.
void set_keep_iterates(bool keep_iterates)
Sets the iterates deque according to a bool.
std::deque< VectorType > * iterates
For debugging purposes, all iterates can be logged to here.
virtual Status _apply_intern(VectorType &vec_sol)
Internal function, applies the solver.
virtual Status correct(VectorType &vec_sol, const VectorType &vec_rhs) override
Solver correction method.
Index _num_restarts
Number of these restarts performed consecutively.
virtual Status _set_new_defect(const VectorType &vec_r, const VectorType &vec_sol) override
Internal function: sets the new defect norm.
VectorType _vec_r
defect vector
virtual String name() const override
Returns a descriptive string.
VectorType _vec_y
temporary vector: y[k+1] = r[k+1] - r[k]
VectorType _vec_p
descend direction vector
VectorType _vec_z
temporary vector: Preconditioned defect
DataType dai_yuan(const DataType gamma) const
Dai-Yuan update.
Functional_::VectorTypeR VectorType
Input type for the gradient.
virtual void done_symbolic() override
Symbolic finalization method.
DataType compute_beta(const DataType &gamma, const DataType &gamma_prev) const
Computes the parameter beta for the search direction update.
Index _restart_freq
Restart frequency, defaults to problemsize+3.
NLOptLS< FunctionalType, FilterType > BaseClass
Our baseclass.
std::shared_ptr< PrecondType > _precond
void set_max_num_restarts(Index max_num_restarts)
Sets the restart frquency.
std::shared_ptr< LinesearchType > _linesearch
The linesearch used along the descent direction.
NLCG(const String §ion_name, const PropertyMap *section, Functional_ &functional, Filter_ &filter, std::shared_ptr< LinesearchType > linesearch, std::shared_ptr< PrecondType > precond=nullptr)
Constructor using a PropertyMap.
virtual ~NLCG()
Virtual destructor.
static constexpr NLCGDirectionUpdate direction_update_default
Default search direction update.
void set_direction_update(NLCGDirectionUpdate update_)
Sets the direction update method.
DataType hestenes_stiefel() const
Hestenes-Stiefel update.
NLCGDirectionUpdate _direction_update
Method to update the search direction.
Functional_::GradientType GradientType
Type of the functional's gradient has.
Functional_::DataType DataType
Underlying floating point type.
Index _max_num_restarts
Maximum number of restarts triggered by the strong Wolfe conditions not holding.
Base class for line search based nonlinear optimizers.
virtual Status _set_initial_defect(const VectorType &vec_def, const VectorType &vec_sol) override
Internal function: sets the initial defect vector.
DataType _fval_prev
Functional value from the previous iteration.
virtual Status _set_new_defect(const VectorType &vec_r, const VectorType &vec_sol) override
Internal function: sets the new defect norm.
DataType _fval
Current functional value.
Functional_ & _functional
Our nonlinear functional.
virtual void set_ls_iter_digits(Index digits)
Sets the number of digits used to plot line search iteration numbers.
Index _ls_its
The number of iterations the linesearch performed in the last iteration of this solver.
Filter_ & _filter
The filter we apply to the gradient.
DataType _steplength
The last step length of the line search.
Abstract base class for preconditioners for nonlinear optimization.
bool _apply_precond(VectorType &vec_cor, const VectorType &vec_def, const Filter_ &filter)
Applies the preconditioner onto a defect vector.
virtual void init_symbolic()
Symbolic initialization method.
virtual void done_symbolic()
Symbolic finalization method.
String class implementation.
T_ sqrt(T_ x)
Returns the square-root of a value.
T_ sqr(T_ x)
Returns the square of a value.
T_ min(T_ a, T_ b)
Returns the minimum of two values.
T_ ilog10(T_ x)
Computes the integral base-10 logarithm of an integer, i.e. its number of non-zero decimal digits.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
NLCGDirectionUpdate
Enum for NLCG search direction updates.
Status
Solver status return codes enumeration.
@ success
solving successful (convergence criterion fulfilled)
@ progress
continue iteration (internal use only)
@ undefined
undefined status
@ stagnated
solver stagnated (stagnation criterion fulfilled)
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
std::shared_ptr< NLCG< Functional_, Filter_ > > new_nlcg(Functional_ &functional, Filter_ &filter, Linesearch_ &linesearch, NLCGDirectionUpdate direction_update=NLCG< Functional_, Filter_ >::direction_update_default, bool keep_iterates=false, std::shared_ptr< NLOptPrecond< typename Functional_::VectorTypeL, Filter_ > > precond=nullptr)
Creates a new NLCG solver object.
String stringify(const T_ &item)
Converts an item into a String.
std::uint64_t Index
Index data type.