Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -111,8 +111,8 @@ def main():
|
|
| 111 |
# Sidebar controls
|
| 112 |
with st.sidebar:
|
| 113 |
st.header("Optimization Settings")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
max_exposure = st.slider("Max player exposure (%)", 10, 100, 60)
|
| 117 |
min_salary = st.slider("Min Salary", 48000, 49900, 48500, 100)
|
| 118 |
max_salary = st.slider("Max Salary", min_salary, 50000, 50000, 100)
|
|
@@ -131,19 +131,88 @@ def main():
|
|
| 131 |
|
| 132 |
# Generate lineups using the optimizer
|
| 133 |
optimizer.setup_model(data_processor.players_df, max_salary)
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
optimizer.add_exposure_constraints(data_processor.players_df, max_exposure/100)
|
| 136 |
|
| 137 |
# Generate the specified number of lineups
|
| 138 |
-
lineups =
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
if not lineups:
|
| 149 |
st.error("No valid lineups found with given constraints")
|
|
@@ -252,4 +321,5 @@ def main():
|
|
| 252 |
st.success(f"✅ Generated {len(st.session_state.df_lineups)} lineups (showing {len(filtered_lineups)} after filtering) and saved to Google Sheets!")
|
| 253 |
|
| 254 |
if __name__ == "__main__":
|
| 255 |
-
|
|
|
|
|
|
| 111 |
# Sidebar controls
|
| 112 |
with st.sidebar:
|
| 113 |
st.header("Optimization Settings")
|
| 114 |
+
# Removed stacking controls
|
| 115 |
+
max_players_per_team = st.slider("Max players from same team", 1, 5, 5)
|
| 116 |
max_exposure = st.slider("Max player exposure (%)", 10, 100, 60)
|
| 117 |
min_salary = st.slider("Min Salary", 48000, 49900, 48500, 100)
|
| 118 |
max_salary = st.slider("Max Salary", min_salary, 50000, 50000, 100)
|
|
|
|
| 131 |
|
| 132 |
# Generate lineups using the optimizer
|
| 133 |
optimizer.setup_model(data_processor.players_df, max_salary)
|
| 134 |
+
|
| 135 |
+
# Add team constraint without stacking requirement
|
| 136 |
+
# Only enforce maximum players per team
|
| 137 |
+
teams = data_processor.players_df["Team Name"].unique()
|
| 138 |
+
for team in teams:
|
| 139 |
+
team_vars = [optimizer.player_vars[f"player_{idx}"]
|
| 140 |
+
for idx, row in data_processor.players_df.iterrows()
|
| 141 |
+
if row["Team Name"] == team]
|
| 142 |
+
if team_vars:
|
| 143 |
+
optimizer.model += lpSum(team_vars) <= max_players_per_team
|
| 144 |
+
|
| 145 |
optimizer.add_exposure_constraints(data_processor.players_df, max_exposure/100)
|
| 146 |
|
| 147 |
# Generate the specified number of lineups
|
| 148 |
+
lineups = []
|
| 149 |
+
|
| 150 |
+
for i in range(num_lineups):
|
| 151 |
+
try:
|
| 152 |
+
# Setup optimizer for each lineup
|
| 153 |
+
if i > 0:
|
| 154 |
+
optimizer.setup_model(data_processor.players_df, max_salary)
|
| 155 |
+
|
| 156 |
+
# Add team constraint without stacking
|
| 157 |
+
for team in teams:
|
| 158 |
+
team_vars = [optimizer.player_vars[f"player_{idx}"]
|
| 159 |
+
for idx, row in data_processor.players_df.iterrows()
|
| 160 |
+
if row["Team Name"] == team]
|
| 161 |
+
if team_vars:
|
| 162 |
+
optimizer.model += lpSum(team_vars) <= max_players_per_team
|
| 163 |
+
|
| 164 |
+
# Add exposure constraint
|
| 165 |
+
optimizer.add_exposure_constraints(data_processor.players_df, max_exposure/100)
|
| 166 |
+
|
| 167 |
+
# Add minimum salary constraint
|
| 168 |
+
optimizer.model += lpSum([optimizer.player_vars[f"player_{idx}"] * row["Credits"]
|
| 169 |
+
for idx, row in data_processor.players_df.iterrows()]) >= min_salary
|
| 170 |
+
|
| 171 |
+
# Add uniqueness constraint
|
| 172 |
+
for prev_lineup in lineups:
|
| 173 |
+
prev_players = [p["Player"] for p in prev_lineup["players"]]
|
| 174 |
+
overlap_vars = [optimizer.player_vars[f"player_{idx}"]
|
| 175 |
+
for idx, row in data_processor.players_df.iterrows()
|
| 176 |
+
if row["Player"] in prev_players]
|
| 177 |
+
if overlap_vars:
|
| 178 |
+
optimizer.model += lpSum(overlap_vars) <= 6 # Allow max 6 players overlap
|
| 179 |
+
|
| 180 |
+
# Solve the optimization model
|
| 181 |
+
from pulp import LpStatus, lpSum, value
|
| 182 |
+
optimizer.model.solve()
|
| 183 |
+
|
| 184 |
+
# Check if a solution was found
|
| 185 |
+
if optimizer.model.status != 1: # 1 means optimal solution found
|
| 186 |
+
st.warning(f"No optimal solution found for lineup {i+1}. Stopping generation.")
|
| 187 |
+
break
|
| 188 |
+
|
| 189 |
+
# Get the selected players from the solution
|
| 190 |
+
selected_players = []
|
| 191 |
+
total_points = 0
|
| 192 |
+
total_salary = 0
|
| 193 |
+
|
| 194 |
+
for var_name, var in optimizer.player_vars.items():
|
| 195 |
+
if var.value() > 0:
|
| 196 |
+
player_idx = int(var_name.split('_')[1])
|
| 197 |
+
player_data = data_processor.players_df.iloc[player_idx]
|
| 198 |
+
selected_players.append({
|
| 199 |
+
"Player": player_data["Player"],
|
| 200 |
+
"Position": player_data["Position"],
|
| 201 |
+
"Team": player_data["Team Name"],
|
| 202 |
+
"Salary": player_data["Credits"],
|
| 203 |
+
"Projected": player_data["Projected Points"]
|
| 204 |
+
})
|
| 205 |
+
total_points += player_data["Projected Points"]
|
| 206 |
+
total_salary += player_data["Credits"]
|
| 207 |
+
|
| 208 |
+
lineups.append({
|
| 209 |
+
"players": selected_players,
|
| 210 |
+
"total_points": total_points,
|
| 211 |
+
"total_salary": total_salary
|
| 212 |
+
})
|
| 213 |
+
except Exception as e:
|
| 214 |
+
st.error(f"Error generating lineup {i+1}: {str(e)}")
|
| 215 |
+
continue
|
| 216 |
|
| 217 |
if not lineups:
|
| 218 |
st.error("No valid lineups found with given constraints")
|
|
|
|
| 321 |
st.success(f"✅ Generated {len(st.session_state.df_lineups)} lineups (showing {len(filtered_lineups)} after filtering) and saved to Google Sheets!")
|
| 322 |
|
| 323 |
if __name__ == "__main__":
|
| 324 |
+
from pulp import LpVariable, LpProblem, LpMaximize, LpBinary, lpSum, LpStatus
|
| 325 |
+
main()
|