ich versuche einen genetischen Algorithmus an meine Aufgabenstellung anzupassen.
Nachdem die Elternpaare ausgesucht wurden, müssen diese Nachkommen produzieren (mit Hilfe von Kreuzung), ie dann bewertet werden.
Meine Frage ist jetzt, wo wird das hier, in dem Ausschnitt gemacht?
und in welchem Fall tritt die while-Schleife auf?
Ich danke für eure Hilfe!
Code:
pick1=ceil(Nodds*rand(1,M)); % mate #1
pick2=ceil(Nodds*rand(1,M)); % mate #2 % ma and pa contain the indicies of the parents
ma=odds(pick1);
pa=odds(pick2);
%_______________________________________________________
% Performs mating for ic=1:M
mate1=pop(ma(ic),:);
mate2=pop(pa(ic),:);
indx=2*(ic-1)+1; % starts at one and skips every % other one
xp=ceil(rand*npar); % random value between 1 and N
temp=mate1;
x0=xp;
while mate1(xp)~=temp(x0)
mate1(xp)=mate2(xp);
mate2(xp)=temp(xp);
xs=find(temp==mate1(xp));
xp=xs;
end
pop(keep+indx,:)=mate1;
pop(keep+indx+1,:)=mate2;
end
Der Code ist aus einem Buch, welches auch online zur Verfügung steht.
Hier der ganze Code zum GA.
Code:
Genetic Algorithm for permuation problems
%
% minimizes the objective function designated in ff clear global iga x y
% Haupt & Haupt % 2003
%_______________________________________________________
% Setup the GA
ff='tspfun'; % objective function
npar=20; % # optimization variables
Nt=npar; % # columns in population matrix rand('state',3)
x=rand(1,npar);y=rand(1,npar); % cities are at % (xcity,ycity)
%_______________________________________________________
% Stopping criteria
maxit=10000; % max number of iterations
%_______________________________________________________
% GA parameters
popsize=20; % set population size
mutrate=.1; % set mutation rate
selection=0.5; % fraction of population kept
keep=floor(selection*popsize); % #population members % that survive
M=ceil((popsize-keep)/2); % number of matings
odds=1;
for ii=2:keep
odds=[odds ii*ones(1,ii)];
end
Nodds=length(odds);
%_______________________________________________________
% Create the initial population
iga=0; % generation counter initialized for iz=1:popsize
pop(iz,:)=randperm(npar); % random population end
cost=feval(ff,pop); % calculates population cost % using ff [cost,ind]=sort(cost); % min cost in element 1
pop=pop(ind,:); % sort population with lowest % cost first
minc(1)=min(cost); % minc contains min of % population
meanc(1)=mean(cost); % meanc contains mean of population
%_______________________________________________________
% Iterate through generations while iga<maxit
iga=iga+1; % increments generation counter
%_______________________________________________________
% Pair and mate
pick1=ceil(Nodds*rand(1,M)); % mate #1
pick2=ceil(Nodds*rand(1,M)); % mate #2 % ma and pa contain the indicies of the parents
ma=odds(pick1);
pa=odds(pick2);
%_______________________________________________________
% Performs mating for ic=1:M
mate1=pop(ma(ic),:);
mate2=pop(pa(ic),:);
indx=2*(ic-1)+1; % starts at one and skips every % other one
xp=ceil(rand*npar); % random value between 1 and N
temp=mate1;
x0=xp;
while mate1(xp)~=temp(x0)
mate1(xp)=mate2(xp);
mate2(xp)=temp(xp);
xs=find(temp==mate1(xp));
xp=xs;
end
pop(keep+indx,:)=mate1;
pop(keep+indx+1,:)=mate2;
end
%_______________________________________________________
% Mutate the population
nmut=ceil(popsize*npar*mutrate);
for ic = 1:nmut
row1=ceil(rand*(popsize-1))+1;
col1=ceil(rand*npar);
col2=ceil(rand*npar);
temp=pop(row1,col1);
pop(row1,col1)=pop(row1,col2);
pop(row1,col2)=temp;
im(ic)=row1;
end
cost=feval(ff,pop);
%_______________________________________________________
% Sort the costs and associated parameters
part=pop; costt=cost;
[cost,ind]=sort(cost);
pop=pop(ind,:);
%_______________________________________________________
% Do statistics
minc(iga)=min(cost);
meanc(iga)=mean(cost);
end %iga
%_______________________________________________________
% Displays the output day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),day( 5),day(6)),0)) disp([ëoptimized functionis ë ff]) format short g
disp([ëpopsize = ë num2str(popsize) ë mutrate = ë
num2str(mutrate) ë # par = ë num2str(npar)]) disp([ë best cost=í num2str(cost(1))]) disp([ëbest solutioní]) disp([num2str(pop(1,:))]) figure(2)
iters=1:maxit;
plot(iters,minc,iters,meanc,íññí);
xlabel(ëgenerationí);ylabel(ëcostí);
figure(1);plot([x(pop(1,:)) x(pop(1,1))],[y(pop(1,:))
y(pop(1,1))],x,y,íoí);axis square
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.