Loading...
Searching...
No Matches
Optimizer.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/Optimizer.h"
13#include "eon/BaseStructures.h"
15#include "eon/FIRE.h"
16#include "eon/LBFGS.h"
17#include "eon/Quickmin.h"
18#include "eon/SteepestDescent.h"
19#ifdef WITH_RGMIN
20#include "eon/XtsciOptimizer.h"
21#endif
22
24
25std::optional<std::string> rgminMethodFor(const ObjectiveFunction &a_objf,
26 OptType a_otype,
27 const Parameters &a_params) {
28 if (!a_objf.gradientMatchesEnergy()) {
29 return std::nullopt;
30 }
31 return rgminBackendMethod(a_otype, a_params);
32}
33
34std::optional<std::string> rgminBackendMethod(OptType a_otype,
35 const Parameters &a_params) {
36 if (a_params.optimizer_options().backend != "rgmin") {
37 return std::nullopt;
38 }
39 switch (a_otype) {
40 case OptType::LBFGS:
41 return "lbfgs";
42 case OptType::FIRE:
43 return "fire";
44 case OptType::CG:
45 return "polak_ribiere";
46 case OptType::SD:
47 return "steepest";
48 case OptType::QM:
49 return "quickmin";
50 default:
51 return std::nullopt;
52 }
53}
54
55std::unique_ptr<Optimizer> mkOptim(std::shared_ptr<ObjectiveFunction> a_objf,
56 OptType a_otype,
57 const Parameters &a_params) {
58 if (auto method = rgminMethodFor(*a_objf, a_otype, a_params)) {
59#ifdef WITH_RGMIN
60 return std::make_unique<XtsciOptimizer>(a_objf, a_params,
61 std::move(*method));
62#else
63 throw std::runtime_error(
64 "[Optimizer] opt_backend=rgmin needs a build that links rgmin "
65 "(-Dwith_rgmin=enabled)");
66#endif
67 }
68 switch (a_otype) {
69 case OptType::FIRE: {
70 return std::make_unique<FIRE>(a_objf, a_params);
71 }
72 case OptType::QM: {
73 return std::make_unique<Quickmin>(a_objf, a_params);
74 }
75 case OptType::CG: {
76 return std::make_unique<ConjugateGradients>(a_objf, a_params);
77 }
78 case OptType::LBFGS: {
79 return std::make_unique<LBFGS>(a_objf, a_params);
80 }
81 case OptType::SD: {
82 return std::make_unique<SteepestDescent>(a_objf, a_params);
83 }
84#ifdef WITH_RGMIN
85 case OptType::XTSCI: {
86 return std::make_unique<XtsciOptimizer>(a_objf, a_params);
87 }
88#else
89 case OptType::XTSCI: {
90 throw std::runtime_error(
91 "[Optimizer] opt_method=xtsci needs a build that links rgmin "
92 "(-Dwith_rgmin=enabled)");
93 }
94#endif
95 case OptType::None: {
96 throw std::runtime_error("[Optimizer] Cannot create None");
97 }
98 case OptType::Unknown: {
99 throw std::runtime_error("[Optimizer] Cannot create Unknown");
100 }
101 default: {
102 throw std::runtime_error("Unsupported optimization type");
103 }
104 }
105}
106} // namespace eonc::helpers::create
Direct optimization for energy minimization.
The optimizer class is used to serve as an abstract class for all optimizers, as well as to call an o...
virtual bool gradientMatchesEnergy() const
const optimizer_options_t & optimizer_options() const
std::optional< std::string > rgminBackendMethod(OptType a_otype, const Parameters &a_params)
rgminMethodFor without the objective test: what a minimization of a potential energy runs under opt_b...
Definition Optimizer.cpp:34
std::unique_ptr< Optimizer > mkOptim(std::shared_ptr< ObjectiveFunction > a_objf, OptType a_otype, const Parameters &a_params)
Definition Optimizer.cpp:55
std::optional< std::string > rgminMethodFor(const ObjectiveFunction &a_objf, OptType a_otype, const Parameters &a_params)
The rgmin method token mkOptim runs for a_otype when [Optimizer] opt_backend = rgmin.
Definition Optimizer.cpp:25
std::string backend
"eon" or "rgmin".