Mathematica 能否完全替代Lingo?
要求从1开始,最终回到1,且路径有方向,要求所有路径都经过。以下是lingo的代码
sets:
nodes/A,B,C,D,E,F,G,H,I,J,K,L/;
arcs(nodes,nodes)/
A,B A,E
B,C B,E B,F
C,B C,D
D,C D,H
E,A E,F
F,B F,E F,G F,I F,J
G,C G,F G,H G,K
H,D H,K H,L
I,E I,J
J,F J,G
K,G K,J
L,K
/: c, x;
endsets
data:
c=
150 165
130 230 160
140 100
100 190
165 144
170 144 128 218 174
200 122 109 185
180 141 190
194 148
174 233
185 135
110;
enddata
n = @size(nodes);
min = @sum(arcs: c * x);
@for(nodes(i):
@sum(arcs(i,j):x(i,j))=@sum(arcs(k,i):x(k,i))
);
@for(arcs: @bnd(1,x,9));
它只能算出路径的步数和路程,并不能得到线路。希望能用mma解决这个问题。---------------------------------------------------------------------自己用mma写的程序对于四点还可以,但是扩展到12点实在繁琐:
P11 = {2, 5}; P12 = {150, 165};P21 = {5, 6}; P22 = {230, 160};P51 = {1, 6}; P52 = {165, 144};P61 = {2, 5}; P62 = {170, 144};(*只考虑1、2、5、6四点,P11为第一点“可去往的点”,P12表述对应的路程*)open :=Module[{i = 1, randomD, randomP, D, randomreal, p1, p2, p5, p6, c, u,Df = 80000, uf, path = {1}, pathf = {1}, pb},(*随机搜索*)For{p1 = {}; p2 = {}; p5 = {}; p6 = {}; u = 0; D = 0; i = 1;randomP = P11; randomD = P12;While[Length != 2 || Length != 2 || Length != 2 ||Length != 2 || i != 1,randomreal = RandomInteger[{1, Length}];c = randomP[]; AppendTo;(*Print;*)Which[i == 1 &&Product] != c, 1, 0], {i, 1, Length}] ==1, {AppendTo},i == 2 &&Product] != c, 1, 0], {i, 1, Length}] ==1, {AppendTo},i == 5 &&Product] != c, 1, 0], {i, 1, Length}] ==1, {AppendTo},i == 6 &&Product] != c, 1, 0], {i, 1, Length}] ==1, {AppendTo}];i = c;D = D + randomD[];Whichi == 2, {randomP = P21, randomD = P22},i == 5, {randomP = P51, randomD = P52},i == 6, {randomP = P61, randomD = P62}];u = u + 1; If];Ifpath = {1}];}];Print;
open
页:
[1]