A simple 'for()' with index set and table assignment
[Example Model: ProductMixTab4.json]
indexSets : {
part", value: ['chas','tube','cone','psup','elec'] },
{ name: "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]]
},
inventory: { indexCols: ['part'], value: [['chas',450], ['tube',250],
['cone',800], ['psup',450], ['elec',600]] }
},
constraints: {
"for(p in 'part')": {
"cons[p]": { formula: "sumproduct(parts[p,], x) - inventory[p]",
upper: 0 }
}
}
In this example we define 5 constraints for each p in the index set 'part', then assign the evaluation values to cons[5]. Within constraints, 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 is likely a table.
|