标准形式:
min21xTPx+qTxs.t.Gx≤hAx=b
注意:
P为对称矩阵,
x是列向量。
示例:
21[x1x2][abbd][x1x2]=21(ax12+dx22+2bx1x2)
minx12+x22+x1x2+2x1+x2s.t.xi≥0,i=1,2i=1∑2xi=1
因此,由
x12+x22+x1x2可以推出:
P=[2112]
由
2x1+x2可以推出:
q=[21]
注意:约束条件需要化成标准形式:
s.t.−xi≤0,i=1,2i=1∑2xi=1
因此,可以推出:
G=[−100−1]h=[00]A=[11]b=1
因此,要求解:
minx12+x22+x1x2+2x1+x2s.t.xi≥0,i=1,2i=1∑2xi=1
可通过:cvxopt.solvers.qp(P,q,G,h,A,b)来进行求解。
其中参数为:
P=[2112],q=[21],G=[−100−1],h=[00],A=[11],b=1
Code:
from cvxopt import solvers, matrix
P=matrix([[2.,1.],[1.,2.]])
q=matrix([2.,1.])
G=matrix([[-1.,0.],[0.,-1.]])
h=matrix([1.,1.])
A=matrix([1.,1.],(1,2))
b=matrix(1.)
solvers.options['show_progress'] = False
sol = solvers.qp(P,q,G,h,A,b)
print(sv['x'])
print(sv['primal objective'])
Result: