[問題] 圖跑不出來!!已回收
各位大大好~
小弟是matlab新手,最近在用該程式進行繪圖卻出現不了!!
懇請板上神手大大能幫小弟解決這個問題,可以的話可以告知錯再哪該如何該才對~
小弟感謝大大們!!
以下為程式碼:
function out = tsp(cities)
cities = [ 0.6606,0.9500;
0.9695,0.6740;
0.5906,0.5029;
0.2124,0.8274;
0.0398,0.9697;
0.1367,0.5979;
0.9536,0.2184;
0.6091,0.7148;
0.8767,0.2395;
0.8148,0.2867;
0.3876,0.8200;
0.7041,0.3296;
0.0213,0.1649;
0.3429,0.3025;
0.7471,0.8192;
0.5449,0.9392;
0.9464,0.8191;
0.1247,0.4351;
0.1636,0.8646;
0.8668,0.6768; ];
city_number = length(cities);
any_two_distance = zeros(city_number);
for x = 1:city_number
for y = 1:city_number
any_two_distance(x,y) = norm(cities(x, :) - cities(y, :));
end
end
all_dE = zeros(city_number, 1);
for i = 1:city_number
path = randperm(city_number);
energy = sum( any_two_distance((path-1)*city_number + [path(2:city_number)
path(1)]));
new_path = path;
index = round(rand(2,1)*city_number+.5);
shift = (min(index):max(index));
new_path(shift) = fliplr(path(shift));
all_dE(i) = abs(energy - sum(sum(diff(cities([new_path
new_path(1)],:))'.^2)));
end
dE = min(all_dE);
temperature = 2;
out = [path path(1)];
plot(cities(out(:), 1), cities(out(:), 2),'black.', 'Markersize', 10);
axis square; hold on
h = plot(cities(out(:), 1), cities(out(:), 2)); hold off
MaxTrialN = city_number*150;
MaxAcceptN = city_number*20;
StopTolerance = 0.001;
cooling_rate = 0.99;
minE = inf;
maxE = -1;
while (maxE - minE)/maxE > StopTolerance,
minE = inf;
maxE = 0;
TrialN = 0;
AcceptN = 0;
while TrialN < MaxTrialN & AcceptN < MaxAcceptN,
new_path = path;
index = round(rand(2,1)*city_number+.5);
shift = (min(index):max(index));
new_path(shift) = fliplr(path(shift));
new_energy =
sum(any_two_distance((new_path-1)*city_number+[new_path(2:city_number)
new_path(1)]));
if rand < exp((energy - new_energy)/temperature),
energy = new_energy;
path = new_path;
minE = min(minE, energy);
maxE = max(maxE, energy);
AcceptN = AcceptN + 1;
end
TrialN = TrialN + 1;
end
out = [path path(1)];
set(h, 'xdata', cities(out(:), 1), 'ydata', cities(out(:), 2));
drawnow;
temperature = temperature*cooling_rate;
end
for i = 1:city_number,
text(cities(path(i),1)+0.01, cities(path(i),2)+0.01, int2str(i), 'tag',
'text');
end
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 120.107.174.109
→
01/27 00:40, , 1F
01/27 00:40, 1F