【公式求解】非线性规划(3)

若非线性规划的目标函数为决策向量x的二次函数,约束条件又全是线性的,就称这种规划为二次规划。
cvxopt.solvers模块中二次规划的标准形为
$ min \quad \frac{1}{2}x^{T}Px+q^{T}x $
$ s.t. \begin{cases}
Ax \leq b \\
Aeq \cdot x = beq
\end{cases}
$

例:求解二次规划模型
$ min \quad z=1.5x_{1}^{2}+x_{2}^{2}+0.85x_{3}^{2}+3x_{1}-8.2x_{2}-1.95x_{3} $
$ s.t. \begin{cases}
x_{1}+x_{3} \leq 2 \\
-x_{1}+2x_{2} \leq 2 \\
x_{2}+2x_{3} \leq 3 \\
x_{1}+x_{2}+x_{3} = 3
\end{cases}
$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
from cvxopt import matrix, solvers

n = 3
P = matrix(0.,(n,n))
P[::n+1] = [3,2,1.7]
q = matrix([3,-8.2,-1.95])
A = matrix([[1.,0,1],[-1,2,0],[0,1,2]]).T
b = matrix([2.,2,3])
Aeq = matrix(1.,(1,n))
beq = matrix(3.)

s = solvers.qp(P,q,A,b,Aeq,beq)

print('最优解为:', s['x'])
print('最优值为:', s['primal objective'])
0%