0

I am learning AMPL for a university project and i don't understand why i'm getting a syntax error, my code:

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:

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;

The error i am getting: alimentation.mod, line 16 (offset 222): syntax error context: sum {f in FOODS} >>> cost[ <<< f] * x[f];

i tried different ways of writing it, and even asking chatgpt but nothing changed

1
  • 2
    Don't you need to declare that the param cost is indexed by food when you declare the parameter? (similar for other indexed params)?
    – AirSquid
    Commented Nov 13 at 20:19

1 Answer 1

1

There are actually several errors in your model and data:

Errors in the model:

  1. Cost is an indexed parameter, but you have declared as an scalar parameter param cost;, yo should rather use param cost{FOODS};.

  2. This happens with other parameters in your model.

  3. You should not name parameters and constraints in the same way, so you have "proteins" as a parameter and also as a constraint.

Errors in the data: 4. In your data section, you are re-declaring parameters. Data section is to assign values rather than declaring new entities:

param calories{FOODS};
param proteins{FOODS};
param calcium{FOODS};
param vitaminA{FOODS};
param cost{FOODS};

All the previous lines in the data section shouldn't be there

  1. The "param" keyword is missing before assigning values to the previous ones:
param calcium :=
Bread   418
Meat    41
  1. "Gelatin" or "Gelatine"? You have 2 different names for a food.

Not the answer you're looking for? Browse other questions tagged or ask your own question.