alpaqa cmake-targets
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
6namespace alpaqa::params {
7
8template <class T>
9static auto possible_keys(const T &tbl) {
10 if (tbl.empty())
11 return std::string{};
12 auto penult = std::prev(tbl.end());
13 auto quote_concat = [](std::string &&a, auto b) {
14 return a + "'" + b.first + "', ";
15 };
16 return std::accumulate(tbl.begin(), penult, std::string{}, quote_concat) +
17 "'" + std::string(penult->first) + "'";
18}
19
20template <>
21void IPOPT_ADAPTER_EXPORT set_param(Ipopt::IpoptApplication &app,
22 ParamString s) {
23
24 // Split the key to get the option name (val_key is expected to be empty)
25 auto [opt_name, val_key] = split_key(s.key);
27 .full_key = s.full_key,
28 .key = val_key,
29 .value = s.value,
30 };
31
32 // Search the option name in the list of Ipopt options
33 const auto &ipopt_opts = app.RegOptions()->RegisteredOptionsList();
34 const auto regops_it = ipopt_opts.find(std::string(opt_name));
35 if (regops_it == ipopt_opts.end())
36 throw invalid_param(
37 "Invalid key '" + std::string(opt_name) + "' for type '" +
38 "IpoptApplication" + "' in '" + std::string(s.full_key) +
39 "',\n possible keys are: " + possible_keys(ipopt_opts));
40
41 // Depending on the type, set the value of the option
42 bool success = false;
43 const auto type = regops_it->second->Type();
44 switch (type) {
45 case Ipopt::OT_Number: {
46 double value;
47 set_param(value, val_param);
48 success = app.Options()->SetNumericValue(std::string(opt_name),
49 value, false);
50 } break;
51 case Ipopt::OT_Integer: {
52 Ipopt::Index value;
53 set_param(value, val_param);
54 success = app.Options()->SetIntegerValue(std::string(opt_name),
55 value, false);
56 } break;
57 case Ipopt::OT_String: {
58 success = app.Options()->SetStringValue(
59 std::string(opt_name), std::string(val_param.value), false);
60 } break;
61 case Ipopt::OT_Unknown:
62 default: {
63 throw invalid_param("Unknown type in '" + std::string(s.full_key) +
64 "'");
65 }
66 }
67 if (!success)
68 throw invalid_param("Invalid option in '" + std::string(s.full_key) +
69 "'");
70}
71
72} // namespace alpaqa::params
std::string_view key
The subkey to resolve next.
Definition params.hpp:20
static auto possible_keys(const T &tbl)
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
void set_param(T &t, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.tpp:24
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
Custom parameter parsing exception.
Definition params.hpp:26