emi*_*ir0 0 python linear-programming pulp
我目前使用 Excel 中的 Solver 来寻找制造的最佳解决方案。这是当前的设置:

它涉及在旋转机器上制造鞋子,也就是说,生产是分批重复进行的。例如,一批为“10x A1”(参见表中的 A1),这将产生 10x 尺寸 36、20x 尺寸 37...10x 尺寸 41。
有一些前缀设置;A1、A2;R7...如上表所示。
然后是一个requested变量(或者更确切地说是一个变量列表),它基本上说明了客户的要求,每个尺寸的数量。
目标函数是找到一组重复,使其尽可能匹配请求的数量。因此,在解算器中(抱歉,没有英文屏幕截图),您可以看到目标是N21(即每个大小的绝对差之和)。变量是N2:N9- 这是每次设置的重复次数,唯一的限制是它N2:N9是一个整数。
我如何用 python 模拟这种行为?我的开始:
from collections import namedtuple
from pulp import *
class Setup(namedtuple('IAmReallyLazy', 'name ' + ' '.join(f's{s}' for s in range(36, 47)))):
# inits with name and sizes 's36', 's37'... 's46'
repetitions = 0
setups = [
Setup('A1', 1, 2, 3, 3, 2, 1, 0, 0, 0, 0, 0),
Setup('A2', 0, 1, 2, 3, 3, 2, 1, 0, 0, 0, 0),
Setup('R7', 0, 0, 1, 1, 1, 1, 2, 0, 0, 0, 0),
Setup('D1', 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0),
# and others
]
setup_names = [s.name for s in setups]
requested = {
's36': 100,
's37': 250,
's38': 300,
's39': 450,
's40': 450,
's41': 250,
's42': 200,
}
def get_quantity_per_size(size: str) -> int:
return sum([getattr(setup, size) * setup.repetitions for setup in setups])
def get_abs_diff(size: str) -> int:
requested_size = requested.get(size, 0)
return abs(get_quantity_per_size(size) - requested_size)
problem = LpProblem('Optimize Batches', LpMinimize)
# goal is to minimise the sum(get_abs_diff(f's{size}') for size in range(36, 47))
# variables are [setup.repetitions for setup in setups]
# constraints are all([isinstance(setup.repetitions, int) for setup in setups])
Run Code Online (Sandbox Code Playgroud)
在理想的情况下,如果存在多个最佳解决方案,则应选择具有最大扩展绝对差异的解决方案(即具有最小最高差异的解决方案)。也就是说,如果一个解决方案的每个尺寸和 10 个尺寸(总共 100 个)的绝对差异为 10,而其他解决方案的绝对差异为 20 + 80 = 100,则第一个解决方案对客户而言更为优化。
另一个约束基本上应该是min(setup.repetitions for setup in setups if setup.repetitions > 0) > 9重复约束应该是:
这里有几件事。首先,如果使用的abs()话问题将是非线性的。相反,您应该引入名为 和 的新变量over_mfg,它们分别表示高于目标的under_mfg生产单位数和低于目标的单位数。您可以这样声明它们:
over_mfg = LpVariable.dicts("over_mfg", sizes, 0, None, LpInteger)
under_mfg = LpVariable.dicts("under_mfg", sizes, 0, None, LpInteger)
Run Code Online (Sandbox Code Playgroud)
我声明了一个名为 的列表sizes,它在上面的定义中使用:
min_size = 36
max_size = 46
sizes = ['s' + str(s) for s in range(min_size, max_size+1)]
Run Code Online (Sandbox Code Playgroud)
您还需要指示每个设置的重复次数的变量:
repetitions = LpVariable.dicts("repetitions", setup_names, 0, None, LpInteger)
Run Code Online (Sandbox Code Playgroud)
然后你的目标函数被声明为:
problem += lpSum([over_mfg[size] + under_mfg[size] for size in sizes])
Run Code Online (Sandbox Code Playgroud)
(请注意,pulp您使用的lpSum是而不是sum。)现在,您需要约束来表示 是over_mfg过剩,under_mfg是不足:
for size in sizes:
problem += over_mfg[size] >= lpSum([repetitions[setup.name] * getattr(setup, size) for setup in setups]) - requested[size], "DefineOverMfg" + size
problem += under_mfg[size] >= requested[size] - lpSum([repetitions[setup.name] * getattr(setup, size) for setup in setups]), "DefineUnderMfg" + size
Run Code Online (Sandbox Code Playgroud)
另请注意,我没有使用您的get_quantity_per_size()和get_abs_diff()函数。这些也会令人困惑pulp,因为它不会意识到这些是简单的线性函数。
这是我的完整代码:
from collections import namedtuple
from pulp import *
class Setup(namedtuple('IAmReallyLazy', 'name ' + ' '.join(f's{s}' for s in range(36, 47)))):
# inits with name and sizes 's36', 's37'... 's46'
repetitions = 0
setups = [
Setup('A1', 1, 2, 3, 3, 2, 1, 0, 0, 0, 0, 0),
Setup('A2', 0, 1, 2, 3, 3, 2, 1, 0, 0, 0, 0),
Setup('R7', 0, 0, 1, 1, 1, 1, 2, 0, 0, 0, 0),
Setup('D1', 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0),
# and others
]
setup_names = [s.name for s in setups]
min_size = 36
max_size = 46
sizes = ['s' + str(s) for s in range(min_size, max_size+1)]
requested = {
's36': 100,
's37': 250,
's38': 300,
's39': 450,
's40': 450,
's41': 250,
's42': 200,
's43': 0, # I added these for completeness
's44': 0,
's45': 0,
's46': 0
}
problem = LpProblem('Optimize Batches', LpMinimize)
# goal is to minimise the sum(get_abs_diff(f's{size}') for size in range(36, 47))
# variables are [setup.repetitions for setup in setups]
# constraints are all([isinstance(setup.repetitions, int) for setup in setups])
repetitions = LpVariable.dicts("repetitions", setup_names, 0, None, LpInteger)
over_mfg = LpVariable.dicts("over_mfg", sizes, 0, None, LpInteger)
under_mfg = LpVariable.dicts("under_mfg", sizes, 0, None, LpInteger)
problem += lpSum([over_mfg[size] + under_mfg[size] for size in sizes])
for size in sizes:
problem += over_mfg[size] >= lpSum([repetitions[setup.name] * getattr(setup, size) for setup in setups]) - requested[size], "DefineOverMfg" + size
problem += under_mfg[size] >= requested[size] - lpSum([repetitions[setup.name] * getattr(setup, size) for setup in setups]), "DefineUnderMfg" + size
# Solve problem
problem.solve()
# Print status
print("Status:", LpStatus[problem.status])
# Print optimal values of decision variables
for v in problem.variables():
if v.varValue is not None and v.varValue > 0:
print(v.name, "=", v.varValue)
Run Code Online (Sandbox Code Playgroud)
这是输出:
Status: Optimal
over_mfg_s38 = 62.0
over_mfg_s41 = 62.0
repetitions_A1 = 25.0
repetitions_A2 = 88.0
repetitions_D1 = 110.0
repetitions_R7 = 1.0
under_mfg_s36 = 75.0
under_mfg_s37 = 2.0
under_mfg_s40 = 25.0
Run Code Online (Sandbox Code Playgroud)
因此,我们生产了 25 个重复的 A1、88 个重复的 A2、110 个重复的 D1 和 1 个重复的 R7。这给出了 25 个单位s36(因此 75 个单位低于 100 的目标);248 单位s37(低于目标 2 单位);362 单位s38(比 300 目标多 62 单位);等等。
现在,对于您必须生成 0 个设置或 >9 个设置的约束,您可以引入新的二进制变量来指示是否生成每个设置:
is_produced = LpVariable.dicts("is_produced", setup_names, 0, 1, LpInteger)
Run Code Online (Sandbox Code Playgroud)
然后添加这些约束:
M = 1000
min_reps = 9
for s in setup_names:
problem += M * is_produced[s] >= repetitions[s] # if it's produced at all, must set is_produced = 1
problem += min_reps * (1 - is_produced[s]) + repetitions[s] >= min_reps
Run Code Online (Sandbox Code Playgroud)
M是一个很大的数字;它应该大于最大可能的重复次数,但不能更大。我定义了min_reps避免在约束中“硬编码”9。因此,这些约束条件表明 (1) if repetitions[s] > 0, thenis_produced[s]必须等于 1,并且 (2) = is_produced[s] 1或 repetitions[s]> 9。
输出:
Status: Optimal
is_produced_A1 = 1.0
is_produced_A2 = 1.0
is_produced_D1 = 1.0
over_mfg_s38 = 63.0
over_mfg_s39 = 1.0
over_mfg_s41 = 63.0
repetitions_A1 = 25.0
repetitions_A2 = 88.0
repetitions_D1 = 112.0
under_mfg_s36 = 75.0
under_mfg_s40 = 24.0
Run Code Online (Sandbox Code Playgroud)
请注意,现在我们没有任何重复次数 >0 但 <9 的设置。
在理想的情况下,如果存在多个最佳解决方案,则应选择具有最大扩展绝对差异的解决方案(即具有最小最高差异的解决方案)。
这个比较棘手,并且(至少现在),我将把它留给一个理想的世界,或者另一个人的答案。
顺便说一句:有人正在努力推出一个用于运筹学的 Stack Exchange 站点,我们将在其中讨论诸如此类的问题。如果您有兴趣,我鼓励您单击链接并“提交”。