Genetic Algorithm .Program running but not giving a maxima

1 次查看(过去 30 天)
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

采纳的回答

Rakesh Jain
Rakesh Jain 2017-3-14
编辑:Rakesh Jain 2017-3-14
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by