How to fix this error in gadecode(Line No 1

9 views (last 30 days)
Karthik Kolle
Karthik Kolle on 3 Mar 2019
Answered: Chisom Mirian on 22 Oct 2021
clear all
ff='testfunction';
npar=2;
maxit=100;
mincost=-9999999;
popsize=16;
mutrate=.15;
selection=0.5;
nbits=8;
Nt=nbits*npar;
keep=floor(selection*popsize);
iga=0;
pop=round(rand(popsize,Nt));
par=gadecode(pop,0,10,nbits);
cost=feval(ff,par);
[cost,ind]=sort(cost);
par=par(ind,:);
pop=pop(ind,:);
minc(1)=min(cost);
meanc(1)=mean(cost);
while iga<maxit
iga=iga+1;
M=ceil((popsize-keep)/2);
prob=flipud([1:keep]'/sum([1:keep]));
odds=[0 cumsum(prob(1:keep))'];
distribution function
pick1=rand(1,M);
pick2=rand(1,M);
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 pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
pa(ic)=id-1;
end
end % id
ic=ic+1;
end
ix=1:2:keep;
xp=ceil(rand(1,M)*(Nt-1));
pop(keep+ix,:)=[pop(ma,1:xp) pop(pa,xp+1:Nt)];
pop(keep+ix+1,:)=[pop(pa,1:xp) pop(ma,xp+1:Nt)];
nmut=ceil((popsize-1)*Nt*mutrate);
mrow=ceil(rand(1,nmut)*(popsize-1))+1;
mcol=ceil(rand(1,nmut)*Nt);
for ii=1:nmut
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1);
end
par(2:popsize,:)=gadecode(pop(2:popsize,:),0,10,nbits);
cost(2:popsize)=feval(ff,par(2:popsize,:));
[cost,ind]=sort(cost);
par=par(ind,:); pop=pop(ind,:);
minc(iga+1)=min(cost);
meanc(iga+1)=mean(cost);
if iga>maxit | cost(1)<mincost
break
end
[iga cost(1)]
end
day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),day(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')
Im getting this error at the end. How do I fix it?
  1 Comment
Star Strider
Star Strider on 3 Mar 2019
You need to download the function to a directory on your MATLAB user search path.
It would not be advisable for you to install it in the MATLAB bin directory!

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!