117int main(
int argc,
const char *argv[])
try {
119 SetConsoleOutputCP(CP_UTF8);
128 std::span args{argv,
static_cast<size_t>(argc)};
129 Options opts{argc - 2, argv + 2};
132 std::ostream &os = std::cout;
138 os <<
"Loading problem " << prob_path << std::endl;
139 auto problem =
load_problem(prob_type, prob_path.parent_path(),
140 prob_path.filename(), opts);
141 os <<
"Loaded problem " << problem.path.stem().string() <<
" from "
142 << problem.path <<
"\nnvar: " << problem.problem.get_n()
143 <<
"\nncon: " << problem.problem.get_m() <<
"\nProvided functions:\n";
148 auto has_opt = [&opts](std::string_view o) {
149 auto o_it = std::ranges::find(opts.options(), o);
150 if (o_it == opts.options().end())
152 auto index =
static_cast<size_t>(o_it - opts.options().begin());
153 ++opts.used()[index];
158 .hessians = !has_opt(
"--no-hessians"),
162 auto seed =
static_cast<unsigned int>(std::time(
nullptr));
167 auto used = opts.used();
168 auto unused_opt = std::ranges::find(used, 0);
169 auto unused_idx =
static_cast<size_t>(unused_opt - used.begin());
170 if (unused_opt != used.end())
171 throw std::invalid_argument(
"Unused option: " +
172 std::string(opts.options()[unused_idx]));
177}
catch (std::exception &e) {
179 << e.what() << std::endl;
201 const auto n = x.size();
202 std::vector<Eigen::Triplet<real_t, index_t>> coo;
203 vec h = vec::Zero(n);
205 const auto δ = 1e-2 * ε;
206 vec grad_x(n), grad_xh(n);
208 for (index_t i = 0; i < n; ++i) {
209 real_t hh = std::abs(x(i)) * ε > δ ? x(i) * ε : δ;
211 grad_L(x + h, grad_xh);
212 grad_xh = (grad_xh - grad_x) / hh;
213 for (index_t j = 0; j < n; ++j)
214 if (real_t v = grad_xh(j); v != 0)
215 coo.emplace_back(std::min(j, i), std::max(i, j),
216 v * (i == j ? 1 : 0.5));
219 Eigen::SparseMatrix<real_t, 0, index_t> hess(n, n);
220 hess.setFromTriplets(coo.begin(), coo.end());
244 auto &te_problem = lproblem.
problem;
249 auto n = te_problem.get_n(), m = te_problem.get_m();
251 std::srand(
static_cast<unsigned int>(std::time(
nullptr)));
252 vec Σ = 1.5 * vec::Random(m).array() + 2;
253 vec y = y0 + y0.norm() * vec::Random(m);
254 vec x = x0 + sc * vec::Random(n);
255 vec v = 5e-6 * sc * vec::Random(n);
259 auto print_compare = [&log, &opts](
const auto &fd,
const auto &ad) {
260 auto abs_err = (fd - ad).reshaped().template lpNorm<Eigen::Infinity>();
262 abs_err / fd.reshaped().template lpNorm<Eigen::Infinity>();
271 auto f = [&](crvec x) {
return te_problem.eval_f(x); };
272 log <<
"Gradient verification: ∇f(x)\n";
275 te_problem.eval_grad_f(x, grad_f);
276 print_compare(fd_grad_f, grad_f);
278 log <<
"Gradient verification: ∇L(x)\n";
279 auto L = [&](crvec x) {
280 te_problem.eval_g(x, gx);
281 return te_problem.eval_f(x) + gx.dot(y);
285 te_problem.eval_grad_L(x, y, grad_L, wn);
286 print_compare(fd_grad_L, grad_L);
288 log <<
"Gradient verification: ∇ψ(x)\n";
289 auto ψ = [&](crvec x) {
return te_problem.eval_ψ(x, y, Σ, wm); };
292 te_problem.eval_grad_ψ(x, y, Σ, grad_ψ, wn, wm);
293 print_compare(fd_grad_ψ, grad_ψ);
295 if (te_problem.provides_eval_hess_L_prod()) {
296 log <<
"Hessian product verification: ∇²L(x)\n";
299 te_problem.eval_grad_L(xv, y, grad_Lv, wn);
300 vec fd_hess_Lv = grad_Lv - grad_L;
302 te_problem.eval_hess_L_prod(x, y, 1, v, hess_Lv);
303 print_compare(fd_hess_Lv, hess_Lv);
306 if (te_problem.provides_eval_hess_ψ_prod()) {
307 log <<
"Hessian product verification: ∇²ψ(x)\n";
310 te_problem.eval_grad_ψ(xv, y, Σ, grad_ψv, wn, wm);
311 vec fd_hess_ψv = grad_ψv - grad_ψ;
313 te_problem.eval_hess_ψ_prod(x, y, Σ, 1, v, hess_ψv);
314 print_compare(fd_hess_ψv, hess_ψv);
317 if (opts.
hessians && te_problem.provides_eval_hess_L()) {
318 log <<
"Hessian verification: ∇²L(x)\n";
320 auto sparsity = te_problem.get_hess_L_sparsity();
321 sp::SparsityConverter<sp::Sparsity<config_t>, sp::Dense<config_t>> cvt{
324 auto eval_h = [&](rvec v) { te_problem.eval_hess_L(x, y, 1., v); };
325 cvt.convert_values(eval_h, hess_L.reshaped());
327 [&](crvec x, rvec g) { te_problem.eval_grad_L(x, y, g, wn); }, x);
328 print_compare(fd_hess_L, hess_L);
331 if (opts.
hessians && te_problem.provides_eval_hess_ψ()) {
332 log <<
"Hessian verification: ∇²ψ(x)\n";
334 auto sparsity = te_problem.get_hess_ψ_sparsity();
335 sp::SparsityConverter<sp::Sparsity<config_t>, sp::Dense<config_t>> cvt{
338 auto eval_h = [&](rvec v) { te_problem.eval_hess_ψ(x, y, Σ, 1., v); };
339 cvt.convert_values(eval_h, hess_ψ.reshaped());
341 [&](crvec x, rvec g) {
342 te_problem.eval_grad_ψ(x, y, Σ, g, wn, wm);
345 print_compare(fd_hess_ψ, hess_ψ);