我是線性編程的新手,並且正在通過一個最小化工廠生產成本的例子。這個例子中包含了打開和關閉工廠的能力。如何在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