alpaqa 1.0.0a13
Nonconvex constrained optimization
Loading...
Searching...
No Matches
ipopt-params.cpp
Go to the documentation of this file.
2#include <alpaqa/ipopt-adapter-export.h>
3
4#include <IpIpoptApplication.hpp>
5
6#include <stdexcept>
7
8namespace alpaqa::params {
9
10template <class T>
11static auto possible_keys(const T &tbl) {
12 if (tbl.empty())
13 return std::string{};
14 auto penult = std::prev(tbl.end());
15 auto quote_concat = [](std::string &&a, auto b) {
16 return a + "'" + b.first + "', ";
17 };
18 return std::accumulate(tbl.begin(), penult, std::string{}, quote_concat) +
19 "'" + std::string(penult->first) + "'";
20}
21
22template <>
23void IPOPT_ADAPTER_EXPORT set_param(Ipopt::IpoptApplication &app,
24 ParamString s) {
25
26 // Split the key to get the option name (val_key is expected to be empty)
27 auto [opt_name, val_key] = split_key(s.key);
29 .full_key = s.full_key,
30 .key = val_key,
31 .value = s.value,
32 };
33
34 // Search the option name in the list of Ipopt options
35 const auto &ipopt_opts = app.RegOptions()->RegisteredOptionsList();
36 const auto regops_it = ipopt_opts.find(std::string(opt_name));
37 if (regops_it == ipopt_opts.end())
38 throw std::invalid_argument(
39 "Invalid key '" + std::string(opt_name) + "' for type '" +
40 "IpoptApplication" + "' in '" + std::string(s.full_key) +
41 "',\n possible keys are: " + possible_keys(ipopt_opts));
42
43 // Depending on the type, set the value of the option
44 bool success = false;
45 const auto type = regops_it->second->Type();
46 switch (type) {
47 case Ipopt::OT_Number: {
48 double value;
49 set_param(value, val_param);
50 success = app.Options()->SetNumericValue(std::string(opt_name),
51 value, false);
52 } break;
53 case Ipopt::OT_Integer: {
54 Ipopt::Index value;
55 set_param(value, val_param);
56 success = app.Options()->SetIntegerValue(std::string(opt_name),
57 value, false);
58 } break;
59 case Ipopt::OT_String: {
60 success = app.Options()->SetStringValue(
61 std::string(opt_name), std::string(val_param.value), false);
62 } break;
63 case Ipopt::OT_Unknown:
64 default: {
65 throw std::invalid_argument("Unknown type in '" +
66 std::string(s.full_key) + "'");
67 }
68 }
69 if (!success)
70 throw std::invalid_argument("Invalid option in '" +
71 std::string(s.full_key) + "'");
72}
73
74} // namespace alpaqa::params
void set_param(bool &b, ParamString s)
Definition params.cpp:26
std::string_view key
The subkey to resolve next.
Definition params.hpp:20
std::string_view full_key
Full key string, used for diagnostics.
Definition params.hpp:18
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
Definition params.hpp:32
std::string_view value
The value of the parameter to store.
Definition params.hpp:22
auto possible_keys()
Return a string enumerating the possible attribute names for the struct type T.
Definition params.tpp:98
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition params.hpp:16
constexpr const auto inf
Definition config.hpp:85