MIP Python School Teacher Timetabling problem - how to check if the number of variables equals zero is equal to a certain number

I'm trying to solve a School Teacher Time problem. I'm at a good point but now I'm stuck at how to model that each teacher have a certain number of free days in a week e.g. the sum of days with zero lesson for each teacher is equal to a certain number.

Basic the question is: How I can add a contraint that count the number of time that a variable is equal to 0.0 ?

y count the number of hours that teacher teach every day. More or less ought to be something like this (but doesn't work, of course)

for t in teacher:
    model += xsum( y[t, d] == 0.0 for d in day ) == 1.0

I tried also using the actual value of the variable:

for t in teacher:
    model += xsum( y[t, d].x == 0.0 for d in day ) == 1.0

and:

for t in teacher:
    model += xsum( y[t, d] for d in day if y[t, d] == 0.0 ) == 1.0

But none seems to work.

I can share some code but actually everything is written in Italian so I tried to boil down straight to the point.


Assuming you want to solve this with a (linear) MIP solver, your attempts are violating linearity. Here is what you can try:

Let y[t,d] be the number of hours of teacher t in day d. Let's assume it is an integer variable. Then we can define the binary variable:

 x[t,d] = 1  if t works at d
        = 0  is t is off at d

The link between x and y can be modeled as:

 x[t,d] <= y[t,d]     (y[t,d]=0 => x[t,d]=0)
 8*x[t,d] >= y[t,d]   (y[t,d]>0 => x[t,d]=1, assuming 8 hours is the maximum per day)  

Now we can say (in pseudo code):

 sum(d, 1-x[t,d]) = 1   (exactly one day off)

or maybe:

 sum(d, x[t,d]) = 4     (exactly 4 days at work)