我是线性编程的新手,并且正在通过一个最小化工厂生产成本的例子。这个例子中包含了打开和关闭工厂的能力。如何在PuLP中添加约束条件以保持工厂开启或关闭一段时间?
我的问题是如何添加额外的限制,如果工厂关闭,那么它需要停留3个月,如果它切换回来,那么它需要保持4个月?
代码:
import pandas as pd
import pulp
factories = pd.DataFrame.from_csv('csv/factory_variables.csv', index_col=['Month', 'Factory'])
demand = pd.DataFrame.from_csv('csv/monthly_demand.csv', index_col=['Month'])
# Production
production = pulp.LpVariable.dicts("production",
((month, factory) for month, factory in factories.index),
lowBound=0,
cat='Integer')
# Factory Status, On or Off
factory_status = pulp.LpVariable.dicts("factory_status",
((month, factory) for month, factory in factories.index),
cat='Binary')
# Factory switch on or off
switch_on = pulp.LpVariable.dicts("switch_on",
((month, factory) for month, factory in factories.index),
cat='Binary')
# Instantiate the model
model = pulp.LpProblem("Cost minimising scheduling problem", pulp.LpMinimize)
# Select index on factory A or B
factory_A_index = [tpl for tpl in factories.index if tpl[1] == 'A']
factory_B_index = [tpl for tpl in factories.index if tpl[1] == 'B']
# Define objective function
model += pulp.lpSum(
[production[m, f] * factories.loc[(m, f), 'Variable_Costs'] for m, f in factories.index]
+ [factory_status[m, f] * factories.loc[(m, f), 'Fixed_Costs'] for m, f in factories.index]
+ [switch_on[m, f] * 20000 for m, f in factory_A_index]
+ [switch_on[m, f] * 400000 for m, f in factory_B_index]
)
# Production in any month must be equal to demand
months = demand.index
for month in months:
model += production[(month, 'A')] + production[(month, 'B')] == demand.loc[month, 'Demand']
# Production in any month must be between minimum and maximum capacity, or zero.
for month, factory in factories.index:
min_production = factories.loc[(month, factory), 'Min_Capacity']
max_production = factories.loc[(month, factory), 'Max_Capacity']
model += production[(month, factory)] >= min_production * factory_status[month, factory]
model += production[(month, factory)] <= max_production * factory_status[month, factory]
# Factory B is off in May
model += factory_status[5, 'B'] == 0
model += production[5, 'B'] == 0
#Constraints for switching factory on and off
for month, factory in factories.index:
# In month 1, if the factory ison, we assume it turned on
if month == 1:
model += switch_on[month, factory] == factory_status[month, factory]
# In other months, if the factory is on in the current month AND off in the previous month, switch on = 1
else:
model += switch_on[month, factory] >= factory_status[month, factory] - factory_status[month-1, factory]
model += switch_on[month, factory] <= 1 - factory_status[month-1, factory]
model += switch_on[month, factory] <= factory_status[month, factory]
model.solve()
pulp.LpStatus[model.status]
output = []
for month, factory in production:
var_output = {
'Month': month,
'Factory': factory,
'Production': production[(month, factory)].varValue,
'Factory Status': factory_status[(month, factory)].varValue,
'Switch On': switch_on[(month, factory)].varValue
}
output.append(var_output)
output_df = pd.DataFrame.from_records(output).sort_values(['Month', 'Factory'])
output_df.set_index(['Month', 'Factory'], inplace=True)
output_df
感谢您的帮助和快速回复我的问题Ioannis!我从来没有考虑过使用'switch_off'变量。我的研究还没有扩展到LP放松或Lazy Pool限制的话题。你能解释他们还是推荐阅读材料?我想我会首先尝试第一个选项,因为我知道那里发生了什么 - 我会随着我的进度更新此帖。干杯! – usermw
在评论中解释整数编程有点困难。简而言之,在实践中解决比线性编程要困难得多,并且有理论上的理由。 [This](http://inside.mines.edu/~anewman/MIP_practice120212.pdf)是开始阅读更多细节的好地方。祝你好运,享受! – Ioannis