GQCP
Loading...
Searching...
No Matches
IterativeAlgorithm.hpp
Go to the documentation of this file.
1// This file is part of GQCG-GQCP.
2//
3// Copyright (C) 2017-2020 the GQCG developers
4//
5// GQCG-GQCP is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GQCG-GQCP is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GQCG-GQCP. If not, see <http://www.gnu.org/licenses/>.
17
18#pragma once
19
20
23
24#include <boost/format.hpp>
25
26#include <cstddef>
27
28
29namespace GQCP {
30
31
37template <typename _Environment>
39public:
40 using Environment = _Environment;
41
42
43private:
44 size_t maximum_number_of_iterations;
45 size_t iteration = 0; // the number of iterations that have been performed
46
47 StepCollection<Environment> steps; // the collection of algorithm steps that is performed in-between convergence checks
48 std::shared_ptr<ConvergenceCriterion<Environment>> convergence_criterion;
49
50
51public:
52 /*
53 * CONSTRUCTORS
54 */
55
65 template <typename Criterion>
66 IterativeAlgorithm(const StepCollection<Environment>& steps, const Criterion& convergence_criterion, const size_t maximum_number_of_iterations = 128) :
67 maximum_number_of_iterations {maximum_number_of_iterations},
68 steps {steps},
69 convergence_criterion {std::make_shared<Criterion>(convergence_criterion)} {}
70
71
72 /*
73 * PUBLIC METHODS
74 */
75
76
80 std::string description() const {
81
82 std::string description_string = (boost::format("An iterative algorithm (with a maximum of %s iterations) consisting of the following steps:\n") % this->maximumNumberOfIterations()).str();
83 description_string += steps.description();
84
85 description_string += "\nWith the following convergence criterion:\n";
86 description_string += convergence_criterion->description();
87
88 return description_string;
89 }
90
91
98 template <typename Z = Step<Environment>>
99 enable_if_t<std::is_same<Environment, typename Z::Environment>::value, void> insert(const Z& step, const size_t index) { this->steps.insert(step, index); }
100
101
105 size_t maximumNumberOfIterations() const { return this->maximum_number_of_iterations; }
106
110 size_t numberOfIterations() const { return this->iteration; }
111
112
118 void perform(Environment& environment) {
119
120 for (this->iteration = 0; this->iteration <= this->maximum_number_of_iterations; this->iteration++) { // do at maximum the maximum allowed number of iterations
121
122 // Every iteration consists of two parts:
123 // - the convergence check, which checks if the iterations may stop
124 // - the iteration cycle, i.e. what happens in-between the convergence checks
125 if (this->convergence_criterion->isFulfilled(environment)) {
126 return; // exit the loop and function early
127 }
128
129 this->steps.execute(environment);
130 }
131
132 // Since we will exit the function early if convergence is achieved, the algorithm is considered non-converging if the loop is done.
133 throw std::runtime_error("IterativeAlgorithm<Environment>::perform(Environment&): The algorithm didn't find a solution within the maximum number of iterations.");
134 }
135
136
142 void remove(const size_t index) { this->steps.remove(index); }
143
144
151 template <typename Z = Step<Environment>>
152 enable_if_t<std::is_same<Environment, typename Z::Environment>::value, void> replace(const Z& step, const size_t index) { this->steps.replace(step, index); }
153};
154
155
156} // namespace GQCP
Definition: IterativeAlgorithm.hpp:38
void perform(Environment &environment)
Definition: IterativeAlgorithm.hpp:118
void remove(const size_t index)
Definition: IterativeAlgorithm.hpp:142
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, void > insert(const Z &step, const size_t index)
Definition: IterativeAlgorithm.hpp:99
size_t numberOfIterations() const
Definition: IterativeAlgorithm.hpp:110
std::string description() const
Definition: IterativeAlgorithm.hpp:80
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, void > replace(const Z &step, const size_t index)
Definition: IterativeAlgorithm.hpp:152
_Environment Environment
Definition: IterativeAlgorithm.hpp:40
size_t maximumNumberOfIterations() const
Definition: IterativeAlgorithm.hpp:105
IterativeAlgorithm(const StepCollection< Environment > &steps, const Criterion &convergence_criterion, const size_t maximum_number_of_iterations=128)
Definition: IterativeAlgorithm.hpp:66
Definition: StepCollection.hpp:41
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, void > insert(const Z &step, const size_t index)
Definition: StepCollection.hpp:105
void execute(Environment &environment) override
Definition: StepCollection.hpp:75
std::string description() const override
Definition: StepCollection.hpp:58
enable_if_t< std::is_same< Environment, typename Z::Environment >::value, void > replace(const Z &step, const size_t index)
Definition: StepCollection.hpp:148
void remove(const size_t index)
Definition: StepCollection.hpp:136
Definition: BaseOneElectronIntegralBuffer.hpp:25
typename std::enable_if< B, T >::type enable_if_t
Definition: type_traits.hpp:37