0

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?

JRP
  • 125
  • 2
  • 10
  • Is it the case that player-position and player-team combinations are each singular? Meaning: is each player only capable of playing 1 position and only rostered on 1 team? – AirSquid Apr 26 '22 at 22:34
  • well, some players have multiple position designations on DraftKings (they can be rostered as 2B OR 3B for example), but I solved for that already. But, yes 1 player can only be rostered once in the optimal lineup, and each player exists on only 1 team – JRP Apr 26 '22 at 22:43
  • 1
    I think a variant of this should work. https://stackoverflow.com/questions/67648449/how-to-incorporate-or-condition-into-pulp-optimization-problem/67733644#67733644 I would probably index the “hire” decision by [player, position] if players can play multiple positions. – AirSquid Apr 26 '22 at 23:06
  • I indexed by [player, position category] where position category can be pitcher or non-pitcher. You can have up to 5 non-pitchers (batters) and 1 pitcher from one team. – JRP Apr 27 '22 at 15:58
  • OK. Did the other post help with the teams reqt? There are a couple options there…. Including indexing or sub-setting (because pairings are unique). Or are you still stuck on that? – AirSquid Apr 27 '22 at 16:13
  • The other post helped, I just adjusted so it worked with pyomo, thank you! – JRP Apr 27 '22 at 16:33

0 Answers0