一个水母BBS上的SA的MATLAB程序

news/2024/5/18 8:51:23 标签: matlab, bbs, distance, path, plot, matrix

发信站: 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, Smilebbs.matwav.com/images/smiles/smile.gif" width="15" /> - loc(j, Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />);
distance(i, j) = norm(loc(i, Smilebbs.matwav.com/images/smiles/smile.gif" width="15" /> - loc(j, Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />);
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_dELight Bulbbbs.matwav.com/images/smiles/lightbulb_smile.gif" width="19" /> = abs(energy - ...
sum(sum(diff(loc([new_path new_path(1)],Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />)'.^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(Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />, 1), loc(out(Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />, 2),'r.', 'Markersize', 20);
axis square; hold on
h = plot(loc(out(Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />, 1), loc(out(Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />, 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(Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />, 1), 'ydata', loc(out(Smilebbs.matwav.com/images/smiles/smile.gif" width="15" />, 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(pathLight Bulbbbs.matwav.com/images/smiles/lightbulb_smile.gif" width="19" />,1)+0.01, loc(pathLight Bulbbbs.matwav.com/images/smiles/lightbulb_smile.gif" width="19" />,2)+0.01, int2strLight Bulbbbs.matwav.com/images/smiles/lightbulb_smile.gif" width="19" />, ...
'fontsize', 8);
end
这个程序是.m文件,在matlab中运行。


89行出错!accept it!什么意思?

呵呵,accept it跳行了,它的上一行又一个%,accept it是跟在%后面的.


http://www.niftyadmin.cn/n/1374144.html

相关文章

try-with-resources 代替 try - catch - finally

目录 1、常规 try - catch - finally 写法 2、try - with - resources 写法 3、try - with - resources 写法资源自动关闭原理 4、动手实现拥有自动关闭功能的类 正常执行时的运行情况&#xff1a; 代码块中存在异常情况时&#xff1a; 1、常规 try - catch - finally 写法…

E-MapReduce(Hadoop)10大类问题之集群规划

集群规划类问题 所有的使用Hadoop或者打算使用Hadoop的人肯定会遇到集群规划的问题&#xff0c;我到底使用多大的集群规模呢?有没有一个标准呢? 本篇文章就为你介绍集群规划。 在云环境E-MapReduce中&#xff0c;各种搭配是比较自由的。当前&#xff0c;cpu跟memory的比例有1…

希尔伯特的二十三个数学问题

转载] 希尔伯特的二十三个数学问题 发信站: 南京大学小百合站 (Fri May 30 01:01:29 2003) 1900年&#xff0c;德国数学家D.希尔伯特在巴黎第二届国际数学家大会上作了题为《数学问题 》的著名讲演&#xff0c;其中对各类数学问题的意义、源泉及研究方法发表了精辟的见解&…

Mac 终端自动补全大小写敏感,不区分大小写的设置(zsh/bash)

目录 1、bash 终端设置 2、zsh 终端设置 Mac 终端&#xff0c;在设置自动补全不区分大小写时&#xff0c;按照 bash 方式设置后&#xff0c;不生效。 经查询&#xff0c;从 macOS Catalina 版开始&#xff0c;zsh (Z shell) 是所有新建用户帐户的默认 Shell&#xff0c;原 b…

RHEL samba 服务多配置文件安全管理

某公司以RHEL系统配置服务器&#xff0c;现为了共享各类文档资料&#xff0c;要求在rhel系统上建立一个共享目录/tec,该目录对技术部门员工完全开放&#xff0c;/sale目录对销售部门人员完全开放。现公司有如下三类人员需要登录samba服务器使用这2个共享目录&#xff0c;要求如…

SA

退火概念是80年代初期研究组合优化问题时提出的&#xff0c;该方法解决优化问题 的出发点是基于物理中固体物质的退火过程与一般组合优化问题之间的相似 性。在对固体物质进行退火处理时&#xff0c;通常是先将它加温熔化&#xff0c;使其中的粒子 可以自由运动&#xff0c;然后…

「云原生 | Docker」手把手教你在 Linux 安装使用 Docker

目录 一、前提 二、安装 Docker 1. 通过仓库进行安装&#xff08;在线方式&#xff09; 1.1 设置存储库 1.2 查看可安装版本 1.3 安装 Docker 1.4 启动 Docker 1.5 验证是否成功 2. 通过 RMP 包安装&#xff08;离线方式&#xff09; 2.1 下载 rpm 包 2.2 安装 Dock…

python列表中的深浅copy

列表中的赋值和平常的赋值是不一样的&#xff0c;看下面的代码&#xff1a;In [1]: a 1In [2]: b aIn [3]: a Out[3]: 1In [4]: b Out[4]: 1In [5]: id(a) Out[5]: 4452948992In [6]: id(b) Out[6]: 4452948992In [7]: id(1) Out[7]: 4452948992In [8]: a 4In [9]: b Out[9]…