// In mathematical optimization, the Rastrigin function is a non-convex function // used as a performance test problem for optimization algorithms //------------------------------------------------------ // real valued genetic algorithm // for the minimization of the rastrigin function // (with dynamic display for the case of 2 parameters) // //----------------------------------------------------- // selection: proportionate with ranking method // crossover: blend // mutation: non uniform // 1-elitism // function y=rastrigin(x,n) // the function to optimize y=n+sum(x.^2-cos(2*%pi*x)); endfunction // function evalpop=evaluation(X); // evaluation of the whole population [Npop,n]=size(X);n=n-1; evalpop=zeros(Npop,1); for i=1:Npop; evalpop(i)=rastrigin(X(i,1:n),n); end endfunction // function [Xpar,bestpar]=evalplus1elitism(Xpar,Xoldpar,Npop,gen) // evaluation plus 1-elitism val=evaluation(Xpar); Xpar(:,n+1)=val; [minval,index]=min(Xoldpar(:,n+1)); bestpar=Xoldpar(index(1),:); [minval,index]=min(val); bestnewpop=Xpar(index(1),:); if (bestnewpop(\$)>bestpar(\$))&(gen>1) then nrand=int(Npop*rand())+1; Xpar(nrand,:)=bestpar; else bestpar=bestnewpop; end endfunction // function Xsel=rankselect(X) // ranking selection Xsel=X; [Npop,n]=size(X);n=n-1; [Fsort,ind]=gsort(X(:,n+1)); X=X(ind,:); p=1:Npop;roulette=cumsum(p)/sum(p); for i=1:Npop; u=rand(); index=find(roulette