112int main(
int argc,
const char *argv[])
try {
120 std::span args{argv,
static_cast<size_t>(argc)};
121 Options opts{argc - 2, argv + 2};
124 std::ostream &os = std::cout;
130 os <<
"Loading problem " << prob_path << std::endl;
131 auto problem =
load_problem(prob_type, prob_path.parent_path(),
132 prob_path.filename(), opts);
133 os <<
"Loaded problem " << problem.path.stem().c_str() <<
" from "
134 << problem.path <<
"\nnvar: " << problem.problem.get_n()
135 <<
"\nncon: " << problem.problem.get_m() <<
"\nProvided functions:\n";
140 auto has_opt = [&opts](std::string_view o) {
141 auto o_it = std::ranges::find(opts.options(), o);
142 if (o_it == opts.options().end())
144 auto index =
static_cast<size_t>(o_it - opts.options().begin());
145 ++opts.used()[index];
150 .hessians = !has_opt(
"--no-hessians"),
154 auto seed =
static_cast<unsigned int>(std::time(
nullptr));
159 auto used = opts.used();
160 auto unused_opt = std::ranges::find(used,
false);
161 auto unused_idx =
static_cast<size_t>(unused_opt - used.begin());
162 if (unused_opt != used.end())
163 throw std::invalid_argument(
"Unused option: " +
164 std::string(opts.options()[unused_idx]));
169}
catch (std::exception &e) {
171 << e.what() << std::endl;
193 const auto n = x.size();
194 std::vector<Eigen::Triplet<real_t, index_t>> coo;
195 vec h = vec::Zero(n);
197 const auto δ = 1e-2 * ε;
198 vec grad_x(n), grad_xh(n);
200 for (index_t i = 0; i < n; ++i) {
201 real_t hh = std::abs(x(i)) * ε > δ ? x(i) * ε : δ;
203 grad_L(x + h, grad_xh);
204 grad_xh = (grad_xh - grad_x) / hh;
205 for (index_t j = 0; j < n; ++j)
206 if (real_t v = grad_xh(j); v != 0)
207 coo.emplace_back(std::min(j, i), std::max(i, j),
208 v * (i == j ? 1 : 0.5));
211 Eigen::SparseMatrix<real_t, 0, index_t> hess(n, n);
212 hess.setFromTriplets(coo.begin(), coo.end());
236 auto &te_problem = lproblem.
problem;
241 auto n = te_problem.get_n(), m = te_problem.get_m();
243 std::srand(
static_cast<unsigned int>(std::time(
nullptr)));
244 vec Σ = 1.5 * vec::Random(m).array() + 2;
245 vec y = y0 + y0.norm() * vec::Random(m);
246 vec x = x0 + sc * vec::Random(n);
247 vec v = 5e-6 * sc * vec::Random(n);
251 auto print_compare = [&log, &opts](
const auto &fd,
const auto &ad) {
252 auto abs_err = (fd - ad).reshaped().template lpNorm<Eigen::Infinity>();
254 abs_err / fd.reshaped().template lpNorm<Eigen::Infinity>();
263 auto f = [&](crvec x) {
return te_problem.eval_f(x); };
264 log <<
"Gradient verification: ∇f(x)\n";
267 te_problem.eval_grad_f(x, grad_f);
268 print_compare(fd_grad_f, grad_f);
270 log <<
"Gradient verification: ∇L(x)\n";
271 auto L = [&](crvec x) {
272 te_problem.eval_g(x, gx);
273 return te_problem.eval_f(x) + gx.dot(y);
277 te_problem.eval_grad_L(x, y, grad_L, wn);
278 print_compare(fd_grad_L, grad_L);
280 log <<
"Gradient verification: ∇ψ(x)\n";
281 auto ψ = [&](crvec x) {
return te_problem.eval_ψ(x, y, Σ, wm); };
284 te_problem.eval_grad_ψ(x, y, Σ, grad_ψ, wn, wm);
285 print_compare(fd_grad_ψ, grad_ψ);
287 if (te_problem.provides_eval_hess_ψ_prod()) {
288 log <<
"Hessian product verification: ∇²ψ(x)\n";
291 te_problem.eval_grad_ψ(xv, y, Σ, grad_ψv, wn, wm);
292 vec fd_hess_ψv = grad_ψv - grad_ψ;
294 te_problem.eval_hess_ψ_prod(x, y, Σ, 1, v, hess_ψv);
295 print_compare(fd_hess_ψv, hess_ψv);
298 if (opts.
hessians && te_problem.provides_eval_hess_L()) {
299 log <<
"Hessian verification: ∇²L(x)\n";
301 auto sparsity = te_problem.get_hess_L_sparsity();
302 sp::SparsityConverter<sp::Sparsity<config_t>, sp::Dense<config_t>> cvt{
304 vec hess_L_nzs(get_nnz(sparsity));
306 te_problem.eval_hess_L(x, y, 1., hess_L_nzs);
307 cvt.convert_values(hess_L_nzs, hess_L.reshaped());
309 [&](crvec x, rvec g) { te_problem.eval_grad_L(x, y, g, wn); }, x);
310 print_compare(fd_hess_L, hess_L);
313 if (opts.
hessians && te_problem.provides_eval_hess_ψ()) {
314 log <<
"Hessian verification: ∇²L(x)\n";
316 auto sparsity = te_problem.get_hess_ψ_sparsity();
317 sp::SparsityConverter<sp::Sparsity<config_t>, sp::Dense<config_t>> cvt{
319 vec hess_ψ_nzs(get_nnz(sparsity));
321 te_problem.eval_hess_ψ(x, y, Σ, 1., hess_ψ_nzs);
322 cvt.convert_values(hess_ψ_nzs, hess_ψ.reshaped());
324 [&](crvec x, rvec g) {
325 te_problem.eval_grad_ψ(x, y, Σ, g, wn, wm);
328 print_compare(fd_hess_ψ, hess_ψ);