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

例:求解非线性规划问题
$ min \quad \frac{2+x_{1}}{1+x_{2}}-3x_{1}+4x_{3} $
$ s.t. \quad 0.1 \leq x_{i} \leq 0.9 \quad i=1,2,3 $

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from numpy import ones
from scipy.optimize import minimize

def obj(x):
x1,x2,x3 = x
return (2+x1)/(1+x2)-3*x1+4*x3

LB = [0.1]*3
UB = [0.9]*3
bound = tuple(zip(LB,UB)) # 生成决策向量界限的元组

res = minimize(obj, ones(3), bounds=bound)

print('求解状态为:', res.success)
print('最优解为:', res.x)
print('最优值为:', res.fun)

例:求解非线性规划问题
$ min \quad z = x_{1}^{2}+x_{2}^{2}+3x_{3}^{2}+4x_{4}^{2}+2x_{5}^{2}-8x_{1}-2x_{2}-3x_{3}-x_{4}-2x_{5} $
$ s.t. \begin{cases}
x_{1}+x_{2}+x_{3}+x_{4}+x_{5} \leq 400 \\
x_{1}+2x_{2}+2x_{3}+x_{4}+6x_{5} \leq 800 \\
2x_{1}+x_{2}+6x_{3} \leq 200 \\
x_{3}+x_{4}+5x_{5} \leq 200 \\
0 \leq x_{i} \leq 99 & i = 1,2,3,4,5
\end{cases}
$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
from scipy.optimize import minimize

c1 = np.array([1,1,3,4,2])
c2 = np.array([-8,-2,-3,-1,-2])
A = np.array([[1,1,1,1,1],[1,2,2,1,6],[2,1,6,0,0],[0,0,1,1,5]])
b = np.array([400,800,200,200])

obj = lambda x:np.dot(-c1,x**2)+np.dot(-c2,x)

cons = {'type':'ineq', 'fun':lambda x:b-A@x}

bound = [(0,99) for i in range(A.shape[1])]

res = minimize(obj, np.ones(5)*90, constraints=cons, bounds=bound)

print('求解状态为:', res.success)
print('最优解为:', res.x)
print('最优值为:', res.fun)
0%