1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| from scipy.optimize import linprog players = playerLS.drop(columns=['minYear','maxYear','1B','2B','3B','HR','BB','nameFirst','nameLast']) players['salary'] = players['salary']/1000000 players = players.sort_values('POS').reset_index().drop(columns=['index'])
start_1B = players[players['POS'] == '1B'].first_valid_index() end_1B = players[players['POS'] == '1B'].last_valid_index()
start_2B = players[players['POS'] == '2B'].first_valid_index() end_2B = players[players['POS'] == '2B'].last_valid_index()
start_3B = players[players['POS'] == '3B'].first_valid_index() end_3B = players[players['POS'] == '3B'].last_valid_index()
start_C = players[players['POS'] == 'C'].first_valid_index() end_C = players[players['POS'] == 'C'].last_valid_index()
start_OF = players[players['POS'] == 'OF'].first_valid_index() end_OF = players[players['POS'] == 'OF'].last_valid_index()
start_SS = players[players['POS'] == 'SS'].first_valid_index() end_SS = players[players['POS'] == 'SS'].last_valid_index()
cofficients = np.zeros_like(np.arange(len(players), dtype=float))
constraint_1B = np.zeros_like(np.arange(len(players), dtype=float)) constraint_2B = np.zeros_like(np.arange(len(players), dtype=float)) constraint_3B = np.zeros_like(np.arange(len(players), dtype=float)) constraint_C = np.zeros_like(np.arange(len(players), dtype=float)) constraint_OF = np.zeros_like(np.arange(len(players), dtype=float)) constraint_SS = np.zeros_like(np.arange(len(players), dtype=float)) constraint_salary = np.zeros_like(np.arange(len(players), dtype=float))
constraints_value_eq = [1,1,1,1,4,1] constraints_value_ub = [25]
bounds = []
for i, row in players.iterrows(): if i >= start_1B and i <= end_1B: constraint_1B[i] = 1 if i >= start_2B and i <= end_2B: constraint_2B[i] = 1 if i >= start_3B and i <= end_3B: constraint_3B[i] = 1 if i >= start_C and i <= end_C: constraint_C[i] = 1 if i >= start_OF and i <= end_OF: constraint_OF[i] = 1 if i >= start_SS and i <= end_SS: constraint_SS[i] = 1 constraint_salary[i] =row['salary'] cofficients[i] = -(row['OPW']) bounds.append([0,1])
constraints_eq = [constraint_1B, constraint_2B, constraint_3B, constraint_C, constraint_OF, constraint_SS]
constraints_ub = [constraint_salary]
res = linprog(cofficients, A_ub=constraints_ub, b_ub=constraints_value_ub, A_eq=constraints_eq, b_eq=constraints_value_eq, bounds=bounds, options={"disp": True, 'maxiter': 20000000000}) results = res.x team = {'playerID':[]} salary = 0 total_opw = 0
for i in range(0, len(results)): if results[i] > 0.5: salary += players.iloc[i]['salary'] team['playerID'].append(players.iloc[i]['playerID']) salary += players.iloc[i]['salary'] total_opw += players.iloc[i]['OPW'] team = pd.DataFrame(team) team = pd.merge(team, playerLS, how='inner', on='playerID') print('The average of OPW is', total_opw/9) team
|