CUTEst loader

In this example, we load a CUTEst problem using the alpaqa.CUTEstProblem class. These problems can be used to run benchmarks.

 1import contextlib
 2import gc
 3import os
 4from pathlib import Path
 5
 6import numpy as np
 7
 8import alpaqa as pa
 9
10# Path containing the compiled CUTEst problems
11cutest_dir = Path(os.getenv("HOME")) / "opt" / "CUTEst" / "QP"
12problem_name = "CBS"
13
14# alpaqa currently only supports one instance of a CUTEst problem at a time 🙃
15with contextlib.suppress(NameError):
16    del prob  # noqa: F821
17gc.collect()
18# Load problem
19prob = pa.CUTEstProblem(str(cutest_dir / problem_name), sparse=True)
20
21# Extract the problem data
22n = prob.num_variables
23m = prob.num_constraints
24Q, Q_sym = prob.eval_lagrangian_hessian(np.zeros(n), np.zeros(m))
25c, q = prob.eval_objective_and_gradient(np.zeros(n))
26x_lb = prob.variable_bounds.lower
27x_ub = prob.variable_bounds.upper
28A, A_sym = prob.eval_constraints_jacobian(np.zeros(n))
29g = prob.eval_constraints(np.zeros(n))
30g_lb = prob.general_bounds.lower - g
31g_ub = prob.general_bounds.upper - g
32
33# You could now pass these matrices to a QP solver, for example.
34
35# Do note the symmetry of the sparse matrices:
36# - Q will usually only contain the elements in the upper triangular
37#   (as indicated by Q_sym).
38# - A will usually be unsymmetric, and store all its elements explicitly
39#   (as indicated by A_sym).
40print(n, m)
41print(A_sym)
42print(A.nnz)
43print(Q_sym)
44print(Q.nnz)