The following problem arose, I wrote a restriction that some shift must be scheduled twice during the week, And this is not entirely correct, I need the shifts to go in a row, and only the last day on which the train operates is issued. That is, if the train travels for 5 days, then only the state on its last day is recorded in the solution
Dictionary<Tuple<int, int, int>, IntVar> shifts = new Dictionary<Tuple<int, int, int>, IntVar>();
foreach (int t in allTrains)
{
foreach (int d in allDays)
{
foreach (int s in allShifts)
{
shifts.Add(Tuple.Create(t, d, s), model.NewBoolVar($"shifts_t{t}d{d}s{s}"));
}
}
}
foreach (int t in allTrains)
{
IntVar[] x = new IntVar[numDays];
foreach (int s in allShifts)
{
foreach (int d in allDays)
{
var key = Tuple.Create(t, d, s);
x[d] = shifts[key];
}
model.Add(LinearExpr.Sum(x) == 2);
}
}
The end result should be something like. Result for 3 shifts in a row and 10 days. The problem itself is that some shift is assigned for a certain number of days, something like intervals. In a real task, the planning horizon can grow up to a month, and the number of shifts up to 20
Valid Solution:
Invalid decision №1
- Train 1
- Days 1 going to the shift 1
- Days 2 going to the shift 2
- Days 3 going to the shift 0
- Days 4 going to the shift 2
- Days 5 going to the shift 1
- Days 6 going to the shift 0
Invalid decision №2
- Train 1
- Days 2 going to the shift 2
- Days 3 going to the shift 3
- Days 5 going to the shift 1
- Days 6 going to the shift 0
Only, please, without links to the github :) It is still too complicated for me, because I have only recently begun to study this tool. Thanks in advance