Nonconvex constrained optimization
Loading...
Searching...
No Matches
alpaqa-driver-main.cpp
Go to the documentation of this file.
1#ifdef _WIN32
2#define NOMINMAX
3#include <Windows.h>
4#endif
5
10#include <alpaqa/util/print.hpp>
11#include <alpaqa/util/span.hpp>
12#include <guanaqo/demangled-typename.hpp>
13#include <guanaqo/string-util.hpp>
14#include <alpaqa-version.h>
15
17
18#include <algorithm>
19#include <filesystem>
20#include <fstream>
21#include <iostream>
22#include <span>
23#include <stdexcept>
24#include <string>
25#include <string_view>
26#include <tuple>
27
28#include "param-complete.hpp"
29
30namespace alpaqa::driver {
31
33
34int main(int argc, const char *argv[]) {
35 // Check command line options
36 if (argc < 1)
37 return -1;
38 if (argc == 1)
39 return print_usage(argv[0], std::cout), 0;
40 if (argc < 2)
41 return print_usage(argv[0], std::cerr), -1;
42 if (argv[1] == "-h"sv || argv[1] == "--help"sv || argv[1] == "?"sv)
43 return print_usage(argv[0], std::cout), 0;
44 if (argv[1] == "-v"sv || argv[1] == "--version"sv)
45 return print_version(std::cout), 0;
46 if (argv[1] == "--complete"sv) {
47 if (argc < 4)
48 return -1;
49 print_completion(argv[2], argv[3]);
50 return 0;
51 }
52
53 std::span args{argv, static_cast<size_t>(argc)};
54 alpaqa::Options opts{argc - 2, argv + 2};
55
56 // Check where to write the output to
57 std::ofstream out_fstream;
58 std::ostream &os = get_output_stream(opts, out_fstream, std::cout);
59
60 // Check which problem to load
61 auto [prob_path, prob_type] = get_problem_path(argv);
62
63 // Check which solver to use
64 SolverBuilders builders;
65 auto [solver_builder, direction] = builders.get_solver_builder(opts);
66
67 // Check output paths
68 fs::path sol_output_dir = get_output_paths(opts);
69
70 // Build solver
71 auto solver = solver_builder(direction, opts);
72
73 // Load problem
74 os << "Loading " << prob_path << " ..." << std::endl;
75 auto problem = alpaqa::load_problem(prob_type, prob_path, opts);
76 // Print problem information
77 bool show_funcs = false;
78 set_params(show_funcs, "show_funcs", opts);
79 print_problem_description(os, problem, show_funcs);
80 os << std::endl;
81
82 // Check options
83 auto used = opts.used();
84 auto unused_opt = std::ranges::find(used, 0);
85 auto unused_idx = static_cast<size_t>(unused_opt - used.begin());
86 if (unused_opt != used.end())
87 throw std::invalid_argument("Unused option: " +
88 std::string(opts.options()[unused_idx]));
89
90 // Solve
91 auto solver_results = solver->run(problem, os);
92
93 // Compute more statistics
94 real_t f = problem.problem.eval_objective(solver_results.solution);
95 auto kkt_err = alpaqa::compute_kkt_error(
96 problem.problem, solver_results.solution, solver_results.multipliers);
97 BenchmarkResults results{
98 .problem = problem,
99 .solver_results = solver_results,
100 .objective = f + solver_results.h,
101 .smooth_objective = f,
102 .error = kkt_err,
103 .options = opts.options(),
104 .timestamp = timestamp_ms<std::chrono::system_clock>().count(),
105 };
106
107 // Print results
108 print_results(os, results);
109
110 // Store solution
111 if (!sol_output_dir.empty())
112 store_solution(sol_output_dir, os, results, *solver, opts, args);
113 return 0;
114}
115
116} // namespace alpaqa::driver
117
118int main(int argc, const char *argv[]) try {
119#ifdef _WIN32
120 SetConsoleOutputCP(CP_UTF8);
121#endif
122 return alpaqa::driver::main(argc, argv);
123} catch (std::exception &e) {
124 std::cerr << "Error: " << guanaqo::demangled_typename(typeid(e)) << ":\n "
125 << e.what() << std::endl;
126 return -1;
127}
int main(int argc, const char *argv[])
std::span< unsigned > used()
Definition options.hpp:63
std::span< const std::string_view > options() const
Definition options.hpp:60
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
ALPAQA_DRIVERS_EXPORT std::string store_solution(const fs::path &sol_output_dir, std::ostream &os, BenchmarkResults &results, const SolverWrapper &solver, const alpaqa::Options &opts, std::span< const char *const > argv)
ALPAQA_DRIVERS_EXPORT std::string get_output_paths(alpaqa::Options &opts)
ALPAQA_DRIVERS_EXPORT std::ostream & get_output_stream(alpaqa::Options &opts, std::ofstream &out_fstream, std::ostream &default_stream)
ALPAQA_DRIVERS_EXPORT std::tuple< fs::path, std::string_view > get_problem_path(const char *const *argv)
auto timestamp_ms()
Definition results.hpp:61
int main(int argc, const char *argv[])
ALPAQA_DRIVERS_EXPORT void print_version(std::ostream &os)
void print_results(std::ostream &os, const BenchmarkResults &results)
Definition results.hpp:137
ALPAQA_DRIVERS_EXPORT void print_usage(const char *a0, std::ostream &os)
PROBLEM_LOADER_EXPORT LoadedProblem load_problem(std::string_view type, const fs::path &file, Options &opts)
EigenConfigd DefaultConfig
Definition config.hpp:31
typename Conf::real_t real_t
Definition config.hpp:86
void set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:128
PROBLEM_LOADER_EXPORT void print_problem_description(std::ostream &os, LoadedProblem &problem, bool show_funcs=true)
KKTError< Conf > compute_kkt_error(const TypeErasedProblem< Conf > &problem, crvec< Conf > x, crvec< Conf > y)
Definition kkt-error.hpp:17
void print_completion(std::string_view method, std::string_view params)
std::tuple< solver_builder_func, std::string > get_solver_builder(alpaqa::Options &opts) const