我正在为大学项目学习 AMPL,我不明白为什么会出现语法错误,我的代码:
model:
set FOODS;
param calories;
param proteins;
param calcium;
param vitaminA;
param cost;
param minCalories;
param minProt;
param minCalcium;
param minVit;
var x {FOODS} >=0;
minimize prix:
sum {f in FOODS} cost[f] * x[f];
subject to calories: sum {f in FOODS} calories[f] * x[f]>=minCalories;
subject to proteins: sum {f in FOODS} proteins[f] * x[f]>=minProt;
subject to vitaminA: sum {f in FOODS} vitaminA[f] * x[f]>=minVit;
subject to calcium: sum {f in FOODS} calcium[f] * x[f]>=minCalcium;
数据:
data;
set FOODS := Bread Meat Potatoes Cabbage Milk Gelatine;
param calories{FOODS};
param proteins{FOODS};
param calcium{FOODS};
param vitaminA{FOODS};
param cost{FOODS};
param minCalories = 3000;
param minProt = 70;
param minCalcium = 800;
param minVit = 500;
calories:=
Bread 1254
Meat 1457
Potatoes 318
Cabbage 48
Milk 309
Gelatin 1725;
proteins :=
Bread 39
Meat 73
Potatoes 8
Cabbage 4
Milk 16
Gelatin 43;
calcium :=
Bread 418
Meat 41
Potatoes 42
Cabbage 141
Milk 536
Gelatin 0;
vitaminA :=
Bread 0
Meat 0
Potatoes 70
Cabbage 860
Milk 720
Gelatin 0;
cost :=
Bread 0.3
Meat 1
Potatoes 0.05
Cabbage 0.08
Milk 0.23
Gelatin 0.48;
我收到的错误: alimentation.mod,第 16 行(偏移量 222): 语法错误 上下文:总和 {f in FOODS} >>> 成本[ <<< f] * x[f];
我尝试了不同的写法,甚至询问chatgpt但没有任何改变
您的模型和数据实际上存在几个错误:
模型中的错误:
Cost 是一个索引参数,但您已声明为标量参数
param cost;
,您应该使用param cost{FOODS};
。
模型中的其他参数也会发生这种情况。
您不应该以相同的方式命名参数和约束,因此您将“蛋白质”作为参数,同时也作为约束。
数据错误: 4. 在您的数据部分中,您正在重新声明参数。数据部分是分配值而不是声明新实体:
param calories{FOODS};
param proteins{FOODS};
param calcium{FOODS};
param vitaminA{FOODS};
param cost{FOODS};
数据部分中所有前面的行不应该在那里
param calcium :=
Bread 418
Meat 41