Algorithm 1 41 27 13 16 3 99 25 35 0 38; 18 12 13 25 22 37 84 13 18 38 0]; [路径,距离]=tsp(表格,长度(表格)) 函数[path,dist]=tsp(
1 41 27 13 16 3 99 25 35 0 38; 18 12 13 25 22 37 84 13 18 38 0]; [路径,距离]=tsp(表格,长度(表格)) 函数[path,dist]=tsp(D,n) L=40*n; epsi=1e-9; x=randperm(n); fx=distCalculatorReturn(D,x); T=1000000; 而T>epsi 对于i=1:L num1=1+楼层(兰特*n); num2=1+楼层(兰特*n); 而num1==num2 num1=1+楼层(兰特*n); 结束 y=x; swap1=y(num1); y(num1)=y(num2); y(num2)=swap1; fy=距离计算返回(D,y); 如果fyAlgorithm 1 41 27 13 16 3 99 25 35 0 38; 18 12 13 25 22 37 84 13 18 38 0]; [路径,距离]=tsp(表格,长度(表格)) 函数[path,dist]=tsp(,algorithm,matlab,Algorithm,Matlab,1 41 27 13 16 3 99 25 35 0 38; 18 12 13 25 22 37 84 13 18 38 0]; [路径,距离]=tsp(表格,长度(表格)) 函数[path,dist]=tsp(D,n) L=40*n; epsi=1e-9; x=randperm(n); fx=distCalculatorReturn(D,x); T=1000000; 而T>epsi 对于i=1:L num1=1+楼层(兰特*n); num2=1+楼层
bruteForce
时,它不会返回到下一个节点的距离,而是返回以下距离的最小和(深度优先),首先非常感谢!不过,我不确定,我理解为什么我的算法只适用于最近的节点。请注意,当递归调用bruteForce
时,它不会返回到下一个节点的距离,而是以下距离的最小和(深度优先),非常感谢!但是,你是如何得出模拟退火的常数T,epsi,L的?模拟退火模拟退火的过程。所以你需要从一个任意大的足够大的温度T开始。epsi只是while循环的公差控制(将控制完成多少次迭代)。L是各设定温度下马尔可夫链的长度。值在某种程度上是默认值。与所有的元启发式一样,如果它们不能解决这个问题,那么您需要进行实验以找到最佳参数。这些参数和基本交换对于大小的TSP问题似乎已经足够好了。非常感谢!但是,你是如何得出模拟退火的常数T,epsi,L的?模拟退火模拟退火的过程。所以你需要从一个任意大的足够大的温度T开始。epsi只是while循环的公差控制(将控制完成多少次迭代)。L是各设定温度下马尔可夫链的长度。值在某种程度上是默认值。与所有的元启发式一样,如果它们不能解决这个问题,那么您需要进行实验以找到最佳参数。这些参数和基本交换似乎足以解决如此规模的TSP问题。
%main function:
[siz, ~] = size(table);
done(1:siz) = false;
done(1) = true;
[dist, path] = bruteForce(table, done, 1);
function [distance, path] = bruteForce(table, done, index)
size = length(done);
dmin = inf;
distance = 0;
path = [];
%finding minimum distance
for i = 1:size
if ~done(i)
done(i) = true;
%iterating through all nodes using recursion
[d, p] = bruteForce(table, done, i);
if (d < dmin)
dmin = d;
path = [i p];
distance = dmin + table(i, index);
end
%freing the node again
done(i) = false;
end
end
if distance == 0
distance = table(1, index);
path = 1;
end
B = [0 29 20 21 16 31 100 12 4 31 18;
29 0 15 29 28 40 72 21 29 41 12;
20 15 0 15 14 25 81 9 23 27 13;
21 29 15 0 4 12 92 12 25 13 25;
16 28 14 4 0 16 94 9 20 16 22;
31 40 25 12 16 0 95 24 36 3 37;
100 72 81 92 94 95 0 90 101 99 84;
12 21 9 12 9 24 90 0 15 25 13;
4 29 23 25 20 36 101 15 0 35 18;
31 41 27 13 16 3 99 25 35 0 38;
18 12 13 25 22 37 84 13 18 38 0];
1-8-5-4-10-6-3-7-2-11-9-1 = 253km
1-8-11-3-4-6-10-5-9-2-7-1 = 271km
% distance matrix
B = [0 29 20 21 16 31 100 12 4 31 18;
29 0 15 29 28 40 72 21 29 41 12;
20 15 0 15 14 25 81 9 23 27 13;
21 29 15 0 4 12 92 12 25 13 25;
16 28 14 4 0 16 94 9 20 16 22;
31 40 25 12 16 0 95 24 36 3 37;
100 72 81 92 94 95 0 90 101 99 84;
12 21 9 12 9 24 90 0 15 25 13;
4 29 23 25 20 36 101 15 0 35 18;
31 41 27 13 16 3 99 25 35 0 38;
18 12 13 25 22 37 84 13 18 38 0];
% compute all possible paths assuming we always start at node 1
nNodes = size(B,1);
paths = perms(2:nNodes);
nPaths = size(paths,1);
paths = [ones(nPaths,1) paths ones(nPaths,1)]; % start and finish tour at node 1
% with a random start point:
% paths = perms(1:nNodes);
% paths = [perms(1:nNodes) paths(:,1)];
% compute overall distance for each path
distance = inf;
for idx=1:nPaths
from = paths(idx,1:end-1);
to = paths(idx,2:end);
d = sum(diag(B(from,to)));
if d<distance
distance = d;
optPath = paths(idx,:);
end
end
optPath = [1 9 11 2 7 3 6 10 4 5 8 1]
distance = 253
table = [0 29 20 21 16 31 100 12 4 31 18;
29 0 15 29 28 40 72 21 29 41 12;
20 15 0 15 14 25 81 9 23 27 13;
21 29 15 0 4 12 92 12 25 13 25;
16 28 14 4 0 16 94 9 20 16 22;
31 40 25 12 16 0 95 24 36 3 37;
100 72 81 92 94 95 0 90 101 99 84;
12 21 9 12 9 24 90 0 15 25 13;
4 29 23 25 20 36 101 15 0 35 18;
31 41 27 13 16 3 99 25 35 0 38;
18 12 13 25 22 37 84 13 18 38 0];
[siz, ~] = size(table);
[bp, b] = bruteForce(table, siz)
function [bestpath, best] = bruteForce(table, siz)
p = perms(1:siz);
[r, c] = size(p);
best = inf;
for i = 1:r
path = p(i, :);
dist = distCalculatorReturn(table, path);
if dist < best
best = dist;
bestpath = path;
end
end
bestpath = [bestpath, bestpath(1)];
end
function [totaldist] = distCalculatorReturn(distMatrix, proposedPath)
dist = 0;
i = 1;
while i ~= length(proposedPath)
dist = dist + distMatrix(proposedPath(i),proposedPath(i+1));
i = i+1;
end
dist = dist + distMatrix(proposedPath(1), proposedPath(end));
totaldist = dist;
end
table = [0 29 20 21 16 31 100 12 4 31 18;
29 0 15 29 28 40 72 21 29 41 12;
20 15 0 15 14 25 81 9 23 27 13;
21 29 15 0 4 12 92 12 25 13 25;
16 28 14 4 0 16 94 9 20 16 22;
31 40 25 12 16 0 95 24 36 3 37;
100 72 81 92 94 95 0 90 101 99 84;
12 21 9 12 9 24 90 0 15 25 13;
4 29 23 25 20 36 101 15 0 35 18;
31 41 27 13 16 3 99 25 35 0 38;
18 12 13 25 22 37 84 13 18 38 0];
[path, dist] = tsp(table, length(table))
function [path, dist] = tsp(D, n)
L = 40*n;
epsi = 1e-9;
x = randperm(n);
fx = distCalculatorReturn(D, x);
T = 1000000;
while T > epsi
for i=1:L
num1 = 1 + floor(rand*n);
num2 = 1 + floor(rand*n);
while num1 == num2
num1 = 1 + floor(rand*n);
end
y = x;
swap1 = y(num1);
y(num1) = y(num2);
y(num2) = swap1;
fy = distCalculatorReturn(D,y);
if fy < fx
x = y;
fx = fy;
elseif rand < exp(-(fy - fx)/T)
x = y;
fx = fy;
end
end
T = 0.9*T;
end
path = [x, x(1)];
dist = fx;
end