iterative-solver 0.0
 
Loading...
Searching...
No Matches
OptimizeSD.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_OPTIMIZESD_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_OPTIMIZESD_H
3#include <molpro/linalg/itsolv/CastOptions.h>
4#include <molpro/linalg/itsolv/DSpaceResetter.h>
5#include <molpro/linalg/itsolv/IterativeSolverTemplate.h>
6#include <molpro/linalg/itsolv/propose_rspace.h>
7#include <molpro/linalg/itsolv/subspace/SubspaceSolverOptSD.h>
8#include <molpro/linalg/itsolv/subspace/XSpace.h>
9
10namespace molpro::linalg::itsolv {
19template <class R, class Q = R, class P = std::map<size_t, typename R::value_type>>
20class OptimizeSD : public IterativeSolverTemplate<Optimize, R, Q, P> {
21public:
24 using typename SolverTemplate::scalar_type;
25 using typename SolverTemplate::value_type;
26 using typename SolverTemplate::value_type_abs;
28
29 explicit OptimizeSD(const std::shared_ptr<ArrayHandlers<R, Q, P>>& handlers,
30 const std::shared_ptr<Logger>& logger_ = std::make_shared<Logger>())
31 : SolverTemplate(std::make_shared<subspace::XSpace<R, Q, P>>(handlers, logger_),
32 std::static_pointer_cast<subspace::ISubspaceSolver<R, Q, P>>(
33 std::make_shared<subspace::SubspaceSolverOptSD<R, Q, P>>(logger_)),
35
36 bool nonlinear() const override { return true; }
37
38 size_t end_iteration(const VecRef<R>& parameters, const VecRef<R>& action) override {
39 this->solution_params(this->m_working_set, parameters);
40 if (this->m_errors.front() < this->m_convergence_threshold) {
41 this->m_working_set.clear();
42 return 0;
43 }
44 this->m_working_set.assign(1, 0);
45 this->m_handlers->rr().axpy(-1, action.front(), parameters.front());
46 this->m_stats->iterations++;
47 return 1;
48 }
49
50 size_t end_iteration(std::vector<R>& parameters, std::vector<R>& action) override {
51 auto result = end_iteration(wrap(parameters), wrap(action));
52 return result;
53 }
54
55 void set_value_errors() override {
56 auto& Value = this->m_xspace->data[subspace::EqnData::value];
57 this->m_value_errors.assign(1, std::numeric_limits<value_type_abs>::max());
58 // the objective is real, whatever the element type of the containers
59 if (this->m_xspace->size() > 1 and real_part(Value(0, 0)) < real_part(Value(1, 0)))
60 this->m_value_errors.front() = real_part(Value(1, 0)) - real_part(Value(0, 0));
61 }
62
63 void set_options(const Options& options) override {
65 if (auto opt2 = dynamic_cast<const molpro::linalg::itsolv::OptimizeSDOptions*>(&options)) {
67 }
68 }
69
70 std::shared_ptr<Options> get_options() const override {
71 auto opt = std::make_shared<OptimizeSDOptions>();
73 return opt;
74 }
75
76 void report(std::ostream& cout, bool endl = true) const override {
78 cout << ", value " << this->value();
79 if (endl)
80 cout << std::endl;
81 }
82
83 int add_vector(R& parameters, R& residual, value_type value) override {
84 using namespace subspace;
85 auto& xspace = this->m_xspace;
86 auto& xdata = xspace->data;
87 const auto n = this->m_xspace->dimensions().nX();
88 xdata[EqnData::value].resize({n + 1, 1});
89 xdata[EqnData::value](0, 0) = value;
91 return nwork;
92 }
93
94 size_t end_iteration(R& parameters, R& actions) override {
95 auto wparams = std::vector<std::reference_wrapper<R>>{std::ref(parameters)};
96 auto wactions = std::vector<std::reference_wrapper<R>>{std::ref(actions)};
98 }
99
100protected:
101 // for non-linear problems, actions already contains the residual
102 void construct_residual(const std::vector<int>& roots, const CVecRef<R>& params, const VecRef<R>& actions) override {}
103};
104
105} // namespace molpro::linalg::itsolv
106#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ITSOLV_OPTIMIZESD_H
Class, containing a collection of array handlers used in IterativeSolver Provides a Builder sub-class...
Definition ArrayHandlers.h:25
Implements IterativeSolver interface that is common to all solvers.
Definition IterativeSolverTemplate.h:163
std::vector< value_type_abs > m_value_errors
value errors from the most recent solution
Definition IterativeSolverTemplate.h:697
void solution_params(const std::vector< int > &roots, std::vector< R > &parameters) override
Definition IterativeSolverTemplate.h:267
std::vector< int > m_working_set
indices of roots in the working set
Definition IterativeSolverTemplate.h:698
scalar_type value() const override
Definition IterativeSolverTemplate.h:389
std::shared_ptr< subspace::IXSpace< R, Q, P > > m_xspace
manages the subspace and associated data
Definition IterativeSolverTemplate.h:694
std::shared_ptr< ArrayHandlers< R, Q, P > > m_handlers
Array handlers.
Definition IterativeSolverTemplate.h:693
std::vector< value_type_abs > m_errors
errors from the most recent solution
Definition IterativeSolverTemplate.h:696
std::shared_ptr< Options > get_options() const override
Definition IterativeSolverTemplate.h:311
int add_vector(const VecRef< R > &parameters, const VecRef< R > &actions) override
Definition IterativeSolverTemplate.h:186
void report() const override
Definition IterativeSolverTemplate.h:352
void set_options(const Options &options) override
Definition IterativeSolverTemplate.h:296
std::shared_ptr< Statistics > m_stats
accumulates statistics of operations performed by the solver
Definition IterativeSolverTemplate.h:703
A class that optimises a function using a simple steepest-descent method.
Definition OptimizeSD.h:20
size_t end_iteration(R &parameters, R &actions) override
Definition OptimizeSD.h:94
void construct_residual(const std::vector< int > &roots, const CVecRef< R > &params, const VecRef< R > &actions) override
Constructs residual for given roots provided their parameters and actions.
Definition OptimizeSD.h:102
void report(std::ostream &cout, bool endl=true) const override
Definition OptimizeSD.h:76
bool nonlinear() const override
Definition OptimizeSD.h:36
size_t end_iteration(const VecRef< R > &parameters, const VecRef< R > &action) override
Definition OptimizeSD.h:38
void set_value_errors() override
Implementation class should overload this to set errors in the current values (e.g....
Definition OptimizeSD.h:55
size_t end_iteration(std::vector< R > &parameters, std::vector< R > &action) override
Definition OptimizeSD.h:50
int add_vector(R &parameters, R &residual, value_type value) override
Definition OptimizeSD.h:83
std::shared_ptr< Options > get_options() const override
Definition OptimizeSD.h:70
OptimizeSD(const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers, const std::shared_ptr< Logger > &logger_=std::make_shared< Logger >())
Definition OptimizeSD.h:29
void set_options(const Options &options) override
Definition OptimizeSD.h:63
Solves subspace problem for minimisation using the Steepest Descent algorithm.
Definition SubspaceSolverOptSD.h:16
4-parameter interpolation of a 1-dimensional function given two points for which function values and ...
Definition helper.h:14
std::unique_ptr< LinearEigensystem< R, Q, P > > create_LinearEigensystem(const LinearEigensystemOptions &options, const std::shared_ptr< ArrayHandlers< R, Q, P > > &handlers=std::make_shared< molpro::linalg::itsolv::ArrayHandlers< R, Q, P > >())
Definition SolverFactory.h:111
auto wrap(ForwardIt begin, ForwardIt end)
Takes a begin and end iterators and returns a vector of references to each element.
Definition wrap.h:32
std::vector< std::reference_wrapper< const A > > CVecRef
Definition wrap.h:14
std::vector< std::reference_wrapper< A > > VecRef
Definition wrap.h:11
real_type_t< T > real_part(const T &x)
The real part of a scalar; the value itself for a real type.
Definition scalar_traits.h:58
const std::shared_ptr< const molpro::Options > options()
Get the Options object associated with iterative-solver.
Definition linalg_options.cpp:4
static std::shared_ptr< OptimizeSDOptions > OptimizeSD(const std::shared_ptr< Options > &options)
Definition CastOptions.h:90
Allows setting and getting of options for OptimizeBFGS instance via IterativeSolver base class.
Definition OptimizeSDOptions.h:9
Access point for different options in iterative solvers.
Definition Options.h:20
Information about performance of IterativeSolver instance.
Definition Statistics.h:10