使用 LPproblem class 添加 "Every other day Constraint"
Add "Every other day Constraint" using LPproblem class
我正在尝试使用PuLP模块优化商店排程,并且已经得到了预期的结果。
但是我只有一个问题,因为我需要向它添加一个新的约束
通过下面的代码,我将能够每天分发商店
基于以下限制
- 一天内Store_demand的总数不能超过容量(1000以内)
- 每家店铺将根据他们的天数分配(Store_Days)
例如:“S2”应该只在三天内安排
现在我需要在商店只有 三天 时添加一个新约束。
“每隔一天”获得一天间隔的条件
EX:“S2”商店
- 如果安排在 SAT,其他日子将是周一和周三
- 如果安排在星期天,其他日子将是星期二和星期四
capacity = 1000
days_list=["SAT","SUN","MON", "TUE","WED","THU"]
no_days_list = range(1,7)
store = ["S1","S2","S3","S4"]
Store_demand = {
"S1":400,
"S2":300,
"S3":250 ,
"S4":200 ,
}
Store_Days = {
"S1":6 ,
"S2":3,
"S3":3 ,
"S4":1 ,
}
prob = LpProblem("schedule",LpMaximize)
storeVars = LpVariable.dicts("Days",(no_days_list,Store),0,1,LpInteger)
for d in no_days_list:
prob += pulp.lpSum([Store_demand[s] * storeVars[d][s] for s in Store]) <= capacity
for s in Store:
prob += pulp.lpSum(storeVars[d][s] for d in no_days_list) == store_Days[s]
prob.solve()
for vi in prob.variables():
if vi.varValue == 1:
print(" On "+days_list[int(vi.name.split("_")[1])-1]+" Pharmacy code: "+vi.name.split("_")[2])
请指教
----------------已更新----------------
rob = LpProblem("schedule",LpMaximize)
storeVars = LpVariable.dicts("Days",(no_days_list,Store),0,1,LpInteger)
for d in no_days_list:
prob += pulp.lpSum([Store_demand[s] * storeVars[d][s] for s in Store]) <= capacity
for s in Store:
prob += pulp.lpSum(storeVars[d][s] for d in no_days_list) == store_Days[s]
if store_Days[s] == 3:
prob += pulp.lpSum([ storeVars[d][s] + storeVars[d][s] for d in no_days_list]) <= 1
prob.solve()
您可以通过为三天内营业的所有商店添加额外限制来实现此目的。下面的约束表示商店不能连续两天开张:
for s in Store:
if Store_Days[s] == 3:
for d in range(1,6):
prob += storeVars[d][s] + storeVars[d+1][s] <= 1
我正在尝试使用PuLP模块优化商店排程,并且已经得到了预期的结果。 但是我只有一个问题,因为我需要向它添加一个新的约束
通过下面的代码,我将能够每天分发商店 基于以下限制
- 一天内Store_demand的总数不能超过容量(1000以内)
- 每家店铺将根据他们的天数分配(Store_Days) 例如:“S2”应该只在三天内安排
现在我需要在商店只有 三天 时添加一个新约束。 “每隔一天”获得一天间隔的条件
EX:“S2”商店
- 如果安排在 SAT,其他日子将是周一和周三
- 如果安排在星期天,其他日子将是星期二和星期四
capacity = 1000
days_list=["SAT","SUN","MON", "TUE","WED","THU"]
no_days_list = range(1,7)
store = ["S1","S2","S3","S4"]
Store_demand = {
"S1":400,
"S2":300,
"S3":250 ,
"S4":200 ,
}
Store_Days = {
"S1":6 ,
"S2":3,
"S3":3 ,
"S4":1 ,
}
prob = LpProblem("schedule",LpMaximize)
storeVars = LpVariable.dicts("Days",(no_days_list,Store),0,1,LpInteger)
for d in no_days_list:
prob += pulp.lpSum([Store_demand[s] * storeVars[d][s] for s in Store]) <= capacity
for s in Store:
prob += pulp.lpSum(storeVars[d][s] for d in no_days_list) == store_Days[s]
prob.solve()
for vi in prob.variables():
if vi.varValue == 1:
print(" On "+days_list[int(vi.name.split("_")[1])-1]+" Pharmacy code: "+vi.name.split("_")[2])
请指教
----------------已更新----------------
rob = LpProblem("schedule",LpMaximize)
storeVars = LpVariable.dicts("Days",(no_days_list,Store),0,1,LpInteger)
for d in no_days_list:
prob += pulp.lpSum([Store_demand[s] * storeVars[d][s] for s in Store]) <= capacity
for s in Store:
prob += pulp.lpSum(storeVars[d][s] for d in no_days_list) == store_Days[s]
if store_Days[s] == 3:
prob += pulp.lpSum([ storeVars[d][s] + storeVars[d][s] for d in no_days_list]) <= 1
prob.solve()
您可以通过为三天内营业的所有商店添加额外限制来实现此目的。下面的约束表示商店不能连续两天开张:
for s in Store:
if Store_Days[s] == 3:
for d in range(1,6):
prob += storeVars[d][s] + storeVars[d+1][s] <= 1