It is currently my first time using the PuLP library in python. The purpose of diving into this library was to make a fantasy football solver in python. I have successfully made the solver but cant figure out how to add a few constraints I need.
I have an excel sheet of 400 players and how I project them to play, and I want to find the optimal combination of 9 players given specific constraints. The excel sheet holds, player name, player projection, team player is on, opponent player is facing, and position. Below is what the head of the panda dataframe looks like.
Name Projection Position Team Salary Opponent
0 Jets 3.528576 DST NYJ 2000 IND
1 Texans 7.936528 DST HOU 2100 PIT
2 Panthers 4.219883 DST CAR 2200 LAC
3 Raiders 0.904948 DST LVR 2300 NE
The constraints i have done successfully: limit a max of 9 players are selected, only 1 person of position QB, 3-4 of position WR, 1-2 of position TE, 1 position DST, and 2-3 position RB.
raw_data = pd.read_csv(file_name,engine="python",index_col=False, header=0, delimiter=",", quoting = 3)
#create new columns that has binary numbers if player == a specific position
raw_data["RB"] = (raw_data["Position"] == 'RB').astype(float)
raw_data["WR"] = (raw_data["Position"] == 'WR').astype(float)
raw_data["QB"] = (raw_data["Position"] == 'QB').astype(float)
raw_data["TE"] = (raw_data["Position"] == 'TE').astype(float)
raw_data["DST"] = (raw_data["Position"] == 'DST').astype(float)
raw_data["Salary"] = raw_data["Salary"].astype(float)
total_points = {}
cost = {}
QBs = {}
RBs = {}
WRs = {}
TEs = {}
DST = {}
number_of_players = {}
# i = row index, player = player attributes
for i, player in raw_data.iterrows():
var_name = 'x' + str(i) # Create variable name
decision_var = pulp.LpVariable(var_name, cat='Binary') # Initialize Variables
total_points[decision_var] = player["Projection"] # Create Projection Dictionary
cost[decision_var] = player["Salary"] # Create Cost Dictionary
# Create Dictionary for Player Types
QBs[decision_var] = player["QB"]
RBs[decision_var] = player["RB"]
WRs[decision_var] = player["WR"]
TEs[decision_var] = player["TE"]
DST[decision_var] = player["DST"]
number_of_players[decision_var] = 1.0
QB_constraint = pulp.LpAffineExpression(QBs)
RB_constraint = pulp.LpAffineExpression(RBs)
WR_constraint = pulp.LpAffineExpression(WRs)
TE_constraint = pulp.LpAffineExpression(TEs)
DST_constraint = pulp.LpAffineExpression(DST)
total_players = pulp.LpAffineExpression(number_of_players)
model += (QB_constraint == 1)
model += (RB_constraint <= 3)
model += (RB_constraint >= 2)
model += (WR_constraint <= 4)
model += (WR_constraint >= 3)
model += (TE_constraint <= 2)
model += (TE_constraint >= 1)
model += (DST_constraint == 1)
model += (total_players == 9)
The constraints I am trying to add and cant figure out how: have 2 players of the 9 selected be on the same team as the QB, the opponent of the DST cant be anyone of the 9's team, have 1 players opponent be the QB's team. Any idea how I would do this? This data is in my excel file but I'm not sure how to add these constraints to the model?
Ive been looking through the cases in the documentation and i cant find any examples where the optimal output is changed based off what the model picks. Example: if picks a quarterback, it affects the rest of the 8 players being selected.
Appreciate any help anyone can provide me