在 PULP 中将约束与 objective 相关联
Relating constraints with objective in PULP
我正在尝试解决 MIP 问题。我试图通过最小化完成需求所用的总时间来找出每个技术人员在一周内的某个日期要完成的考试数量。
我在单独的数据框中有需求、每个技术花费的时间、技术列表等。
我能够找到最小化 objective 的考试数量,但是我希望添加一个限制,将技术人员的最大数量设置为 8。
我添加了一些二进制变量来添加条件,但是我无法将其与 objective 函数相关联。
下面是我目前的代码:
model = pulp.LpProblem("Time minimizing problem", pulp.LpMinimize)
capacity = pulp.LpVariable.dicts("capacity",
((examdate , techname, region) for examdate, techname, region in tech_data_new.index),
lowBound=0,
cat='Integer')
for examdate, techname,region in tech_data_new.index:
var = capacity[(examdate,techname,region)]
var.upBound = tech_data_new.loc[(examdate,techname,region), 'Max Capacity']
model += pulp.lpSum(capacity[examdate,techname, region] * tech_data_new.loc[(examdate,techname, region), 'Time taken'] for examdate,techname, region in tech_data_new.index)
for date in demand_data.index.get_level_values('Exam Date').unique():
for i in demand_data.loc[date].index.tolist():
model += pulp.lpSum([capacity[examdate,techname,region] for examdate, techname, region in tech_data_new.index
if (date == examdate and i == region)]) == demand_data.loc[(demand_data.index.get_level_values('Exam Date') == date) & (demand_data.index.get_level_values('Body Region') == i), shiftname].item()
这些是二进制变量,我尝试添加但无法与 objective 相关,因为乘法会使它成为非线性。
techs = pulp.LpVariable.dicts("techs",
(techname for techname in tech_data_new.index.get_level_values('Technologist Name').unique()),
cat='Binary')
days = pulp.LpVariable.dicts("day",
(examdate for examdate in tech_data_new.index.get_level_values('Exam Date').unique()),
cat='Binary')
如有任何线索,我们将不胜感激。
提前致谢。
如果需要任何其他支持,请告诉我。
如果要为每个 examdate
设置一个约束条件,并且具有索引 (examdate, techname, region)
的整数变量 capacity
表示每个技术在每个日期的考试次数区域,然后我将创建一组二进制变量 tech_used
,索引为 (examdate, techname)
,表示每天是否使用 reach tech。
定义这些变量后,您需要设置约束,以便它们按要求运行...假设列表 examdates, technames, regions
已被适当声明:
for examdate in examdates:
for techname in technames:
model += Lp.sum([capacity[examdate, techname, region] for region in regions]) < max_capacity*tech_used(examdate, techname)
请注意,上面的 max_capacity
应该是每个技术的容量,这些限制可以取代您在其他地方设置的最大容量限制。
那么你只需要将你的技术数量限制设置为最多 8:
for examdate in examdates:
model += Lp.sum([tech_used[examdate, techname] for techname in technames]) <= 8
我正在尝试解决 MIP 问题。我试图通过最小化完成需求所用的总时间来找出每个技术人员在一周内的某个日期要完成的考试数量。
我在单独的数据框中有需求、每个技术花费的时间、技术列表等。
我能够找到最小化 objective 的考试数量,但是我希望添加一个限制,将技术人员的最大数量设置为 8。
我添加了一些二进制变量来添加条件,但是我无法将其与 objective 函数相关联。
下面是我目前的代码:
model = pulp.LpProblem("Time minimizing problem", pulp.LpMinimize)
capacity = pulp.LpVariable.dicts("capacity",
((examdate , techname, region) for examdate, techname, region in tech_data_new.index),
lowBound=0,
cat='Integer')
for examdate, techname,region in tech_data_new.index:
var = capacity[(examdate,techname,region)]
var.upBound = tech_data_new.loc[(examdate,techname,region), 'Max Capacity']
model += pulp.lpSum(capacity[examdate,techname, region] * tech_data_new.loc[(examdate,techname, region), 'Time taken'] for examdate,techname, region in tech_data_new.index)
for date in demand_data.index.get_level_values('Exam Date').unique():
for i in demand_data.loc[date].index.tolist():
model += pulp.lpSum([capacity[examdate,techname,region] for examdate, techname, region in tech_data_new.index
if (date == examdate and i == region)]) == demand_data.loc[(demand_data.index.get_level_values('Exam Date') == date) & (demand_data.index.get_level_values('Body Region') == i), shiftname].item()
这些是二进制变量,我尝试添加但无法与 objective 相关,因为乘法会使它成为非线性。
techs = pulp.LpVariable.dicts("techs",
(techname for techname in tech_data_new.index.get_level_values('Technologist Name').unique()),
cat='Binary')
days = pulp.LpVariable.dicts("day",
(examdate for examdate in tech_data_new.index.get_level_values('Exam Date').unique()),
cat='Binary')
如有任何线索,我们将不胜感激。 提前致谢。 如果需要任何其他支持,请告诉我。
如果要为每个 examdate
设置一个约束条件,并且具有索引 (examdate, techname, region)
的整数变量 capacity
表示每个技术在每个日期的考试次数区域,然后我将创建一组二进制变量 tech_used
,索引为 (examdate, techname)
,表示每天是否使用 reach tech。
定义这些变量后,您需要设置约束,以便它们按要求运行...假设列表 examdates, technames, regions
已被适当声明:
for examdate in examdates:
for techname in technames:
model += Lp.sum([capacity[examdate, techname, region] for region in regions]) < max_capacity*tech_used(examdate, techname)
请注意,上面的 max_capacity
应该是每个技术的容量,这些限制可以取代您在其他地方设置的最大容量限制。
那么你只需要将你的技术数量限制设置为最多 8:
for examdate in examdates:
model += Lp.sum([tech_used[examdate, techname] for techname in technames]) <= 8