📄 copymyga.m
字号:
function [endpop,result,publicmem,submem,resultall]=myga;
%this is a main function
%function [endpop,result,publicmem,submem,resultall]=myga
clc;
clear;
N=30; %the number of cities
c=clock;
resultall=zeros(40,N+3);
% initpop and selecttest
publicmem=ones(N,N); %record the information of allpop
submem=ones(10,N,N); %record the information of subgrooup's the top six
[lowest pop publicmem submem]=selecttest(N,initpop(N),publicmem,submem);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for number=1:10 %the number of circyle
if number~=1
[lowest,pop,publicmem,submem]=selecttest(N,pop,publicmem,submem);
end
temppop=pop;
best=zeros(1,N+3);
tempmax=0;
if mod(number,100)==0
%current the best individual
for i=1:10
for j=1:10
% if temppop(i,j,N+2)==-1 & tempmax<temppop(i,j,N+1)
if tempmax<temppop(i,j,N+1)
tempmax=temppop(i,j,N+1);
best(1,N+1)=i;
best(1,N+2)=j;
best(1,N+3)=temppop(i,j,N+1);
for k=1:N
best(1,k)=temppop(i,j,k);
end
end
end
end
%the best result
result=best;
if N==10
result(1,N+3)=1/result(1,N+3);
else
result(1,N+3)=1000/result(1,N+3);
end
resultall(number/100,:)=result;
end % end of if mod(numble,100)
%create a board recording the information of saved seed in every group
temparraymain=zeros(10,7);
for i=1:10
m=1;
for j=1:10
if pop(i,j,N+2)<0 %record the six excellent
temparraymain(i,m)=j;
m=m+1;
else
temparraymain(i,7)=j; %record a null individual for saving the excellent
end
end
end
%ok
% xover
for i=1:10
if temparraymain(i,1)~=0
for j=1:3
x=randint(1,1,[1 6]);
y=randint(1,1,[1 6]);
if x==y
y=mod(y+randint(1,1,[1 5]),6);
if y==0
y=6;
end
end
[pop(i,temparraymain(i,x),:),pop(i,temparraymain(i,y),:)]=xover(pop(i,temparraymain(i,x),:),pop(i,temparraymain(i,y),:),N);
% pop(i,temparraymain(i,x),:)=temptest1;
% pop(i,temparraymain(i,y),:)=temptest2;
end
end
end
% save the best in every group
for i=1:10
for j=1:10
if temppop(i,j,N+2)==-1
pop(i,temparraymain(i,7),1:N+1)=temppop(i,j,1:N+1); %record the excellent individual in subgroup
end
end
end
% complete the subgroup of pop
i=lowest;
for j=1:10
pop(i,j,1:N)=complete(publicmem,N);
end
%complete the individual of pop
for i=1:10
for j=1:10
if pop(i,j,5)==0 | pop(i,j,2)==0
pop(i,j,1:N)=complete(publicmem,N);
end
end
end
%compute the pop's eval for next selection
for i=1:10
for j=1:10
pop(i,j,1:N+3)=mygaeval1(pop(i,j,1:N+3),N);
end
end
%mutation
for i=1:10
for j=1:10
a=randint(1,1,[1 1000]);
if a<2 & number<700
pop(i,j,1:N+3)=mutation(pop(i,j,1:N+3),N);
elseif a<20 & number>699
pop(i,j,1:N+3)=mutation(pop(i,j,1:N+3),N);
end
end
end
if number>250 %add overordering operation
for i=1:10
for j=1:10
a=randint(1,1,[1 N]);
b=randint(1,1,[1 N]);
if a==b
b=mod(b+randint(1,1,[1 N-1]),N);
if b==0
b=N;
end
end
if a<b
temp=a;
a=b;
b=temp;
end
pop(i,j,1:N+3)=overorder(pop(i,j,1:N+3),N,a,b);
end
end
end %end of if number>750
end % end of while
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%current the best individual
tempmax=0;
for i=1:10
for j=1:10
if tempmax<pop(i,j,N+1)
tempmax=pop(i,j,N+1);
best(1,N+1)=i;
best(1,N+2)=j;
best(1,N+3)=pop(i,j,N+1);
for k=1:N
best(1,k)=pop(i,j,k);
end
end
end
end
%the best result
result=best;
if N==10
result(1,N+3)=1/result(1,N+3);
else
result(1,N+3)=1000/result(1,N+3);
end
endpop=pop;
alltime=etime(clock,c);
strcat('运行时间:',num2str(alltime))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -