# Inconsistencies in modeling a binary variable that indicates a switch

this is a follow-up question to this post here and to @RobPratt's answer. I have implemented the whole thing, and now it happened that on day 1 the machine did not run and was only used for the first time on day 2, but a shift change was modeled, even though there was actually none. What could be the reason for this and how do I have to adjust the constraints to prevent this from happening? That is the answer:

\begin{align} \rho_{ptm} &\le 1-s_{ptm} &&\text{for all p,t,m} \\ \rho_{ptm} &\le d_{ptm} &&\text{for all p,t,m} \\ \rho_{ptm} &\ge d_{ptm} - s_{ptm} &&\text{for all p,t,m}\\ \sum_m d_{ptm} + x_{pt} &= 1 &&\text{for all p,t} \\ s_{p,t+1,m} &= d_{ptm} + y_{ptm} &&\text{for all p,t\not=T,m} \\ y_{ptm} &\le s_{ptm} &&\text{for all p,t,m} \\ y_{ptm} &\le x_{pt} &&\text{for all p,t,m} \\ y_{ptm} &\ge s_{ptm} + x_{pt} - 1 &&\text{for all p,t,m} \end{align}

The variables are defined as:

1. $$d_{ptm}$$: Binary variable indicating if product $$p$$ was serviced by machine $$m$$ in period $$t$$
2. $$\rho_{ptm}$$: Binary variable indicating if there was a switch in machines for product $$p$$ in period $$t$$ to machine $$m$$
3. $$s_{ptm}$$: State variable indicating if the last servicing of product $$p$$ prior to period $$t$$ was by machine $$m$$
4. $$y_{ptm}$$: Auxiliary variable to assist in defining constraints related to $$s_{ptm}$$ and $$x_{pt}$$
5. $$x_{pt}$$: Binary variable indicating if product $$p$$ is not being serviced in period $$t$$

And that is the original question:

I have the following question / concern. I have a modeling problem with the decision variable $$d_{ptm}$$ which indicates whether product $$p$$ was serviced by machine $$m$$ in period $$t$$. It takes the value 1 if so, and 0 otherwise. A product can be serviced by different machine in different periods. In addition, a product may not be serviced for two days and then again, possibly by a different machine. I now want to introduce a binary variable $$\rho$$ that indicates such a switch in machines a product was serviced by. It should take the value = 1 if there was a machine switch from the last time a product was serviced till the current period. How can I model this?

– prubin
Commented Jun 7 at 15:36
• As mentioned in the comments to the linked question, there is some ambiguity about a switch for the first servicing. Is there anything in the rest of your model that encourages switches? Commented Jun 8 at 12:56

I'm going to assume that time indexing starts at 1. You can introduce a new binary variable $$\gamma_{p,t}$$ together with the constraints $$\gamma_{p,1}=1$$ $$\gamma_{p,t} \le \gamma_{p, t-1}$$ $$\gamma_{p,t} \le x_{p,t-1}$$ and $$\gamma_{p,t} \ge \gamma_{p,t-1} + x_{p,t-1} - 1.$$ Collectively this ensures that $$\gamma_{p,t}=1$$ if and only if $$p$$ has not been in production (on any machine) prior to time $$t.$$
Now adjust your first and third constraints as follows: $$\rho_{p,t,m}\le 1-s_{p,t,m}-\gamma_{p,t}$$ $$\rho_{p,t,m} \ge d_{p,t,m}-s_{p,t,m} - \gamma_{p,t}.$$
• Dear @prubin. Thank you for your anwser. Does this also hold, if $d_{p1m} = 1 \forall p,m$ is true. So does it work if a machine is active on day 1 or not? Could you maybe explain how this works? Doesnt $\gamma_{p,1}=1$ force the machine to be not in production? As you stated in your anwser? Commented Jul 8 at 11:52
• The first three inequalities I listed apply for $t>1$ (so that $t-1$ is meaningful). Yes, it works if the machine is active on day 1. $\gamma_{p,1}=1$ implies that the machine was not in production prior to day 1 (which it cannot be, since day 1 is the first day of the time horizon).
• Thanks. I misread that. One final question. With your two lower constraints (the modified ones), they originate from the product $\rho_{ptm}=(1-s_{ptm})d_{ptm}$. What would the non-linear formulation of the modification look like? And I guess the constraints 2-4 from you could also be compromised to $\gamma_{pt}=\gamma_{pt}\cdot x_{pt}$. Correct? Commented Jul 8 at 15:59
• Final answer(s); I assume you mean $\gamma_{p,t}=\gamma_{p,t-1}\cdot x_{p,t},$ which would be equivalent to my constraints 2-4. The nonlinear definition of $\rho$ would be $\rho_{ptm}=(1-s_{ptm}-\gamma_{pt})d_{ptm}.$