Reputation: 323
I'm enjoying learning about or-tools, and hope someone can help me out a bit. I've got a problem that's a bit like the nurse-scheduling problem, but instead it's for sports-scheduling. Here's what I've got so far to generate the combinations of possible games.
# Create schedule variables
for h in range(divisions):
for i in range(teams_per_division):
for j in range(i+1, teams_per_division):
for k in range(weeks):
for l in range(venues):
schedule[h, i, j, k, l] = model.NewBoolVar( f"schedule_div{h}_team{i}_team{j}_week{k}_venue{l}")
The constraints I'm trying to add are:
So far, I've tried introducing a new variable, 'played', like this:
played[h, i, k, l] = model.NewBoolVar( f"played_div{h}_team{i}_week{k}_venue{l}")
but I'm having trouble linking this 'played' to 'schedule', and then modeling the constraints.
Any and all help much appreciated!
Upvotes: 1
Views: 148
Reputation: 323
Looks like I figured it out.
First I created a new variable like so:
for h in range(divisions):
for i in range(teams_per_division):
for k in range(weeks):
weekPlayed[h, i, k] = model.NewIntVar( 0, 10, f"played_div{h}_team{i}_week{k}")
for l in range(venues):
played[h, i, k, l] = model.NewBoolVar( f"played_div{h}_team{i}_week{k}_venue{l}")
Now, with the 'played' variable, I could just increment these counters during schedule-creation, like so (note that both i and j are incremented):
...
# Increment key counters
weekPlayed[h, i, k] = weekPlayed[h, i, k] + schedule[h, i, j, k, l]
weekPlayed[h, j, k] = weekPlayed[h, j, k] + schedule[h, i, j, k, l]
Finally, the needed constraint:
# Each team plays once per week
for h in range(divisions):
for i in range(teams_per_division):
for k in range(weeks):
model.Add(weekPlayed[h, i, k] <= 1)
I hope this is helpful to someone else trying to link offshoot variables, and then constrain them independently.
Upvotes: 0