-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathQPP.py
71 lines (68 loc) · 2.3 KB
/
QPP.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016-2017 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/>.
from numpy import hstack, vstack
from quadprog import solve_qp
def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None):
"""
Solve a Quadratic Program defined as:
minimize
(1/2) * x.T * P * x + q.T * x
subject to
G * x <= h
A * x == b
using quadprog <https://pypi.python.org/pypi/quadprog/>.
Parameters
----------
P : numpy.array
Symmetric quadratic-cost matrix.
q : numpy.array
Quadratic-cost vector.
G : numpy.array
Linear inequality constraint matrix.
h : numpy.array
Linear inequality constraint vector.
A : numpy.array, optional
Linear equality constraint matrix.
b : numpy.array, optional
Linear equality constraint vector.
initvals : numpy.array, optional
Warm-start guess vector (not used).
Returns
-------
x : numpy.array
Solution to the QP, if found, otherwise ``None``.
Note
----
The quadprog solver only considers the lower entries of `P`, therefore it
will use a wrong cost function if a non-symmetric matrix is provided.
"""
if initvals is not None:
print("quadprog: note that warm-start values ignored by wrapper")
qp_G = P
qp_a = -q
if A is not None:
qp_C = -vstack([A, G]).T
qp_b = -hstack([b, h]).squeeze(0)
meq = A.shape[0]
else: # no equality constraint
qp_C = -G.T
qp_b = -h
meq = 0
return solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]