一个塑料大框里装满了鸡蛋,两个两个地数,余1个鸡蛋;三个三个地数,正好数完;四个四个地数,余1个鸡蛋;五个五个地数,余4个鸡蛋;六个六个地数,余3个鸡蛋;七个七个地数,余4个鸡蛋;八个八个地数,余1个鸡蛋;九个九个地数,正好数完。建立数学规划模型求大框中鸡蛋个数的最小值是多少。
m i n x s . t . = { x m o d 2 = 1 x m o d 3 = 0 x m o d 4 = 1 x m o d 5 = 4 x m o d 6 = 3 x m o d 7 = 4 x m o d 8 = 1 x m o d 9 = 0 x ≥ 9 = 0 且 为 整 数 min\ x\\ s.t.= \begin{cases} x\ mod\ 2 = 1\\ x\ mod\ 3 = 0\\ x\ mod\ 4 = 1\\ x\ mod\ 5 = 4\\ x\ mod\ 6 = 3\\ x\ mod\ 7 = 4\\ x\ mod\ 8 = 1\\ x\ mod\ 9 = 0\\ x \geq 9 = 0\ \ 且为整数 \end{cases} min xs.t.=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧x mod 2=1x mod 3=0x mod 4=1x mod 5=4x mod 6=3x mod 7=4x mod 8=1x mod 9=0x≥9=0 且为整数
# 决策变量
n = 1
z = 8
x = cp.Variable(n,integer = True)
y = cp.Variable(z,integer = True)
prob = cp.Problem(cp.Minimize(x),
[x - 2 * y[0] == 1,
x - 3 * y[1] == 0,
x - 4 * y[2] == 1,
x - 5 * y[3] == 4,
x - 6 * y[4] == 3,
x - 7 * y[5] == 4,
x - 8 * y[6] == 1,
x - 9 * y[7] == 0,
x >= 0,y >= 0])
# 求解
ans = prob.solve(solver='GLPK_MI')
# 输出结果
print("目标函数最小值:", ans)
print(x.value)
输出:
目标函数最小值: 1089.0
[1089.]
求解下列非线性整数规划问题:
m a x z = x 1 2 + x 2 2 + 3 x 3 2 + 4 x 4 2 + 2 x 5 2 − 8 x 1 − 2 x 2 − 3 x 3 − x 4 − 2 x 5 s . t . = { 0 ≤ x i ≤ 99 , 且 x i 为 整 数 ( i = 1 , 2 , ⋯ , 5 ) x 1 + x 2 + x 3 + x 4 + x 5 ≤ 400 x 1 + 2 x 2 + 2 x 3 + x 4 + 6 x 5 ≤ 800 2 x 1 + x 2 + 6 x 3 ≤ 200 x 3 + x 4 + 5 x 5 ≤ 200 max\ 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} 0 \leq x_i\leq 99,\ \ 且x_i为整数(i = 1,2,\cdots,5)\\ 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 \end{cases} max z=x12+x22+3x32+4x42+2x52−8x1−2x2−3x3−x4−2x5s.t.=⎩⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎧0≤xi≤99, 且xi为整数(i=1,2,⋯,5)x1+x2+x3+x4+x5≤400x1+2x2+2x3+x4+6x5≤8002x1+x2+6x3≤200x3+x4+5x5≤200
cplex
包,即pip install cplex
,求解器变更为CPLEX
#'CVXOPT', 'ECOS', 'ECOS_BB', 'GLOP', 'GLPK', 'GLPK_MI', 'OSQP', 'PDLP', 'SCIPY', 'SCS'
# 决策变量
n = 5
x = cp.Variable(n,integer = True)
# 约束1
A1 = np.array([[1,1,1,1,1],
[1,2,2,1,6],
[2,1,6,0,0],
[0,0,1,1,5]])
b1 = np.array([400,800,200,200])
prob = cp.Problem(cp.Minimize(x[0]**2+x[1]**2+3*x[2]**2+4*x[3]**2+2*x[4]**2-8*x[0]-2*x[1]-3*x[2]-x[3]-2*x[4]),
[A1 @ x <= b1,
x >= 0,
x <= 99])
# 求解
ans = prob.solve(solver='CPLEX')
# 输出结果
print("目标函数最小值:", ans)
print(x.value)
输出:
目标函数最小值: -17.0
[4. 1. 0. 0. 0.]
求解下列非线性整数规划问题:
m a x z = ∑ i = 1 100 x i s . t . = { x 1 ≤ 10 x 1 + 2 x 2 ≤ 20 x 1 + 2 x 2 + 3 x 3 ≤ 30 x 1 + 2 x 2 + 3 x 3 + 4 x 4 ≤ 40 ∑ i = 1 100 ( 101 − i ) x i ≤ 1000 x ≤ 0 , i = 1 , 2 , ⋯ , 100 max\ z = \sum^{100}_{i =1}\sqrt{x_i}\\ s.t.= \begin{cases} x_1 \leq 10\\ x_1 + 2x_2 \leq 20\\ x_1 + 2x_2 + 3x_3 \leq 30\\ x_1 + 2x_2 + 3x_3 + 4x_4 \leq 40\\ \sum\limits_{i = 1}^{100}(101 - i)x_i \leq 1000\\ x \leq 0,\ i = 1,2,\cdots,100 \end{cases} max z=i=1∑100xis.t.=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧x1≤10x1+2x2≤20x1+2x2+3x3≤30x1+2x2+3x3+4x4≤40i=1∑100(101−i)xi≤1000x≤0, i=1,2,⋯,100
# 决策变量
n = 100
x = cp.Variable(n,nonneg=True)
constraints = [np.arange(100,0,-1) @ x <= 1000,x >= 0]
for i in range(0,4):
constraints += [np.arange(1,i+2,1) @ x[0:(i+1)] <= 10 * (i+1)]
prob = cp.Problem(cp.Maximize(cp.sum(x ** 0.5)),constraints)
# 求解
ans = prob.solve(solver='CPLEX')
# 输出结果
print("目标函数最大值:", ans)
print(x.value)
输出:
目标函数最大值: 72.02345192718317
[1.92719315e-02 1.96624761e-02 2.00666987e-02 2.04825241e-02
2.09106132e-02 2.13532034e-02 2.18099927e-02 2.22815952e-02
2.27686585e-02 2.32718659e-02 2.37919389e-02 2.43296398e-02
2.48857743e-02 2.54611949e-02 2.60568038e-02 2.66735567e-02
2.73124664e-02 2.79746072e-02 2.86611190e-02 2.93732130e-02
3.01121761e-02 3.08793775e-02 3.16762745e-02 3.25044197e-02
3.33654686e-02 3.42611877e-02 3.51934637e-02 3.61643134e-02
3.71758946e-02 3.82305182e-02 3.93306612e-02 4.04789815e-02
4.16783338e-02 4.29317875e-02 4.42426461e-02 4.56144693e-02
4.70510970e-02 4.85566764e-02 5.01356916e-02 5.17929973e-02
5.35338562e-02 5.53639807e-02 5.72895798e-02 5.93174120e-02
6.14548443e-02 6.37099199e-02 6.60914332e-02 6.86090162e-02
7.12732358e-02 7.40957050e-02 7.70892096e-02 8.02678530e-02
8.36472233e-02 8.72445841e-02 9.10790957e-02 9.51720707e-02
9.95472708e-02 1.04231253e-01 1.09253771e-01 1.14648255e-01
1.20452360e-01 1.26708635e-01 1.33465297e-01 1.40777166e-01
1.48706782e-01 1.57325743e-01 1.66716332e-01 1.76973482e-01
1.88207190e-01 2.00545474e-01 2.14138042e-01 2.29160848e-01
2.45821821e-01 2.64368095e-01 2.85095228e-01 3.08359051e-01
3.34591041e-01 3.64318474e-01 3.98191128e-01 4.37017097e-01
4.81811436e-01 5.33863182e-01 5.94829142e-01 6.66867381e-01
7.52830854e-01 8.56554284e-01 9.83289336e-01 1.14038267e+00
1.33836510e+00 1.59276339e+00 1.92723977e+00 2.37929941e+00
3.01128040e+00 3.93305381e+00 5.35321168e+00 7.70834768e+00
1.20435661e+01 2.14094656e+01 4.81886937e+01 1.93004451e+02]
m a x f ( x ) = 2 x 1 + 3 x 1 2 + 3 x 2 + x 2 2 + x 3 s . t . = { x 1 + 2 x 1 2 + x 2 + 2 x 2 2 + x 3 ≤ 10 x 1 + x 1 2 + x 2 + x 2 2 − x 3 ≤ 50 2 x 1 + x 1 2 + 2 x 2 + x 3 ≤ 40 x 1 2 + x 3 = 2 x 1 + 2 x 2 ≥ 1 x 1 ≥ 0 x 2 , x 3 不 约 束 max\ f(x) = 2x_1 + 3x_1^2+3x_2 + x_2^2+x_3\\ s.t.= \begin{cases} x_1 + 2x_1^2+x_2+2x_2^2+x_3 \leq 10\\ x_1 + x_1^2+x_2+x_2^2-x_3 \leq 50\\ 2x_1 + x_1^2 + 2x_2 + x_3 \leq 40\\ x_1^2 + x_3 = 2\\ x_1 + 2x_2 \geq 1\\ x_1 \geq 0\ \ x_2,x_3不约束 \end{cases} max f(x)=2x1+3x12+3x2+x22+x3s.t.=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧x1+2x12+x2+2x22+x3≤10x1+x12+x2+x22−x3≤502x1+x12+2x2+x3≤40x12+x3=2x1+2x2≥1x1≥0 x2,x3不约束
n = 3
x = cp.Variable(n)
prob = cp.Problem(cp.Maximize(2*x[0]+3*x[0]**2+3*x[1]+x[1]**2+x[2]),
[x[0]+2*x[0]**2+x[1]+2*x[1]**2+x[2] <= 10,
x[0]+x[0]**2+x[1]+x[1]**2-x[2] <= 50,
2*x[0]+x[0]**2+2*x[1]+x[2] <= 40,
x[0]**2+x[2] == 2,
x[0]+2*x[1] >= 1,
x[0] >= 0])
# 求解
ans = prob.solve(solver='CPLEX')
# 输出结果
print("目标函数最大值:", ans)
print(x.value)
输出:
目标函数最大值: 18.0833
[2.3333 0.1667 -3.4445]