7

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.

davidconf
  • 71
  • 1

2 Answers2

6

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$.

Johan Löfberg
  • 1,702
  • 5
  • 9
6

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.

Libra
  • 937
  • 5
  • 14