## Technote (FAQ)

## Question

There can be times when a model requires a decision variable to be continuous (as a float type) or discrete (as an int type) over a set of non-contiguous ranges. How do I formulate/declare such a decision variable?

## Answer

Let us assume a particular use-case where decision variables in an integer array can only take integer values in the (1..6) or (9..10) range. You could model this requirement in OPL using one of the two approaches as shown in the following code snippet:

`range`` ``i``=``1``..``5``;`

`range x_all=``1..``10;`

`dvar`` ``int ``x[``i``] ``in`` ``x_all``;`

`{``int``} ``x_no`` = {``7``,``8``};`

`{``int``} ``x_yes`` = {``j``|``j`` ``in`` ``x_all``: ``j`` ``not`` ``in`` ``x_no``};`

`....`

`subject`` ``to``{`

` ``//1st approach`

` ``forall``(``index`` ``in`` ``i``){`

` ``forall``(``t`` ``in`` ``x_no``){`

` ``x``[``index``]!=``t``;`

` } `

` }`

` ``//2nd approach`

` ``forall``(``index`` ``in`` ``i``){`

` ``sum``(``t`` ``in`` ``x_yes``)(``x``[``index``]==``t``) == ``1``;`

` } `

`...``.`

`}`

The decision variables are first declared to lie in the overall range of allowed values (1..10 in this case). The first approach involves adding logical constraints to disallow assigning values belonging to the x_no set (the set of dis-allowed values). The second approach forces the decision variables to be equal to only one value in the x_yes set (the set of allowed values).

If you are using the CP engine (indicated by the `using CP;`

statement at the beginning of the mod file) instead of the CPLEX engine, then you could use the following simplified approach:

`subject to{`

` ``//1st approach`

` ``forall``(``index`` ``in`` ``i``){`

` ``x``[``index``] ``not`` ``in`` ``x_no``;`

` }`

` ``//2nd approach`

` ``forall``(``index`` ``in`` ``i``){`

` ``x``[``index``] ``in`` ``x_yes``;`

` }`` `

`....`

`}`

Segment | Product | Component | Platform | Version | Edition |
---|---|---|---|---|---|

Commerce | IBM ILOG CPLEX Optimization Studio | Area:Modelling new constraints and variables | AIX, Linux, Windows | 12.5.1, 12.5.0.1, 12.5, 12.4, 12.3, 12.2.0.1, 12.2 | Edition Independent |

### Document information

**More support for:**
IBM ILOG CPLEX Optimization Studio

Area:Modelling new constraints and variables

**Software version:**
5.0, 5.1, 5.2, 5.5, 5.5.1, 6.0, 6.0.1, 6.1, 6.1.1, 6.2, 6.3, 12.2, 12.3, 12.4, 12.5, 12.5.0.1

**Operating system(s):**
AIX, Linux, Windows

**Software edition:**
Edition Independent

**Reference #:**
1608891

**Modified date:**
2012-08-29