Tuples of parameters
forall statements,
and generic sets.
The following code sample states the precedence constraints between tasks. The constraint
declaration requires explicit accesses to the fields of the tuple to state the constraints. In
addition, the field before is accessed twice. An alternative way to state the same
constraint is to use a tuple of formal parameters, as shown in the last line of the code sample,
precluding the need to access the tuple fields explicitly. The tuple <p in
Prec> in the forall quantifier contains two components that are
successively given the values of the fields of each tuple in Prec.
Tuple of formal parameters
int minTime=7*60;
int maxTime=9*60;
{string} Tasks = { "Make dinner","Have dinner","Clean post dinner" };
tuple Precedence {
string pre;
string post;
}
{Precedence} Prec = {
<"Make dinner","Have dinner">,
<"Have dinner","Clean post dinner">
};
int Duration[Tasks]= [20,60,10];
dvar int Start[Tasks] in minTime..maxTime;
subject to {
forall(p in Prec) Start[p.post] >= Start[p.pre] + Duration[p.pre];
}
More generally, an expression
p in S
where S is a set of tuples containing n fields, can be replaced by a formal parameter expression
<p1,...,pn> in S
that contains n formal parameters. Each
time a tuple r is selected from S, its fields are assigned to the corresponding
formal parameters. This functionality is often useful in producing
more readable models.