Genetic Algorithm .Program running but not giving a maxima

1 visualizzazione (ultimi 30 giorni)
The Genetic algorithm given below is to maximise a given non linear function.It runs correctly and gives some output. But the output is not a maximum of my desired function. There seems a error in the program. Please point out the error. I would also like to mention that this program was taken from File Exchange section. I have modified it and corrected it to suit my needs. Can someone please find out the mistake due to which I am not able to get the maxima of my function? If someone can find out the error and correct it, it will help me in that it will become a template to maximise a function using GA and probably I can further extend it to a multivariable case. The maxima of the below function should be near to -5 but this program gives it as -0.09
%Start of program
clc;
clear all;
%------------------------------ Intialization -----------------------------%
x=randint(1,10,[0 1023]);
x1=zeros(1,10);
%------------------------- Generating binary code ------------------------%
for i=1:1:10
x1(i)=x(i);
for j=1:1:10
X(i,11-j)=rem(x1(i),2);
x1(i)=floor(x1(i)/2);
end
end
x2=(x./102.4)-5;
x2=x2';
for l=1:1:2000
%--------------------- Generating Non Linear Function --------------------%
y= ((1)./(x2.^2/20 + x2/2+ (4) ))
p= max(y);
Y= sort(y,'descend'); %-------- sorting in descending order
%-------------- Discarding Lower Strata and Starting Crossover------------%
%step1 - swapping X data in the descending order
for i=1:1:10
for j=1:1:10
if (y(j)==Y(i))
Z(i,:)=X(j,:);
end
end
end
%step2 - discard lower 2 data values of Y and Z(Probability = 0.8)
Y=Y(1:8);
Z=Z(1:8,:);
Parents=Z; %Parents
%step3 - starting crossover
n=randperm(8);
m=ceil(rand(1)*10)-1;
for i=1:2:7
CrossoverZ(n(i),1:10)=[Z(n(i),1:m) Z(n(i+1),m+1:10)];
CrossoverZ(n(i+1),1:10)=[Z(n(i+1),1:m) Z(n(i),m+1:10)];
end
Crossover_Child=CrossoverZ; %crossover
%--------------------------- Starting Mutation ---------------------------%
%Probability =0.1, change any single random value in each to opposite(0to1)
Mutation_Child=Crossover_Child;
o=ceil(rand(1)*10);
for i=1:1:8
if (Crossover_Child(i,o)==0)
Mutation_Child(i,o)=1;
else if (Crossover_Child(i,o)==1)
Mutation_Child(i,o)=0;
end
end
end
%Parent,Crossover and Mutation childs are made
%Putting them into Fitness Funtion
Final_data=[Parents; Crossover_Child; Mutation_Child];
Final_data_int=zeros(1,24)';
%Converting binary to integers
for i=1:1:24
for j=1:1:10
Final_data_int(i)= Final_data_int(i)+power(2,10-j).*Final_data(i,j);
end
end
Final_data_int1=((Final_data_int)/102.4)-5;
Output=1./(((Final_data_int1).^2/20)+(Final_data_int1/2) + (4));
Output1=sort(Output,'descend');
for i=1:1:24
for j=1:1:24
if (Output1(i)==Output(j))
Final_Output(i,:)=Final_data(j,:);
end
end
end
Final_Output=Final_Output(1:10,:);
for i = 1:1:10
for j = 1:1:24
if (Final_Output(i,:)== Final_data(j,:))
x2(i,:) = Final_data_int1(j,:);
X(i,:) = Final_data(j,:);
end
end
end
end

Risposta accettata

Rakesh Jain
Rakesh Jain il 14 Mar 2017
Modificato: Rakesh Jain il 14 Mar 2017
I have understood my mistake. I was my by mistake using different function for calculating output and y. Silly mistake on my part. but wasted the whole day.Thanks. So the above program can optimise any function of 1 variable using Genetic Algorithm. I have corrected the program above

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by