一道数学模型题
某服务部门一周中每天需要不同数目的雇员:周一到周四每天至少需要50人,周五至少需要80人,周六和周日至少需要90人,现规定应聘者需要连续工作5天,是确定方案,即周一到周日每天聘用多少人,使在满足需要的条件下聘用总人数最少?
1、首先使用pulp库
from pulp import*
prob = LpProblem("The Whiskas Problem",LpMinimize)#用LpProblem来创建一个变量prob。这个函数具有两个参数,第一个为这个
#问题的描述字符串,第二个参数描述你需要解决的LP的类型,“LpMinimize"或则"LpMaximize":求最大解或者最小解
2、设置变量
day1 = LpVariable("day1",0,None,LpInteger)#描述 第一个字符串 第二个下边界 第三个上边界 第四个变量类型
day2 = LpVariable("day2",0,None,LpInteger)#如果是浮点型换成float
day3 = LpVariable("day3",0,None,LpInteger)
day4 = LpVariable("day4",0,None,LpInteger)
day5 = LpVariable("day5",0,None,LpInteger)
day6 = LpVariable("day6",0,None,LpInteger)
day7 = LpVariable("day7",0,None,LpInteger)
3.制定约束条件
prob += day1 + day2 + day3 + day4 + day5 + day6 + day7
prob += day1 + day4 + day5 + day6 + day7 >= 50
prob += day2 + day1 + day5 + day6 + day7 >= 50
prob += day3 + day1 + day2 + day6 + day7 >= 50
prob += day4 + day1 + day2 + day3 + day7 >= 50
prob += day5 + day1 + day2 + day3 + day4 >= 80
prob += day2 + day3 + day4 + day5 + day6 >= 90
prob += day3 + day4 + day5 + day6 + day7 >= 90
5.求最优解
prob.solve()
prob.writeLP("WhiskasModel.lp") #打印出来相关信息
print("Status:", LpStatus[prob.status]) #最优解
for v in prob.variables():
print(v.name, "=", v.varValue) #打印每个变量名字
print( "Total Cost of Ingredients per can = ", value(prob.objective))#百分比可以为
6.加一个比较高端的,慢慢看吧
Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE', 'WHEAT', 'GEL']
costs = {'CHICKEN': 0.013,
'BEEF': 0.008,
'MUTTON': 0.010,
'RICE': 0.002,
'WHEAT': 0.005,
'GEL': 0.001}
proteinPercent = {'CHICKEN': 0.100,
'BEEF': 0.200,
'MUTTON': 0.150,
'RICE': 0.000,
'WHEAT': 0.040,
'GEL': 0.000}
fatPercent = {'CHICKEN': 0.080,
'BEEF': 0.100,
'MUTTON': 0.110,
'RICE': 0.010,
'WHEAT': 0.010,
'GEL': 0.000}
fibrePercent = {'CHICKEN': 0.001,
'BEEF': 0.005,
'MUTTON': 0.003,
'RICE': 0.100,
'WHEAT': 0.150,
'GEL': 0.000}
saltPercent = {'CHICKEN': 0.002,
'BEEF': 0.005,
'MUTTON': 0.007,
'RICE': 0.002,
'WHEAT': 0.008,
'GEL': 0.000}
prob = LpProblem("The Whiskas Problem", LpMinimize)
ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)
prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), "Total Cost of Ingredients per can"
prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "PercentagesSum"
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 8.0, "ProteinRequirement"
prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 6.0, "FatRequirement"
prob += lpSum([fibrePercent[i] * ingredient_vars[i] for i in Ingredients]) <= 2.0, "FibreRequirement"
prob += lpSum([saltPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 0.4, "SaltRequirement"
prob.solve() #处理出最优解
prob.writeLP("WhiskasModel.lp") #打印出来相关信息
print("Status:", LpStatus[prob.status]) #最优解
for v in prob.variables():
print(v.name, "=", v.varValue) #打印每个变量名字
print( "Total Cost of Ingredients per can = ", value(prob.objective))#百分比可以为