Arrays
Describes one-dimensional arrays and multidimensional arrays.
Arrays are fundamental in many applications.
One-dimensional arrays
One-dimensional arrays are the simplest arrays in OPL and vary according to the type of their elements and index sets. A declaration of the form
int a[1..4] = [10, 20, 30, 40];
declares an array of four integers a[1],...,a[4] whose values are 10, 20, 30, and 40.
It is of course possible to define arrays of other basic types. For
instance, the instructions
int a[1..4] = [10, 20, 30, 40];
float f[1..4] = [1.2, 2.3, 3.4, 4.5];
string d[1..2] = [“Monday”, “Wednesday”];
declare arrays of natural numbers, floats, and strings, respectively.
The index sets of arrays in OPL are very general and can be integer ranges and arbitrary finite sets. In the examples so far, index sets were given explicitly, but it is possible to use a previously defined range, as in
range R = 1..4;
int a[R] = [10, 20, 30, 40];
The declaration:
int a[Days] = [10, 20, 30, 40, 50, 60, 70];
describes an array indexed by a set of strings;
its elements are a[“Monday”],...,a[“Sunday”].
Arrays can also be indexed by finite sets of arbitrary types. This feature is fundamental in OPL to exploit sparsity in large linear programming applications, as discussed in detail in Exploiting sparsity in the Language User’s Manual.
For example, the declaration:
tuple Edges {
int orig;
int dest;
}
{Edge} Edges = {<1,2>, <1,4>, <1,5>};
int a[Edges] = [10,20,30];
defines an integer array, a,
indexed by a finite set of tuples. Its elements are a[<1,2>],
a[<1,4>], and a[<1,5>].
Tuples are described in detail in Tuples.
Multidimensional arrays
OPL supports the declaration of multidimensional arrays (see Data initialization about the ellipsis syntax). For example, the declaration:
int a[1..2][1..3] = ...;
declares a two-dimensional array, a, indexed by two integer ranges. Indexed sets of
different types can of course be combined, as in
int a[Days][1..3] = ...;
which is a two-dimensional array whose elements
are of the form a[Monday][1]. It is interesting
to contrast multidimensional and one-dimensional arrays of tuples.
Consider the declaration:
{string} Warehouses = ...;
{string} Customers = ...;
int transp[Warehouses,Customers] = ...;
that declares a two-dimensional array transp. This array may represent the units shipped
from a warehouse to a customer. In large-scale applications, it is
likely that a given warehouse delivers only to a subset of the customers.
The array transp is thus likely to be sparse,
i.e. it will contain many zero values.
The sparsity can be exploited by declarations of the form:
{string} Warehouses = ...;
{string} Customers = ...;
tuple Route {
string w;
string c;
}
{Route} routes = ...;
int transp[routes] = ... ;
This declaration specifies a set, routes, that contains only the relevant pairs (warehouse,
customer). The array transp can then be
indexed by this set, exploiting the sparsity present in the application.
It should be clear that, for large-scale applications, this approach
leads to substantial reductions in memory consumption.
You can initialize arrays by listing its values, as in most of the examples presented so far. See Initializing arrays, As generic arrays, and As generic indexed arrays for details.