15,615,787 members
3.00/5 (1 vote)
See more:
Dear Sir

could you please help me to find the mistake in the Matlab programe as the I try to find the max. solution of the fitness function y= cos(x)+sin(x)+2tan(x) by Genetic Algorithm.
Here is my program.

```% Eng. Wael Mohamed Ahmed Sayed AboEl Dahab
% f(x)=cos(x)+sin(x)+2*tan(x)=y
% 0<=x<=400 with accuracy = 2
% intial population 20
% crossover Prob. = %25
% mutation Prob. = %8
% end when last four solution are equally with error = 0.1

clc
clear

x=0:2:400;
y=cos(x)+sin(x)+2*tan(x);                        % found 200 solution
plot(x,y);
hold on ;
grid on ;

gen=round(400*rand(20,1))                        % gen === 1st generation of initial population
fit=cos(gen)+sin(gen)+2*tan(gen);                % fit ==== fitness function

for k=1:200

npp(1)=max(gen)
total=sum(fit);
for i=1:19
p(i)=fit(i)/total;                           %calculate the probability of each fitness function
end

% comulative probability

q(1)=p(1)
for i=2:19
q(i)=q(i-1)+p(i);
end

for t=2:20
b(t)=rand;                                   % b====ball of roullete
for i=1:19
if b(t)<=q(i)
npp(t)=gen(i);                       % npp--> new population
break;
else
continue
end
end
end

npp

for i=1:20
npb(i,:)=dec2bin(npp(i),9)                   % npb --> new population in binary format
end

npb

x = npb(1,:)

% crossover operation

n=round(20*0.25)                                 % n --> number Of gene That Applyed Crossover On Them

if(mod(n,2)~=0)
n=n+1
end

% select the position of solution to make crossover operation
flag=1;
for i=1:10
b=round(20*rand);
if b > 0
ppp(flag)=b                             % ppp -->position of solution
flag=flag+1;
end
if (flag > 6)
break;
end
end

%
for i=1:8
nn=round(9*rand)
if (nn>0 )
nc=nn                                     %nc --> point of crossover
break;
end
end

x1=npb(ppp(1),:);
x2=npb(ppp(2),:);

temp=x1(1:nc);
x1(1:nc)=x2(1:nc);
x2(1:nc)=temp ;

npp(ppp(1))=bin2dec(x1);
npp(ppp(2))=bin2dec(x2);

x3=npb(ppp(3),:);
x4=npb(ppp(4),:);

temp=x3(1:nc);
x3(1:nc)=x4(1:nc);
x4(1:nc)=temp;

npp(ppp(3))=bin2dec(x3);
npp(ppp(4))=bin2dec(x4);

x5=npb(ppp(5),:);
x6=npb(ppp(6),:);

temp=x5(1:nc);
x5(1:nc)=x6(1:nc);
x6(1:nc)=temp ;

npp(ppp(5))=bin2dec(x5);
npp(ppp(6))=bin2dec(x6);

% mutation

mu=round(20*0.08);

% mutation point

for i=1:8
nn=round(9*rand);
if ( nn>0)
nm=nn
break;
end
end

% solutions that applied mutation of them

flag=1;
for i=1:8
bbb=round(20*rand)
if bbb>0
pm(flag)=bbb                                 % pm --> position of solution
flag=flag+1
end
if(flag > 2)
break;
end
end

xm1=npb(pm(1),:);
if xm1(nm) == '1'
xm1(nm)='0'
else
xm1(nm)='1'
end

xm2=npb(pm(2),:);
if xm2(nm) == '1'
xm2(nm)='0'
else
xm2(nm)='1'
end

npp(pm(1))=bin2dec(xm1);
npp(pm(2))=bin2dec(xm2);

fitness=cos(npp)+sin(npp)+2*tan(npp);

maxx=fitness(1);
for i=2:20
if (fitness(i) >= maxx)
maxx = fitness(i)
id = i ;
end
end

maxi(k)= maxx;
maxsolution=npp(i);
gen = npp ;

end

max=max(maxi);
text(maxsolution,max,'max');```
Posted
Updated 4-Nov-13 11:06am
v2