Skip to content

Commit

Permalink
allow dupe score lineups (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbusche authored Oct 14, 2024
1 parent 40f580e commit 2e27c30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
41 changes: 23 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
}

def calculate_lineups(lineup_type, output_file):
total_score = 0
lineup_results = []
previous_lineups = []

for lineup_num in range(1, MAX_LINEUPS + 1):
player_vars = {pos: LpVariable.dicts(pos, players, cat="Binary") for pos, players in points.items()}
Expand All @@ -49,39 +49,44 @@ def calculate_lineups(lineup_type, output_file):
for pos, count in lineup_type.items():
prob += lpSum([player_vars[pos][player] for player in player_vars[pos]]) == count

# Ensure each new lineup has a lower score than the previous
if lineup_num > 1:
prob += lpSum([points[pos][player] * player_vars[pos][player] for pos in player_vars for player in player_vars[pos]]) <= total_score - 0.001
# Add each unique lineup only once
for prev_lineup in previous_lineups:
prob += lpSum([player_vars[pos][player] for pos, player in prev_lineup]) <= len(prev_lineup) - 1

prob.solve()

current_lineup_players = [(pos, player) for pos in player_vars for player, var in player_vars[pos].items() if var.varValue == 1]

# break when we run out of unique lineups
if not current_lineup_players:
break

# Add the current lineup's players to the list of previous lineups
previous_lineups.append(current_lineup_players)

lineup = {
"Lineup #": lineup_num
}
total_score = 0
total_salary = 0
column_count = 1

for pos in player_vars:
for player, var in player_vars[pos].items():
if var.varValue == 1:
# Adding player details in a structured way for each position
lineup[f"Player {column_count} Position"] = pos
lineup[f"Player {column_count} Name"] = player
lineup[f"Player {column_count} Salary"] = salaries[pos][player]
lineup[f"Player {column_count} Projected Points"] = points[pos][player]
total_score += points[pos][player]
total_salary += salaries[pos][player]
column_count += 1
for pos, player in current_lineup_players:
lineup[f"Player {column_count} Position"] = pos
lineup[f"Player {column_count} Name"] = player
lineup[f"Player {column_count} Salary"] = salaries[pos][player]
lineup[f"Player {column_count} Projected Points"] = points[pos][player]
total_score += points[pos][player]
total_salary += salaries[pos][player]
column_count += 1

lineup["Total Salary"] = total_salary
lineup["Total Score"] = '{0:.1f}'.format(total_score)
lineup_results.append(lineup)

pd.DataFrame(lineup_results).to_csv(output_file + ".csv", index=False, header=False)

# Calculate lineups for each configuration
for lineup_name, lineup_type in lineup_configs.items():
calculate_lineups(lineup_type, lineup_name)
for name, config in lineup_configs.items():
calculate_lineups(config, name)

print("Lineup files created")
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pandas==2.2.3
pulp==2.9.0
openpyxl==3.1.5

0 comments on commit 2e27c30

Please sign in to comment.