答案 0 :(得分:0)
以下是来自AMPL的here(由Glpk使用的GNU MathProg的超集)中的TSP问题示例:
set S ordered;
param n := card {S};
set SS := 0 .. (2**n - 1);
set POW {k in SS} := {i in S: (k div 2**(ord(i)-1)) mod 2 = 1};
set LINKS := {i in S, j in S: ord(i) < ord(j)};
param cost {LINKS} >= 0;
var X {LINKS} binary;
minimize TotCost: sum {(i,j) in LINKS} cost[i,j] * X[i,j];
subj to Tour {i in S}:
sum {(i,j) in LINKS} X[i,j] + sum {(j,i) in LINKS} X[j,i] = 2;
subj to SubtourElim {k in SS diff {0,2**n-1}}:
sum {i in POW[k], j in S diff POW[k]: (i,j) in LINKS} X[i,j] +
sum {i in POW[k], j in S diff POW[k]: (j,i) in LINKS} X[j,i] >= 2;
data;
set S := a b c d ;
param cost: a b c d :=
a . 43 21 19
b . . 21 12
c . . . 39
d . . . . ;
如果您在每个城市之间没有链接,则可以通过仅提供LINKS
集中的现有链接对其进行建模,例如:
# model:
set LINKS;
# data:
set LINKS := (a b) (b d);