I am trying to model the following scenario:
If $L_0=0, L_1=0, L_2=0, L_3=1, L_4=1, L_5=0, L_6=0, L_7=1, L_8=1, L_9=1, L_{10}=0$
I would like to create a new variable that keeps track of consecutive times L is 1 before turning back to zero.
That is, I would like to create a new variable with the following output: $Z_1=0, Z_2=0, Z_3=0, Z_4=0, Z_5=2, Z_6=0, Z_7=0, Z_8=0, Z_9=0, Z_{10}=3$.
I need help with modelling this in an LP.
thanks in advance
Exactly doing what you want is difficult. Usually we can get away with simpler things because we know more about the model. But let's try anyway: call the original binary variables $x_i \in \{0,1\}$.
First introduce an integer variable $a_i$ (accumulator) that is defined by: $$a_i = \begin{cases} a_{i-1}+1 & \text{if $x_i=1$ }\\ 0 & \text{otherwise}\end{cases} $$
This can be modeled as: $$\begin{align} & a_{i-1}+1 - (1-x_i)M \le a_i \le a_{i-1}+1 + (1-x_i)M\\ & 0 \le a_i \le x_i M \end{align}$$
Now we need: $$z_i = \begin{cases} a_{i-1} & \text{if $x_i=0$ and $x_{i-1}=1$ }\\ 0 & \text{otherwise}\end{cases} $$
This can be modeled as: $$\begin{align} & a_{i-1} - x_i M - (1-x_{i-1})M \le z_i \le a_{i-1} + x_i M + (1-x_{i-1})M\\ & 0 \le z_i \le x_{i-1} M \\& z_i\le (1-x_i)M \end{align}$$
The results look like:
(Only nonzero values are printed).
Notes: