در پست قبل حل یک معادله ساده ریاضی را توسط الگوریتم ژنتیک توضیح دادیم.در این پست کد پیاده سازی این مثال در زبان متلب آورده می شود.زبان متلب یک زبان بسیار قوی برای پیاده سازی مسائل علمی است که با قابلیت های زیاد کار برنامه نویسی را برای کاربر بسیار آسان می نماید.کدها دقیقا پیاده سازی همان چیزی است که در مثال قبل گفته شده است و برای درک آن نیاز به آشنایی با کدنویسی تحت زبان برنامه نیسی متلب می باشد.
global popSize;
global chromosomeLength;
global F_OBJ;
global Fitness;
global arrProbability;
global arrCumulative;
global newPop;
global population;
global xoRate;
global mutRate;
global maxTor;
maxTor=100;
popSize=6;
chromosomeLength=4;
xoRate =0.3;
mutRate = 0.4;
initalPopulation();
for maxTorIndex=1:maxTor
global Fitness;
calculateFOBJ();
solution=find(F_OBJ==0);
if ~(isempty(solution))
numRuleFound=size(solution,1);
fprintf("We Finde %d Solution in generation %d \n",numRuleFound,maxTorIndex);
for i=1:numRuleFound
ruleIndex=solution(i);
disp(population(ruleIndex,:))
end
return;
end
calculateFitness();
calculateprobability();
calculateCumulative();
rouletteWheel();
OnePointCrossOver();
MutateGens();
end
function pop=initalPopulation()
%global population;
global popSize;
global population;
x=popSize;
population(1,:)= [12,05,23,08];
population(2,:)= [02,21,18,03];
population(3,:)= [10,04,13,14];
population(4,:)= [20,01,10,06];
population(5,:)= [01,04,13,19];
population(6,:)= [20,05,17,01];
%pop=population;
end
function calculateFOBJ()
global F_OBJ;
global popSize;
global population;
for i=1:popSize
a=population(i,1);
b=population(i,2);
c=population(i,3);
d=population(i,4);
F_OBJ(i)=abs(a+2*b+3*c+4*d-30);
end
end
function calculateFitness()
global F_OBJ;
global popSize;
global Fitness;
for i=1:popSize
Fitness(i)=1/(1+F_OBJ(i));
end
end
function calculateprobability()
global popSize;
global Fitness;
global arrProbability;
sumOfFitness=sum(Fitness);
arrProbability=Fitness/sumOfFitness;
end
function calculateCumulative()
global popSize;
global arrProbability;
global arrCumulative;
for i=1:popSize
arrCumulative(i)=sum(arrProbability(1:i));
end
end
function rouletteWheel()
global popSize;
global arrCumulative;
global population;
global newPop;
newPop=zeros(popSize,4);
randMatrix=rand(popSize,1);
for i=1:popSize
for j=1:popSize
rValue=randMatrix(i);
cumulativeValue=arrCumulative(j);
if rValue< cumulativeValue;
newPop(i,:)=population(j,:);
break;
end
end
end
end
%cross over
function OnePointCrossOver()
global population;
global popSize;
global xoRate;
global chromosomeLength;
newPop=population;
pSize=popSize;
tempPop=[];
ParentChromosome=[];
randMatrix=rand(pSize,1);
k=1;
while(k<=pSize)
rndValue=randMatrix(k);
if rndValue < xoRate
ParentChromosome=[ParentChromosome k];
end
k=k+1;
end
arrSize=size(ParentChromosome,2);
pChromosome=zeros(1,1);
for i=1:arrSize
if(i==arrSize)
pChromosome(i,1)=ParentChromosome(i);%i;
pChromosome(i,2)=ParentChromosome(1);
else
pChromosome(i,1)=ParentChromosome(i);%i;
pChromosome(i,2)=ParentChromosome(i+1);%i+1;
end
end
for i=1:arrSize
crossPoints=randperm(chromosomeLength,1);
pChromosome(i,3)=crossPoints;
end
for i=1:arrSize
newRule=zeros(1,1);
rIndex1=pChromosome(i,1);
rule1=newPop(rIndex1,:);
rIndex2=pChromosome(i,2);
rule2=newPop(rIndex2,:);
cPoint=pChromosome(i,3);
rBegin1=1;
rEnd1=cPoint;
rBegin2=cPoint+1;
newRule(rBegin1:rEnd1)=rule1(rBegin1:rEnd1);
newRule(rBegin2:chromosomeLength)=rule2(rBegin2:chromosomeLength);
tempPop(i:i,1:chromosomeLength)=newRule;
end
for i=1:arrSize
ruleIndex=pChromosome(i,1);
newPop(ruleIndex:ruleIndex,1:chromosomeLength)=tempPop(i:i,1:chromosomeLength);
end
population=newPop;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
function MutateGens()
global population;
global popSize;
global mutRate;
global chromosomeLength;
newPopulation=population;
temp=newPopulation(:,:);
totalOfGens=chromosomeLength*(popSize);
totalOfMutatedGens=round(totalOfGens*mutRate);
mutatedGenRowIndex=0;%chromose number
mutatedGenColIndex=0;%gen number
randomGenMutated=randperm(totalOfGens,totalOfMutatedGens);
for i=1:totalOfMutatedGens
mutatedGen=randomGenMutated(i);
xMod=mod(mutatedGen,chromosomeLength); %32 find gens position in the chromose
xDiv=fix(mutatedGen/chromosomeLength);
if (xMod==0)
mutatedGenRowIndex=xDiv;
mutatedGenColIndex=chromosomeLength;
elseif(xMod~=0)
mutatedGenRowIndex=(xDiv+1);
mutatedGenColIndex=xMod;
end
genValue=randomBetweenToNumber(0,30);
newPopulation(mutatedGenRowIndex,mutatedGenColIndex)=genValue;
end
population=newPopulation;
end
function rnd=randomBetweenToNumber(lower,upper)
rng shuffle;
rnd= randi([lower upper],1,1);
end
برای اجرا کد را در یک اسکریپ درون ویرایشگر متلب کپی نموده و آن را اجرا نمایید.فایل اسکریپ مطلب برنامه را هم می توانید از آدرس ذیل دانلود نمایید.با اجرای برنامه در صورت یافته شدن پاسخ قانون متناظر با آن نمایش داده شده و اجرای برنامه خاتمه می یابد و در صورت عدم یافتن پاسخ بعد از اتمام شرط تکرار حلقه برنامه پایان می یابد
در ذیل تصاویری از اجرای برنامه را مشاهده می نمایید.