Reinderien
Reinderien

Reputation: 15231

Initialize a two-dimensional array from literals

I have this data file:

param:                 name   car pro fat vit cal :=
 1             'Fiddleheads'   3   1   0   3   80
 2         'Fireweed Shoots'   3   0   0   4  150
 3      'Prickly Pear Fruit'   2   1   1   3  190
;

and this model:

set I;
set J;

param name{I} symbolic;

param  car{I} integer >= 0;
param  pro{I} integer >= 0;
param  fat{I} integer >= 0;
param  vit{I} integer >= 0;
param  cal{I} integer >= 0;
param  nut{i in I, J} = (car[i], pro[i], fat[i], vit[i]);

The last line is invalid:

mod, line 10 (offset 176):
        syntax error
context:  param  nut{i in I, J} =  >>> (car[i], <<<  pro[i], fat[i], vit[i]);

but I don't know how to get an equivalent working. Essentially, I want to form a {3,4} array based on a literal expression. I've tried a handful of different syntaxes both in the data and model file and haven't been able to get any working.

Upvotes: 0

Views: 639

Answers (1)

G_B
G_B

Reputation: 1573

Model:

set names;
set components;
param nut{names,components} default 0;

Data:

set names := 
Fiddleheads
'Fireweed Shoots' 
'Prickly Pear Fruit';

set components := car pro fat vit cal
;

param nut :=
[Fiddleheads,*] 
car 3 pro 1 vit 3 cal 80
['Fireweed Shoots',*]
car 3 vit 4 cal 150
['Prickly Pear Fruit',*]
car 2 pro 1 fat 1 vit 3 cal 190
;

See Chapter 9 of the AMPL Book for variants.

The "default 0" option avoids the need to explicitly list zero values, which can be useful for sparse data sets.

It would be useful to have an AMPL input format that allows a 2-D parameter to be specified in a simple table layout with row and column headers, along the lines of your data step, but I'm not aware of one that does this.

Upvotes: 1

Related Questions