I am trying to optimize the store schedule using the PuLP module, but I am facing a problem with the 4th constraint The constraints will be elaborated on below
Ex: "S4" should be scheduled in three days only
EX:" S4" store
If its first day was scheduled on SAT the other days will be MON and WED
If its first day was scheduled on SUN the other days will be TUE and THU
EX:" S8" store
I got an optimal solution although it is not the result that I need as the output shows two consecutive days, so I guess I have a locig problem
Ex: enter image description here
The Result that I want to be shown as the below table Store ROUTE Carton SAT SUN MON TUE WED THU DROPS
import pulp
import pandas as pd
import numpy as np
from pulp import *
StoreSched = pd.DataFrame(columns = ["Store_Code","Route","Demand"])
Capacity = 5000
route="R1"
days_list=["SAT","SUN","MON", "TUE","WED","THU"]
no_days_list = range(1,7)
Store = ["S1","S2","S3","S4","S5","S6","S7","S8","S9","S10"]
Store_demand = {
"S1":400,
"S2":300,
"S3":250 ,
"S4":200 ,
"S5":300,
"S6":200 ,
"S7":300,
"S8":200 ,
"S9":300,
"S10":300,
}
store_Days = {
"S1":6 ,
"S2":6,
"S3":6 ,
"S4":3,
"S5":3,
"S6":3,
"S7":2,
"S8":2,
"S9":2,
"S10":1 ,
}
prob = LpProblem("store_schedule",LpMaximize)
storeVars = LpVariable.dicts("Days",(no_days_list,Store),0,1,LpInteger)
for d in no_days_list:
# The capacity should not exceeed 1500 in one day
prob += pulp.lpSum([Store_demand[s] * storeVars[d][s] for s in Store]) <= Capacity
for s in Store:
# Every store should be assigned based on its DayNo.
prob += pulp.lpSum(storeVars[d][s] for d in no_days_list) == store_Days[s]
for s in Store:
# one day gap between the assigned dayes for the stores that have three days
if store_Days[s] == 3 :
for d in no_days_list[:-1]:
prob += storeVars[d][s] + storeVars[d+1][s] == 1
for s in Store:
if store_Days[s] == 2 :
for d in no_days_list[:-2]:
prob += storeVars[d][s] + storeVars[d+2][s] == 1
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])
code= vi.name.split("_")[2];
#print(code)
day = days_list[int(vi.name.split("_")[1])-1];
#print(day)
if ((StoreSched['Store_Code'] == code).any() == False):
StoreSched = StoreSched.append({'Store_Code': code,"Route":route,"Days":store_Days[code],"Demand":Store_demand[code]}, ignore_index=True)
for index in StoreSched.index:
if StoreSched.loc[index,'Store_Code']== code:
StoreSched.loc[index,day] = 1
StoreSched.fillna(0,inplace=True)
StoreSched
Change the constraint to prob += storeVars[d][s] + storeVars[d+1][s] + storeVars[d+2][s] == 1
if you want a gap of 2 between the days.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.