QPALM main
Proximal Augmented Lagrangian method for Quadratic Programs
examples/python/qpalm_python_demo.py
1import qpalm as qp
2import numpy as np
3import scipy.sparse as sp
4
5data = qp.QPALMData(3, 4)
6
7row = np.array([0, 0, 1, 1])
8col = np.array([0, 1, 0, 1])
9values = np.array([1, -1, -1, 2])
10data.Q = sp.csc_matrix((values, (row, col)), shape=(3, 3))
11
12data.q = np.array([-2, -6, 1])
13data.bmin = np.array([0.5, -10, -10, -10])
14data.bmax = np.array([0.5, 10, 10, 10])
15
16row = np.array([0, 1, 0, 2, 0, 3])
17col = np.array([0, 0, 1, 1, 2, 2])
18values = np.array([1, 1, 1, 1, 1, 1])
19data.A = sp.csc_matrix((values, (row, col)), shape=(4, 3))
20
21settings = qp.QPALMSettings()
22solver = qp.QPALMSolver(data, settings)
23solver.solve()
24sol_x = solver.solution.x
25tol = 1e-5
26assert abs(sol_x[0] - 5.5) < tol
27assert abs(sol_x[1] - 5.0) < tol
28assert abs(sol_x[2] - (-10)) < tol
29
30# Warm start with solution to check whether the solver exits immediately
31solver.warm_start(solver.solution.x, solver.solution.y)
32solver.solve()
33assert solver.info.iter == 0
34
35# Update functions
36# It is possible to update the bounds, the linear part of the cost (q) and the settings
37settings.eps_abs = 1e-10
38settings.eps_rel = 0
39
40solver.update_settings(settings)
41solver.solve()
42sol_x = solver.solution.x
43strict_tol = 1e-10
44assert abs(sol_x[0] - 5.5) < strict_tol
45assert abs(sol_x[1] - 5.0) < strict_tol
46assert abs(sol_x[2] - (-10)) < strict_tol
47
48settings.eps_abs = 1e-4
49settings.eps_rel = 1e-4
50solver.update_settings(settings)
51
52data.bmin[3] = -15
53solver.update_bounds(bmin=data.bmin)
54solver.solve()
55sol_x = solver.solution.x
56assert abs(sol_x[0] - 8.5) < tol
57assert abs(sol_x[1] - 7) < tol
58assert abs(sol_x[2] - (-15)) < tol
59
60sol_x[0] = 0
61sol_x[1] = 0
62sol_x[2] = 0
63
64data.q[0] = 0
65data.q[1] = 0
66data.q[2] = 0
67solver.update_q(data.q)
68solver.solve()
69sol_x = solver.solution.x
70assert abs(sol_x[0] - 0) < tol
71assert abs(sol_x[1] - 0) < tol
72assert abs(sol_x[2] - 0.5) < tol