Using A Simple For() with Index Sets and a Table Assignment
The example in the previous topic uses arrays to store the data used in the model. Arrays in the RASON Modeling language are limited to 2-dimensions. If more than 2-dimensions are required, you must use a table, instead. There are several advantages to using a table over an array for data storage.
- Tables may hold more than 2 dimensions.
- A table result may be used in an indexed array formula.
- A table can be sparse while an array is dense.
- The evaluation of a table is less expensive (time consuming) than the evaluation of an array with more than 2 dimensions.
RASON uses index sets to establish a basis of order for each dimension appearing in a table (or array). An index set is always a 1-dimensional
array and must be created within the indexSets portion of the model.
The example code below creates two ordered sets, part and prod. The part set contains
five items (in order as entered): chas, tube, cone, psup and elec while the prod set contains 3 items: tv, stereo and
speakers. For more information on index sets, see Index Sets in the RASON Reference Guide.
{
modelName: "RGProductMixTable4",
modelType: "optimization",
indexSets: {
part: { value: ['chas', 'tube', 'cone', 'psup', 'elec'] },
prod: { value: ['tv', 'stereo', 'speaker'] }
},
data: {
parts: { indexCols: ['part', 'prod'],
value: [['chas', 'tv', 1],
['elec', 'stereo', 1],
['tube', 'tv', 1],
['cone', 'tv', 2],
['cone', 'stereo', 2],
['chas', 'stereo', 1],
['cone', 'speaker', 1],
['psup', 'tv', 1],
['psup', 'stereo', 1],
['elec', 'tv', 2],
['elec', 'speaker', 1]]
},
profits: { dimensions: ['prod'], value: [75, 50, 35] },
inventory: { indexCols: ['part'], value: [['chas', 450],['tube', 250],['cone', 800],['psup', 450], ['elec', 600]] }
},
variables: {
x: { dimensions: ['prod'], value: 0, lower: 0,finalValue: [] }},
constraints:
{"for(p in 'part')": {
"cons[p]": { formula: "sumproduct(parts[p, ], x) - inventory[p]", upper: 0 }}
},
objective: {
total: { formula: "sumproduct(x, profits)", type: "maximize", finalValue: [] }
}
}
In the data section, an inline table object, parts, is created containing two index columns (part
and prod) and a value column. (To open this full example, RGProductMixTab4.json, click the down arrow under RASON examples on the
Editor page of www.RASON.com or browse to
C:\Program Files\Frontline Systems\Solver SDK Platform\Examples\RASON if using the RASON IDE.)
Since the index set prod exists, we can dimension the profits array according to this set in order to assign
the correct profit values to the appropriate products. The inventory values are contained in the table, inventory. This table
has one index column, part ( indexCols: ['part']) .
Within constraints, we define 5 constraints for each p in the index set 'part', then assign
the evaluation values to cons[5]. The component, cons[5], is defined as a table since neither the
dimensions property nor the upper/lower/value properties are used to implicitly define
cons[5] as an array. The variable p belongs to an index set. To reference a table element,
we would use, say, cons['chas'] rather than cons[1] as the latter is a typical array reference.
In practice, when defining model functions with index sets, the formula result identifier will likely be a table, rather than an array.
For more information on the use of index sets, tables, for() and loop(), see the later topic Using Arrays, For, Loops and Tables.
If the sortIndexCols (or sort) property is used, all indexCols will be sorted alphabetically. (Note: The properties sort and sortIndexCols perform the same function.) Otherwise, the table will be sorted as entered. In the example below, the order for prod will be: speaker, stereo, tv. While the order for part will be: chas, cone, elec, psup, tube.
data : {
parts: {indexCols: ['part', 'prod'], sortIndexCols: true,
value: [
['chas', 'tv', 1], ['chas', 'stereo', 1], ['chas', 'speaker', 0],
['tube', 'tv', 1], ['tube', 'stereo', 0], ['tube', 'speaker', 0],
['cone', 'tv', 2], ['cone', 'stereo', 2], ['cone', 'speaker', 1],
['psup', 'tv', 1], ['psup', 'stereo', 1], ['psup', 'speaker', 0],
['elec', 'tv', 2], ['elec', 'stereo', 1], ['elec', 'speaker', 1]
]
},
|