Initializing sets
In the revised formulation of the burglar problem in Chapter Some illustrative examples and also in the models in Chapter More advanced modeling features we have already seen different examples for the use of index sets. We recall here the relevant parts of the respective models.
Constant sets
In the Burglar example the index set is assigned directly in the model:
declarations ITEMS={"camera", "necklace", "vase", "picture", "tv", "video", "chest", "brick"} end-declarations
Since in this example the set contents is set in the declarations section, the index set ITEMS is a constant set (its contents cannot be changed). To declare it as a dynamic set, the contents needs to be assigned after its declaration:
declarations ITEMS: set of string end-declarations ITEMS:={"camera", "necklace", "vase", "picture", "tv", "video", "chest", "brick"}
Set initialization from file, finalized and fixed sets
In Chapter Integer Programming the reader has encountered several examples how the contents of sets may be initialized from data files.
The contents of the set may be read in directly as in the following case:
declarations WHICH: set of integer end-declarations initializations from 'idata.dat' WHICH end-initializations
Where idata.dat contains data in the following format:
WHICH: [1 4 7 11 14]
Unless a set is constant (or finalized), arrays that are indexed by this set (and that are not explicitly marked as sparse arrays) are created as non-fixed dense arrays. Since in many cases the contents of a set does not change any more after its initialization, Mosel's automatic finalization mechanism finalizes the set WHICH in the initializations from block. Consider the continuation of the example above:
declarations x: array(WHICH) of mpvar end-declarations
The array of variables x will be created as a static array since its index set is finalized. Declaring arrays in the form of static arrays is preferable if the indexing set is known before because this allows Mosel to handle them in a more efficient way.
Index sets may also be initialized indirectly during the initialization of non-fixed or sparse arrays:
declarations REGION: set of string DEMAND: array(REGION) of real end-declarations initializations from 'transprt.dat' DEMAND end-initilizations
If file transprt.dat contains the data:
DEMAND: [(Scotland) 2840 (North) 2800 (West) 2600 (SEast) 2820 (Midlands) 2750]
then printing the set REGION after the initialization will give the following output:
{`Scotland',`North',`West',`SEast',`Midlands'}
Once a set is used for indexing an array (of data, decision variables etc.) it is fixed, that is, its elements can no longer be removed, but it may still grow in size.
The indirect initialization of (index) sets is not restricted to the case that data is input from file. In the following example (model chess2.mos) we add an array of variable descriptions to the chess problem introduced in Chapter Getting started with Mosel. These descriptions may, for instance, be used for generating a nice output. Since the indexing set Allvars of array DescrV is not known at declaration time the resulting array is not fixed and both grow with each new variable description that is added to DescrV.
model "Chess 2" uses "mmxprs" declarations Allvars: set of mpvar DescrV: array(Allvars) of string small, large: mpvar end-declarations DescrV(small):= "Number of small chess sets" DescrV(large):= "Number of large chess sets" Profit:= 5*small + 20*large Lathe:= 3*small + 2*large <= 160 Boxwood:= small + 3*large <= 200 maximize(Profit) writeln("Solution:\n Objective: ", getobjval) writeln(DescrV(small), ": ", getsol(small)) writeln(DescrV(large), ": ", getsol(large)) end-model
The reader may have already remarked another feature that is illustrated by this example: the indexing set Allvars is of type mpvar. So far only basic types have occurred as index set types but as mentioned earlier, sets in Mosel may be of any elementary type, including the MP types mpvar and linctr.
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.