为什么我在执行代码时遇到此错误?
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;
在AMPL中模型和数据是分开声明的。您拥有集合
BOXES
和 TRUCKS
的方式是在定义集合的同时修复它们的值。但是,对于 Volume
和 Priority
等参数,您需要在从文件或数据段加载数据之前声明它们,如下所示:
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;
这将产生输出:
Gurobi 11.0.1: optimal solution; objective 20
0 simplex iteration(s)
但请注意,为了获得更大的灵活性,建议仅使用
set BOXES; set TRUCKS;
声明集合,然后在数据部分中加载它们的数据,除非保证它们是静态的。