这里有一个AMPL脚本(TSP问题与成本),但我不明白错误。
这里是代码:
param n, integer, >= 3;
/* number of nodes */
param MAX_TIME := 600;
param MAX_X :=20;
param maxspeed := 1;
set CITIES := 1..n;
/* set of nodes */
param p{CITIES};
param a{CITIES};
param b{CITIES};
set ARCS, within CITIES cross CITIES;
/* set of arcs */
param DIST{(i,j) in ARCS};
/* distance from node i to node j */
param NEXTC, integer, >= 0;
/* Next city after i in the solution */
var x{(i,j) in ARCS}, binary;
/* x[i,j] = 1 means that the salesman goes from node i to node j */
param bigM := 5000;
var tar{CITIES}; /*arrival */
var tlv{CITIES}; /* departure */
var tea{CITIES} >= 0; /* early arrival (arrival before the designated time window) */
var tla{CITIES} >= 0; /* late arrival (arrival after the designated time window) */
var ted{CITIES} >= 0; /* early departure (departure before the designated time window) */
var tld{CITIES} >= 0; /* late departure (departure after the designated time window) */
set days := 1..5;
param root := 1;
var y{(i,j) in ARCS}, >= 0;
/* y[i,j] is a flow through arc (i,j) */
minimize total: sum{(i,j) in ARCS} DIST[i,j] * x[i,j];
s.t. t1 {i in CITIES} : tlv[i] >= tar[i];
s.t. t2 {i in CITIES, j in CITIES} : tar[j] >= tlv[i] + p[i] + DIST[i,j]/maxspeed - bigM*(1-x[i,j]);
s.t. t3 {i in CITIES} : tea[i] >= a[i] - tar[i]; /* early arrival */
s.t. t4 {i in CITIES} : tla[i] >= tar[i] - b[i]; /* late arrival */
s.t. t5 {i in CITIES} : ted[i] >= a[i] - tlv[i]; /* early departure */
s.t. t6 {i in CITIES} : tld[i] >= tlv[i] - b[i]; /* late departure */
data;
param n := 9 ;
param : ARCS : DIST :=
1 2 21
1 3 8
1 4 6
1 5 10
1 6 2
1 7 4
1 8 5
1 9 5
2 1 21
2 3 18
2 4 21
2 5 23
2 6 22
2 7 20
2 8 22
2 9 25
3 1 7
3 2 16
3 4 3
3 5 9
3 6 8
3 7 4
3 8 7
3 9 9
4 1 8
4 2 18
4 3 3
4 5 10
4 6 9
4 7 6
4 8 9
4 9 11
5 1 9
5 2 25
5 3 11
5 4 9
5 6 11
5 7 8
5 8 11
5 9 13
6 1 4
6 2 23
6 3 8
6 4 7
6 5 11
6 7 5
6 8 7
6 9 8
7 1 3
7 2 20
7 3 6
7 4 5
7 5 8
7 6 4
7 8 3
7 9 8
8 1 3
8 2 20
8 3 5
8 4 4
8 5 8
8 6 5
8 7 4
8 9 7
9 1 7
9 2 24
9 3 10
9 4 8
9 5 13
9 6 7
9 7 7
9 8 6
;
param : a :=
1 705
2 420
3 420
4 420
5 420
6 420
7 420
8 420
9 420
;
param : b :=
1 785
2 795
3 725
4 500
5 785
6 785
7 430
8 785
9 785
;
param : p :=
1 65
2 55
3 125
4 65
5 65
6 65
7 65
8 65
9 65
;
option solver './cplex';
solve;
代码来自(魔术!)Stackoverflow。
在我的Kubuntu-pc中,使用AMPL和cplex,我看到了这个错误:
_cmdno 2执行“solve”命令时出错(文件 tsp_nodi_archi_costo.run,第170行,偏移2582):错误处理 约束t2 [1,1]: 无效的下标DIST [1,1]
我有其他.run文件,但它们运行良好,但这不行!为什么?谢谢!
答案 0 :(得分:0)
请注意,您的集合ARCS不包含(1,1),(2,2),....
因此,DIST [i,i]未定义,并且x [i,i]是无效索引,因为您将ARCS声明为x的索引集。
但是,正如错误消息所示,您的约束t2在{CITIES中的i,CITIES中的j}上被索引,并且它将尝试创建的第一种情况是i = 1,j = 1,这会导致下标无效。
你可以通过将t2中的索引更改为:{i in CITIES,j in CITIES:j<> I}。
编辑:我注意到其他一些问题: