Spaces:
Sleeping
Sleeping
James McCool
commited on
Commit
·
15867d1
1
Parent(s):
1e41a61
Enhance lineup optimization in app.py: Modify optimize_lineup function to accept sidebar_site parameter, allowing for different salary cap constraints for DraftKings and FanDuel. Update UI to display optimal players by position, improving clarity and user experience.
Browse files
app.py
CHANGED
|
@@ -124,7 +124,7 @@ tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Team Stacks Range of Outcom
|
|
| 124 |
# ... existing code ...
|
| 125 |
|
| 126 |
@st.cache_data
|
| 127 |
-
def optimize_lineup(player_data):
|
| 128 |
"""
|
| 129 |
Creates optimal lineup based on median projections while respecting position and salary constraints
|
| 130 |
"""
|
|
@@ -140,7 +140,10 @@ def optimize_lineup(player_data):
|
|
| 140 |
prob += pulp.lpSum([players[row['Player']] * row['Median'] for idx, row in player_data.iterrows()])
|
| 141 |
|
| 142 |
# Constraint: Salary cap
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# Constraint: 9 players
|
| 146 |
prob += pulp.lpSum([players[row['Player']] for idx, row in player_data.iterrows()]) == 9
|
|
@@ -215,11 +218,43 @@ with st.sidebar:
|
|
| 215 |
roo_data = fd_roo_raw[fd_roo_raw['slate'] == str(sidebar_slate)]
|
| 216 |
roo_data = roo_data[roo_data['version'] == 'overall']
|
| 217 |
|
| 218 |
-
optimal_players, total_salary, total_median = optimize_lineup(roo_data)
|
| 219 |
-
|
| 220 |
st.write("Optimal Lineup:")
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
st.write(f"Total Salary: ${total_salary:,}")
|
| 224 |
st.write(f"Projected Points: {total_median:.2f}")
|
| 225 |
|
|
|
|
| 124 |
# ... existing code ...
|
| 125 |
|
| 126 |
@st.cache_data
|
| 127 |
+
def optimize_lineup(player_data, sidebar_site):
|
| 128 |
"""
|
| 129 |
Creates optimal lineup based on median projections while respecting position and salary constraints
|
| 130 |
"""
|
|
|
|
| 140 |
prob += pulp.lpSum([players[row['Player']] * row['Median'] for idx, row in player_data.iterrows()])
|
| 141 |
|
| 142 |
# Constraint: Salary cap
|
| 143 |
+
if sidebar_site == 'Draftkings':
|
| 144 |
+
prob += pulp.lpSum([players[row['Player']] * row['Salary'] for idx, row in player_data.iterrows()]) <= 50000
|
| 145 |
+
elif sidebar_site == 'Fanduel':
|
| 146 |
+
prob += pulp.lpSum([players[row['Player']] * row['Salary'] for idx, row in player_data.iterrows()]) <= 60000
|
| 147 |
|
| 148 |
# Constraint: 9 players
|
| 149 |
prob += pulp.lpSum([players[row['Player']] for idx, row in player_data.iterrows()]) == 9
|
|
|
|
| 218 |
roo_data = fd_roo_raw[fd_roo_raw['slate'] == str(sidebar_slate)]
|
| 219 |
roo_data = roo_data[roo_data['version'] == 'overall']
|
| 220 |
|
| 221 |
+
optimal_players, total_salary, total_median = optimize_lineup(roo_data, sidebar_site)
|
| 222 |
+
|
| 223 |
st.write("Optimal Lineup:")
|
| 224 |
+
|
| 225 |
+
# Sort players into position groups
|
| 226 |
+
qb = [p for p in optimal_players if p['Position'] == 'QB'][0]
|
| 227 |
+
rbs = [p for p in optimal_players if p['Position'] == 'RB']
|
| 228 |
+
wrs = [p for p in optimal_players if p['Position'] == 'WR']
|
| 229 |
+
tes = [p for p in optimal_players if p['Position'] == 'TE']
|
| 230 |
+
dst = [p for p in optimal_players if p['Position'] == 'DST'][0]
|
| 231 |
+
|
| 232 |
+
# Display QB
|
| 233 |
+
st.write(f"QB: {qb['Player']} (${qb['Salary']:,})")
|
| 234 |
+
|
| 235 |
+
# Display RB1 and RB2
|
| 236 |
+
st.write(f"RB: {rbs[0]['Player']} (${rbs[0]['Salary']:,})")
|
| 237 |
+
st.write(f"RB: {rbs[1]['Player']} (${rbs[1]['Salary']:,})")
|
| 238 |
+
|
| 239 |
+
# Display WR1, WR2, WR3
|
| 240 |
+
st.write(f"WR: {wrs[0]['Player']} (${wrs[0]['Salary']:,})")
|
| 241 |
+
st.write(f"WR: {wrs[1]['Player']} (${wrs[1]['Salary']:,})")
|
| 242 |
+
st.write(f"WR: {wrs[2]['Player']} (${wrs[2]['Salary']:,})")
|
| 243 |
+
|
| 244 |
+
# Display TE1
|
| 245 |
+
st.write(f"TE: {tes[0]['Player']} (${tes[0]['Salary']:,})")
|
| 246 |
+
|
| 247 |
+
# Display FLEX (either RB3, WR4, or TE2)
|
| 248 |
+
if len(rbs) > 2:
|
| 249 |
+
st.write(f"FLEX (RB): {rbs[2]['Player']} (${rbs[2]['Salary']:,})")
|
| 250 |
+
elif len(wrs) > 3:
|
| 251 |
+
st.write(f"FLEX (WR): {wrs[3]['Player']} (${wrs[3]['Salary']:,})")
|
| 252 |
+
elif len(tes) > 1:
|
| 253 |
+
st.write(f"FLEX (TE): {tes[1]['Player']} (${tes[1]['Salary']:,})")
|
| 254 |
+
|
| 255 |
+
# Display DST
|
| 256 |
+
st.write(f"DST: {dst['Player']} (${dst['Salary']:,})")
|
| 257 |
+
|
| 258 |
st.write(f"Total Salary: ${total_salary:,}")
|
| 259 |
st.write(f"Projected Points: {total_median:.2f}")
|
| 260 |
|