9

Let $x \in \mathbb{R}^n$ be an optimization variable. Now, at a constraint, I would like to count how many times a value, say $2$, appears in $x$ decision.

I think we can have a binary variable $y_i$ indicating whether $x_i =2$. So, $x_i - 2 = 0$ should imply $y_i = 1$. But, anything except $0$ should imply $y_i = 0$. What is the easiest way for this?

Note: since we can subtract $2$ from each element of $x$, we are interested in the number of zeros in $x-2$. So, 'the number of zeros in a decision vector' constraint will also make it.

We may assume $x$ consists of elements $x_i< M$ for some constant $M$

independentvariable
  • 3,980
  • 10
  • 36

1 Answers1

8

As LarrySnyder610 said, you cannot do exactly what you want when $x_i$ is continuous. (You can if it is an integer variable.) I discussed how to model this particular issue here: Flagging a Specific Variable Value.

prubin
  • 39,078
  • 3
  • 37
  • 104