51#define CUTEST_csjp FUNDERSCORE(cutest_csjp)
52void CUTEST_csjp(integer *status, integer *nnzj,
const integer *lj,
53 integer *J_var, integer *J_con);
57#define LOAD_DL_FUNC(f) dlfun<decltype(f)>(STR(f))
59using namespace std::string_literals;
62void throw_error(std::string s,
int code) {
63 throw std::runtime_error(s +
" (" + std::to_string(code) +
")");
65void throw_if_error(std::string s,
int code) {
69void log_if_error(std::string s,
int code) {
71 std::cerr << s <<
" (" << code <<
")\n";
74std::shared_ptr<void> load_lib(
const char *so_filename) {
77 void *h = ::dlopen(so_filename, RTLD_LOCAL | RTLD_NOW);
78 if (
auto *err = ::dlerror())
79 throw std::runtime_error(err);
80 return std::shared_ptr<void>{h, &::dlclose};
96 [func{std::forward<F>(func)}](
void *) { func(); }};
103 fptr_open(&
funit, outsdif_fname, &status);
104 throw_if_error(
"Failed to open "s + outsdif_fname, status);
107 fptr_close(&
funit, &status);
108 log_if_error(
"Failed to close OUTSDIF.d file", status);
114 return cleanup([fptr_cterminate] {
116 fptr_cterminate(&status);
117 log_if_error(
"Failed to call cutest_cterminate", status);
133 throw_if_error(
"Failed to call cutest_cdimen", status);
137 decltype(CUTEST_cfn) *
cfn;
147 decltype(CUTEST_cdh) *
cdh;
150 decltype(CUTEST_csh) *
csh;
172 &e_order, &l_order, &v_order);
173 throw_if_error(
"Failed to call cutest_csetup", status);
176 throw std::runtime_error(
177 "Unconstrained CUTEst problems are currently unsupported");
207 std::string name(FSTRING_LEN,
'0');
210 throw_if_error(
"Failed to call CUTEST_probname", status);
211 name.resize(name.find_last_not_of(
' '));
219 fptr_report(&status, calls, time);
226 auto res =
reinterpret_cast<T *
>(::dlsym(
so_handle.get(), name));
227 if (
const char *error = dlerror())
228 throw std::runtime_error(error);
254 impl = std::make_unique<CUTEstLoader>(so_fname, outsdif_fname);
255 name = impl->get_name();
256 resize(
static_cast<length_t>(impl->nvar),
260 impl->setup_problem(x0, y0, C, D);
266CUTEstProblem &CUTEstProblem::operator=(CUTEstProblem &&) noexcept = default;
267CUTEstProblem::~CUTEstProblem() = default;
273 using stat_t =
decltype(r.
status);
274 r.
status =
static_cast<stat_t
>(impl->get_report(calls, time));
275 r.
name = impl->get_name();
278 r.
calls.objective =
static_cast<unsigned>(calls[0]);
279 r.
calls.objective_grad =
static_cast<unsigned>(calls[1]);
280 r.
calls.objective_hess =
static_cast<unsigned>(calls[2]);
281 r.
calls.hessian_times_vector =
static_cast<unsigned>(calls[3]);
282 r.
calls.constraints = impl->
ncon > 0 ?
static_cast<unsigned>(calls[4]) : 0;
283 r.
calls.constraints_grad =
284 impl->
ncon > 0 ?
static_cast<unsigned>(calls[5]) : 0;
285 r.
calls.constraints_hess =
286 impl->
ncon > 0 ?
static_cast<unsigned>(calls[6]) : 0;
293 assert(x.size() ==
static_cast<length_t>(impl->nvar));
296 logical grad = FALSE_;
297 impl->funcs.cofg(&status, &impl->nvar, x.data(), &f,
nullptr, &grad);
298 throw_if_error(
"eval_f: CUTEST_cofg", status);
303 assert(grad_fx.size() ==
static_cast<length_t>(
impl->nvar));
306 logical grad = TRUE_;
307 impl->funcs.cofg(&status, &
impl->nvar, x.data(), &f, grad_fx.data(), &grad);
308 throw_if_error(
"eval_grad_f: CUTEST_cofg", status);
314 logical jtrans = TRUE_, grad = FALSE_;
316 impl->funcs.ccfg(&status, &
impl->nvar, &
impl->ncon, x.data(), gx.data(),
317 &jtrans, &zero, &zero,
nullptr, &grad);
318 throw_if_error(
"eval_g: CUTEST_ccfg", status);
323 assert(grad_gxy.size() ==
static_cast<length_t>(
impl->nvar));
325 auto lvector =
static_cast<integer
>(y.size()),
326 lresult =
static_cast<integer
>(grad_gxy.size());
327 logical gotj = FALSE_, jtrans = TRUE_;
328 impl->funcs.cjprod(&status, &
impl->nvar, &
impl->ncon, &gotj, &jtrans,
329 x.data(), y.data(), &lvector, grad_gxy.data(), &lresult);
330 throw_if_error(
"eval_grad_g_prod: CUTEST_cjprod", status);
335 rvec J_values)
const {
337 if (J_values.size() > 0) {
346 assert(outer_ptr.size() ==
static_cast<length_t>(
impl->nvar + 1));
347 const integer nnz =
nnz_J;
348 logical grad = TRUE_;
349 impl->funcs.ccfsg(&status, &
impl->nvar, &
impl->ncon, x.data(),
352 throw_if_error(
"eval_jac_g: CUTEST_ccfsg", status);
353 auto t0 = std::chrono::steady_clock::now();
355 auto t1 = std::chrono::steady_clock::now();
356 std::cout <<
"Permutation of J took: "
357 << std::chrono::duration<double>{t1 - t0}.count() * 1e6
362 assert(J_values.size() ==
static_cast<length_t>(
impl->nvar) *
365 logical jtrans = FALSE_, grad = TRUE_;
366 impl->funcs.ccfg(&status, &
impl->nvar, &
impl->ncon, x.data(),
367 impl->work.data(), &jtrans, &
impl->ncon,
368 &
impl->nvar, J_values.data(), &grad);
369 throw_if_error(
"eval_jac_g: CUTEST_ccfg", status);
376 assert(outer_ptr.size() ==
static_cast<length_t>(
impl->nvar + 1));
378 const integer nnz =
nnz_J;
380 throw_if_error(
"eval_jac_g: CUTEST_csjp", status);
383 util::convert_triplets_to_ccs<config_t>(
J_row,
J_col, inner_idx,
393 throw_if_error(
"get_jac_g_num_nonzeros: CUTEST_cdimsj", status);
405 assert(grad_gi.size() ==
static_cast<length_t>(
impl->nvar));
407 auto iprob =
static_cast<integer
>(i + 1);
408 impl->funcs.cigr(&status, &
impl->nvar, &iprob, x.data(), grad_gi.data());
409 throw_if_error(
"eval_grad_gi: CUTEST_cigr", status);
417 const auto *mult = y.data();
420 mult =
impl->work.data();
423 logical goth = FALSE_;
424 impl->funcs.chprod(&status, &
impl->nvar, &
impl->ncon, &goth, x.data(), mult,
425 const_cast<real_t *
>(v.data()), Hv.data());
426 throw_if_error(
"eval_hess_L_prod: CUTEST_chprod", status);
432 rvec H_values)
const {
434 if (H_values.size() > 0) {
437 const auto *mult = y.data();
440 mult =
impl->work.data();
449 assert(outer_ptr.size() ==
static_cast<length_t>(
impl->nvar + 1));
450 const integer nnz =
nnz_H;
451 impl->funcs.csh(&status, &
impl->nvar, &
impl->ncon, x.data(),
454 throw_if_error(
"eval_hess_L: CUTEST_csh", status);
455 auto t0 = std::chrono::steady_clock::now();
457 auto t1 = std::chrono::steady_clock::now();
458 std::cout <<
"Permutation of H took: "
459 << std::chrono::duration<double>{t1 - t0}.count() * 1e6
464 assert(H_values.size() ==
static_cast<length_t>(
impl->nvar) *
466 impl->funcs.cdh(&status, &
impl->nvar, &
impl->ncon, x.data(), mult,
467 &
impl->nvar, H_values.data());
468 throw_if_error(
"eval_hess_L: CUTEST_cdh", status);
477 assert(outer_ptr.size() ==
static_cast<length_t>(
impl->nvar + 1));
479 const integer nnz =
nnz_H;
482 throw_if_error(
"eval_hess_L: CUTEST_cshp", status);
485 util::convert_triplets_to_ccs<config_t>(
H_row,
H_col, inner_idx,
495 throw_if_error(
"get_hess_L_num_nonzeros: CUTEST_cdimsh", status);
505 assert(x.size() ==
static_cast<length_t>(impl->nvar));
506 assert(grad_fx.size() ==
static_cast<length_t>(impl->nvar));
509 logical grad = TRUE_;
510 impl->funcs.cofg(&status, &impl->nvar, x.data(), &f, grad_fx.data(), &grad);
511 throw_if_error(
"eval_f_grad_f: CUTEST_cofg", status);
515 assert(x.size() ==
static_cast<length_t>(impl->nvar));
516 assert(g.size() ==
static_cast<length_t>(impl->ncon));
519 impl->funcs.cfn(&status, &impl->nvar, &impl->ncon, x.data(), &f, g.data());
520 throw_if_error(
"eval_f_g: CUTEST_cfn", status);
526 assert(grad_L.size() ==
static_cast<length_t>(
impl->nvar));
529 logical grad = TRUE_;
530 impl->funcs.clfg(&status, &
impl->nvar, &
impl->ncon, x.data(), y.data(), &L,
531 grad_L.data(), &grad);
532 throw_if_error(
"eval_f_g: CUTEST_clfg", status);
538 case Status::Success:
return "Success";
539 case Status::AllocationError:
return "AllocationError";
540 case Status::ArrayBoundError:
return "ArrayBoundError";
541 case Status::EvaluationError:
return "EvaluationError";
544 throw std::out_of_range(
545 "invalid value for pa::CUTEstProblem::Report::Status");
553 os <<
"CUTEst problem: " << r.
name <<
"\r\n\n"
554 <<
"Number of variables: " << r.
nvar <<
"\r\n"
555 <<
"Number of constraints: " << r.
ncon <<
"\r\n\n"
556 <<
"Status: " << r.
status <<
" (" << +r.
status <<
")\r\n\n"
557 <<
"Objective function evaluations: " << r.
calls.objective
559 <<
"Objective function gradient evaluations: "
560 << r.
calls.objective_grad <<
"\r\n"
561 <<
"Objective function Hessian evaluations: "
562 << r.
calls.objective_hess <<
"\r\n"
563 <<
"Hessian times vector products: "
564 << r.
calls.objective_hess <<
"\r\n\n";
566 os <<
"Constraint function evaluations: "
567 << r.
calls.constraints <<
"\r\n"
568 <<
"Constraint function gradients evaluations: "
569 << r.
calls.constraints_grad <<
"\r\n"
570 <<
"Constraint function Hessian evaluations: "
571 << r.
calls.constraints_hess <<
"\r\n\n";
573 return os <<
"Setup time: " << r.
time_setup <<
"s\r\n"
574 <<
"Time since setup: " << r.
time <<
"s";
Implements common problem functions for minimization problems with box constraints.
cleanup_t cutest_terminate
Responsible for calling CUTEST_xterminate.
decltype(CUTEST_cigr) * cigr
decltype(CUTEST_ccfg) * ccfg
decltype(CUTEST_csh) * csh
integer ncon
Number of constraints.
decltype(CUTEST_cjprod) * cjprod
std::shared_ptr< void > cleanup_t
cleanup_t cleanup_outsdif
Responsible for closing the OUTSDIF.d file.
decltype(CUTEST_cofg) * cofg
decltype(CUTEST_chprod) * chprod
CUTEstLoader(const char *so_fname, const char *outsdif_fname)
decltype(CUTEST_ccifg) * ccifg
integer iout
Fortran Unit Number for standard output.
integer get_report(doublereal *calls, doublereal *time)
integer funit
Fortran Unit Number for OUTSDIF.d file.
decltype(CUTEST_clfg) * clfg
cleanup_t load_outsdif(const char *outsdif_fname)
decltype(CUTEST_cdimsj) * cdimsj
cleanup_t cleanup(F &&func)
integer nvar
Number of decision variabls.
T * dlfun(const char *name)
std::shared_ptr< void > so_handle
dlopen handle to shared library
decltype(CUTEST_cdimsh) * cdimsh
decltype(CUTEST_cdh) * cdh
Eigen::VectorX< logical > logical_vec
Pointers to loaded problem functions.
decltype(FUNDERSCORE(cutest_csjp)) * csjp
decltype(CUTEST_ccfsg) * ccfsg
void setup_problem(rvec x0, rvec y0, Box &C, Box &D)
logical_vec equatn
whether the constraint is an equality
decltype(CUTEST_cshp) * cshp
decltype(CUTEST_cfn) * cfn
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.
void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const
real_t eval_f_g(crvec x, rvec g) const
CUTEstProblem(const char *so_fname, const char *outsdif_fname, bool sparse=false)
Load a CUTEst problem from the given shared library and OUTSDIF.d file.
void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const
Eigen::VectorX< int > J_row
Eigen::VectorX< int > J_col
Eigen::VectorX< int > H_col
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
util::copyable_unique_ptr< class CUTEstLoader > impl
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
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
Eigen::VectorX< int > H_row
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
length_t get_jac_g_num_nonzeros() const
CUTEstProblem & operator=(const CUTEstProblem &)
void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const
length_t get_hess_L_num_nonzeros() const
#define USING_ALPAQA_CONFIG(Conf)
std::ostream & operator<<(std::ostream &os, PANOCStopCrit s)
typename Conf::real_t real_t
typename Conf::rindexvec rindexvec
typename Conf::index_t index_t
typename Conf::length_t length_t
typename Conf::crvec crvec
constexpr const char * enum_name(PANOCStopCrit s)
The report generated by CUTEst.
int ncon
Number of constraints.
double time_setup
CPU time (in seconds) for CUTEST_csetup.
enum alpaqa::CUTEstProblem::Report::Status status
Exit status.
int nvar
Number of independent variables.
Status
Status returned by CUTEst.
struct alpaqa::CUTEstProblem::Report::@0 calls
Function call counters.
std::string name
Name of the problem.
double time
CPU time (in seconds) since the end of CUTEST_csetup.