QPALM 1.2.3
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
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)
43assert solver.info.status_val == qpalm.Info.SOLVED
44
45# %% Warm start with solution
46solver.warm_start(solver.solution.x, solver.solution.y)
47solver.solve()
48print(solver.solution.x)
49
50# %% Update functions
51
52# It is possible to update the settings, the bounds, the linear part of the
53# cost (q) and the values of matrices Q and A
54
55settings.eps_abs = 1e-10
56settings.eps_rel = 0
57solver.update_settings(settings)
58solver.solve()
59print(solver.solution.x)
60
61data.bmin = [0, 0, -15, 1]
62solver.update_bounds(bmin=data.bmin)
63solver.solve()
64print(solver.solution.x)
65
66data.q = [1, 0, -2]
67solver.update_q(data.q)
68solver.solve()
69print(solver.solution.x)
70
71# Note that QPALM internally only uses the upper-triangular part of Q, so when
72# updating the values of Q, you have to pass the data of the upper-triangular
73# part only.
74# If you need to update the sparsity pattern, create a new solver.
75Qup = sp.triu(data.Q)
76Qup.data[1] = -0.5
77solver.update_Q_A(Qup.data, valuesA)
78solver.solve()
79print(solver.solution.x)
Stores the matrices and vectors that define the problem.
Definition qpalm.hpp:39
Main QPALM solver.
Definition qpalm.hpp:140
Settings and parameters for the QPALM solver.
Definition qpalm.hpp:110