一道數(shù)學(xué)模型題
某服務(wù)部門一周中每天需要不同數(shù)目的雇員:周一到周四每天至少需要50人,周五至少需要80人,周六和周日至少需要90人,現(xiàn)規(guī)定應(yīng)聘者需要連續(xù)工作5天,是確定方案,即周一到周日每天聘用多少人,使在滿足需要的條件下聘用總?cè)藬?shù)最少?
1、首先使用pulp庫
from pulp import*
prob = LpProblem("The Whiskas Problem",LpMinimize)#用LpProblem來創(chuàng)建一個變量prob奔垦。這個函數(shù)具有兩個參數(shù),第一個為這個
#問題的描述字符串,第二個參數(shù)描述你需要解決的LP的類型,“LpMinimize"或則"LpMaximize":求最大解或者最小解
2酸纲、設(shè)置變量
day1 = LpVariable("day1",0,None,LpInteger)#描述 第一個字符串 第二個下邊界 第三個上邊界 第四個變量類型
day2 = LpVariable("day2",0,None,LpInteger)#如果是浮點(diǎn)型換成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.求最優(yōu)解
prob.solve()
prob.writeLP("WhiskasModel.lp") #打印出來相關(guān)信息
print("Status:", LpStatus[prob.status]) #最優(yōu)解
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() #處理出最優(yōu)解
prob.writeLP("WhiskasModel.lp") #打印出來相關(guān)信息
print("Status:", LpStatus[prob.status]) #最優(yōu)解
for v in prob.variables():
print(v.name, "=", v.varValue) #打印每個變量名字
print( "Total Cost of Ingredients per can = ", value(prob.objective))#百分比可以為