Skip to content

Commit

Permalink
pre process more stuff (#13)
Browse files Browse the repository at this point in the history
* pre process more stuff

* clean up tests
  • Loading branch information
mrbusche authored Dec 21, 2024
1 parent a7c753b commit 84aa8a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 72 deletions.
73 changes: 41 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time

import pandas as pd
from pulp import LpMaximize, LpProblem, LpVariable, lpSum
from pulp import PULP_CBC_CMD, LpMaximize, LpProblem, LpVariable, lpSum

# Constants
POSITION = "DK Position"
PROJECTION = "DK Projection"
SALARY = "DK Salary"
Expand All @@ -16,48 +17,52 @@
"two_te": {"QB": 1, "RB": 2, "WR": 3, "TE": 2, "DST": 1}
}


def calculate_lineups(lineup_type, output_file, csv_file):
players = pd.read_csv(csv_file, usecols=[PLAYER, POSITION, PROJECTION, SALARY])
# trim whitespace from columns
players = players.apply(lambda x: x.str.strip() if x.dtype == "object" else x)

# Group players by position
available_players = players.groupby([POSITION, PLAYER, PROJECTION, SALARY]).size().reset_index()

# Create salary and points dicts by position
salaries = {
pos: available_players[available_players[POSITION] == pos].set_index(PLAYER)[SALARY].to_dict()
for pos in available_players[POSITION].unique()
}
points = {
pos: available_players[available_players[POSITION] == pos].set_index(PLAYER)[PROJECTION].to_dict()
for pos in available_players[POSITION].unique()
}
player_data = {}
for _, row in players.iterrows():
pos = row[POSITION]
if pos not in player_data:
player_data[pos] = {}
player_data[pos][row[PLAYER]] = (row[PROJECTION], row[SALARY])

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()}

prob = LpProblem("Fantasy", LpMaximize)
prob += lpSum([points[pos][player] * player_vars[pos][player] for pos in player_vars for player in player_vars[pos]])
prob += lpSum([salaries[pos][player] * player_vars[pos][player] for pos in player_vars for player in player_vars[pos]]) <= SALARY_CAP

player_vars = {}
for pos, players in player_data.items():
player_vars[pos] = LpVariable.dicts(f"{pos}_players", players.keys(), cat="Binary")

prob += lpSum([player_data[pos][player][0] * player_vars[pos][player]
for pos in player_vars for player in player_vars[pos]]), "Total_Points"

# Salary constraint
prob += lpSum([player_data[pos][player][1] * player_vars[pos][player]
for pos in player_vars for player in player_vars[pos]]) <= SALARY_CAP, "Salary_Cap"

# Enforce lineup constraints (how many players from each position)
for pos, count in lineup_type.items():
prob += lpSum([player_vars[pos][player] for player in player_vars[pos]]) == count
prob += lpSum([player_vars[pos][player] for player in player_vars[pos]]) == count, f"{pos}_constraint"

# 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
for counter, prev_lineup in enumerate(previous_lineups):
prob += lpSum([player_vars[pos][player] for pos, player in prev_lineup]) <= len(
prev_lineup) - 1, f"unique_lineup_{lineup_num}_{counter}"

prob.solve()
prob.solve(PULP_CBC_CMD(msg=0)) # Suppress noisy output

current_lineup_players = [(pos, player) for pos in player_vars for player, var in player_vars[pos].items() if var.varValue == 1]
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:
# only find unique lineups up to MAX_LINEUPS
if not current_lineup_players or len(previous_lineups) >= MAX_LINEUPS:
break

# Add the current lineup's players to the list of previous lineups
Expand All @@ -68,16 +73,16 @@ def calculate_lineups(lineup_type, output_file, csv_file):
}
total_score = 0
total_salary = 0
column_count = 1

for pos, player in current_lineup_players:
for column_count, (pos, player) in enumerate(current_lineup_players):
proj, sal = player_data[pos][player]
column_count = column_count + 1
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[f"Player {column_count} Salary"] = sal
lineup[f"Player {column_count} Projected Points"] = proj
total_score += proj
total_salary += sal

lineup["Total Salary"] = total_salary
lineup["Total Score"] = '{0:.1f}'.format(total_score)
Expand All @@ -94,5 +99,9 @@ def generate_lineup_files(csv_file):


if __name__ == "__main__":
start_time = time.time()
file_name = "draftkings.csv"
generate_lineup_files(file_name)
end_time = time.time()

print(f"Total execution time: {end_time - start_time:.2f} seconds")
60 changes: 20 additions & 40 deletions tests/test_things.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,16 @@ def test_three_rb():

conditions = [
len(three_rb) == 10,
three_rb[
0] == "1,DST,Cardinals,2600,7.1,QB,Lamar Jackson,8000,24.3,RB,Austin Powers,5300,16.2,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,148.2",
three_rb[
1] == "2,DST,Cardinals,2600,7.1,QB,Jameis Winston,5400,17.4,RB,Alvin and the Chipmunks,7800,22.9,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,148.0",
three_rb[
2] == "3,DST,Cardinals,2600,7.1,QB,Jameis Winston,5400,17.4,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,148.0",
three_rb[
3] == "4,DST,Titans,3200,8.7,QB,Jameis Winston,5400,17.4,RB,Alvin and the Chipmunks,7800,22.9,RB,Austin Powers,5300,16.2,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,147.9",
three_rb[
4] == "5,DST,Titans,3200,8.7,QB,Jameis Winston,5400,17.4,RB,Austin Powers,5300,16.2,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,147.9",
three_rb[
5] == "6,DST,Cardinals,2600,7.1,QB,Justin Herbert,5300,17.2,RB,Alvin and the Chipmunks,7800,22.9,RB,Austin Powers,5300,16.2,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,147.9",
three_rb[
6] == "7,DST,Cardinals,2600,7.1,QB,Gardner Minshew II,4500,14.7,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,49900,147.8",
three_rb[
7] == "8,DST,Cardinals,2600,7.1,QB,Justin Herbert,5300,17.2,RB,Alvin and the Chipmunks,7800,22.9,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,147.8",
three_rb[
8] == "9,DST,Cardinals,2600,7.1,QB,Justin Herbert,5300,17.2,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,147.8",
three_rb[
9] == "10,DST,Cardinals,2600,7.1,QB,Dak Prescott,6300,19.5,RB,Alvin and the Chipmunks,7800,22.9,RB,Austin Powers,5300,16.2,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,147.7",
three_rb[0][-5:] == "148.2",
three_rb[1][-5:] == "148.0",
three_rb[2][-5:] == "148.0",
three_rb[3][-5:] == "147.9",
three_rb[4][-5:] == "147.9",
three_rb[5][-5:] == "147.9",
three_rb[6][-5:] == "147.8",
three_rb[7][-5:] == "147.8",
three_rb[8][-5:] == "147.8",
three_rb[9][-5:] == "147.7",
]

assert all(conditions)
Expand All @@ -75,26 +65,16 @@ def test_two_te():

conditions = [
len(two_te) == 10,
two_te[
0] == "1,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,TE,Taysom Hill,3800,9.9,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,146.0",
two_te[
1] == "2,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,TE,Taysom Hill,3800,9.9,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,146.0",
two_te[
2] == "3,DST,Cardinals,2600,7.1,QB,Lamar Jackson,8000,24.3,RB,Alvin and the Chipmunks,7800,22.9,RB,Matt Busche,7800,22.9,TE,AJ Barner,2600,7.2,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,146.0",
two_te[
3] == "4,DST,Titans,3200,8.7,QB,Justin Herbert,5300,17.2,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,145.9",
two_te[
4] == "5,DST,Titans,3200,8.7,QB,Justin Herbert,5300,17.2,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,145.9",
two_te[
5] == "6,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,AJ Barner,2600,7.2,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,145.8",
two_te[
6] == "7,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,AJ Barner,2600,7.2,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,145.8",
two_te[
7] == "8,DST,Titans,3200,8.7,QB,Dak Prescott,6300,19.5,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,145.7",
two_te[
8] == "9,DST,Titans,3200,8.7,QB,Dak Prescott,6300,19.5,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,145.7",
two_te[
9] == "10,DST,Cardinals,2600,7.1,QB,Lamar Jackson,8000,24.3,RB,Chase from Paw Patrol,5900,17.9,RB,Matt Busche,7800,22.9,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,145.7",
two_te[0][-5:] == "146.0",
two_te[1][-5:] == "146.0",
two_te[2][-5:] == "146.0",
two_te[3][-5:] == "145.9",
two_te[4][-5:] == "145.9",
two_te[5][-5:] == "145.8",
two_te[6][-5:] == "145.8",
two_te[7][-5:] == "145.7",
two_te[8][-5:] == "145.7",
two_te[9][-5:] == "145.7",
]

assert all(conditions)

0 comments on commit 84aa8a8

Please sign in to comment.