26namespace fs = std::filesystem;
31 std::cout <<
"Usage: " << a0
32 <<
" [<problem-type>:][<path>/]<name> [method=<solver>] "
33 "[<key>=<value>...]\n";
39 auto tok_pos = s.find(tok);
40 if (tok_pos == s.npos)
41 return std::make_tuple(std::string_view{}, s);
42 std::string_view key{s.begin(), s.begin() + tok_pos};
43 std::string_view rem{s.begin() + tok_pos + 1, s.end()};
44 return std::make_tuple(key, rem);
48 std::string out_path =
"-";
51 if (out_fstream.open(out_path); !out_fstream)
52 throw std::runtime_error(
"Unable to open '" + out_path +
"'");
53 return out_fstream.is_open() ? out_fstream : std::cout;
57 std::string_view sol_path;
63 bool rel_to_exe = argv[1][0] ==
'^';
64 std::string_view prob_path_s = argv[1] +
static_cast<ptrdiff_t
>(rel_to_exe);
65 std::string_view prob_type;
66 std::tie(prob_type, prob_path_s) =
split_once(prob_path_s,
':');
67 fs::path prob_path{prob_path_s};
69 prob_path = fs::canonical(fs::path(argv[0])).parent_path() / prob_path;
70 return std::make_tuple(std::move(prob_path), prob_type);
74 std::string_view method =
"panoc", direction;
78 std::map<std::string_view, solver_builder_func_t> solvers{
89 auto solver_it = solvers.find(method);
90 if (solver_it == solvers.end())
91 throw std::invalid_argument(
92 "Unknown solver '" + std::string(method) +
"'\n" +
93 " Available solvers: " +
95 return std::make_tuple(std::move(solver_it->second), direction);
101 auto timestamp_str = std::to_string(results.
timestamp);
104 auto suffix =
'_' + name +
'_' + timestamp_str +
'_' + rnd_str;
105 fs::create_directories(sol_output_dir);
106 std::array solutions{
107 std::tuple{
"solution",
"sol_x", &sol_res.solution},
108 std::tuple{
"multipliers for g",
"mul_g", &sol_res.multipliers},
109 std::tuple{
"multipliers for x",
"mul_x", &sol_res.multipliers_bounds},
111 for (
auto [name, fname, value] : solutions) {
112 if (value->size() == 0)
114 auto pth = sol_output_dir / (std::string(fname) + suffix +
".csv");
115 os <<
"Writing " << name <<
" to " << pth << std::endl;
116 std::ofstream output_file(pth);
119 auto pth = sol_output_dir / (
"cmdline" + suffix +
".txt");
120 os <<
"Writing arguments to " << pth << std::endl;
121 std::ofstream output_file(pth);
122 for (
const char *arg : argv)
123 output_file << std::quoted(arg,
'\'') <<
' ';
127int main(
int argc,
const char *argv[])
try {
135 std::span args{argv,
static_cast<size_t>(argc)};
136 Options opts{argc - 2, argv + 2};
139 std::ofstream out_fstream;
152 auto solver = solver_builder(direction, opts);
155 os <<
"Loading problem " << prob_path << std::endl;
156 auto problem =
load_problem(prob_type, prob_path.parent_path(),
157 prob_path.filename(), opts);
158 os <<
"Loaded problem " << problem.path.stem().c_str() <<
" from "
159 << problem.path <<
"\nProvided functions:\n";
164 auto used = opts.used();
165 auto unused_opt = std::find(used.begin(), used.end(),
false);
166 auto unused_idx =
static_cast<size_t>(unused_opt - used.begin());
167 if (unused_opt != used.end())
168 throw std::invalid_argument(
"Unused option: " +
169 std::string(opts.options()[unused_idx]));
172 auto solver_results = solver(problem, os);
175 real_t f = problem.problem.eval_f(solver_results.solution);
177 problem.problem, solver_results.solution, solver_results.multipliers);
180 .solver_results = solver_results,
181 .objective = f + solver_results.h,
182 .smooth_objective = f,
184 .options = opts.options(),
185 .timestamp = timestamp_ms<std::chrono::system_clock>().count(),
192 if (!sol_output_dir.empty())
195}
catch (std::exception &e) {
197 << e.what() << std::endl;
auto get_solver_builder(Options &opts)
std::ostream & get_output_stream(Options &opts, std::ofstream &out_fstream)
void store_solution(const fs::path &sol_output_dir, std::ostream &os, BenchmarkResults &results, std::span< const char * > argv)
auto get_problem_path(const char *const *argv)
void print_usage(const char *a0)
auto split_once(std::string_view s, char tok='.')
Split the string s on the first occurrence of tok.
std::string_view get_output_paths(Options &opts)
int main(int argc, const char *argv[])
#define USING_ALPAQA_CONFIG(Conf)
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
void print_provided_functions(std::ostream &os, const TypeErasedProblem< Conf > &problem)
solver_func_t make_ipopt_driver(std::string_view direction, Options &opts)
solver_func_t make_lbfgsb_driver(std::string_view direction, Options &opts)
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
KKTError< Conf > compute_kkt_error(const alpaqa::TypeErasedProblem< Conf > &problem, alpaqa::crvec< Conf > x, alpaqa::crvec< Conf > y)
std::ostream & print_csv(std::ostream &os, const Eigen::Ref< const Eigen::MatrixX< float > > &M, std::string_view sep, std::string_view begin, std::string_view end)
decltype(auto) set_params(T &t, std::string_view prefix, Options &opts)
solver_func_t make_panoc_driver(std::string_view direction, Options &opts)
solver_func_t make_zerofpr_driver(std::string_view direction, Options &opts)
solver_func_t make_pantr_driver(std::string_view direction, Options &opts)
LoadedProblem load_problem(std::string_view type, const fs::path &dir, const fs::path &file, Options &opts)
std::string random_hex_string(auto &&rng)
void print_results(std::ostream &os, const BenchmarkResults &results)
SolverResults solver_results
std::string format_string_list(const auto &container, const auto &proj=[](const auto &x) -> decltype(auto) { return x;})