5
$\begingroup$

I declare an array of binary variables as $y(i), i = 1, ..., N$

I would like to model the following:

If $y(i-1) + y(i) = 1$ then $y(k) = 0$ for $k < i$ and $y(m) = 1$ for $m \geq i$

To make the question clear, here is an example:

Suppose I have the following 10 binary variables.

$$y(1), y(2), y(3), y(4), y(5), y(6), y(7), y(8), y(9), y(10)$$

The following is true: $y(i-1) \leq y(i)$ for $i > 2 $

The optimiser is supposed to set the values of the variables in a pattern like the following: $(y(1), y(2), y(3), y(4), y(5), y(6), y(7), y(8), y(9), y(10)) = ( 0,0,0,0,0,1,1,1,1,1)$

I know that I will get a pattern like the one mentioned, but I don't know when the first $1$ will appear. I need to determine the variable $y(i)$ that gets first the value $1$ and then set all variables to its right to $1$ and all variables to its left to $0$. So I need to determine when $y(i) + y(i-1) = 1$, knowing that this implies $y(i) = 1$ and $y(i-1)=0$.

$\endgroup$
  • $\begingroup$ Hi Marco How can I switch to math mode? $\endgroup$ – Clement Mar 11 at 23:23
  • 1
    $\begingroup$ @Clement you can use LaTeX formatting using MathJax $\endgroup$ – LarrySnyder610 Mar 12 at 1:26
  • $\begingroup$ Is it a solution? I just wonder if I missed something $\endgroup$ – Claudio Contardo Mar 12 at 1:27
  • 1
    $\begingroup$ It is a solution. I'm not guaranteeing it's a correct solution (though I think it is), but it seems to me you should post it as a solution and let the OP and/or the community weigh in by voting. $\endgroup$ – LarrySnyder610 Mar 12 at 1:27
2
$\begingroup$

New answer based on modified question. If you have constraints $y_i \ge y_{i-1}$, the value of $y_i-y_{i-1}$ indicates whether $y_i+y_{i-1}=1$, and this can happen only once.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hi Rob! You are right. The discussion sofar, before the modification, led me to that answer as well. Thank you all, especially Claudio and you! $\endgroup$ – Clement Mar 12 at 16:39
7
$\begingroup$

You could simply write $$y(i) - y(i - 1) \ge 0, \qquad i=2,...,N$$

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Yes, this does seem correct. The $m \ge i$ part implies that $(y_{i-1},y_i)\not=(1,0)$. $\endgroup$ – RobPratt Mar 12 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.