AMPL not running

16 Views Asked by At

Why am I facing this error when executing my code?

ampl: model Assingment_3.mod;

Assingment_3.mod, line 11 (offset 202):
    syntax error
context:  1  >>> 30 <<< 
# Define the set of boxes
set BOXES := 1..8;

reset; # Clears all previous AMPL definitions

set TRUCKS := 1..2;

param Volume := 
1 30
2 60
3 70
4 20
5 10
6 10
7 50
8 50;

param Priority :=
1 2 # Box 1 has priority 2 (Food)
2 3 # Box 2 has priority 3 (Urgent)
3 2 # Box 3 has priority 2 (Food)
4 2 # Box 4 has priority 2 (Food)
5 3 # Box 5 has priority 3 (Urgent)
6 3 # Box 6 has priority 3 (Urgent)
7 1 # Box 7 has priority 1 (None)
8 4; # Box 8 has priority 4 (Urgent Food)

var x{BOXES, TRUCKS} binary;

maximize TotalPriority: sum{i in BOXES, j in TRUCKS} Priority[i] * x[i,j];

subject to VolumeConstraint{j in TRUCKS}: 
    140 <= sum{i in BOXES} Volume[i] * x[i,j] <= 160;

subject to OneTruckEach{i in BOXES}: 
    sum{j in TRUCKS} x[i,j] = 1;
1

There are 1 best solutions below

0
fdabrandao On

In AMPL model and data are declared separately. The way you have sets BOXES and TRUCKS you are defining the sets at the same time you fix their values. However, for parameters such as Volume and Priority, you need to declare them before load the data from a file or from a data section as follows:

set BOXES := 1..8;
set TRUCKS := 1..2;
param Volume{BOXES}; # Declare Volume
param Priority{BOXES}; # Declare Priority

var x{BOXES, TRUCKS} binary;

maximize TotalPriority: sum{i in BOXES, j in TRUCKS} Priority[i] * x[i,j];

subject to VolumeConstraint{j in TRUCKS}: 
    140 <= sum{i in BOXES} Volume[i] * x[i,j] <= 160;

subject to OneTruckEach{i in BOXES}: 
    sum{j in TRUCKS} x[i,j] = 1;

# Load the data for Volume and Priority
data;
param Volume := 
1 30
2 60
3 70
4 20
5 10
6 10
7 50
8 50;

param Priority :=
1 2 # Box 1 has priority 2 (Food)
2 3 # Box 2 has priority 3 (Urgent)
3 2 # Box 3 has priority 2 (Food)
4 2 # Box 4 has priority 2 (Food)
5 3 # Box 5 has priority 3 (Urgent)
6 3 # Box 6 has priority 3 (Urgent)
7 1 # Box 7 has priority 1 (None)
8 4; # Box 8 has priority 4 (Urgent Food)
model;

option solver gurobi;
solve;

This will produce the output:

Gurobi 11.0.1: optimal solution; objective 20
0 simplex iteration(s)

Note however that for more flexibility, declaring the sets with just set BOXES; set TRUCKS; and then also loading the data for them in the data section would be recommended unless it is guaranteed that they will be static.