Data Assignment
Introduction
After declaring sets and parameters, the next step is to assign values to them. We do this in the data section of our program (see Program Structure).
Set Data Assignment
A set is a model entity that holds a collection of items. This collection can contain strings, numbers or both.
1D Sets
Sets are 1 dimensional (1D) by default. A 1 dimensional set is a collection of items in
which each of its items is a single value (string or number). The set Cities
declared using the statement
"London"
"Rio de Janeiro"
Set of Numbers
There are many cases in which we want to define a set of numbers ranging from a to b. But it would be very inconvenient if we had to explicitly list all the numbers we want to our set.
Suppose we want a set containing the integers from 1 to n, where n is a
parameter. The easy way to do it is to declare the set like this:
1D Collection of 1D Sets
A 1D collection of 1D sets is a collection in which each of its items
is a 1D set associated with a single index. The collection Attractions
declared using the statement
2D+ Collection of 1D Sets
In general, a n-dimensional collection of 1D sets is a collection in
which each of its items is a 1D set that we can only refer to using n
indices. The collection Products declared using the statement
Like in the previous case, we need one assignment statement for each set within the Products collection. In each statement, we have an index part specifying which set within the collection we are assigning data to:
2D+ Sets
In general, a n-dimensional set is a collection of items in
which each of its items is a tuple of values (strings and/or numbers).
A tuple can be a pair, a triplet, a quartet, etc.
The set DirectFlights
declared using the statement
The data assignment for higher dimension sets is basically the same, with each tuple being specified between parenthesis.
Collection of 2D+ Sets
Assigning data to a n-dimensional collection of m-dimensional sets is not so different from assigning data to a 2D+ collection of 1D sets.
Suppose that the DirectFlights of the previous example was the name not of a set,
but of a collection of sets, declared like this:
We need one assignment statement for each set within the DirectFlights collection. In each statement, we have an index part (in yellow) specifying which set within the collection we are assigning data to:
("New York", "Rio de Janeiro")
("Austin", "Amsterdam")
Parameter Data Assignment
A parameter is a model entity that holds a single value, which can be a number or a string.
Single Parameter
Assigning data to a single parameter is very straightforward:
1D Collection of Parameters
A 1D collection of parameters is an indexed list of values. In order
to assign data to it, we need to list each index with its corresponding value.
For instance, given a parameter collection declared with
"London"
"Rio de Janeiro"
8.98
6.75
In the assignment statement above, the actual values in the parameter collection appear in orange, each of which being placed just after its index in yellow.
When we have multiple 1D parameter collections indexed over the same set, instead
of writing multiple assignment statements like the above (which would work just fine),
we can conveniently make all data assignments in a single assignemnt statement. For
instance, say we have a parameter collection
"London"
"Rio de Janeiro"
8.98
6.75
1572
1255
Moreover, the above statement could be slightly modified so that it assigns data to our Cities set as well:
"London"
"Rio de Janeiro"
8.98
6.75
1572
1255
In this last case, we are using a single assignment statement to set the values for the Cities set and the parameter collections Population and Area.
2D Collection of Parameters
A 2D collection of parameters is a collection of parameters in which each parameter is associated with a pair of indices. We can treat 2D parameter collections as simple tables and assign data to them in a table-like fashion. In the declaration of a 2D parameter collection, it is useful to think that we are defining, in that order, the labels of the rows and columns of the collection:
Suppose that we have already declared and initialized the set Months with the first three months. This is how we assign data to our RainProbability parameter collection:
"London"
"Rio de Janeiro"
0.30
0.55
0.26
0.48
0.25
0.45
The RainProbability collection is 2D because it is indexed over two 1D sets, Cities and Months.
But we can also have a 2D collection indexed over a single 2D set, and the assignment statement structure
is basically the same. For instance, this is how we assign data to the Distances collection declared with
"Shanghai"
.
1767
Notice that we don't have direct flights from "Toronto" to "Tokyo" nor from "Shanghai" to
"San Francisco". The
3D+ Collection of Parameters
We can think of a 3D collection of parameters as a collection of tables. The data assignment of a collection of tables is done in multiple chunks, each chunk assigning the data of a table within the collection.
Let's say we want to assign data to the following parameter collection:
Suppose that we have already declared and initialized the Departments, DeliveryTypes and DeliveryTypes sets like this:
This is how we assign data to the DeliveryPrices collection:
"International"
"International"
"International"
60.00
12.50
13.50
48.00
10.80
11.80
25.00
5.00
6.00
The statement has three chunks, each chunk assigning data to a table within the collection.
The part of the statement that is between brackets specify the subcollection of DeliveryPrices that we
are going to assign data to. For instance,
Data for parameter collections with higher dimensions can be assigned in the same way.
The difference will be that the "tables" will be referenced with more indices. For instance,
if our delivery prices varied between "Gold" and "Platinum" customers,
each chunk would begin with an indexation pattern with 4 indices, like this:
Default Values
We can define default values for sets and parameters.
For sets, this is done in the declaration of the set (or set collection),
not in the data section. For instance, we can declare a set I that contains the numbers
1 to 10, by default:
For parameters, this can be done either in the declaration of the parameter or in the data section. For instance, the DeliveryPrices parameter collection above could be declared like this:
Alternatively, in the data assignment statement for this collection, we could set a default value like this:
In both cases, all the parameters in the collection would have the value 10 by default.
When assigning data to this collection, we can either preserve the default value
for a given entry or overwrite it with new data. In order to preserve the default
value, we put a
Variable Data Assignment
The assignment statements for variables and collection of variables are exactly
the same as those for parameters, except
for the assignment keyword, which is