alpaqa develop
Nonconvex constrained optimization
Loading...
Searching...
No Matches
options.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <algorithm>
8#include <ranges>
9#include <span>
10#include <stdexcept>
11#include <string_view>
12#include <vector>
13
14#if ALPAQA_WITH_JSON
16#include <nlohmann/json.hpp>
17#include <fstream>
18#endif
19
20class Options {
21 private:
22 std::vector<std::string_view> opts_storage;
23 std::vector<unsigned> used_storage;
24 size_t num_json;
25#if ALPAQA_WITH_JSON
26 std::vector<nlohmann::json> json_storage;
27 nlohmann::json json_out;
28#endif
29
30 public:
31 Options(int argc, const char *const argv[])
32 : opts_storage{argv, argv + argc} {
33 // Arguments starting with an '@' sign refer to JSON files with options
34 auto with_at = [](std::string_view s) { return s.starts_with('@'); };
35 // JSON options are always applied before command-line options
36 auto non_json = std::ranges::stable_partition(opts_storage, with_at);
37 num_json = static_cast<size_t>(non_json.begin() - opts_storage.begin());
38#if ALPAQA_WITH_JSON
39 // Load the JSON data for each '@' option
40 auto load = [](std::string_view s) {
41 return load_json(std::string(s.substr(1)));
42 };
43 auto json_obj = std::views::transform(json_flags(), load);
44 json_storage = decltype(json_storage){json_obj.begin(), json_obj.end()};
45#else
46 if (num_json > 0)
47 throw std::logic_error(
48 "This version of alpaqa was compiled without JSON support: "
49 "cannot parse options " +
50 alpaqa::util::join_quote(json_flags(), {.sep = " "}));
51#endif
52 // Keep track of which options are used
53 used_storage.resize(opts_storage.size() - num_json);
54 }
55 [[nodiscard]] std::span<const std::string_view> json_flags() const {
56 return std::span{opts_storage}.first(num_json);
57 }
58 [[nodiscard]] std::span<const std::string_view> options() const {
59 return std::span{opts_storage}.subspan(num_json);
60 }
61 [[nodiscard]] std::span<unsigned> used() { return used_storage; }
62
63 template <class T>
64 void set_params(T &t, std::string_view prefix);
65
66#if ALPAQA_WITH_JSON
67 [[nodiscard]] std::span<const nlohmann::json> json_data() const {
68 return json_storage;
69 }
70
71 [[nodiscard]] const nlohmann::json &get_json_out() const {
72 return json_out;
73 }
74
75 private:
76 [[nodiscard]] static nlohmann::json load_json(const std::string &name) {
77 nlohmann::json j;
78 std::ifstream f{name};
79 if (!f)
80 throw std::runtime_error("Unable to open JSON file '" + name + "'");
81 try {
82 f >> j;
83 } catch (nlohmann::json::exception &e) {
84 throw std::runtime_error("Unable to parse JSON file '" + name +
85 "': " + e.what());
86 }
87 return j;
88 }
89#endif
90};
91
92template <class T>
93void Options::set_params(T &t, std::string_view prefix) {
94#if ALPAQA_WITH_JSON
95 auto json_data = this->json_data();
96 for (size_t i = 0; i < json_data.size(); ++i)
97 try {
98 if (auto j = json_data[i].find(prefix); j != json_data[i].end())
101 std::string fname{this->json_flags()[i].substr(1)};
102 throw std::invalid_argument(
103 "Error in JSON file '" + fname + "' at '" +
104 std::string(prefix) +
105 alpaqa::util::join_quote(std::views::reverse(e.backtrace),
106 {.sep = "",
107 .empty = "",
108 .quote_left = ".",
109 .quote_right = ""}) +
110 "': " + e.what());
111 } catch (nlohmann::json::exception &e) {
112 std::string fname{this->json_flags()[i].substr(1)};
113 throw std::invalid_argument("Error in JSON file '" + fname +
114 "': " + e.what());
115 }
116#endif
117 alpaqa::params::set_params(t, prefix, this->options(), this->used());
118#if ALPAQA_WITH_JSON
120 if constexpr (!std::is_same_v<T, vec_from_file>)
121 alpaqa::params::get_param(t, this->json_out[std::string{prefix}]);
122#endif
123}
124
125template <class T>
126void set_params(T &t, std::string_view prefix, Options &opts) {
127 opts.set_params(t, prefix);
128}
std::span< unsigned > used()
Definition options.hpp:61
std::span< const std::string_view > json_flags() const
Definition options.hpp:55
std::span< const std::string_view > options() const
Definition options.hpp:58
std::vector< unsigned > used_storage
Definition options.hpp:23
size_t num_json
Definition options.hpp:24
void set_params(T &t, std::string_view prefix)
Definition options.hpp:93
std::vector< std::string_view > opts_storage
Definition options.hpp:22
Options(int argc, const char *const argv[])
Definition options.hpp:31
void get_param(const T &, json &j)
Get the first argument as a JSON object j.
Definition json.cpp:253
void set_param(T &, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.cpp:249
void set_params(T &t, std::string_view prefix, std::span< const std::string_view > options, std::optional< std::span< unsigned > > used=std::nullopt)
Overwrites t based on the options that start with prefix.
Definition params.hpp:49
std::string join_quote(std::ranges::input_range auto strings, join_quote_opt opt={})
Join the list of strings into a single string, using the separator given by opt.
void set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:126
Custom parameter parsing exception.
Definition json.hpp:28
std::vector< std::string > backtrace
Definition json.hpp:30