Explicit arrays
Describes explicit arrays in OPL.
Another useful syntax to dynamically create arrays to be used in expressions or constraints is to
explicitly define the array using the [ ] notation and including any variables or
values into it. Explicit arrays are only allowed in the variable declaration part of your
code, and not in the constraint section.
For example, given:
range R=1..5;
dvar int X in 1..10;
dvar int Y in 1..10;
dvar int Z in 1..10;
dvar int XX[R] in 1..10;
dvar int YY[R] in 1..10;
dvar int ZZ[R] in 1..10;
You can write the following:
dvar int XYZ[1..3] = [X,Y,Z];
...
allDifferent(XYZ);
Or you can also write the following:
dvar int TT[R] = [ XX[1], YY[2], ZZ[3], YY[4], XX[5] ];
...
allDifferent(TT);
However, you cannot write the following:
allDifferent([X,Y,Z]);
Nor, can you write the following:
allDifferent([XX[1], YY[3]]);
Nor, can you write the following:
forall(i in R)
allDifferent([XX[i], YY[i], ZZ[i]]);