模拟退火算法求解TSP问题

模拟退火算法求解TSP问题

1.问题

我方有一个基地,经度和纬度为(70, 40).假设我方飞机速度为1000km/h.我方派一架飞机从基地出发,侦查完所有目标,再返回原来的基地.再每一目标点的侦查时间不计,求该飞机所花费的最短时间.

已知两点经纬度为(x1, y1), (x2, y2), 则两点再地球表面的距离为

d = Rarccos(cos(x1 - x2)*cos(y1)*cos(y2) + sin(y1)*sin(y2))

2.数据

53.7121   15.3046	51.1758    0.0322	46.3253   28.2753	30.3313    6.9348
56.5432   21.4188	10.8198   16.2529	22.7891   23.1045	10.1584   12.4819
20.1050   15.4562	1.9451    0.2057	26.4951   22.1221	31.4847    8.9640
26.2418   18.1760	44.0356   13.5401	28.9836   25.9879	38.4722   20.1731
28.2694   29.0011	32.1910    5.8699	36.4863   29.7284	0.9718   28.1477
8.9586   24.6635	16.5618   23.6143	10.5597   15.1178	50.2111   10.2944
8.1519    9.5325	22.1075   18.5569	0.1215   18.8726	48.2077   16.8889
31.9499   17.6309	0.7732    0.4656	47.4134   23.7783	41.8671    3.5667
43.5474    3.9061	53.3524   26.7256	30.8165   13.4595	27.7133    5.0706
23.9222    7.6306	51.9612   22.8511	12.7938   15.7307	4.9568    8.3669
21.5051   24.0909	15.2548   27.2111	6.2070    5.1442	49.2430   16.7044
17.1168   20.0354	34.1688   22.7571	9.4402    3.9200	11.5812   14.5677
52.1181    0.4088	9.5559   11.4219	24.4509    6.5634	26.7213   28.5667
37.5848   16.8474	35.6619    9.9333	24.4654    3.1644	0.7775    6.9576
14.4703   13.6368	19.8660   15.1224	3.1616    4.2428	18.5245   14.3598
58.6849   27.1485	39.5168   16.9371	56.5089   13.7090	52.5211   15.7957
38.4300    8.4648	51.8181   23.0159	8.9983   23.6440	50.1156   23.7816
13.7909    1.9510	34.0574   23.3960	23.0624    8.4319	19.9857    5.7902
40.8801   14.2978	58.8289   14.5229	18.6635    6.7436	52.8423   27.2880
39.9494   29.5114	47.5099   24.0664	10.1121   27.2662	28.7812   27.6659
8.0831   27.6705	9.1556   14.1304	53.7989    0.2199	33.6490    0.3980
1.3496   16.8359	49.9816    6.0828	19.3635   17.6622	36.9545   23.0265
15.7320   19.5697	11.5118   17.3884	44.0398   16.2635	39.7139   28.4203
6.9909   23.1804	38.3392   19.9950	24.6543   19.6057	36.9980   24.3992
4.1591    3.1853	40.1400   20.3030	23.9876    9.4030	41.1084   27.7149


3.代码

%clc;clear;

R = 6730;

sj0 = load('sj.txt'); 
% load initial data the 0 in 'sj0' may mean initial

x = sj0(:, [1:2:8]);x = x(:); 
y = sj0(:, [2:2:8]);y = y(:);
% x = x(:) and y = y(:) will transfer matrix into a n*1 matrix without loss of data

sj = [x, y]; d1 = [70, 40];
% d1 is both the beginning and end 

sj = [d1; sj; d1];
sj = sj*pi/180;
% deg2rad()

d = zeros(102);

for i = 1:101
	for j = i+1:102
		d(i, j) = R*acos(cos(sj(i, 1) - sj(j, 1))*cos(sj(i, 2))*cos(sj(j, 2)) + sin(sj(i, 2))*sin(sj(j, 2)));
		d(i, j) = R*acos(cos(sj(i, 1) - sj(j, 1))*cos(sj(i, 2))*cos(sj(j, 2)) + sin(sj(i, 2))*sin(sj(j, 2)));
	end
end

d = d + d';
% magical

path = []; long = inf;

rand('state', sum(clock));

for j = 1:100
	path0 = [1, 1 + randperm(100), 102];
	temp = 0;
	for i = 1:101
		temp = temp + d(path0(i), path0(i+1));
    end
	if temp < long
		path = path0;
		long = temp;
	end
end

e = 1e-30;
L = 20000;
alpha = 0.999;
T = 1;
for k = 1:L
	c = 2 + floor(100*rand(1, 2));
    c = sort(c);
	c1 = c(1); c2 = c(2);
	df = d(path(c1-1), path(c2)) + d(path(c1), path(c2 + 1)) - d(path(c1 - 1), path(c1)) - d(path(c2), path(c2 + 1));
    if df < 0 | exp(-df/T) >= rand
        path = [path(1:c1 - 1), path(c2: -1: c1), path(c2+1: 102)];
        long = long + df;
    end
    T = T*alpha;
    if T < e
        break; 
    end
end

path,long
xx = sj(path, 1); yy = sj(path, 2);
plot(xx, yy, '-*');


猜你喜欢

转载自blog.csdn.net/sinat_29278271/article/details/52296453