How to model a variable that can only take a couple of values, for example:
Often people will see that you can use binary variables to ensure exactly one of the values is selected, but then the remaining problem always is how to get the chosen option as a variable value again.
So if you have the situation as given above, to have exactly one of the values selected, you can introduce the following three binary variables:
To ensure only one value gets selected, we add the following constraint:
To solve the remaining problem of how to get the option that was chosen as a variable value again, we can use an easy modeling trick: just sum the multiplications of the binary variables with their corresponding values. You can do so by introducing the following new equality constraint (or use everything right of the = sign in the definition attribute of variable ):
Because exactly one of the variables will take the value of 1, the sum will result in the variable taking the value of the chosen option. All the other terms in the sum will have value 0, as the corresponding binary variables will have the value 0.