QPALM 1.0.0
Proximal Augmented Lagrangian method for Quadratic Programs
examples/python/qpalm_python_demo.py
1import scipy.sparse as sp
2import qpalm
3
4# %% Construct the matrices describing the problem
5"""
6 minimize ½〈x, Qx〉+〈q, x〉+ c
7 x
8 subject to bmin ≤ Ax ≤ bmax
9"""
10data = qpalm.Data(3, 4)
11
12# Q is sparse and symmetric
13row = [0, 0, 1, 1, 2]
14col = [0, 1, 0, 1, 2]
15valuesQ = [1, -1, -1, 2, 1]
16data.Q = sp.csc_matrix((valuesQ, (row, col)), shape=(3, 3))
17
18# q, bmin and bmax are dense vectors
19data.q = [-2, -6, 1]
20data.bmin = [0.5, -10, -10, -10]
21data.bmax = [0.5, 10, 10, 10]
22
23# A is sparse and rectangular
24row = [0, 1, 0, 2, 0, 3]
25col = [0, 0, 1, 1, 2, 2]
26valuesA = [1, 1, 1, 1, 1, 1]
27data.A = sp.csc_matrix((valuesA, (row, col)), shape=(4, 3))
28
29# %% Configure the solver
30
31settings = qpalm.Settings()
32settings.eps_abs = 1e-8
33
34# %% Solve the problem
35
36solver = qpalm.Solver(data, settings)
37solver.solve()
38
39# %% Print the results
40print("Status: ", solver.info.status)
41print("Solution: ", solver.solution.x)
42print("Multipliers:", solver.solution.y)
43
44# %% Warm start with solution
45solver.warm_start(solver.solution.x, solver.solution.y)
46solver.solve()
47print(solver.solution.x)
48
49# %% Update functions
50
51# It is possible to update the settings, the bounds, the linear part of the
52# cost (q) and the values of matrices Q and A
53
54settings.eps_abs = 1e-10
55settings.eps_rel = 0
56solver.update_settings(settings)
57solver.solve()
58print(solver.solution.x)
59
60data.bmin = [0, 0, -15, 1]
61solver.update_bounds(bmin=data.bmin)
62solver.solve()
63print(solver.solution.x)
64
65data.q = [1, 0, -2]
66solver.update_q(data.q)
67solver.solve()
68print(solver.solution.x)
69
70# Note that QPALM internally only uses the upper-triangular part of Q, so when
71# updating the values of Q, you have to pass the data of the upper-triangular
72# part only.
73# If you need to update the sparsity pattern, create a new solver.
74Qup = sp.triu(data.Q)
75Qup.data[1] = -0.5
76solver.update_Q_A(Qup.data, valuesA)
77solver.solve()
78print(solver.solution.x)