# Modeling variables with limited number of possible values

Modeling discrete values

I have seen a question appear a couple of times on the AIMMS Google group, though every time explained in different words. I have also seen a similar kind of question a number of times as a support question to our support department. Although always formulated a bit different, the bottom line of the question always boils down to the following:

How to model a variable that can only take a couple of values, for example:
$x \in \{1.5 , 2.5, 3.5\}$.

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:
$x_{1.5} = \left\{ \begin{array}{ll} 1 & \mathrm{if\ } x=1.5; \\ 0 & \mathrm{otherwise.}\end{array} \right.$
$x_{2.5} = \left\{ \begin{array}{ll} 1 & \mathrm{if\ } x=2.5; \\ 0 & \mathrm{otherwise.}\end{array} \right.$
$x_{3.5} = \left\{ \begin{array}{ll} 1 & \mathrm{if\ } x=3.5; \\ 0 & \mathrm{otherwise.}\end{array} \right.$
To ensure only one value gets selected, we add the following constraint:
$x_{1.5} + x_{2.5} + x_{3.5} = 1$

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 $x$):
$x = 1.5 * x_{1.5} + 2.5 * x_{2.5} + 3.5 * x_{3.5}$

Because exactly one of the variables will take the value of 1, the sum will result in the variable $x$ 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.

This entry was posted in Beginner and tagged , by Guido Diepen. Bookmark the permalink.

## About Guido Diepen

Guido has been working at AIMMS since August 2008. He is part of the Professional Services team that helps customers getting the most out of AIMMS for their problems. His background is Integer Linear Programming and he is now also looking more at Constraint Programming.