I'm trying to model selection of 15 players for specific number of fixtures. My LpProblem consists of 2 binary variables player and fixture.
choices = LpVariable.dicts(
"Choices", (fixtures, constraints["player"]), 0, 1, LpBinary)
I would like to limit the amount of player picked for set of fixtures using this constraint(which is bad - it counts all the pick not a number of players used):
prob += lpSum([choices[f][p] for f in fixtures for p in constraints["player"]]
) <= player_count + len(fixtures) - 1, "Transfers limit"
I also set up a constraint to pick exactly 15 players for each fixture:
for fixture in fixtures:
prob += lpSum([choices[fixture][p]
for p in constraints["player"]]) == player_count, str(fixture) + " Total of " + str(player_count) + " players"
My aim is to pick 15 and small amount of changes form fixture to fixture, but for some reason these constraints produce infeasible problem. For example if I search for fixtures = [0,1,2]
the problem becomes feasible when I set transfer limit of 45 (15*3). I'm not sure how to formulate transfer limit constraint to achive my goal.
Example:
players = [1, 2, 3, 4, 5, 6]
fixtures = [1, 2, 3]
prob = LpProblem(
"Fantasy football selection", LpMaximize)
choices = LpVariable.dicts(
"Players", (fixtures, players), 0, 1, LpBinary)
# objective function
prob += lpSum([predict_score(f, p) * choices[f][p]
for p in players for f in fixtures]), "Total predicted score"
# constraints
for f in fixtures:
# total players for each fixture
prob += lpSum([choices[f][p] for p in players]) == 2, ""
if f != fixtures[0]:
# max of 1 change between fixtures
prob += lpSum([1 if choices[f-1][p] != choices[f]
[p] else 0 for p in players]) <= 2, ""
prob.solve()
print("Status: ", LpStatus[prob.status])