AMPL 未运行

问题描述 投票:0回答:1

为什么我在执行代码时遇到此错误?

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
1个回答
0
投票

在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;
声明集合,然后在数据部分中加载它们的数据,除非保证它们是静态的。

© www.soinside.com 2019 - 2024. All rights reserved.