Python,puLP,变量与整数相乘并将其分配给某个变量

问题描述 投票:0回答:1

在这里,当我尝试我的代码时,无法计算 y1,y2 和 y3 y1,y2 和 y3 未计算。我该如何解决这个问题

from pulp import *

model = LpProblem("LineBalancing",LpMinimize)

x11 = LpVariable('x11', lowBound = 0, upBound = 1, cat = 'Integer')
x12 = LpVariable('x12', lowBound = 0, upBound = 1, cat = 'Integer')
x13 = LpVariable('x13', lowBound = 0, upBound = 1, cat = 'Integer')
x21 = LpVariable('x21', lowBound = 0, upBound = 1, cat = 'Integer')
x22 = LpVariable('x22', lowBound = 0, upBound = 1, cat = 'Integer')
x23 = LpVariable('x23', lowBound = 0, upBound = 1, cat = 'Integer')
x31 = LpVariable('x31', lowBound = 0, upBound = 1, cat = 'Integer')
x32 = LpVariable('x32', lowBound = 0, upBound = 1, cat = 'Integer')
x33 = LpVariable('x33', lowBound = 0, upBound = 1, cat = 'Integer')
x41 = LpVariable('x41', lowBound = 0, upBound = 1, cat = 'Integer')
x42 = LpVariable('x42', lowBound = 0, upBound = 1, cat = 'Integer')
x43 = LpVariable('x43', lowBound = 0, upBound = 1, cat = 'Integer')
x51 = LpVariable('x51', lowBound = 0, upBound = 1, cat = 'Integer')
x52 = LpVariable('x52', lowBound = 0, upBound = 1, cat = 'Integer')
x53 = LpVariable('x53', lowBound = 0, upBound = 1, cat = 'Integer')
x61 = LpVariable('x61', lowBound = 0, upBound = 1, cat = 'Integer')
x62 = LpVariable('x62', lowBound = 0, upBound = 1, cat = 'Integer')
x63 = LpVariable('x63', lowBound = 0, upBound = 1, cat = 'Integer')
Z = LpVariable('Z', lowBound = 0, upBound = None, cat = 'Integer')


t1 = 20
t2 = 15
t3 = 35
t4 = 55
t5 = 40
t6 = 5

y1 = (x11 * t1) + (x21 * t2) + (x31 * t3) + (x41 * t4) + (x51 * t5) + (x61 * t6)
y2 = (x12 * t1) + (x22 * t2) + (x32 * t3) + (x42 * t4) + (x52 * t5) + (x62 * t6)
y3 = (x13 * t1) + (x23 * t2) + (x33 * t3) + (x43 * t4) + (x53 * t5) + (x63 * t6)


model += Z

model += 1 <= x11 + x12 + x13 <= 1
model += 1 <= x21 + x22 + x23 <= 1
model += 1 <= x31 + x32 + x33 <= 1
model += 1 <= x41 + x42 + x43 <= 1
model += 1 <= x51 + x52 + x53 <= 1
model += 1 <= x61 + x62 + x63 <= 1

model += x11 >= x41
model += x11 + x12 >= x41 + x42
model += x11 + x12 + x13 >= x41 + x42 + x43

model += Z >= y1
model += Z >= y2
model += Z >= y3

status = model.solve()
 
print(f"minumum hat süresi {Z.varValue}")


print(status)
print(y1)

print(LpStatus[status])
print(f"minumum hat süresi {Z.varValue}")

----------当我尝试这段代码时,我将得到这个输出(我很快就会粘贴它)。--------------------------------

最小帽子苏雷西 0.0 1 20x11 + 15x21 + 35x31 + 55x41 + 40x51 + 5x61 最佳的 最小帽子苏雷西 0.0

所以,我打印了y1,它给了我这个(20x11 + 15x21 + 35x31 + 55x41 + 40x51 + 5x61)。我必须得到一些整数值来最小化我的 Z。

python optimization linear-programming pulp integer-programming
1个回答
0
投票

试试这个:

from pulp import *

model = LpProblem("LineBalancing", LpMinimize)

x11 = LpVariable('x11', lowBound=0, upBound=1, cat='Integer')
x12 = LpVariable('x12', lowBound=0, upBound=1, cat='Integer')
x13 = LpVariable('x13', lowBound=0, upBound=1, cat='Integer')
x21 = LpVariable('x21', lowBound=0, upBound=1, cat='Integer')
x22 = LpVariable('x22', lowBound=0, upBound=1, cat='Integer')
x23 = LpVariable('x23', lowBound=0, upBound=1, cat='Integer')
x31 = LpVariable('x31', lowBound=0, upBound=1, cat='Integer')
x32 = LpVariable('x32', lowBound=0, upBound=1, cat='Integer')
x33 = LpVariable('x33', lowBound=0, upBound=1, cat='Integer')
x41 = LpVariable('x41', lowBound=0, upBound=1, cat='Integer')
x42 = LpVariable('x42', lowBound=0, upBound=1, cat='Integer')
x43 = LpVariable('x43', lowBound=0, upBound=1, cat='Integer')
x51 = LpVariable('x51', lowBound=0, upBound=1, cat='Integer')
x52 = LpVariable('x52', lowBound=0, upBound=1, cat='Integer')
x53 = LpVariable('x53', lowBound=0, upBound=1, cat='Integer')
x61 = LpVariable('x61', lowBound=0, upBound=1, cat='Integer')
x62 = LpVariable('x62', lowBound=0, upBound=1, cat='Integer')
x63 = LpVariable('x63', lowBound=0, upBound=1, cat='Integer')
Z = LpVariable('Z', lowBound=0, upBound=None, cat='Integer')

t1 = 20
t2 = 15
t3 = 35
t4 = 55
t5 = 40
t6 = 5

model += lpSum([x11, x12, x13]) == 1, "C_x11_x12_x13_EQ_1"
model += lpSum([x21, x22, x23]) == 1, "C_x21_x22_x23_EQ_1"
model += lpSum([x31, x32, x33]) == 1, "C_x31_x32_x33_EQ_1"
model += lpSum([x41, x42, x43]) == 1, "C_x41_x42_x43_EQ_1"
model += lpSum([x51, x52, x53]) == 1, "C_x51_x52_x53_EQ_1"
model += lpSum([x61, x62, x63]) == 1, "C_x61_x62_x63_EQ_1"

model += x11 - x41 >= 0, "C_x11_x41_GTE_0"
model += x11 + x12 - x41 - x42 >= 0, "C_x11_x12_x41_x42_GTE_0"
model += x11 + x12 + x13 - x41 - x42 - x43 >= 0, "C_x11_x12_x13_x41_x42_x43_GTE_0"

y1 = (x11 * t1) + (x21 * t2) + (x31 * t3) + (x41 * t4) + (x51 * t5) + (x61 * t6)
y2 = (x12 * t1) + (x22 * t2) + (x32 * t3) + (x42 * t4) + (x52 * t5) + (x62 * t6)
y3 = (x13 * t1) + (x23 * t2) + (x33 * t3) + (x43 * t4) + (x53 * t5) + (x63 * t6)

model += Z >= y1, "C_y1"
model += Z >= y2, "C_y2"
model += Z >= y3, "C_y3"

model.setObjective(Z)

status = model.solve(PULP_CBC_CMD(msg=False))

print("\033[1m+" + "-" * 48 + "+")
print(f"| {' Optimization Results ':^46} |")
print("\033[1m+" + "-" * 48 + "+")
print(f"| \033[1mStatus:\033[0m\033[92m {LpStatus[model.status]:38}\033[0m |")
print(f"\033[1m| Objective Value:\033[0m {f'{model.objective.value():.0f}':29} |")
print(f"| \033[1m{f'Minimum hat süresi: {Z.value():.0f}':46}\033[0m |")
print("+" + "-" * 48 + "+")
print(f"| y1 = {f'{y1.value():.0f}':41} |")
print(f"| y2 = {f'{y2.value():.0f}':41} |")
print(f"| y3 = {f'{y3.value():.0f}':41} |")
print(f"| Z  = {f'{Z.value():.0f}':41} |")
print("+" + "-" * 48 + "+")

输出:

+------------------------------------------------+
|              Optimization Results              |
+------------------------------------------------+
| Status: Optimal                                |
| Objective Value: 60                            |
| Minimum hat süresi: 60                         |
+------------------------------------------------+
| y1 = 55                                        |
| y2 = 60                                        |
| y3 = 55                                        |
| Z  = 60                                        |
+------------------------------------------------+

注释

在模型中,您定义以下约束,如下所示:

model += 1 <= x11 + x12 + x13 <= 1
model += 1 <= x21 + x22 + x23 <= 1
model += 1 <= x31 + x32 + x33 <= 1
model += 1 <= x41 + x42 + x43 <= 1
model += 1 <= x51 + x52 + x53 <= 1
model += 1 <= x61 + x62 + x63 <= 1

1 <= f(x) <= 1
这样的约束与
f(x) == 1
是一样的。 当您按照自己的方式设置这些约束时,它们会转化为以下等式:

x11 + x12 + x13 - 1 <= 0
->
x11 + x12 + x13 <= 1

这意味着 x11、x12 和 x13 的和可以是 0 或 1。 由于您正在定义一个最小化问题,因此返回可能的最小值的 x11、x12 和 x13 的值是当它们全部等于 0 时。

当您将约束定义为

model += x11 + x12 + x13 == 1
时,PuLP 会将约束定义为:

x11 + x12 + x13 - 1 == 0
->
x11 + x12 + x13 == 1

因此,它强制 x11、x12 或 x13 之一等于 1,其余等于 0。

© www.soinside.com 2019 - 2024. All rights reserved.