forked from qpsolvers/qpsolvers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_solvers.py
80 lines (71 loc) · 2.58 KB
/
test_solvers.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
#!/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 solvers on all combinations of inequality/equality API calls.
"""
from __future__ import print_function # Python 2 compatibility
from numpy import array, dot
from numpy.linalg import norm
from qpsolvers import available_solvers
from qpsolvers import solve_qp
M = array([
[1., 2., 0.],
[-8., 3., 2.],
[0., 1., 1.]])
P = dot(M.T, M)
q = dot(array([3., 2., 3.]), M).reshape((3,))
G = array([
[1., 2., 1.],
[2., 0., 1.],
[-1., 2., -1.]])
h = array([3., 2., -2.]).reshape((3,))
h0 = array([h[0]])
A = array([
[1., 0., 0.],
[0., 0.4, 0.5]])
b = array([-0.5, -1.2])
b0 = array([b[0]])
lb = array([-1., -1., -1.])
ub = array([+1., +1., +1.])
if __name__ == "__main__":
cases = [
{'P': P, 'q': q},
{'P': P, 'q': q, 'G': G, 'h': h},
{'P': P, 'q': q, 'A': A, 'b': b},
{'P': P, 'q': q, 'G': G[0], 'h': h0},
{'P': P, 'q': q, 'A': A[0], 'b': b0},
{'P': P, 'q': q, 'G': G, 'h': h, 'A': A, 'b': b},
{'P': P, 'q': q, 'G': G[0], 'h': h0, 'A': A, 'b': b},
{'P': P, 'q': q, 'G': G, 'h': h, 'A': A[0], 'b': b0},
{'P': P, 'q': q, 'G': G[0], 'h': h0, 'A': A[0], 'b': b0},
{'P': P, 'q': q, 'G': G[0], 'h': h0, 'A': A[0], 'b': b0, 'lb': lb,
'ub': ub},
]
for (i, case) in enumerate(cases):
print("\nTest %1d\n======\n" % i)
expected_sol = solve_qp(solver=available_solvers[0], **case)
for solver in available_solvers:
sol = solve_qp(solver=solver, **case)
delta = norm(sol - expected_sol)
print("%9s's solution: %s\toffset: %.1e" % (
solver, sol.round(decimals=5), delta))
critical_offset = 2e-4
assert delta < critical_offset, \
"%s's solution offset by %.1e on test #%d" % (solver, delta, i)