SA解决TSP问题的程序

转载 2004年08月03日 19:37:00
发信人: lonelyu (小鱼儿◎大海的方向), 信区: AI
标  题: Re: 谁有模拟退火的程序
发信站: 饮水思源 (2002年01月21日21:46:45 星期一), 站内信件

发信站: BBS 水木清华站 (Wed May 16 11:36:04 2001)
这是SA解决TSP问题的程序。
function out = tsp(loc)
% TSP Traveling salesman problem (TSP) using SA (simulated annealing).
%      TSP by itself will generate 20 cities within a unit cube and
%      then use SA to slove this problem.
%
%      TSP(LOC) solve the traveling salesman problem with cities'
%      coordinates given by LOC, which is an M by 2 matrix and M is
%      the number of cities.
%
%      For example:
%
%              loc = rand(50, 2);
%              tsp(loc);
if nargin == 0,
% The following data is from the post by Jennifer Myers (jmyers@nwu.
edu)
edu)
% to comp.ai.neural-nets. It's obtained from the figure in
% Hopfield & Tank's 1985 paper in Biological Cybernetics
% (Vol 52, pp. 141-152).
        loc = [0.3663, 0.9076; 0.7459, 0.8713; 0.4521, 0.8465;
                0.7624, 0.7459; 0.7096, 0.7228; 0.0710, 0.7426;
                0.4224, 0.7129; 0.5908, 0.6931; 0.3201, 0.6403;
                0.5974, 0.6436; 0.3630, 0.5908; 0.6700, 0.5908;
                0.6172, 0.5495; 0.6667, 0.5446; 0.1980, 0.4686;
                0.3498, 0.4488; 0.2673, 0.4274; 0.9439, 0.4208;
                0.8218, 0.3795; 0.3729, 0.2690; 0.6073, 0.2640;
                0.4158, 0.2475; 0.5990, 0.2261; 0.3927, 0.1947;
                0.5347, 0.1898; 0.3960, 0.1320; 0.6287, 0.0842;
                0.5000, 0.0396; 0.9802, 0.0182; 0.6832, 0.8515];
end
NumCity = length(loc);          % Number of cities
distance = zeros(NumCity);      % Initialize a distance matrix
% Fill the distance matrix
for i = 1:NumCity,
        for j = 1:NumCity,
                distance(i, j) = norm(loc(i, :) - loc(j, :));
                distance(i, j) = norm(loc(i, :) - loc(j, :));
        end
end
% To generate energy (objective function) from path
%path = randperm(NumCity);
%energy = sum(distance((path-1)*NumCity + [path(2:NumCity) path(1)]));
% Find typical values of dE
count = 20;
all_dE = zeros(count, 1);
for i = 1:count
        path = randperm(NumCity);
        energy = sum(distance((path-1)*NumCity + [path(2:NumCity)
path(1)]));
        new_path = path;
        index = round(rand(2,1)*NumCity+.5);
        inversion_index = (min(index):max(index));
        new_path(inversion_index) = fliplr(path(inversion_index));
        all_dE(i) = abs(energy - ...
                sum(sum(diff(loc([new_path new_path(1)],:))'.^2)));
end
dE = max(all_dE);
dE = max(all_dE);
temp = 10*dE;  % Choose the temperature to be large enough
fprintf('Initial energy = %f/n/n',energy);
% Initial plots
out = [path path(1)];
plot(loc(out(:), 1), loc(out(:), 2),'r.', 'Markersize', 20);
axis square; hold on
h = plot(loc(out(:), 1), loc(out(:), 2)); hold off
MaxTrialN = NumCity*100;              % Max. # of trials at a
temperature
MaxAcceptN = NumCity*10;             % Max. # of acceptances at a
temperature
StopTolerance = 0.005;           % Stopping tolerance
TempRatio = 0.5;                % Temperature decrease ratio
minE = inf;                     % Initial value for min. energy
maxE = -1;                     % Initial value for max. energy
% Major annealing loop
while (maxE - minE)/maxE > StopTolerance,
        minE = inf;
        minE = inf;
        maxE = 0;
        TrialN = 0;             % Number of trial moves
        AcceptN = 0;           % Number of actual moves
        while TrialN < MaxTrialN & AcceptN < MaxAcceptN,
                new_path = path;
                index = round(rand(2,1)*NumCity+.5);
                inversion_index = (min(index):max(index));
                new_path(inversion_index) =
fliplr(path(inversion_index));
                new_energy = sum(distance( ...
                        (new_path-1)*NumCity+[new_path(2:NumCity)
new_path(1)]));
                if rand < exp((energy - new_energy)/temp),      % 
accept
 it!
                        energy = new_energy;
                        path = new_path;
                        minE = min(minE, energy);
                        maxE = max(maxE, energy);
                        AcceptN = AcceptN + 1;
                end
                TrialN = TrialN + 1;
        end
        end
        % Update plot
        out = [path path(1)];
        set(h, 'xdata', loc(out(:), 1), 'ydata', loc(out(:), 2));
        drawnow;
        % Print information in command window
        fprintf('temp. = %f/n', temp);
        tmp = sprintf('%d ',path);
        fprintf('path = %s/n', tmp);
        fprintf('energy = %f/n', energy);
        fprintf('[minE maxE] = [%f %f]/n', minE, maxE);
        fprintf('[AcceptN TrialN] = [%d %d]/n/n', AcceptN, TrialN);
        % Lower the temperature
        temp = temp*TempRatio;
end
% Print sequential numbers in the graphic window
for i = 1:NumCity,
        text(loc(path(i),1)+0.01, loc(path(i),2)+0.01, int2str(i), ...
                'fontsize', 8);
end
这个程序是.m文件,在matlab中运行。
【 在 killtime (小小虾) 的大作中提到: 】
: 谁有模拟退火算法求解TSP问题的源程序(最好是C语言的),发给我
: 一份,小弟不胜感激!


--
磨刀中。。。。。。

基于遗传算法求解TSP问题(C#界面)

最近一直在忙毕设的事,深深后悔当初的选题,选了个自动信任协商的题目,纠结啊...这论文写得感觉受到严重挫败,整个人都变得暴躁不少...一直没时间更新博客,估计下篇博客又得等12月了... 今天给大家...
  • wangqiuyun
  • wangqiuyun
  • 2013-11-22 15:47:55
  • 8333

C语言编写遗传算法解决TSP旅行商问题

最近在上计算智能的课,老师刚刚教了遗传算法,布置了用遗传算法解决TSP的问题的作业,于是经过几小时的奋战,终于编写完成。 首先先对TSP问题进行分析。TSP问题,也就是旅行商问题,题目的大题内容是 一...
  • sun15980
  • sun15980
  • 2015-10-22 13:22:18
  • 4341

C语言解决TSP问题

  • 2013年05月26日 12:20
  • 10KB
  • 下载

遗传算法 求解旅行商 TSP 问题,matlab代码

学习启发式算法时,旅行商问题是一个经典的例子。其中,遗传算法可以用来求解该问题。遗传算法是一种进化算法,由于其启发式算法的属性,并不能保证得到最优解。求解效果与初始种群选取,编码方法,选择方法,交叉变...
  • robert_chen1988
  • robert_chen1988
  • 2016-11-02 01:24:11
  • 13032

模拟退火算法求解旅行商问题

一. 爬山算法 ( Hill Climbing )          介绍模拟退火前,先介绍爬山算法。爬山算法是一种简单的贪心搜索算法,该算法每次从当前解的临近解空间中选择一个最优解作为当前解,...
  • lalor
  • lalor
  • 2012-06-24 22:54:25
  • 11819

SA解决TSP问题的程序

发信人: lonelyu (小鱼儿◎大海的方向), 信区: AI标 题: Re: 谁有模拟退火的程序发信站: 饮水思源 (2002年01月21日21:46:45 星期一), 站内信件发信站: BBS...
  • yuanqingfei
  • yuanqingfei
  • 2004-08-03 19:37:00
  • 2266

c语言实现ping源码

从Android源码中拷贝下来的,可以在Linux直接编译运行 ping.c /* * Copyright (c) 1989 The Regents of the University of C...
  • earbao
  • earbao
  • 2013-12-11 16:34:06
  • 5756

elbycheck.exe

  进程知识库 elbycheck - elbycheck.exe - 进程信息进程文件: elbycheck 或者 elbycheck.exe进程名称: ElbyCheck  描述:elbychec...
  • ProcessInfo
  • ProcessInfo
  • 2007-12-02 23:37:00
  • 197

Simulated annealing algorithm

<em>SA</em>模拟退火算法 立即下载 上传者: fhy12410 时间:...<em>TSP</em> 问题的模拟退火算法与穷举算法 立即下载 上传者...yy3806 时间: 2011-03-<em>23</em> 综合评分: 3 积分/<em>C</em>...
  • 2018年04月09日 00:00
收藏助手
不良信息举报
您举报文章:SA解决TSP问题的程序
举报原因:
原因补充:

(最多只允许输入30个字)