python中带线性约束和二元变量的线性目标函数的优化

2024-06-26 13:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的优化和试图解决一个问题,我觉得在伞优化下降。你知道吗

我有一个目标函数需要最大化

def objective(bat1,bat2,bat3,bat4,bat5,bat6,bat7,wk1,wk2,ar1,ar2,ar3,ar4,ar5,bowl1,bowl2,bowl3,bowl4,bowl5,bowl6):
    total_score_batsman =  bat1*60 + bat2*40 + bat3*36 + bat4*35 + bat5*25 + bat6*22 +bat7*9
    total_score_wks = wk1*24 + wk2* 14 
    total_score_ar = ar1*45 + ar2*24 + ar3*15 + ar4*1
    total_score_bowler = bowl1*64 + bowl2*47 + bowl3*16 + bowl4*7 + bowl5*5 + bowl6*4
    return total_score_batsman + total_score_wks + total_score_ar + total_score_bowler #needs to be maximized

约束条件

#budget constraint

def budget(bat1,bat2,bat3,bat4,bat5,bat6,bat7,wk1,wk2,ar1,ar2,ar3,ar4,ar5,bowl1,bowl2,bowl3,bowl4,bowl5,bowl6):
    batsman_budget = bat1*10.5 + bat2*8.5 + bat3*10.5 + bat4*8.5 + bat5*9.5 + bat6*9 +bat7*9
    wk_budget = wk1*8.5 + wk2*8
    ar_budget = ar1*8.5 + ar2*9 + ar3*8.5 + ar4*8
    bowler_budget = bowl1*9 + bowl2*8.5 + bowl3*8.5 + bowl4*8.5 + bowl5*9 + bowl6*9
    total_budget = batsman_budget + wk_budget + ar_budget + bowler_budget
    return total_budget

total_budget <= 100 #constraint
#player_role constraints

bat1 + bat2 + bat3 + bat4 + bat5 + bat6 + bat7 >= 3
bat1 + bat2 + bat3 + bat4 + bat5 + bat6 + bat7 <= 5

wk1 + wk2 = 1

ar1 + ar2 + ar3 + ar4 + ar5 >= 1
ar1 + ar2 + ar3 + ar4 + ar5 <= 3

bowl1 + bowl2 + bowl3 + bowl4 + bowl5 + bowl6 >= 3
bowl1 + bowl2 + bowl3 + bowl4 + bowl5 + bowl6 <= 5

# no of players in a team constraint
bat1 + bat2 + bat3 + bat4 + bat5 + bat6 + bat7 + wk1 + wk2 + ar1 + ar2 + ar3 + ar4 + ar5 + bowl1 + bowl2 + bowl3 + bowl4 + bowl5 + bowl6 = 11

where bat1,bat2,bat3.....bowl5,bowl6 are 0 or 1

它完全是一个线性问题,不需要非线性优化技术。可以有人帮助我解决这些问题,或者python中有没有库可以帮助我解决这些问题?你知道吗

谢谢


Tags: totalscorebudgetbat1bat2ar2ar1ar3
1条回答
网友
1楼 · 发布于 2024-06-26 13:27:57

你可以使用PuLP

from pulp import *

prob = LpProblem("The Whiskas Problem",LpMaximize)

bat1 = LpVariable("bat1", cat=LpBinary)
bat2 = LpVariable("bat2", cat=LpBinary)
bat3 = LpVariable("bat3", cat=LpBinary)
bat4 = LpVariable("bat4", cat=LpBinary)
bat5 = LpVariable("bat5", cat=LpBinary)
bat6 = LpVariable("bat6", cat=LpBinary)
bat7 = LpVariable("bat7", cat=LpBinary)

wk1 = LpVariable("wk1", cat=LpBinary)
wk2 = LpVariable("wk2", cat=LpBinary)

ar1 = LpVariable("ar1", cat=LpBinary)
ar2 = LpVariable("ar2", cat=LpBinary)
ar3 = LpVariable("ar3", cat=LpBinary)
ar4 = LpVariable("ar4", cat=LpBinary)
ar5 = LpVariable("ar5", cat=LpBinary)

bowl1 = LpVariable("bowl1", cat=LpBinary)
bowl2 = LpVariable("bowl2", cat=LpBinary)
bowl3 = LpVariable("bowl3", cat=LpBinary)
bowl4 = LpVariable("bowl4", cat=LpBinary)
bowl5 = LpVariable("bowl5", cat=LpBinary)
bowl6 = LpVariable("bowl6", cat=LpBinary)

total_score_batsman = LpVariable("total_score_batsman")
total_score_wks = LpVariable("total_score_wks")
total_score_ar = LpVariable("total_score_ar")
total_score_bowler = LpVariable("total_score_bowler")

batsman_budget = LpVariable("batsman_budget")
wk_budget = LpVariable("wk_budget")
ar_budget = LpVariable("ar_budget")
bowler_budget = LpVariable("bowler_budget")

prob += total_score_batsman + total_score_wks + total_score_ar + total_score_bowler, "Objective"

prob += total_score_batsman ==  bat1*60 + bat2*40 + bat3*36 + bat4*35 + bat5*25 + bat6*22 +bat7*9
prob += total_score_wks == wk1*24 + wk2* 14 
prob += total_score_ar == ar1*45 + ar2*24 + ar3*15 + ar4*1
prob += total_score_bowler == bowl1*64 + bowl2*47 + bowl3*16 + bowl4*7 + bowl5*5 + bowl6*4

prob += batsman_budget == bat1*10.5 + bat2*8.5 + bat3*10.5 + bat4*8.5 + bat5*9.5 + bat6*9 +bat7*9
prob += wk_budget == wk1*8.5 + wk2*8
prob += ar_budget == ar1*8.5 + ar2*9 + ar3*8.5 + ar4*8
prob += bowler_budget == bowl1*9 + bowl2*8.5 + bowl3*8.5 + bowl4*8.5 + bowl5*9 + bowl6*9

prob += batsman_budget + wk_budget + ar_budget + bowler_budget <= 100


prob += bat1 + bat2 + bat3 + bat4 + bat5 + bat6 + bat7 >= 3
prob += bat1 + bat2 + bat3 + bat4 + bat5 + bat6 + bat7 <= 5

prob += wk1 + wk2 == 1

prob += ar1 + ar2 + ar3 + ar4 + ar5 >= 1
prob += ar1 + ar2 + ar3 + ar4 + ar5 <= 3

prob += bowl1 + bowl2 + bowl3 + bowl4 + bowl5 + bowl6 >= 3
prob += bowl1 + bowl2 + bowl3 + bowl4 + bowl5 + bowl6 <= 5

prob += bat1 + bat2 + bat3 + bat4 + bat5 + bat6 + bat7 + wk1 + wk2 + ar1 + ar2 + ar3 + ar4 + ar5 + bowl1 + bowl2 + bowl3 + bowl4 + bowl5 + bowl6 == 11


status = prob.solve()

print("Status:", LpStatus[status])
print("Object = {}".format(prob.objective.value()))
for i, x in enumerate(prob.variables()[1:]):
    print("{} = {}".format(x.name, x.varValue))

结果:

Status: Optimal
Object = 416.0
ar2 = 1.0
ar3 = 0.0
ar4 = 0.0
ar5 = 0.0
ar_budget = 17.5
bat1 = 1.0
bat2 = 1.0
bat3 = 1.0
bat4 = 1.0
bat5 = 1.0
bat6 = 0.0
bat7 = 0.0
batsman_budget = 47.5
bowl1 = 1.0
bowl2 = 1.0
bowl3 = 1.0
bowl4 = 0.0
bowl5 = 0.0
bowl6 = 0.0
bowler_budget = 26.0
total_score_ar = 69.0
total_score_batsman = 196.0
total_score_bowler = 127.0
total_score_wks = 24.0
wk1 = 1.0
wk2 = 0.0
wk_budget = 8.5

相关问题 更多 >