I am creating an integer program in Python using pyomo for Major League Baseball (MLB) daily fantasy. Given a lineup of 10 players (1 of each position plus 2 pitchers) and a salary cap, the program returns the lineup that maximizes projected points. I am trying to include an additional constraint - each lineup must have a 'stack' of at least 4, but no more than 5 players from one team. So, a feasible lineup could be:
player | position | salary | team |
---|---|---|---|
player1 | P | 5k | Reds |
player2 | P | 5k | Orioles |
player3 | 1B | 5k | Braves |
player4 | 2B | 5k | Braves |
player5 | 3B | 5k | Braves |
player6 | SS | 5k | Braves |
player7 | OF | 5k | Braves |
player8 | OF | 5k | Red Sox |
player9 | OF | 5k | Brewers |
player10 | C | 5k | Giants |
I am focused on the team constraint. Here is example code for how I set up the salary constraint:
model.lineup is the binary decision variable = 1 if player is selected and = 0 if not selected
players = ...some list of players
salaries = ...some list of salaries
model = ConcreteModel()
model.lineup = Var(players, domain = Boolean)
salary_dict = dict(zip(players, salaries))
max_salary = 50000
model.salary_ct.add(sum(model.lineup[p] * salary_dict[p] for p in players) <= max_salary)
The issue with doing something like this for the team constraint is that it is not feasible to have lineups with exactly 'n' players from each team. How can I set up a constraint that forces at least 4 and no more than 5 (non-pitchers) from one team?