range
Restricts the bounds of an integer or floating-point expression.
Syntax
boolExpr range(floatExpr x, float lb, float ub)
boolExpr range(intExpr x, float lb, float ub)
Parameters
-
x: The integer or floating-point expression. -
lb: The lower bound. -
ub: The upper bound.
Description
This Boolean expression (which is interpreted as a constraint outside of
an expression) determines whether the value of expression x is
inside the range [lb, ub]. The returned expression will be true if and
only if x is no less than lb and no greater than ub.
range(y, a, b) is also a more efficient form of
writing a <= y && y <= b.
Example
In this example there are 4 mathematics lessons per week, and 6 hours per day (a 30 hour week). We want to avoid scheduling two or more mathematics lessons in one day.
m1 = intVar(0..29);
m2 = intVar(0..29);
m3 = intVar(0..29);
m4 = intVar();
range(m4, 0, 29);
range(m1, 0, 5) + range(m2, 0, 5) + range(m3, 0, 5) + range(m4, 0, 5) <= 1;
range(m1, 6, 11) + range(m2, 6, 11) + range(m3, 6, 11) + range(m4, 6, 11) <= 1;
range(m1, 12, 17) + range(m2, 12, 17) + range(m3, 12, 17) + range(m4, 12, 17) <= 1;
range(m1, 18, 23) + range(m2, 18, 23) + range(m3, 18, 23) + range(m4, 18, 23) <= 1;
range(m1, 24, 29) + range(m2, 24, 29) + range(m3, 24, 29) + range(m4, 24, 29) <= 1;
The variables m1 to m4 represent the hours of the four mathematics lessons.
Since there is a 30 hour week, then domains are from 0 to 29. For
illustrative purposes, the example shows how to limit the bounds of variable
m4 using range as a constraint. Thereafter, five constraints (one per
day) make use of range as a Boolean expression which is automatically
interpreted as a 0-1 integer expression. These expressions ensure that only
one lesson can be in the period of the corresponding day.
Notes
If lb is strictly greater than ub then the expression will always be
false, and if used as a constraint, will cause the model to be insoluble.