tsp问题matlab程序,TSP问题的matlab程序(类似GA)

TSP问题的matlab程序(类似GA) 但又不是。。。。。 注意淘汰处理  觉得 有点启发。。呵呵 /////////////////// %test ap3=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71;58,69;54,62;51,67;37,84;41,94; 2,99;7,64;22,60;25,62;18,54;4,50;13,40;18,40;24,42;25,38;41,26;45,21;44,35;58,35;62,32]; Map1=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71]; Map2=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71;58,69;54,62;51,67;37,84;41,94;      2,99;7,64;22,60;25,62;18,54]; MaxIter=100; SizeScale=1000; ChangeRate=0.05; [time,opt,fval]=TspGa(Map1,MaxIter,SizeScale,ChangeRate) function  [time,opt,fval]=TspGa( Map,MaxIter,SizeScale,ChangeRate ) %This is function %Input: %Output: t=cputime; %MaxIter=100; %SizeScale=100; n=max(size(Map)); %compute the roadmatrix DistMatrix=zeros(n,n); for i=1:n     for j=1:n         DistMatrix(i,j)=distance(Map(i,:),Map(j,:));     end end %to inital the road( oder the cites ) Road=ones(SizeScale,n); for i=1:SizeScale     Road(i,:)=randperm(n); end % Dist=zeros(SizeScale,1); for i=1:SizeScale     for j=1:(n-1)         Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));     end end [MinRoad,II]=min(Dist); for iter=1:MaxIter        flag=0;     %SumDist=sum(Dist);     MinRoad=min(Dist);     MaxRoad=max(Dist);     for i=1:SizeScale         fit=fitness(MinRoad,MaxRoad,Dist(i));         if fit==1             flag=flag+1;         %get new road             Road(i,:)=Road(II,:);             ChangeLocation=randint(1,2,[1 n]);             if ChangeLocation(1)==ChangeLocation(2)                 continue;             end                         temp=Road(i,ChangeLocation(1));             Road(i,ChangeLocation(1))=Road(i,ChangeLocation(2));             Road(i,ChangeLocation(2))=temp;                         Dist(i)=0;             for j=1:(n-1)                 Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));             end                     end     end         %change     %bianyi     Numchange=int16(ChangeRate*SizeScale);     IDchange=randint(1,Numchange,[1,SizeScale]);     step=3;     for i=1:Numchange         if IDchange(i)==II             continue;         end                 L=1;         while (L+step)< n             temp=Road(IDchange(i),L);             Road(IDchange(i),L)=Road(IDchange(i),L+step);             Road(IDchange(i),L+step)=temp;             L=L+step;         end                 Dist(i)=0;         for j=1:(n-1)             Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));         end     end         [MinRoad,II]=min(Dist); %    disp('this Iter the Opt is'); %    Road(II,:) %    MinRoad     flag     %Dist end         plot(Map(:,1),Map(:,2),'*'); hold on XX=Map(Road(II,:),1); XX=[XX;Map(Road(II,1),1)]; YY=Map(Road(II,:),2); YY=[YY;Map(Road(II,1),2)]; plot(XX,YY); time=cputime-t; opt=Road(II,:); fval=MinRoad; function d=distance(A,B) %this function to get the distance of two point A,B d=sqrt((A(1)-B(1))^2+(A(2)-B(2))^2); function f=fitness(fmin,fmax,froad) %this function to computer the fitness this road %this the f is 0 or 1   froad more less the f more posible 1 t=(froad-fmin)/(fmax-fmin); f=rand

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值