7
$\begingroup$

Consider a decision variable in a MILP constrained:

$$\sum_i p_i = 1$$

$$p_i\ \in \{0, 1\}$$

Obviously one element in $p$ is 1 and all others are 0. How can I set a decision variable to the index i of the element $p_i$ = 1?

I think I can do this by multiple if-then-else constraints but that's a bit clunky.

$\endgroup$
6
$\begingroup$

My interpretation is that you want $y$ to be $i$ if $p_i=1$. You can do that with a simple multiplication $y=c^Tp$ where the constant vector $c$ is given by $c_i=i$.

$\endgroup$
6
$\begingroup$

Assuming your index goes from 0 to $n$ you can do $k = \sum_{i = 0}^{n}i \cdot p_i$ where $k$ is the desired index.

$\endgroup$
2
  • 1
    $\begingroup$ After I posted, I saw @Johan Löfberg answer that I think is exactly the same. He got it first :-) $\endgroup$
    – Libra
    Mar 27 at 10:31
  • $\begingroup$ Thanks to Libra and Johan. Much appreciated. $\endgroup$
    – davidconf
    Mar 27 at 11:19

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.