Skip to content

Commit 4c2f18d

Browse files
fabinschjcarpent
authored andcommitted
unittest: set sparse_backend to MatrixFree in solve method
1 parent 3c30bb0 commit 4c2f18d

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

test/src/sparse_qp_solve.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,78 @@ DOCTEST_TEST_CASE(
216216
}
217217
}
218218

219+
DOCTEST_TEST_CASE(
220+
"sparse random strongly convex qp with equality and "
221+
"inequality constraints: test setting specific sparse backend")
222+
{
223+
224+
std::cout
225+
<< "---testing sparse random strongly convex qp with equality and "
226+
"inequality constraints: test setting specific sparse backend ---"
227+
<< std::endl;
228+
for (auto const& dims : { // proxsuite::linalg::veg::tuplify(50, 0, 0),
229+
// proxsuite::linalg::veg::tuplify(50, 25, 0),
230+
// proxsuite::linalg::veg::tuplify(10, 0, 10),
231+
// proxsuite::linalg::veg::tuplify(50, 0, 25),
232+
// proxsuite::linalg::veg::tuplify(50, 10, 25),
233+
proxsuite::linalg::veg::tuplify(10, 3, 2) }) {
234+
VEG_BIND(auto const&, (n, n_eq, n_in), dims);
235+
236+
double eps_abs = 1.e-9;
237+
T sparsity_factor = 0.15;
238+
T strong_convexity_factor = 0.01;
239+
::proxsuite::proxqp::utils::rand::set_seed(1);
240+
proxqp::sparse::SparseModel<T> qp = utils::sparse_strongly_convex_qp(
241+
n, n_eq, n_in, sparsity_factor, strong_convexity_factor);
242+
proxsuite::proxqp::InitialGuessStatus initial_guess =
243+
proxsuite::proxqp::InitialGuessStatus::NO_INITIAL_GUESS;
244+
proxsuite::proxqp::SparseBackend sparse_backend =
245+
proxsuite::proxqp::SparseBackend::MatrixFree;
246+
proxsuite::proxqp::Results<T> results =
247+
proxsuite::proxqp::sparse::solve<T, I>(qp.H,
248+
qp.g,
249+
qp.A,
250+
qp.b,
251+
qp.C,
252+
qp.l,
253+
qp.u,
254+
nullopt,
255+
nullopt,
256+
nullopt,
257+
eps_abs,
258+
nullopt,
259+
nullopt,
260+
nullopt,
261+
nullopt,
262+
nullopt,
263+
true,
264+
true,
265+
nullopt,
266+
initial_guess,
267+
sparse_backend);
268+
T dua_res = proxqp::dense::infty_norm(
269+
qp.H.selfadjointView<Eigen::Upper>() * results.x + qp.g +
270+
qp.A.transpose() * results.y + qp.C.transpose() * results.z);
271+
T pri_res =
272+
std::max(proxqp::dense::infty_norm(qp.A * results.x - qp.b),
273+
proxqp::dense::infty_norm(
274+
sparse::detail::positive_part(qp.C * results.x - qp.u) +
275+
sparse::detail::negative_part(qp.C * results.x - qp.l)));
276+
DOCTEST_CHECK(pri_res <= eps_abs);
277+
DOCTEST_CHECK(dua_res <= eps_abs);
278+
DOCTEST_CHECK(results.info.sparse_backend == SparseBackend::MatrixFree);
279+
280+
std::cout << "------using API solving qp with dim: " << n
281+
<< " neq: " << n_eq << " nin: " << n_in << std::endl;
282+
std::cout << "primal residual: " << pri_res << std::endl;
283+
std::cout << "dual residual: " << dua_res << std::endl;
284+
std::cout << "total number of iteration: " << results.info.iter
285+
<< std::endl;
286+
std::cout << "setup timing " << results.info.setup_time << " solve time "
287+
<< results.info.solve_time << std::endl;
288+
}
289+
}
290+
219291
DOCTEST_TEST_CASE("sparse random strongly convex qp with equality and "
220292
"inequality constraints: test warm starting")
221293
{

test/src/sparse_qp_solve.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,47 @@ def test_case_different_no_initial_guess(self):
291291
)
292292
)
293293

294+
def test_case_different_matrix_free_sparse_backend(self):
295+
print(
296+
"------------------------sparse random strongly convex qp with equality and inequality constraints: test setting matrix free backend"
297+
)
298+
n = 10
299+
H, g, A, b, C, u, l = generate_mixed_qp(n)
300+
n_eq = A.shape[0]
301+
n_in = C.shape[0]
302+
results = proxsuite.proxqp.sparse.solve(
303+
H=H,
304+
g=np.asfortranarray(g),
305+
A=A,
306+
b=np.asfortranarray(b),
307+
C=C,
308+
l=np.asfortranarray(l),
309+
u=np.asfortranarray(u),
310+
eps_abs=1.0e-9,
311+
verbose=false,
312+
sparse_backend=proxsuite.proxqp.SparseBackend.MatrixFree,
313+
)
314+
dua_res = normInf(
315+
H @ results.x + g + A.transpose() @ results.y + C.transpose() @ results.z
316+
)
317+
pri_res = max(
318+
normInf(A @ results.x - b),
319+
normInf(
320+
np.maximum(C @ results.x - u, 0) + np.minimum(C @ results.x - l, 0)
321+
),
322+
)
323+
assert dua_res <= 1e-9
324+
assert pri_res <= 1e-9
325+
assert results.info.sparse_backend == proxsuite.proxqp.SparseBackend.MatrixFree
326+
print("--n = {} ; n_eq = {} ; n_in = {}".format(n, n_eq, n_in))
327+
print("dual residual = {} ; primal residual = {}".format(dua_res, pri_res))
328+
print("total number of iteration: {}".format(results.info.iter))
329+
print(
330+
"setup timing = {} ; solve time = {}".format(
331+
results.info.setup_time, results.info.solve_time
332+
)
333+
)
334+
294335
def test_sparse_problem_with_exact_solution_known(self):
295336
print(
296337
"------------------------sparse random strongly convex qp with inequality constraints and exact solution known"

0 commit comments

Comments
 (0)