# optimizing a linear optimization function with linear constarints and binary variables

I am new to optimizations and trying to solve a problem, which I feel falls in the umbrella of optimization.

I have an ojective function that needs to be maximized

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



# constraints

#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

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


Its completely a linear problem, and there is no requirement of non linear optimization techniques.Can some one help me with how to solve such problems or are there any libraries in python which will help me solving this?

Thanks

Use scipy.optimize.minimize of the negative objective function.

Use the link to play and modify as you wish but here is application in the nutshell. Lets say you have your data

> array([[ 0.00749589,  0.01255155,  0.02396251,  0.04750988, 0.09495377]
>        [ 0.01255155,  0.02510441,  0.04794055,  0.09502834,  0.18996269],
>        [ 0.02396251,  0.04794055,  0.09631614,  0.19092151,  0.38165151],
>        [ 0.04750988,  0.09502834,  0.19092151,  0.38341252,  0.7664427 ],
>        [ 0.09495377,  0.18996269,  0.38165151,  0.7664427,   1.53713523]])


You should define your constraints dictionary in the following way (for example):

> cons = ({'type': 'eq', 'fun': lambda x:  x[0] - 2 * x[1] + 2},
> {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
> {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})


(accesing array/columns with indexing (x[0]) thats exactly your bats, bowls etc columns). Next just pass your negative objective function and you maximized it.