21using namespace std::string_literals;
34 std::cerr << s <<
" (" << code <<
")\n";
37auto checked(F &&func, std::string_view msg) {
38 return [msg, func{std::forward<F>(func)}]<
class... Args>(
39 Args &&...args)
mutable {
41 std::forward<F>(func)(&status, std::forward<Args>(args)...);
46std::shared_ptr<void>
load_lib(
const char *so_filename) {
49 void *h = ::dlopen(so_filename, RTLD_LOCAL | RTLD_NOW);
50 if (
auto *err = ::dlerror())
51 throw std::runtime_error(err);
54 return std::shared_ptr<void>{h, +[](
void *) {}};
56 return std::shared_ptr<void>{h, &::dlclose};
80 auto load() -> F::signature_t * {
84 template <
class F,
class...
Args>
91 if (!std::filesystem::is_regular_file(p))
92 throw std::invalid_argument(
"CUTEstLoader: OUTSDIF path does not "
93 "exist or is not a regular file: \"" +
102 log_if_error(
"Failed to close OUTSDIF.d file", status);
111 log_if_error(
"Failed to call cutest_cterminate", status);
131 throw_if_error(
"Failed to call cutest_cdimen", status);
174 throw_if_error(
"Failed to call cutest_csetup", status);
179 throw std::runtime_error(
180 "Unconstrained CUTEst problems are currently unsupported");
214 throw_if_error(
"Failed to call CUTEST_probname", status);
215 if (
auto last = name.find_last_not_of(
' ');
last != name.npos)
216 name.resize(
last + 1);
224 throw_if_error(
"Failed to call CUTEST_creport", status);
227 throw_if_error(
"Failed to call CUTEST_ureport", status);
254 resize(
static_cast<length_t>(impl->nvar),
258 impl->setup_problem(x0, y0, C, D);
259 name = impl->get_name();
266CUTEstProblem::~CUTEstProblem() =
default;
271 impl->get_report(calls, time);
272 const bool constr = impl->ncon > 0;
275 .
objective =
static_cast<unsigned>(calls[0]),
276 .objective_grad =
static_cast<unsigned>(calls[1]),
277 .objective_hess =
static_cast<unsigned>(calls[2]),
278 .hessian_times_vector =
static_cast<unsigned>(calls[3]),
279 .constraints = constr ?
static_cast<unsigned>(calls[4]) : 0,
280 .constraints_grad = constr ?
static_cast<unsigned>(calls[5]) : 0,
281 .constraints_hess = constr ?
static_cast<unsigned>(calls[6]) : 0,
283 .time_setup = time[0],
292 checked(impl->funcs.cofg,
"eval_f: CUTEST_cofg")(&impl->nvar, x.data(), &f,
301 checked(
impl->funcs.cofg,
"eval_grad_f: CUTEST_cofg")(
309 checked(
impl->funcs.ccfg,
"eval_g: CUTEST_ccfg")(
320 checked(
impl->funcs.cjprod,
"eval_grad_g_prod: CUTEST_cjprod")(
336 checked(
impl->funcs.ccfsg,
"eval_jac_g: CUTEST_ccfsg")(
346 checked(
impl->funcs.ccfg,
"eval_jac_g: CUTEST_ccfg")(
359 checked(
impl->funcs.cdimsj,
360 "get_jac_g_sparsity: CUTEST_cdimsj")(&
nnz_J);
366 checked(
impl->funcs.csjp,
"eval_jac_g: CUTEST_csjp")(
376 .order = SparseCOO::Unsorted,
384 checked(
impl->funcs.cigr,
"eval_grad_gi: CUTEST_cigr")(
385 &
impl->nvar, &
iprob, x.data(), grad_gi.data());
393 const auto *
mult = y.data();
399 checked(
impl->funcs.chprod,
"eval_hess_L_prod: CUTEST_chprod")(
401 const_cast<real_t *
>(
v.data()),
Hv.data());
412 auto &&
ζ =
impl->work.topRows(
impl->ncon);
413 auto &&ŷ =
impl->work2.topRows(
impl->ncon);
416 ζ += Σ.asDiagonal().inverse() * y;
420 ŷ.array() *= Σ.array();
433 checked(
impl->funcs.cjprod,
"eval_hess_ψ_prod: CUTEST_cjprod-1")(
437 Jv.array() *=
ζ.array();
442 checked(
impl->funcs.cjprod,
"eval_hess_ψ_prod: CUTEST_cjprod-2")(
452 const auto *
mult = y.data();
464 checked(
impl->funcs.csh,
"eval_hess_L: CUTEST_csh")(
473 checked(
impl->funcs.cdh,
474 "eval_hess_L: CUTEST_cdh")(&
impl->nvar, &
impl->ncon, x.data(),
488 checked(
impl->funcs.cdimsh,
489 "get_hess_L_sparsity: CUTEST_cdimsh")(&
nnz_H);
494 checked(
impl->funcs.cshp,
"eval_hess_L: CUTEST_cshp")(
505 .order = SparseCOO::Unsorted,
514 checked(impl->funcs.cofg,
"eval_f_grad_f: CUTEST_cofg")(
522 checked(impl->funcs.cfn,
"eval_f_g: CUTEST_cfn")(&impl->nvar, &impl->ncon,
523 x.data(), &f, g.data());
532 checked(
impl->funcs.clfg,
"eval_f_g: CUTEST_clfg")(
533 &
impl->nvar, &
impl->ncon, x.data(), y.data(), &L, grad_L.data(), &
grad);
538 os <<
"CUTEst problem: " <<
name <<
"\r\n\n"
539 <<
"Number of variables: " <<
n <<
"\r\n"
540 <<
"Number of constraints: " <<
m <<
"\r\n\n"
541 <<
"Objective function evaluations: "
543 <<
"Objective function gradient evaluations: "
545 <<
"Objective function Hessian evaluations: "
547 <<
"Hessian times vector products: "
550 os <<
"Constraint function evaluations: "
552 <<
"Constraint function gradients evaluations: "
554 <<
"Constraint function Hessian evaluations: "
557 return os <<
"Setup time: " << r.
time_setup <<
"s\r\n"
558 <<
"Time since setup: " << r.
time <<
"s";
Implements common problem functions for minimization problems with box constraints.
void eval_proj_diff_g(crvec z, rvec p) const
length_t m
Number of constraints, dimension of g(x) and z.
length_t n
Number of decision variables, dimension of x.
Box D
Other constraints, .
cleanup_t cutest_terminate
Responsible for calling CUTEST_xterminate.
cutest::ccifg::signature_t * ccifg
cutest::cigr::signature_t * cigr
cutest::cfn::signature_t * cfn
integer ncon
Number of constraints.
cutest::ccfsg::signature_t * ccfsg
std::shared_ptr< void > cleanup_t
cutest::csjp::signature_t * csjp
cleanup_t cleanup_outsdif
Responsible for closing the OUTSDIF.d file.
cutest::cdimsh::signature_t * cdimsh
CUTEstLoader(const char *so_fname, const char *outsdif_fname)
integer iout
Fortran Unit Number for standard output.
cutest::cdimsj::signature_t * cdimsj
integer funit
Fortran Unit Number for OUTSDIF.d file.
decltype(auto) call(Args &&...args)
cutest::csh::signature_t * csh
cutest::clfg::signature_t * clfg
cleanup_t load_outsdif(const char *outsdif_fname)
cutest::cofg::signature_t * cofg
cleanup_t cleanup(F &&func)
integer nvar
Number of decision variabls.
std::shared_ptr< void > so_handle
dlopen handle to shared library
cutest::cjprod::signature_t * cjprod
cutest::cdh::signature_t * cdh
Eigen::VectorX< logical > logical_vec
Pointers to loaded problem functions.
cutest::chprod::signature_t * chprod
void setup_problem(rvec x0, rvec y0, Box &C, Box &D)
logical_vec equatn
whether the constraint is an equality
cutest::cshp::signature_t * cshp
auto load() -> F::signature_t *
void get_report(double *calls, double *time)
cutest::ccfg::signature_t * ccfg
logical_vec linear
whether the constraint is linear
integer io_buffer
Fortran Unit Number for internal IO.
Wrapper for CUTEst problems loaded from an external shared library.
Eigen::VectorX< int > cols
void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const
Calls calls
Function call counters.
void eval_jac_g(crvec x, rvec J_values) const
unsigned constraints_grad
Number of calls to the constraint gradients.
Sparsity get_jac_g_sparsity() const
real_t eval_f_g(crvec x, rvec g) const
double time_setup
CPU time (in seconds) for CUTEST_csetup.
Sparsity get_hess_L_sparsity() const
unsigned objective
Number of calls to the objective function.
struct alpaqa::CUTEstProblem::SparseStorage storage_hess_L
void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const
CUTEstProblem(const char *so_fname, const char *outsdif_fname=nullptr, bool sparse=false)
Load a CUTEst problem from the given shared library and OUTSDIF.d file.
unsigned constraints
Number of calls to the constraint functions.
std::string name
Problem name.
Eigen::VectorX< int > rows
util::copyable_unique_ptr< class CUTEstLoader > impl
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
double time
CPU time (in seconds) since the end of CUTEST_csetup.
void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const
void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
void eval_grad_f(crvec x, rvec grad_fx) const
real_t eval_f(crvec x) const
void eval_g(crvec x, rvec gx) const
unsigned objective_hess
Number of calls to the objective Hessian.
struct alpaqa::CUTEstProblem::SparseStorage storage_jac_g
std::ostream & format_report(std::ostream &os, const Report &r) const
void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const
unsigned constraints_hess
Number of calls to the constraint Hessians.
CUTEstProblem & operator=(const CUTEstProblem &)
unsigned objective_grad
Number of calls to the objective gradient.
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
The report generated by CUTEst.
#define USING_ALPAQA_CONFIG(Conf)
constexpr size_t fstring_len
@ Unsymmetric
No symmetry.
@ Upper
Symmetric, upper-triangular part is stored.
typename Conf::real_t real_t
typename Conf::index_t index_t
typename Conf::length_t length_t
typename Conf::crvec crvec
std::shared_ptr< void > load_lib(const char *so_filename)
auto checked(F &&func, std::string_view msg)
void throw_error(std::string_view s, int code)
void throw_if_error(std::string_view s, int code)
void log_if_error(std::string_view s, int code)
Sparse coordinate list structure (COO).
Stores any of the supported sparsity patterns.