Hi, I am creating a timetabling program using ILP. In the timetable I am supposed to create, sometimes an order is defined in which some events need to take place.
I have binary variables x_{i,j,k} which indicate whether or not event 'i' is scheduled in timeslot 'j' in room 'k'.
Suppose event 'a' needs to happen some time before event 'b'. I have tried adding constraints like this:
m.addConstrs( (x.sum(a,j,'*') - (x.sum(b,j2,'*')) == 0 for j in range(timeslots) if (j2 > j)), "Order")
In other words, I am trying to create a constraint for each timeslot j which I associate with event 'a'. I sum over all the possible rooms event 'a' can be scheduled in in timeslot 'j', and then I make sure that event 'b' is scheduled sometime after that (j2 > j) by summing over all the variables associated to event 'b' and timeslot 'j2', and make the difference between these sums zero.
I know for a fact the model is correct: I have used the same constraint by writing it in .lp file format. The problem is the error message that global name "j2" is not defined.
This is confusing to me, because I have another constraint:
m.addConstrs(( x.sum(i,j,'*') <= 1 for j in range(timeslots) if i in set ), "Overlap" )
where it does not complain about i not being defined. When I change the i's in this constraint into another letter it does give me the same error however.
Might be relevant to know I created and added variables like this:
v = [(i,j,k) for i in eventnames for j in range(timeslots) for k in roomnames]
x = m.addVars(v, name="x", vtype=GRB.BINARY)
So this leads to the questions:
1. Why is it so sensitive to which letter I am using when I am just using it as index?
2. How do I resolve the problem of using another letter in my constraint than the ones I used to create the variables with?