AMPL 循环和省略变量
AMPL loops and omitting variables
最近开始使用 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 ;
模型 运行 适合数据集,但我的问题是:
是否可以创建一个给出最佳解决方案的循环,一次从 FOOD 集合中省略一种产品?
您可以在 .运行 中相当轻松地执行此操作,方法是循环 FOOD,然后使用集合差分运算符定义集合 "everything from FOOD except this one element"。这应该给你大致的想法:
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}".
}
最近开始使用 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 ;
模型 运行 适合数据集,但我的问题是: 是否可以创建一个给出最佳解决方案的循环,一次从 FOOD 集合中省略一种产品?
您可以在 .运行 中相当轻松地执行此操作,方法是循环 FOOD,然后使用集合差分运算符定义集合 "everything from FOOD except this one element"。这应该给你大致的想法:
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}".
}