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);
53 return std::shared_ptr<void>{h, &::dlclose};
76 auto load() -> F::signature_t * {
80 template <
class F,
class...
Args>
87 if (!std::filesystem::is_regular_file(p))
88 throw std::invalid_argument(
"CUTEstLoader: OUTSDIF path does not "
89 "exist or is not a regular file: \"" +
98 log_if_error(
"Failed to close OUTSDIF.d file", status);
107 log_if_error(
"Failed to call cutest_cterminate", status);
127 throw_if_error(
"Failed to call cutest_cdimen", status);
170 throw_if_error(
"Failed to call cutest_csetup", status);
175 throw std::runtime_error(
176 "Unconstrained CUTEst problems are currently unsupported");
210 throw_if_error(
"Failed to call CUTEST_probname", status);
211 if (
auto last = name.find_last_not_of(
' ');
last != name.npos)
212 name.resize(
last + 1);
220 throw_if_error(
"Failed to call CUTEST_creport", status);
223 throw_if_error(
"Failed to call CUTEST_ureport", status);
250 resize(
static_cast<length_t>(impl->nvar),
254 impl->setup_problem(x0, y0, C, D);
255 name = impl->get_name();
262CUTEstProblem::~CUTEstProblem() =
default;
267 impl->get_report(calls, time);
268 const bool constr = impl->ncon > 0;
271 .
objective =
static_cast<unsigned>(calls[0]),
272 .objective_grad =
static_cast<unsigned>(calls[1]),
273 .objective_hess =
static_cast<unsigned>(calls[2]),
274 .hessian_times_vector =
static_cast<unsigned>(calls[3]),
275 .constraints = constr ?
static_cast<unsigned>(calls[4]) : 0,
276 .constraints_grad = constr ?
static_cast<unsigned>(calls[5]) : 0,
277 .constraints_hess = constr ?
static_cast<unsigned>(calls[6]) : 0,
279 .time_setup = time[0],
288 checked(impl->funcs.cofg,
"eval_f: CUTEST_cofg")(&impl->nvar, x.data(), &f,
297 checked(
impl->funcs.cofg,
"eval_grad_f: CUTEST_cofg")(
305 checked(
impl->funcs.ccfg,
"eval_g: CUTEST_ccfg")(
316 checked(
impl->funcs.cjprod,
"eval_grad_g_prod: CUTEST_cjprod")(
332 checked(
impl->funcs.ccfsg,
"eval_jac_g: CUTEST_ccfsg")(
342 checked(
impl->funcs.ccfg,
"eval_jac_g: CUTEST_ccfg")(
355 checked(
impl->funcs.cdimsj,
356 "get_jac_g_sparsity: CUTEST_cdimsj")(&
nnz_J);
362 checked(
impl->funcs.csjp,
"eval_jac_g: CUTEST_csjp")(
372 .order = SparseCOO::Unsorted,
380 checked(
impl->funcs.cigr,
"eval_grad_gi: CUTEST_cigr")(
381 &
impl->nvar, &
iprob, x.data(), grad_gi.data());
389 const auto *
mult = y.data();
395 checked(
impl->funcs.chprod,
"eval_hess_L_prod: CUTEST_chprod")(
397 const_cast<real_t *
>(
v.data()),
Hv.data());
408 auto &&
ζ =
impl->work.topRows(
impl->ncon);
409 auto &&ŷ =
impl->work2.topRows(
impl->ncon);
412 ζ += Σ.asDiagonal().inverse() * y;
416 ŷ.array() *= Σ.array();
429 checked(
impl->funcs.cjprod,
"eval_hess_ψ_prod: CUTEST_cjprod-1")(
433 Jv.array() *=
ζ.array();
438 checked(
impl->funcs.cjprod,
"eval_hess_ψ_prod: CUTEST_cjprod-2")(
448 const auto *
mult = y.data();
460 checked(
impl->funcs.csh,
"eval_hess_L: CUTEST_csh")(
469 checked(
impl->funcs.cdh,
470 "eval_hess_L: CUTEST_cdh")(&
impl->nvar, &
impl->ncon, x.data(),
484 checked(
impl->funcs.cdimsh,
485 "get_hess_L_sparsity: CUTEST_cdimsh")(&
nnz_H);
490 checked(
impl->funcs.cshp,
"eval_hess_L: CUTEST_cshp")(
501 .order = SparseCOO::Unsorted,
510 checked(impl->funcs.cofg,
"eval_f_grad_f: CUTEST_cofg")(
518 checked(impl->funcs.cfn,
"eval_f_g: CUTEST_cfn")(&impl->nvar, &impl->ncon,
519 x.data(), &f, g.data());
528 checked(
impl->funcs.clfg,
"eval_f_g: CUTEST_clfg")(
529 &
impl->nvar, &
impl->ncon, x.data(), y.data(), &L, grad_L.data(), &
grad);
534 os <<
"CUTEst problem: " <<
name <<
"\r\n\n"
535 <<
"Number of variables: " <<
n <<
"\r\n"
536 <<
"Number of constraints: " <<
m <<
"\r\n\n"
537 <<
"Objective function evaluations: "
539 <<
"Objective function gradient evaluations: "
541 <<
"Objective function Hessian evaluations: "
543 <<
"Hessian times vector products: "
546 os <<
"Constraint function evaluations: "
548 <<
"Constraint function gradients evaluations: "
550 <<
"Constraint function Hessian evaluations: "
553 return os <<
"Setup time: " << r.
time_setup <<
"s\r\n"
554 <<
"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.