Initialize a two-dimensional array from literals

时间:2019-03-25 16:34:01

标签: ampl

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.

1 个答案:

答案 0 :(得分:1)

型号:

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

数据:

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
;

有关变体,请参见Chapter 9 of the AMPL Book

“默认0”选项无需显式列出零值,这对于稀疏数据集很有用。

拥有一种AMPL输入格式,该格式允许在简单的表格布局中使用数据行的标题行和列标题来指定2-D参数,但我不知道这样。