forked from qpsolvers/qpsolvers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_problem.py
93 lines (78 loc) · 2.96 KB
/
sparse_problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016-2020 Stephane Caron <[email protected]>
#
# This file is part of qpsolvers.
#
# qpsolvers is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# qpsolvers is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with qpsolvers. If not, see <http://www.gnu.org/licenses/>.
"""
Test all available QP solvers on a sparse quadratic program.
"""
from __future__ import print_function # Python 2 compatibility
import numpy
import scipy.sparse
from IPython import get_ipython
from numpy.linalg import norm
from os.path import basename
from scipy.sparse import csc_matrix
from qpsolvers import dense_solvers, sparse_solvers
from qpsolvers import solve_qp
n = 500
M = scipy.sparse.lil_matrix(scipy.sparse.eye(n))
for i in range(1, n - 1):
M[i, i + 1] = -1
M[i, i - 1] = 1
P = csc_matrix(M.dot(M.transpose()))
q = -numpy.ones((n,))
G = csc_matrix(-scipy.sparse.eye(n))
h = -2 * numpy.ones((n,))
P_array = numpy.array(P.todense())
G_array = numpy.array(G.todense())
def check_same_solutions(tol=0.05):
sol0 = solve_qp(P, q, G, h, solver=sparse_solvers[0])
for solver in sparse_solvers:
sol = solve_qp(P, q, G, h, solver=solver)
relvar = norm(sol - sol0) / norm(sol0)
assert relvar < tol, "%s's solution offset by %.1f%%" % (
solver, 100. * relvar)
for solver in dense_solvers:
sol = solve_qp(P_array, q, G_array, h, solver=solver)
relvar = norm(sol - sol0) / norm(sol0)
assert relvar < tol, "%s's solution offset by %.1f%%" % (
solver, 100. * relvar)
def time_dense_solvers():
instructions = {
solver: "u = solve_qp(P_array, q, G_array, h, solver='%s')" % solver
for solver in dense_solvers}
print("\nDense solvers\n-------------")
for solver, instr in instructions.items():
print("%s: " % solver, end='')
get_ipython().magic('timeit %s' % instr)
def time_sparse_solvers():
instructions = {
solver: "u = solve_qp(P, q, G, h, solver='%s')" % solver
for solver in sparse_solvers}
print("\nSparse solvers\n--------------")
for solver, instr in instructions.items():
print("%s: " % solver, end='')
get_ipython().magic('timeit %s' % instr)
if __name__ == "__main__":
if get_ipython() is None:
print("Usage: ipython -i %s" % basename(__file__))
exit()
print("\nTesting all QP solvers on a sparse quadratic program...")
check_same_solutions()
time_dense_solvers()
time_sparse_solvers()