# Model Variables with Limited Values

Note

In this article we’ll learn how to model a variable that can only take a limited set of values, for example:

$x \in \{1.5 , 2.5, 4.5\}$

You can use binary variables to ensure exactly one of the values is selected. But let’s find out how to get the chosen option as a variable value again.

If you have the situation as given above, to have exactly one of the values selected, you can introduce the following three binary variables:

$\begin{split}x_{1.5} = \left\{ \begin{array}{ll} 1 & \mathrm{if\ } x=1.5; \\ 0 & \mathrm{otherwise.}\end{array} \right.\end{split}$
$\begin{split}x_{2.5} = \left\{ \begin{array}{ll} 1 & \mathrm{if\ } x=2.5; \\ 0 & \mathrm{otherwise.}\end{array} \right.\end{split}$
$\begin{split}x_{4.5} = \left\{ \begin{array}{ll} 1 & \mathrm{if\ } x=4.5; \\ 0 & \mathrm{otherwise.}\end{array} \right.\end{split}$

To ensure only one value gets selected, we add the following constraint:

$x_{1.5} + x_{2.5} + x_{4.5} = 1$

To get the option that was chosen as a variable value again, we can use an easy modeling trick: sum the products of the binary variables with their corresponding values.

You can do so by introducing the following equality constraint (or use everything right of the equals sign in the definition attribute of variable $$x$$):

$x = 1.5 * x_{1.5} + 2.5 * x_{2.5} + 4.5 * x_{4.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.