二次规划
二次规划标准型
例如
其中
[egin{array}{*{20}{l}}
{P = left[ {egin{array}{*{20}{c}}
{4,}&1\
{1,}&2
end{array}}
ight]}&{q = left[ {egin{array}{*{20}{c}}
1\
1
end{array}}
ight]}\
egin{array}{l}
G = left[ {egin{array}{*{20}{c}}
{ - 1,}&0\
{0,}&{ - 1}
end{array}}
ight]\
A = left[ {egin{array}{*{20}{c}}
{1,}&1
end{array}}
ight]
end{array}&egin{array}{l}
h = left[ {egin{array}{*{20}{c}}
0\
0
end{array}}
ight]\
b = 1
end{array}
end{array}]
代码如下:
from cvxopt import matrix, solvers P = 2*matrix([ [2, .5], [.5, 1] ]) q = matrix([1.0, 1.0]) G = matrix([[-1.0,0.0],[0.0,-1.0]]) h = matrix([0.0,0.0]) A = matrix([1.0, 1.0], (1,2)) b = matrix(1.0) sol=solvers.qp(Q, p, G, h, A, b) print(sol['x'])
from cvxopt import matrix P = matrix([[1.0,0.0],[0.0,0.0]]) q = matrix([3.0,4.0]) G = matrix([[-1.0,0.0,-1.0,2.0,3.0],[0.0,-1.0,-3.0,5.0,4.0]]) h = matrix([0.0,0.0,-15.0,100.0,80.0])
注:
matrix元素的类型必须是double类型,可以通过如下语句设置。
import numpy as np from cvxopt import matrix P = matrix(np.diag([1,0]), tc=’d’) q = matrix(np.array([3,4]), tc=’d’) G = matrix(np.array([[-1,0],[0,-1],[-1,-3],[2,5],[3,4]]), tc=’d’) h = matrix(np.array([0,0,-15,100,80]), tc=’d’)
参考链接:
https://cvxopt.org/examples/index.html