AMPL循环并忽略变量

时间:2018-10-06 17:09:36

标签: syntax iteration ampl

最近开始使用ampl,在迭代和索引编制方面遇到了麻烦。

我有以下模型脚本:

set NUTR;
set FOOD;

param Cost {FOOD} > 0; #Costs pr. gramm

param f_min {FOOD} >=0;
param f_max {j in FOOD} >= f_min[j];

param n_min {NUTR} >=0;
param n_max {i in NUTR} >= n_min[i];

param amount {NUTR,FOOD} >= 0; #amount nutrients pr. gramm

var Purchase {j in FOOD} >= f_min[j], <= f_max[j];

minimize Total_cost: sum{j in FOOD} Cost[j] * Purchase[j];
s.t. Intake {i in NUTR}: n_min[i] <= sum {j in FOOD} amount[i,j] *  
Purchase[j] <= n_max[i]; 

data;

set NUTR := PRT CRB FAT KCAL;
set FOOD := CD VL HM SLM TN MCR EGG BN PS FSL PEN RC ORG PT ON AVC CLF CBG ALM TMT OLV MLK ;

该模型在数据集中运行良好,但我的问题是: 是否有可能创建一个提供最佳解决方案的循环,一次从设置的食品中省去一种产品?

1 个答案:

答案 0 :(得分:0)

您可以在.run中相当容易地做到这一点,方法是循环遍历FOOD,然后使用集合差异运算符定义集合“来自FOOD的所有内容,但该元素除外”。这应该给您大致的想法:

model;
set FOOD;
data; 
set FOOD := CD VL HM SLM TN MCR EGG BN PS FSL PEN RC ORG PT ON AVC CLF CBG ALM TMT OLV MLK ;

for{f in FOOD}{
    printf "\nThe set Food minus %s is:", f;
    display (FOOD diff {f});
    # replace with your model and solve commands, indexing over "FOOD diff {f}". 
}