模拟退火算法团簇matlab,模拟退火算法及其Matlab实现

clc,clear %清空环境中的变量

tic

iter= 1; %迭代次数初值

a=0.99; %温度衰减系数

t0=120; %初始温度

tf=1; %最后温度

t=t0;

Markov=10000; %Markov链长度

load data1.txt%读入城市的坐标

city=data1;

n= size(city,1); %城市距离初始化

D=zeros(n,n);for i = 1:nfor j = 1:n

D(i,j)= sqrt(sum((city(i,:) - city(j,:)).^2));

end

end

route=1:n;

route_new=route;

best_length=Inf;

Length=Inf;

best_route=route;%%

while t>=tffor j=1:Markov%进行扰动,长生新的序列route_new;if (rand<0.7)%交换两个数的顺序

ind1=0;ind2=0;while(ind1==ind2&&ind1>=ind2)

ind1=ceil(rand*n);

ind2=ceil(rand*n);

end

temp=route_new(ind1);

route_new(ind1)=route_new(ind2);

route_new(ind2)=temp;elseind=zeros(3,1);

L_ind=length(unique(ind));while (L_ind<3)

ind=ceil([rand*n rand*n rand*n]);

L_ind=length(unique(ind));

end

ind0=sort(ind);

a1=ind0(1);b1=ind0(2);c1=ind0(3);

route0=route_new;

route0(a1:a1+c1-b1-1)=route_new(b1+1:c1);

route0(a1+c1-b1:c1)=route_new(a1:b1);

route_new=route0;

end%计算路径的距离,Length_new

length_new= 0;

Route=[route_new route_new(1)];for j = 1:n

length_new= length_new+ D(Route(j),Route(j + 1));

endif length_new

Length=length_new;

route=route_new;%对最优路线和距离更新if length_new

iter= iter + 1;

best_length=length_new;

best_route=route_new;

endelse

if rand

route=route_new;

Length=length_new;

end

end

route_new=route;

end

t=t*a;

end%--------------------------------------------------------------------------

%%结果显示

toc

Route=[best_route best_route(1)];

plot([city(Route ,1)], [city(Route ,2)],'o-');

disp('最优解为:')

disp(best_route)

disp('最短距离:')

disp(best_length)

disp('最优解迭代次数:')

disp(iter)for i = 1:n%对每个城市进行标号

text(city(i,1),city(i,2),[' 'num2str(i)]);

end

xlabel('城市位置横坐标')

ylabel('城市位置纵坐标')

title(['模拟退火算法(最短距离):' num2str(best_length) ''])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值