# How to set a limit for a switch to 0 of a variable

I would like to know how to define a constraint to set a limit for switching to 0 for a decision variable? So I have a linear variable $$x(t)$$ which quantifies the modulation degree of a heating device. It can be between $$0$$ (meaning that the device is switched off) and $$1$$ (meaning that the device is heating with full power). My $$t$$ is between $$1$$ and $$288$$ (timeslots for every $$5$$ minutes of $$1$$ day). As you can imagine it is not really good to change frequently between $$0$$ and a non-zero value because this would mean, that the heating device starts and stopps frequently during one day (sometime I have $$5$$ starts and stopps during $$1$$ hour) which should be avoided as it increases the wear of the device.

So I would like to set a limit to switching to 0 during one day. Is there a way how I could define a constraint for that? The rule in pseudocode is basically

if x(t-1)>0 and x(t)=0 then increase count by 1
Constraint: count <= limit


Do you have an idea how I could model that (if it is possible)? I'd appreciate every comment and would be thankful for your help.

You can model this as follows. Let $$y(t)\in \{0,1\}$$ be a binary variable that is activated if the heating device is switched off at time $$t\in \{1,...,288\}$$ (and was active at time $$t-1$$).

This variable is activated every time $$x(t-1)=1$$ and $$x(t)=0$$: $$x(t-1) \le x(t) + y(t)\quad \forall t=1,...,288$$

So if $$x(t-1)=1$$, either $$x(t)$$ also takes value $$1$$ (and the device remains active), either $$y(t)$$ is activated.

You might want to minimize $$\sum_{t=1}^{288}y(t)$$ so that $$y(t)$$ is never activated "for free". Or you could also enforce this with additional constraints: $$y(t)\le x(t-1) \quad \forall t=1,...,288\\ x(t)+y(t) \le 1 \quad \forall t=1,...,288$$

And you can limit the number of activated variables to an upper bound $$\ell$$ (or equivalently, the number of times the device is switched off): $$\sum_{t=1}^{288}y(t) \le \ell$$

EDIT:

If $$x$$ is continuous, you need to create a binary variable $$b(t)$$ that takes value $$1$$ when $$x(t)>0$$. You can do this with the following constraint: $$x(t)\le b(t)\quad \forall t$$ And in the above approach, replace $$x$$ by $$b$$.

• Thanks Kuifje for your answer. Basically x(t) is not a binary variable but a continuous variable with values BETWEEN 0 and 1 (so it can also have the value 0,432 for example). Does your approach also work with continuous variables? As far as I understand, it would not work because if x(t-1)=0,7 and x(t) = 0,3, y(t) would have to be 1 (as it is a binary variable). This would violate your 2nd and 3rd constraint. If y(t) would be 0, the 1st constraint would be violated. Commented Apr 21, 2021 at 10:16
• a ok! in this case you need to create a binary variable for $x$. I will update. Commented Apr 21, 2021 at 10:32
• Thanks a lot Kuifje for your answer. Now I would like to incrase the complexity a little bit but I am not sure whether this should be asked in a separate question. Basically the heating device can not only heat up one storage with x(t) but it can also heat up another storage system with another linear variable z(t) but not both at the same time (so x(t) + z(t)<=1). Now I would like to count and limit the starts and stopps of the heating device considering the 2 storage systems. So x(t-1) could be 1 and x(t) could be 0 and it would not count as a start if z(t) is bigger than 0. Commented Apr 21, 2021 at 14:11
• glad to help! I suggest you post a new question which describes your full problem in detail. Commented Apr 21, 2021 at 14:23
• Thanks Kuifje for your tremendous help. I upvoted and accepted your answer. Commented Apr 21, 2021 at 14:57