Artificial Intelligence - هوش مصنوعی  
انجمن را در گوگل محبوب کنيد :

بازگشت   Artificial Intelligence - هوش مصنوعی > محاسبات نرم > الگوریتم ژنتیک(Genetic Algorithm)


 
تبليغات سايت
Iranian Association for the Advancement of Artificial Intelligence
 
 
LinkBack ابزارهاي تاپيک نحوه نمايش
قديمي ۰۷-۲-۱۳۹۱, ۱۱:۳۹ بعد از ظهر   #3 (لینک دائم)
عضو جدید
 
آواتار ariyan1234
 
تاريخ عضويت: شهريور ۱۳۹۱
پست ها: 3
تشكرها: 0
0 تشكر در 0 پست
پيش فرض

اگر امکانش هست این کد را خط به خط توضیح دهید .ممنون
%



clear
%_________________________________________________ ____________
% I. Setup the GA
ff='testfunction'; % objective function
npar=2; % number of optimization variables

%_________________________________________________ ____________
% II. Stopping criteria
maxit=100; % max number of iterations
mincost=-9999999; % minimum cost

%_________________________________________________ ____________
% III. GA parameters
popsize=16; % set population size
mutrate=.15; % set mutation rate
selection=0.5; % fraction of population kept
nbits=8; % number of bits in each parameter
Nt=nbits*npar; % total number of bits in a chormosome
keep=floor(selection*popsize); % #population members that survive

%_________________________________________________ ____________
% Create the initial population
iga=0; % generation counter initialized
pop=round(rand(popsize,Nt)); % random population of 1s and 0s
par=gadecode(pop,0,10,nbits); % convert binary to continuous values
cost=feval(ff,par); % calculates population cost using ff
[cost,ind]=sort(cost); % min cost in element 1
par=par(ind,:); pop=pop(ind,:); % sorts 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
M=ceil((popsize-keep)/2); % number of matings
prob=flipud([1:keep]'/sum([1:keep])); % weights chromosomes based upon position in list
odds=[0 cumsum(prob(1:keep))']; % probability distribution function
pick1=rand(1,M); % mate #1
pick2=rand(1,M); % mate #2

% ma and pa contain the indicies of the chromosomes that will mate
ic=1;
while ic<=M
for id=2:keep+1
if pick1(ic)<=odds(id) & pick1(ic)>odds(id-1)
ma(ic)=id-1;
end % if
if pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
pa(ic)=id-1;
end % if
end % id
ic=ic+1;
end % while

%_________________________________________________ ____________
% Performs mating using single point crossover
ix=1:2:keep; % index of mate #1
xp=ceil(rand(1,M)*(Nt-1)); % crossover point
pop(keep+ix,:)=[pop(ma,1:xp) pop(pa,xp+1:Nt)]; % first offspring
pop(keep+ix+1,:)=[pop(pa,1:xp) pop(ma,xp+1:Nt)]; % second offspring

%_________________________________________________ ____________
% Mutate the population
nmut=ceil((popsize-1)*Nt*mutrate); % total number of mutations
mrow=ceil(rand(1,nmut)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,nmut)*Nt); % column to mutate
for ii=1:nmut
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1); % toggles bits
end % ii

%_________________________________________________ ____________
% The population is re-evaluated for cost
par(2:popsize,:)=gadecode(pop(2:popsize,:),0,10,nb its); % decode
cost(2:popsize)=feval(ff,par(2:popsize,:));

%_________________________________________________ ____________
% Sort the costs and associated parameters
[cost,ind]=sort(cost);
par=par(ind,:); pop=pop(ind,:);

%_________________________________________________ ____________
% Do statistics for a single nonaveraging run
minc(iga+1)=min(cost);
meanc(iga+1)=mean(cost);

%_________________________________________________ ____________
% Stopping criteria
if iga>maxit | cost(1)<mincost
break
end

[iga cost(1)]

end %iga

%_________________________________________________ ____________
% Displays the output
day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),d ay(5),day(6)),0))
disp(['optimized function is ' ff])
format short g
disp(['popsize = ' num2str(popsize) ' mutrate = ' num2str(mutrate) ' # par = ' num2str(npar)])
disp(['#generations=' num2str(iga) ' best cost=' num2str(cost(1))])
disp(['best solution'])
disp([num2str(par(1,:))])
disp('binary genetic algorithm')
disp(['each parameter represented by ' num2str(nbits) ' bits'])
figure(24)
iters=0:length(minc)-1;
plot(iters,minc,iters,meanc,'--');
xlabel('generation');ylabel('cost');
text(0,minc(1),'best');text(1,minc(2),'population average')
ariyan1234 آفلاين است   پاسخ با نقل قول
 



كاربران در حال ديدن تاپيک: 1 (0 عضو و 1 مهمان)
 

قوانين ارسال
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is فعال
شکلکها فعال است
كد [IMG] فعال است
كدهاي HTML غير فعال است
Trackbacks are فعال
Pingbacks are فعال
Refbacks are فعال




زمان محلي شما با تنظيم GMT +3.5 هم اکنون ۱۱:۰۴ بعد از ظهر ميباشد.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.

Teach and Learn at Hexib | Sponsored by www.Syavash.com and Product In Review

استفاده از مطالب انجمن در سایر سایت ها، تنها با ذکر انجمن هوش مصنوعي به عنوان منبع و لینک مستقیم به خود مطلب مجاز است

Inactive Reminders By Icora Web Design