Constraining on a Minimum of Distinct Values with PuLP

1k views Asked by At

I've been using the PuLP library for a side project (daily fantasy sports) where I optimize the projected value of a lineup based on a series of constraints.

I've implemented most of them, but one constraint is that players must come from at least three separate teams.

This paper has an implementation (page 18, 4.2), which I've attached as an image:

enter image description here

It seems that they somehow derive an indicator variable for each team that's one if a given team has at least one player in the lineup, and then it constrains the sum of those indicators to be greater than or equal to 3.

Does anybody know how this would be implemented in PuLP?

Similar examples would also be helpful.

Any assistance would be super appreciated!

1

There are 1 answers

1
Stuart Mitchell On

In this case you would define a binary variable t that sets an upper limit of the x variables. In python I don't like to name variables with a single letter but as I have nothing else to go on here is how I would do it in pulp.

assume that the variables lineups, players, players_by_team and teams are set somewhere else

x_index = [i,p for i in lineups for p in players]
t_index = [i,t for i in lineups for t in teams] 
x = LpVariable.dicts("x", x_index, lowBound=0) 
t = LpVAriable.dicts("t", t_index, cat=LpBinary)
for l in teams:
   prob += t[i,l] <=lpSum([x[i,k] for k in players_by_team[l]])
prob += lpSum([t[i,l] for l in teams]) >= 3