You are on page 1of 37

//

//
//
steepest descent method with backtracking line search applied to the
rastrigin function
//
//
function y=rastrigin(x)
// the function to
optimize
n=max(size(x));
y=n+sum(x.^2-cos(2*%pi*x));
endfunction
//----------------------------------------------------function y=rastrigingrad(x)
// the gradient of the
function to optimize
y=2*x+2*%pi*sin(2*%pi*x);
endfunction
//-------------------------------------------------------function z=dotprod(x,y);
// computes the dot product
of x and y
z=sum(x.*y);
endfunction
//----------------------------------------------------function d=descentdirection(f,x,fx,gx);
// descent direction:
gradient
d=-gx;
endfunction
//---------------------------------------------------function [xnew,fnew,itback]=backtracking(f,x,fx,gx,d);// line search by
backtracking until Armijo condition
tau=0.3;
bet=0.0001;
alphainit=1;
alpha=alphainit;xnew=x+alpha*d;
fnew=f(xnew);
itback=1;
while(fnew>fx+bet*alpha*dotprod(gx,d))
alpha=tau*alpha;
xnew=x+alpha*d;
fnew=f(xnew);
itback=itback+1;
end
endfunction
//------------------------------------------------// main program
//
disp('steepest descent method for the rastrigin function:');
//
timer();
n=evstr(x_dialog('number of variables of the rastrigin function to
minimize','2'));
epsilon=1E-5;
//
xmin=-5*ones(1,n);
xmax=5*ones(1,n);
u=rand(1,n);
x0=xmin+(xmax-xmin).*u;
x=x0;fx=rastrigin(x);gx=rastrigingrad(x);
itgrad=1;
itfct=1;
Xbest=x;Fbest=fx;

//
while (norm(gx)>epsilon)
d=descentdirection(rastrigin,x,fx,gx);
[x,fx,itback]=backtracking(rastrigin,x,fx,gx,d);
Xbest=[Xbest;x];
Fbest=[Fbest;fx];
gx=rastrigingrad(x);
itgrad=itgrad+1;
itfct=itfct+itback;
end
//-----------------------------------------------------// results display
//
disp('function evaluation number:');disp(itfct);
disp('gradient evaluation number:');disp(itgrad);
//
disp('minimum obtained:');disp(x);
disp('corresponding value by f:');disp(fx);
//disp('corresponding value by g:');disp(gx);
//disp('computational time:');disp(timer());
//
// case of the rastrigin function with 2 parameters (trajectory display)----//
if (n==2)
xmin=-5.12;xmax=5.12;N=300;
xplot=xmin:((xmax-xmin)/(N-1)):xmax;
yplot=xplot;
zplot=zeros(N,N);
for i=1:N
for j=1:N
zplot(i,j)=rastrigin([xplot(i),yplot(j)]);
end
end
xset('window',0)
clf()
plot2d(Xbest(:,1),Xbest(:,2),rect=[-5.12,-5.12,5.12,5.12]);
contour2d(xplot,yplot,zplot,[0:0.01:0.1,0.2:1,1:10]);
xtitle('trajectory display');
xset('window',1)
clf()
plot2d(Xbest(:,1),Xbest(:,2),rect=[x(1)-0.1,x(2)-0.1,x(1)+0.1,x(2)+0.1]);
contour2d(xplot,yplot,zplot,[fx:0.1:(fx+1)]);
xtitle('trajectory display');
end

//
//-----------------------------------------------------// BFGS for Rastrigin
disp('BFGS for Rastrigin');
//
//----------------------------------------------------//
function y=rastrigin(x) // the function to optimize
n=max(size(x));
y=n+sum(x.^2-cos(2*%pi*x));
endfunction
//----------------------------------------------------function y=rastrigingrad(x) // the gradient of the function to optimize
y=2*x+2*%pi*sin(2*%pi*x);
endfunction
//-------------------------------------------------------function z=dotprod(x,y); // computes the dot product of x and y
z=sum(x.*y);
endfunction
//----------------------------------------------------function [d,gx]=descentdirection(Bk,gx);
// descent direction: gradient
d=linsolve(Bk,gx);
endfunction
//---------------------------------------------------function [xnew,fnew,itback]=backtracking(f,x,fx,gx,d);// line search by
backtracking until Armijo condition
tau=0.3;
bet=0.0001;
alphainit=1;
alpha=alphainit;xnew=x+alpha*d;
fnew=f(xnew);
itback=1;
while(fnew>fx+bet*alpha*dotprod(gx,d))
alpha=tau*alpha;
xnew=x+alpha*d;
fnew=f(xnew);
itback=itback+1;
end
endfunction
//------------------------------------------------// main program
//
timer();
n=2;
epsilon=1E-5;
//
xmin=-5*ones(1,n);
xmax=5*ones(1,n);
u=rand(1,n);
x0=xmin+(xmax-xmin).*u;
x=x0;fx=rastrigin(x);gx=rastrigingrad(x);
itgrad=1;
itfct=1;
Xbest=x;Fbest=fx;
Bk=eye(n,n);
//
while (norm(gx)>epsilon)
x0=x;gx0=gx;
d=descentdirection(Bk,gx');
[x,fx,itback]=backtracking(rastrigin,x,fx,gx,d');
// disp('backtracking number');disp(itback);

Xbest=[Xbest;x];
Fbest=[Fbest;fx];
gx=rastrigingrad(x);
itgrad=itgrad+1;
itfct=itfct+itback;
yk=gx'-gx0';sk=x'-x0';
Bk=Bk+(yk*yk')/dotprod(sk,yk)-(Bk*sk*sk'*Bk)/dotprod(sk,Bk*sk);
// disp(mini(spec(Bk)));
end
//-----------------------------------------------------// results display
//
disp('function evaluation number:');disp(itfct);
disp('gradient evaluation number:');disp(itgrad);
//
disp('minimum obtained:');disp(x);
disp('corresponding value by f:');disp(fx);
//disp('computational time:');disp(timer());
//
// case of the rastrigin function with 2 parameters (n=2)-----if (n==2)
xmin=-5.12;xmax=5.12;N=300;
xplot=xmin:((xmax-xmin)/(N-1)):xmax;
yplot=xplot;
zplot=zeros(N,N);
for i=1:N
for j=1:N
zplot(i,j)=rastrigin([xplot(i),yplot(j)]);
end
end
xset('window',0)
clf()
plot2d(Xbest(:,1),Xbest(:,2),rect=[-5.12,-5.12,5.12,5.12]);
contour2d(xplot,yplot,zplot,[0:0.01:0.1,0.2:1,1:10]);
xset('window',1)
clf()
plot2d(Xbest(:,1),Xbest(:,2),rect=[x(1)-0.1,x(2)-0.1,x(1)+0.1,x(2)+0.1]);
contour2d(xplot,yplot,zplot,[fx:0.1:(fx+1)]);
end

//
// Methode d'optimisation sans gradient
//
//
Methode Nelder Mead
//---------------------------------------// definition de la fonction minimiser
//
//deff('y=f(x)','y=sum(x.*x)');
// fonction quadratique
deff('y=f(x)','y=sum(x.*x-cos(2*%pi*x))+n');
// fonction de Rastrigin
//
//
// un cas qui ne marche pas pour Nelder Mead (Mac Kinnon)
//deff('y=f(x)','if x(1)<0 then y=360*x(1)^2+x(2)+x(2)^2;else
y=6*x(1)^2+x(2)+x(2)^2;end');
//X=[1,1;(1+sqrt(33))/8,(1-sqrt(33))/8;0,0]; // simplexe initial
n=evstr(x_dialog('nombre de parametres (contours si n=2)','2'));
Nit=evstr(x_dialog('nombre d''iterations','100'));
//
xmin=-5;xmax=5;
//
// trace des lignes de niveau
//
if (n==2)
N=300;
xplot=xmin:((xmax-xmin)/(N-1)):xmax;
yplot=xplot;
zplot=zeros(N,N);
for i=1:N
for j=1:N
zplot(i,j)=f([xplot(i),yplot(j)]);
end
end
xset('window',0)
clf()
contour2d(xplot,yplot,zplot,20);
end
//
// creation du premier simplexe
//
X=xmin+(xmax-xmin)*rand(n+1,n+1);
// un cas qui ne marche pas pour Nelder Mead (Mac Kinnon)
//X=[1,1;(1+sqrt(33))/8,(1-sqrt(33))/8;0,0]; // simplexe initial
//
//
// evaluation du premier simplexe
for i=1:n+1
X(i,n+1)=f(X(i,1:n))
end
Neval=n+1;
//
// debut boucle principale
for k=1:Nit
sh=0;
// ordonnement du simplexe
[a,b]=gsort(-X(:,n+1));
X=X(b,:);
//
xc=1/n*sum(X(1:n,1:n),'r');
d=xc-X(n+1,1:n);
xr=X(n+1,1:n)+2*d;

fr=f(xr);fnew=fr;Neval=Neval+1;
// cas 1
if (X(1,n+1)<fr)&(fr<X(n,n+1)) then
xnew=xr
end
// cas 2
if (fr<X(1,n+1)) then
xe=X(n+1,1:n)+3*d;fe=f(xe);
if (fe<fr) then xnew=xe;fnew=fe;Neval=Neval+1;else xnew=xr;end;
end
// cas 3
if (X(n,n+1)<fr)&(fr<X(n+1,n+1)) then
xtilde=X(n+1,1:n)+3/2*d;ftilde=f(xtilde);
if (ftilde<fr) then xnew=xtilde;fnew=ftilde;Neval=Neval+1;else
xnew=xr;end;
end
// cas 4
if (X(n+1,n+1)<fr) then
xhat=X(n+1,1:n)+1/2*d;fhat=f(xhat);
if (fhat<X(n+1,n+1)) then xnew=xhat;fnew=fhat;Neval=Neval+1;else
//
retrecissement
sh=1;
for j=2:n+1
X(j,1:n)=X(1,1:n)+1/2*(X(j,1:n)-X(1,1:n));
X(j,n+1)=f(X(j,1:n));
end
Neval=Neval+n;
end;
end
// ajout du nouveau point
//
if (sh==0) then
X(n+1,1:n)=xnew;X(n+1,n+1)=fnew;
end
//
//trace si n=2
if (n==2) then
plot2d([X(:,1);X(1,1)],[X(:,2);X(1,2)],k,rect=[xmin,xmin,xmax,xmax]);
end
end
disp('meilleur point:'); disp(X(1,1:n));
disp('valeur de f en ce point:'); disp(X(1,n+1));
disp('nombre d''evaluations:');disp(Neval)
//

// Methode d'optimisation sans gradient


//
//
Mthode MDS
//---------------------------------------// definition de la fonction minimiser
//
//deff('y=f(x)','y=sum(x.*x)');
// fonction quadratique
deff('y=f(x)','y=sum(x.*x-cos(2*%pi*x))+n');
// fonction de Rastrigin
//
n=evstr(x_dialog('nombre de parametres (contours si n=2)','2'));
Nit=evstr(x_dialog('nombre d''iterations','100'));
//
xmin=-5;xmax=5;
//
function Y=evalsimplexe(X)
[n1,n2]=size(X);
Y=X;
for i=2:n1
Y(i,n2)=f(X(i,1:n2-1));
end
[a,b]=gsort(-Y(:,n2));
Y=Y(b,:);
endfunction
//
// trac des lignes de niveau
//
if (n==2)
N=300;
xplot=xmin:((xmax-xmin)/(N-1)):xmax;
yplot=xplot;
zplot=zeros(N,N);
for i=1:N
for j=1:N
zplot(i,j)=f([xplot(i),yplot(j)]);
end
end
xset('window',0)
clf()
contour2d(xplot,yplot,zplot,20);
end
//
// creation du premier simplexe
//
X=xmin+(xmax-xmin)*rand(n+1,n+1);
//
// evaluation du premier simplexe
for i=1:n+1
X(i,n+1)=f(X(i,1:n))
end
Neval=n+1;
//
// debut boucle principale
for k=1:Nit
// ordonnement du simplexe
[a,b]=gsort(-X(:,n+1));
X=X(b,:);
//
Xr=X;
Xr(2:n+1,1:n)=2*ones(n,1)*X(1,1:n)-X(2:n+1,1:n);
//
Xr=evalsimplexe(Xr);

//
if (Xr(1,n+1)<X(1,n+1)) then
Xe=X;
Xe(2:n+1,1:n)=3*ones(n,1)*X(1,1:n)-2*X(2:n+1,1:n);
Xe=evalsimplexe(Xe);Neval=Neval+n;
if (Xe(1,n+1)<Xr(1,n+1)) then Xnew=Xe, else Xnew=Xr;end
else
Xc=X;
Xc(2:n+1,1:n)=0.5*ones(n,1)*X(1,1:n)+0.5*X(2:n+1,1:n);
Xc=evalsimplexe(Xc);Neval=Neval+n;
Xnew=Xc;
end
//
//trac si n=2
if (n==2) then
plot2d([X(:,1);X(1,1)],[X(:,2);X(1,2)],k,rect=[xmin,xmin,xmax,xmax]);
end
X=Xnew;
end
//
disp('meilleur point:'); disp(X(1,1:n));
disp('valeur de f en ce point:'); disp(X(1,n+1));
disp('nombre d''evaluations:');disp(Neval)

// Methode d'optimisation sans gradient


//
//
Methode de recuit simule
//---------------------------------------// definition de la fonction minimiser
//
function y=f(x) // the function to optimize
n=prod(size(x));
y=n+sum(x.^2-cos(2*%pi*x));
endfunction
//
///////////////////////////////////////////////////////////////
//
n=evstr(x_dialog('nombre de parametres de Rastrigin','2'));
Nmax=evstr(x_dialog('nombre maximal d''valuations','2000'));
//
disp('Niter Temperature sigma');
u=rand(1,n);
xmin=-5*ones(1,n);
xmax=5*ones(1,n);
x0=xmin+(xmax-xmin).*u; // random initialisation
Neval=[];Fbest=[];
y0 = f(x0);
x = x0; y = y0; alpha = 0.92; T = 1; s = 0.2;icount=1;
Neval=[];
Fbest=[];
for k = 1 : 20
T = alpha * T;
accept = 0;
for l = 1 : Nmax/20
x1 = x0 + s*(0.5*ones(x0) - rand (x0)); // uniform
//
x1= x0+s*rand(x0,'normal');
// gaussian
y1 = f(x1);
icount=icount+1;
dy = y1 - y0;
if rand() < exp (- dy / T) then
x0 = x1; y0 = y1; accept = accept + 1;
if y0 < y then x = x0; y = y0; end
end
Neval=[Neval;icount];
Fbest=[Fbest;f(x0)];
end
if accept < 25/100*Nmax/20 then s = s / 2; end
if accept > 75/100*Nmax/20 then s = 2 * s; end
disp([icount,T,s]);
x0 = x; y0 = y;
end
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//
disp('minimum obtained after SA:');disp(x);
disp('corresponding value by f:');disp(y);
disp('function evaluation number by SA:');disp(Neval($));
xset('window',0)
clf();
plot2d(Neval,Fbest)
//
/////////////////////////////////////////////////////////////

//
//-----------------------------------------------------// 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<u);Nselect=max(index)+1;
Xsel(i,:)=X(Nselect,:);
end
endfunction
//
function xoff=blcross(xpar)
// blend crossover
[np,n]=size(xpar);n=n-1;
xoff=xpar;
for i=1:n
u=rand();
xoff(1,:)=u*xpar(1,:)+(1-u)*xpar(2,:);
xoff(2,:)=u*xpar(2,:)+(1-u)*xpar(1,:);
end

endfunction
//
function xoff=mutate(xpar,ngen,Ngen,xmin,xmax) // non uniform mutation
[np,n]=size(xpar);n=n-1;
b=5;
xoff=xpar;
for i=1:n
u1=rand();u2=rand();
if (u1<1/2) then
xoff(1,i)=xpar(1,i)+(xmax(i)-xpar(i))*u2*(1-(ngen-1)/Ngen)^b;
else
xoff(1,i)=xpar(1,i)-(xpar(i)-xmin(i))*u2*(1-(ngen-1)/Ngen)^b;
end;
end;
endfunction
//
//---------main program --------------------//
clf()
clear all
disp('real valued GA for minimizing the rastrigin function');
disp('(with dynamic display for 2 parameters)');
//
xmin=-5;xmax=5;N=100;
x=xmin:((xmax-xmin)/(N-1)):xmax;
y=x;
z=zeros(N,N);
for i=1:N
for j=1:N
z(i,j)=rastrigin([x(i),y(j)],2);
end
end
//
n=evstr(x_dialog('parameter number of the rastrigin function','2'));
Npop=evstr(x_dialog('population number','30'));
Ngen=evstr(x_dialog('generation number','50'));
pc=evstr(x_dialog('crossover probability','0.9'));
pm=evstr(x_dialog('mutation probability','0.6'));
//
xmin=-5*ones(1,n);
xmax=5*ones(1,n);
Xmin=ones(Npop,1)*xmin;
Xmax=ones(Npop,1)*xmax;
u=rand(Npop,n);
pop=Xmin+(Xmax-Xmin).*u; // random initialisation of the population
Xpar=[pop,zeros(Npop,1)];
Xoff=Xpar;Xoldpar=Xpar;fmin=[];mineval=0;newval=zeros(Npop,1);Traj=[];
//
xset('window',2);
xset('window',3);
//
for gen=1:Ngen;
Xpar=Xoff;
[Xpar,bestpar]=evalplus1elitism(Xpar,Xoldpar,Npop,gen); // evaluation
plus 1-elitism
Xoldpar=Xpar;
fmin=[fmin,bestpar($)];
//
Xpar=rankselect(Xpar); // rank selection
for i=1:2:(Npop-1)
u1=int(Npop*rand())+1;u2=int(Npop*rand())+1;u3=rand();

xpar=[Xpar(u1,:);Xpar(u2,:)];Xoff(i:(i+1),:)=xpar;
if (u3<pc) then
xoff=blcross(xpar);
//crossover
Xoff(i:(i+1),:)=xoff;
end
for j=1:2
u4=rand();
if (u4<pm) then
xoffl=mutate(xoff(j,:),gen,Ngen,xmin,xmax); // mutation
Xoff(i+j-1,:)=xoffl;
end
end
end
//----- dyanmic display if n=2 ---------------------------if (n==2) then
xset('window',2);
drawlater
clf()
Traj=[Traj;bestpar(1),bestpar(2)];
contour2d(x,y,z,[0:0.01:0.1,0.2:1,1:10],rect=[-5.12,-5.12,5.12,5.12]);
plot2d(Xpar(:,1),Xpar(:,2),-1,rect=[-5.12,-5.12,5.12,5.12]);
plot2d(Traj(:,1),Traj(:,2),5,rect=[-5.12,-5.12,5.12,5.12]);
xtitle('trajectory display');
drawnow
xset('window',3);
drawlater
clf()
contour2d(x,y,z,[0:0.01:0.1,0.2:1,1:10],rect=[-0.5,-0.5,0.5,0.5]);
plot2d(Xpar(:,1),Xpar(:,2),-1,rect=[-0.5,-0.5,0.5,0.5]);
plot2d(Traj(:,1),Traj(:,2),5,rect=[-0.5,-0.5,0.5,0.5]);
xtitle('trajectory display');
drawnow
end
//----- end of dyanmic display
--------------------------//
end
[Xpar,bestpar]=evalplus1elitism(Xoff,Xoldpar,Npop,gen); // evaluation plus
1-elitism
fmin=[fmin,bestpar($)];
//
//----------- results displays -------------//
xset('window',0);
clf();
plot2d(0:Npop:(Npop*Ngen),fmin);
xtitle('convergence history','Neval','fmin');
xset('window',1);
clf();
plot2d1('onl',0:Npop:(Npop*Ngen),fmin);
xtitle('convergence history (log scale)','Neval','fmin');
//
disp('minimum obtained:');disp(bestpar(1:n));
disp('corresponding value by f:');disp(bestpar($));
disp('evaluation number:');disp(Npop*Ngen);

//
//-----------------------------------------------------//
disp('ES with CSA adaptation for Rastrigin');
//
//----------------------------------------------------function y=rastrigin(x)
// the function to optimize
n=prod(size(x));
y=n+sum(x.^2-cos(2*%pi*x));
endfunction
//
//----------------------------------------------------function Xpar=evaluation(X); // evaluation of the whole population
Xpar=X;
[Npo,n]=size(X);n=n-1;
evalpop=zeros(Npo,1);
for i=1:Npo;
evalpop(i)=rastrigin(X(i,1:n));
end
Xpar(:,n+1)=evalpop;
endfunction
//
//----------------------------------------------------function [Xparnew,Wnew]=selection(Xpar,Xoff,W);
[mu,n]=size(Xpar);
[lambda,n]=size(Xoff);
n=n-1;
Xtot=Xoff;
// selection comma
[Fsort,ind]=gsort(-Xtot(:,n+1));
indbest=ind(1:mu);
Xparnew=Xtot(indbest,:);
Wnew=W(indbest,:);
endfunction
//
//----------------------------------------------------//----------------------------------------------------//---------main program ------------------------------//
disp('real valued ES for minimizing black box function');
//
n=evstr(x_dialog('parameter number of the rastrigin function','15'));
mu=evstr(x_dialog('parent number','20'));
lambda=evstr(x_dialog('offspring number','200'));
Ngen=evstr(x_dialog('generation number','100'));
//
siginit=1;
// initial mutation strength
//
// ------- parameters for CSA adaptation------------c=1/sqrt(n);
chiN=sqrt(n)*(1-1/(4*n)+1/(21*n^2));
D=sqrt(n);
//
// ---------------------------------------------------//
xmin=-5.*ones(1,n);
xmax=5.*ones(1,n);
Xmin=ones(mu,1)*xmin;
Xmax=ones(mu,1)*xmax;
u=rand(mu,n);
pop=Xmin+(Xmax-Xmin).*u; // random initialisation of the population
Xpar=[pop,rand(mu,1)]; Xoldpar=Xpar;

Xoff=Xpar;min=[];Ntot=[];
mineval=0;newval=zeros(mu,1);itfct=0;fmin=[];
//
//----------------------------------------------------//----------------------------------------------------//
sigma=siginit;
V=zeros(1,n);
Xpar=evaluation(Xpar);
Y=1/mu*sum(Xpar(:,1:n),1);
Nevalf=mu;
//
// ---- MAIN LOOP : BEGINNING--------------------------//-----------------------------------------------------//
for gen=1:(Ngen-1)
//
Woff=[];
for i=1:lambda
w=sigma*rand(1,n,'normal'); // mutation
Woff=[Woff;w];
xoff=Y+w;
Xoff(i,1:n)=xoff;
end
//
Xoff=evaluation(Xoff);
// evaluation
Nevalf=Nevalf+lambda;
//
[Xpar,Wpar]=selection(Xpar,Xoff,Woff); // selection
//
fmin=[fmin,Xpar(1,$)];
bestpar=Xpar(1,1:n);
Ntot=[Ntot,Nevalf];
//
// --- adaptativity of sigma----------------//
Z=1/mu*sum(Wpar,1);
Y=Y+Z;
V=(1-c)*V+sqrt(c*(2-c))*sqrt(mu)/sigma*Z;
sigma=sigma*exp((norm(V)-chiN)/(D*chiN));
//
//
MAIN LOOP : ENDING
//
end
//----------------------------------------------------//----------------------------------------------------//
//
xset('window',0);
clf();
plot2d(Ntot,fmin);
disp('minimum obtained:');disp(bestpar(1:n));
disp('corresponding value by f:');disp(fmin($));
disp('evaluation number:');disp(Nevalf);
//

//
//-----------------------------------------------------//
// Evolution Strategy
// for the minimization of the rastrigin function
//
//----------------------------------------------------//
function y=rastrigin(x)
// the function to optimize
n=prod(size(x));
y=n+sum(x.^2-cos(2*%pi*x));
endfunction
//
//----------------------------------------------------function Xpar=evaluation(X); // evaluation of the whole population
Xpar=X;
[Npo,n]=size(X);n=n-1;
evalpop=zeros(Npo,1);
for i=1:Npo;
evalpop(i)=rastrigin(X(i,1:n));
end
Xpar(:,n+1)=evalpop;
endfunction
//
//----------------------------------------------------function Xparnew=selection(Xpar,Xoff); // selection plus or comma
[mu,n]=size(Xpar);
[lambda,n]=size(Xoff);
n=n-1;
Xtot=[Xpar;Xoff];
// plus selection
//Xtot=[Xoff];
// comma selection
[Fsort,ind]=gsort(-Xtot(:,n+1));
Xtot=Xtot(ind,:);
Xparnew=Xtot(1:mu,:);
endfunction
//
//-----------------------------------------------------//
function xoff=mutate(xpar,sigma) // normal mutation
[np,n]=size(xpar);n=n-1;
xoff=xpar;
u=rand(1,n,'n');
xoff(1,1:n)=xpar(1,1:n)+sigma*u;
endfunction
//
//----------------------------------------------------//----------------------------------------------------//---------main program ------------------------------//
disp('real valued ES for minimizing black box function');
//
// ---------------------------------------------------//
n=evstr(x_dialog('parameter number of the rastrigin function','2'));
mu=evstr(x_dialog('parent number','5'));
lambda=evstr(x_dialog('offspring number','50'));
Ngen=evstr(x_dialog('generation number','20'));
//
siginit=1;
// initial mutation strength
alpha=1.25;
// 1/5 rule coefficient
//
// ----------------------------------------------------

//
xmin=-5.*ones(1,n);
xmax=5.*ones(1,n);
Xmin=ones(mu,1)*xmin;
Xmax=ones(mu,1)*xmax;
u=rand(mu,n);
pop=Xmin+(Xmax-Xmin).*u; // random initialisation of the population
Xpar=[pop,rand(mu,1)]; Xoldpar=Xpar;
Xoff=Xpar;min=[];Ntot=[];
mineval=0;newval=zeros(mu,1);itfct=0;fmin=[];
//
//----------------------------------------------------//----------------------------------------------------//
sigma=siginit;
Xpar=evaluation(Xpar);
Nevalf=mu;
//
//
MAIN LOOP : BEGINNING
//
//
for gen=1:(Ngen-1)
//
xcros=1/lambda*sum(Xpar,'r');
// parent crossover
//
for i=1:lambda
u=int(mu*rand())+1;
xpar=Xpar(u,:);
xoff=mutate(xcros,sigma);
// parent crossover+mutation
//
xoff=mutate(xpar,sigma);
// parent mutation, no crossover
Xoff(i,:)=xoff;
end
//
oldval=Xoff(:,$);
Xoff=evaluation(Xoff);
// offspring evaluation
Nevalf=Nevalf+lambda;
//
newval=Xoff(:,$);
ind=find(oldval>newval);
taux=length(ind)/lambda;
//
// --- adaptativity of sigma----------------//
if (taux<0.2) then sigma=sigma/alpha; else sigma=alpha*sigma;end
//
//----------------------------------------------------//
Xpar=selection(Xpar,Xoff);
// selection plus or comma
fmin=[fmin,Xpar(1,$)];
bestpar=Xpar(1,1:n);
Ntot=[Ntot,Nevalf];
//
//
MAIN LOOP : ENDING
//
end
//----------------------------------------------------//----------------------------------------------------//
//
xset('window',0);
clf();

plot2d(Ntot,fmin)
xtitle('convergence history','Neval','fmin');
//
disp('minimum obtained:');disp(bestpar(1:n));
disp('corresponding value by f:');disp(fmin($));
disp('evaluation number:');disp(Nevalf);
//

//
// PSO optimization for Rastrigin function
//
function y=J(x)
y=sum(x.*x-cos(2*%pi*x))+length(x)
// y=sum(x.*x)
endfunction
////////////////////////////////////////////////////
//
//
clf();clear all;
//
xmin=-5;xmax=5;N=100;
x=xmin:((xmax-xmin)/(N-1)):xmax;
y=x;
z=zeros(N,N);
for i=1:N
for j=1:N
z(i,j)=J([x(i),y(j)]);
end
end
//
n=evstr(x_dialog('parameter number of the rastrigin function','2'));
Npop=evstr(x_dialog('population number','10'));
Ngen=evstr(x_dialog('generation number','50'));
Vmax=evstr(x_dialog('maximal speed','1'));
//
// PSO parameters
//
w=0.7;c1=0.7;c2=0.7;
xset('window',0);
xset('window',2);
xset('window',3);
//
//initialisation
//
PSO=zeros(Npop,3*n+1);
PSO(:,1:n)=xmin+(xmax-xmin)*rand(Npop,n);
PSO(:,(n+1:2*n))=-Vmax+2*Vmax*rand(Npop,n);
PSO(:,(2*n+1:3*n))=PSO(:,1:n);
PSO(:,3*n+1)=1E10;
pg=zeros(1,n+1);pg(n+1)=1E10;pgval=[];Traj=[];
for i=1:Ngen
val=[];
for j=1:Npop
valj=J(PSO(j,1:n))
val=[val;valj] // the evaluation of the swarm
// computation of the new pi
if (valj<PSO(j,3*n+1)) then
PSO(j,2*n+1:3*n)=PSO(j,1:n);
PSO(j,3*n+1)=valj;
end
end
// computation of pg
[a,b]=gsort(PSO(:,3*n+1));ind=b($);
pg=[PSO(ind,2*n+1:3*n),PSO(ind,3*n+1)];
pgval=[pgval,pg(n+1)];
// new velocitiy
PSO(:,(n+1:2*n))=w*PSO(:,(n+1:2*n))+c1*rand(Npop,n).*(PSO(:,2*n+1:3*n)PSO(:,1:n))+c2*rand(Npop,n).*(ones(Npop,1)*pg(1:n)-PSO(:,1:n));

// new value of x
PSO(:,1:n)=PSO(:,1:n)+PSO(:,(n+1:2*n));
//
//----- dyanmic display if n=2 ---------------------------if (n==2) then
xset('window',2);
drawlater
clf()
Traj=[Traj;pg(1),pg(2)];
contour2d(x,y,z,[0:0.01:0.1,0.2:1,1:10],rect=[-5.12,-5.12,5.12,5.12]);
plot2d(PSO(:,1),PSO(:,2),-1,rect=[-5.12,-5.12,5.12,5.12]);
plot2d(Traj(:,1),Traj(:,2),5,rect=[-5.12,-5.12,5.12,5.12]);
xtitle('trajectory display');
drawnow
xset('window',3);
drawlater
clf()
contour2d(x,y,z,[0:0.01:0.1,0.2:1,1:10],rect=[-0.5,-0.5,0.5,0.5]);
plot2d(PSO(:,1),PSO(:,2),-1,rect=[-0.5,-0.5,0.5,0.5]);
plot2d(Traj(:,1),Traj(:,2),5,rect=[-0.5,-0.5,0.5,0.5]);
xtitle('trajectory display');
drawnow
end
//
end
xset('window',1)
clf()
disp('best elemet found:');disp(pg(1:n))
plot2d(Npop*(1:Ngen),pgval)

//
// multi objective optimization
// NSGA method without sharing
// comparaison of the exact and computed Pareto front on a simple example.
//
function Xp=paretoplot(Pop,MOpop)
// divide the population with values MOpop by J1 and J2 into different
front
[Npop,b]=size(Pop);
Xp=[];JXp=[];
ind=(1:Npop);
count=1;
while(length(ind)>0)
Nfront=[];
for i=ind;
l=find(MOpop(ind,1)<=MOpop(i,1) & MOpop(ind,2)<=MOpop(i,2))
if (length(l)==1) then
Xp=[Xp;[Pop(i,:),count]];
JXp=[JXp;[MOpop(i,:),count]];
else
Nfront=[Nfront,i];
end
end
ind=Nfront;
// exclude the current front
count=count+1;
end
clf()
for i=1:(count-1)
ind=find(JXp(:,3)==i)
[S1,S2]=gsort(JXp(ind,1));
plot2d(JXp(ind(S2),1),JXp(ind(S2),2),i+1,rect=[0,0,30,30]);
plot2d(JXp(ind(S2),1),JXp(ind(S2),2),-1,rect=[0,0,30,30]);
end
//
lambda=0:0.01:1;
J1p=10*(lambda-1)^2;
J2p=10*lambda^2;
plot2d(J1p, J2p)
endfunction
//
/////////////////////////////////////////
Npop=40;
L=2;
Ngen=100;
pc=0.6;
pm=0.2;
xmin=-5;xmax=5;
sigma=(xmax-xmin)/100;
//
//////////////////////////////////////////////////////
function [J1,J2]=MO(x)
//
J1=(x(1)-1)^2+(x(2)-3)^2;
J2=(x(1)-4)^2+(x(2)-2)^2;
endfunction
/////////////////////////////////////////////////////
A= xmin+(xmax-xmin)*rand(Npop,L+1);
//
val=[];oldbestval=0;
for gen=1:Ngen
// evaluation //////////////////////////////////////

MOpop=[];
for i=1:Npop
[J1,J2]=MO(A(i,1:L));
MOpop=[MOpop;[J1,J2]];
end
A=paretoplot(A(:,1:L),MOpop+1E-10*rand(Npop,2));
//
//
// selection
// 1st step: gsort elements
[s,p]=gsort(A(:,L+1));
A=A(p,:);
val=[val,A(Npop,L+1)];
bestelem=A(Npop,:);
oldbestval=A(Npop,L+1);
//
// 2st step: choose Npop elements with probability pi
//
Asel=[];
p=(1:Npop)/sum(1:Npop);ps=cumsum(p);
for i=1:Npop
u=rand();isel=1;
while (u>ps(isel))
isel=isel+1;
end
Asel=[Asel;A(isel,:)];
end
// barycentric crossover
Acrois=[];
for k=1:Npop/2
u1=int(Npop*rand()+1);
u2=int(Npop*rand()+1);
if (rand()<pc) then
alpha=rand();
uenf1=alpha*Asel(u1,:)+(1-alpha)*Asel(u2,:);
uenf2=(1-alpha)*Asel(u1,:)+alpha*Asel(u2,:);
else
uenf1=Asel(u1,1:L+1);
uenf2=Asel(u2,1:L+1);
end
Acrois=[Acrois;uenf1;uenf2];
end
// normal mutation
Amut=Acrois;
for k=1:Npop
if (rand()<pm) then
epsilon=sigma*rand(1,L+1,'normal');
Amut(k,:)=Amut(k,:)+epsilon;
end
end
A=Amut;
end
//

//
// Genetic Algorithm with an approximated evaluation principle
// Only a decreasing percent of the population is evaluated exactly
// The approximation is build with a local RBF interpolation
//
function y=exactfunction(x)
//
the function to optimize
n=length(x);
y=n+sum(x.^2-cos(2*%pi*x));
// Rastrigin
endfunction
//
//---------------------------------------------------------------//
function y=approxfunction(x,Xexact,A,b) // the approximated function by
RBF interpolation
[N1,N2]=size(Xexact);n=N2-1;
u=linsolve(A,-b);
y=0;
for i=1:N1
y=y+u(i)*phi(x-Xexact(i,1:n))
end
endfunction
//
//---------------------------------------------------------------//
function y=phi(x)
// RBF function
y=norm(x)
endfunction
//
//---------------------------------------------------------------//
function [A,b]=construct(Xexact)
// construction of the matrix for
interpolation
//
[N1,N2]=size(Xexact);n=N2-1;
A=zeros(N1,N1);b=zeros(N1,1);
for i=1:N1
for j=1:N1
A(i,j)=phi(Xexact(i,1:n)-Xexact(j,1:n));
end
b(i)=Xexact(i,n+1);
end
endfunction
//
//--------------------------------------------------------------------function Xexactsel=selection(x,Xexact,Nsize) // selection of the centers
for interpolation
//
[N1,N2]=size(Xexact);n=N2-1;normx=[];
for i=1:N1
normx(i)=norm(x-Xexact(i,1:n))
end
[x1,x2]=gsort(-normx);
Xexactsel=Xexact(x2(1:Nsize),:)
endfunction
//---------------------------------------------------------------function evalpop=evaluation(X);
// exact evaluation of the
whole population
[Npop,N2]=size(X);n=N2-1;
evalpop=zeros(Npop,1);
for i=1:Npop;

evalpop(i)=exactfunction(X(i,1:n));
end
endfunction
//
//---------------------------------------------------------------//
function evalpop=evaluationapprox(X,Xexact,Nsize); // approximated
evaluation of the whole population
[Npop,N2]=size(X);n=N2-1;
evalpop=zeros(Npop,1);
for i=1:Npop;
Xexactsel=selection(X(i,1:n),Xexact,Nsize);
[A,b]=construct(Xexactsel);
evalpop(i)=approxfunction(X(i,1:n),Xexactsel,A,b);
end
endfunction
//
//-----------------------------------------------------------------//
function [Xpar,bestpar]=elitism(Xnew,Xpar,Xtot,Npop,gen) // Elitism
[minval,index]=min(Xtot(:,n+1));
bestpar=Xtot(index(1),:);
[minval,index]=min(Xnew(:,n+1));
bestnewpop=Xnew(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;
[Fgsort,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<u);Nselect=max(index)+1;
Xsel(i,:)=X(Nselect,:);
end
endfunction
//
//-------------------------------------------------------------//
function xoff=blcross(xpar)
// blend crossover
[np,n]=size(xpar);n=n-1;
xoff=xpar;
for i=1:n
u=rand();
xoff(1,:)=u*xpar(1,:)+(1-u)*xpar(2,:);
xoff(2,:)=u*xpar(2,:)+(1-u)*xpar(1,:);
end
endfunction
//
//-------------------------------------------------------------//

function xoff=mutate(xpar,ngen,Ngen,xmin,xmax)
// non uniform
mutation
[np,n]=size(xpar);n=n-1;
b=5;
xoff=xpar;
for i=1:n
u1=rand();u2=rand();
if (u1<1/2) then
xoff(1,i)=xpar(1,i)+(xmax(i)-xpar(i))*u2*(1-(ngen-1)/Ngen)^b;
else
xoff(1,i)=xpar(1,i)-(xpar(i)-xmin(i))*u2*(1-(ngen-1)/Ngen)^b;
end;
end;
endfunction
//
//--------------------------------------------------------------------//-----------------------------------------------------------------//
//
irep=evstr(x_dialog('new computation (0) or resume (1)','0'));
n=evstr(x_dialog('number of parameters (Rastrigin function)','2'));
Xup=evstr(x_dialog('upper bound of the domain','5'));
Xlo=evstr(x_dialog('lower bound of the domain','-5'));
Npop=evstr(x_dialog('population size','10'));
Ngen=evstr(x_dialog('number of generations','30'));
Ngeninit=evstr(x_dialog('number of generations with only exact
evaluations','2'));
Nsize=evstr(x_dialog('number of centers used for RBF
reconstruction','10'));
Nexacttot=evstr(x_dialog('range of allowable exact evaluation
number','70'));
//
deff('val=valalpha(alpha)','Npercent0=[ones(1,Ngeninit),linspace(alpha,1/Np
op,Ngen-Ngeninit)];val=sum(int(Npercent0*Npop))+1-Nexacttot');
alpha0=fsolve(0.4,valalpha);alpha0=min(1,alpha0);alpha0=max(0,alpha0);
Npercent0=[ones(1,Ngeninit),linspace(alpha0,1/Npop,Ngen-Ngeninit)];
// percent of exact evaluations at generation gen
//
//-----------------------------------------------------------------//---------------------------------------------------------------------//
xmin=Xlo*ones(1,n);
xmax=Xup*ones(1,n);
Xmin=ones(Npop,1)*xmin;
Xmax=ones(Npop,1)*xmax;
u=rand(Npop,n);
pop=Xmin+(Xmax-Xmin).*u; // random initialisation of the population
Xpar=[pop,zeros(Npop,1)];
Xoff=Xpar;Xoldpar=Xpar;mineval=0;newval=zeros(Npop,1);Traj=[];
Xtot=[];Nexact=[];fmin=[];
//
//
if (irep==1) then,load('session.sav');gen0=gen+1;else gen0=1;end
//
for gen=gen0:Ngen;
Npercent=Npercent0(gen);
//
Xpar=Xoff;
//
// ----------------------------------Evaluation phase ------------------------------------

//--------------------------------------------------------------------------------------------if (gen<Ngeninit+1) then


val=evaluation(Xpar);
Nexact=[Nexact,Nexact($)+Npop];
Xpar(:,n+1)=val;
Xnew=Xpar;
Xtot=[Xtot;Xnew];
else
val2=evaluationapprox(Xpar,Xtot,Nsize);
Xpar(:,n+1)=val2;
[approxvalues,u]=gsort(-val2);
u=u(1:int(Npercent*Npop));
val3=evaluation(Xpar(u,:));
Xpar(u,$)=val3;
Xnew=Xpar(u,:);
Xtot=[Xtot;Xnew];
Nexact=[Nexact,Nexact($)+length(u)];
end
[Xpar,bestpar]=elitism(Xnew,Xpar,Xtot,Npop,gen);
fmin=[fmin,bestpar($)];
//
//
// --------------------------------------------------------------------------------------//
//
Xpar=rankselect(Xpar);
// rank selection
for i=1:2:(Npop-1)
u1=int(Npop*rand())+1;u2=int(Npop*rand())+1;u3=rand();
xpar=[Xpar(u1,:);Xpar(u2,:)];Xoff(i:(i+1),:)=xpar;
xoff=blcross(xpar);
//crossover
Xoff(i:(i+1),:)=xoff;
for j=1:2
xoffl=mutate(xoff(j,:),gen,Ngen,xmin,xmax);
// mutation
Xoff(i+j-1,:)=xoffl;
end
end
save('session.sav');
end
//
// ----------------------------------last evaluation phase ---------------------------//--------------------------------------------------------------------------------------//
if (gen<Ngeninit+1) then
val=evaluation(Xpar);
Nexact=[Nexact,Nexact($)+Npop];
Xpar(:,n+1)=val;
Xnew=Xpar;
Xtot=[Xtot;Xpar];
else
val2=evaluationapprox(Xpar,Xtot,Nsize);
Xpar(:,n+1)=val2;
[approxvalues,u]=gsort(-val2);
u=u(1:int(Npercent*Npop));
val3=evaluation(Xpar(u,:));
Xpar(u,$)=val3;
Xnew=Xpar(u,:);
Xtot=[Xtot;Xnew];

Nexact=[Nexact,Nexact($)+length(u)];
end
[Xpar,bestpar]=elitism(Xnew,Xpar,Xtot,Npop,gen);
//
fmin=[fmin,bestpar($)];
save('session.sav');
//----------- results displays -------------xset('window',0);
plot2d(Nexact,fmin,3);
xtitle('convergence history','Neval','fmin');
xset('window',1);
plot2d1('onl',Nexact,fmin,3);
xtitle('convergence history (log scale)','Neval','fmin');
//
disp('minimum obtained:');disp(bestpar(1:n));
disp('corresponding value by f:');disp(bestpar($));
disp('evaluation number:');disp(Nexact($));

/////////////////////////////// VERSION AVEC INTERFACE UNIX


//
////----------------------------------------------------//function writedata(str0)
// unix('rm -f data_heart');
// fddata=file('open','data_heart','unknown')
// write(fddata,str0);
// write(fddata,'[ensight]');
// write(fddata,'start
= 0');
// write(fddata,'verbose
= 4');
// write(fddata,'#multimesh = 1');
// file('close',fddata);
//endfunction
//
//
///////////////////////////////////////////////////////////////////////////
//////////////////////////////////
//function y=exactfunctionbis(X)
//z1=X(1);
//theta1=X(2);
//r1=aiL*sqrt(1-(z1/ciL)**2);
//x1=r1*cos(theta1);
//y1=r1*sin(theta1);
// tx=[t1 t2 t3 f1 f1 f2 x1 x2 x3];
// ty=[t1 t2 t3 f1 f1 f2 y1 y2 y3];
// tz=[t1 t2 t3 f1 f1 f2 z1 z2 z3];
// ts=[ti1 ti2 ti3 tf1 tf2 tf3 s1 s2 s3];
//str0=ts + a + string(tx) + e + string(ty) + e + string(tz) + a;
//writedata(str0);
////
//str2='/afs/inria.fr/rocq/home/reo/ldumas/lifevREOopt/testsuite/test_bidomain_Schaf/test_bidomain_Schaf -f data_heart >output
';
////
//unix('rm -f time_dep');
//unix('rm -f time_temp');
//unix('rm -f output');
//unix(str2);
////
//y=read('time_temp',1,1);

////
////
//endfunction
////

//
//-----------------------------------------------------// hybrid method (GA + steepest descent) for Rastrigin
disp('hybrid method (GA + SD) for Rastrigin');
//
//
//----------------------------------------------------//clear();
//
function y=rastrigin(x) // the function to optimize
n=prod(size(x));
//y=n+sum(x.^2-cos(2*%pi*x));
y=sum(x.^2-10*cos(2*%pi*x)+10);
//y=n+exp(1)-n*exp(-0.2*sqrt(1/n*sum(x.^2)))-exp(1/n*sum(cos(2*%pi*x)));
endfunction
//
//----------------------------------------------------function y=rastrigingrad(x)
// the gradient of the
function to optimize
//y=2*x+2*%pi*sin(2*%pi*x);
y=2*x+10*2*%pi*sin(2*%pi*x);
//n=prod(size(x));
//y1=-n*exp(-0.2*sqrt(1/n*sum(x.^2)))*((0.2)*1/n*2*x)/(2*(sqrt(1/n*sum(x.^2))));
//y2=-exp(1/n*sum(cos(2*%pi*x)))*1/n*(-2*%pi*sin(2*%pi*x));
//y=y1+y2;
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));
end
endfunction
//
//----------------------------------------------------function [bestpar,bestval]=thebest(Xtot);
[mu,n]=size(Xtot);n=n-1;
[Fsort,indice]=sort(-Xtot(:,n+1));

bestpar=Xtot(indice(1),1:n);
bestval=Xtot(indice(1),n+1);
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]=sort(X(:,n+1));
X=X(ind,:);
p=1:Npop;roulette=cumsum(p)/sum(p);
for i=1:Npop;
u=rand();
index=find(roulette<u);Nselect=max(index)+1;
Xsel(i,:)=X(Nselect,:);
end
endfunction
//
//----------------------------------------------------//
function xoff=blcross(xpar)
// blend crossover
[np,n]=size(xpar);n=n-1;
xoff=xpar;
for i=1:n
u=rand();
xoff(1,i)=u*xpar(1,i)+(1-u)*xpar(2,i);
xoff(2,i)=u*xpar(2,i)+(1-u)*xpar(1,i);
end
endfunction
//
//----------------------------------------------------//
function xoff=mutate(xpar,ngen,Ngen,xmin,xmax) // non uniform mutation
[np,n]=size(xpar);n=n-1;
b=5;
xoff=xpar;
for i=1:n
u1=rand();u2=rand();
if (u1<1/2) then
xoff(1,i)=xpar(1,i)+(xmax(i)-xpar(i))*u2*(1-(ngen-1)/Ngen)^b;
else

xoff(1,i)=xpar(1,i)-(xpar(i)-xmin(i))*u2*(1-(ngen-1)/Ngen)^b;
end;
end;
endfunction
//
//----------------------------------------------------function z=dotprod(x,y);
// computes the dot product
of x and y
z=sum(x.*y);
endfunction
//----------------------------------------------------function d=descentdirection(f,x,fx,gx);
// descent direction:
gradient
d=-gx;
endfunction
//---------------------------------------------------function [xnew,fnew,itback]=backtracking(f,x,fx,gx,d,tau,bet,alphainit);//
line search by backtracking until Armijo condition
alpha=alphainit;xnew=x+alpha*d;
fnew=f(xnew);
itback=1;
while(fnew>fx+bet*alpha*dotprod(gx,d))
alpha=tau*alpha;
xnew=x+alpha*d;
fnew=f(xnew);
itback=itback+1;
end
endfunction
//------------------------------------------------//
// steepest descent method
with Armijo condition
function
[xbest,fbest,itfct,itgrad]=steepest(x,fx,epsilongrad,itermax,deltagain,tau,
bet,alphainit)
itgrad=0;itfct=0;
gx=rastrigingrad(x);itgrad=itgrad+1;
//
delta=1;
while (norm(gx)>epsilongrad)&(delta>0)&(itgrad<itermax)
d=descentdirection(rastrigin,x,fx,gx);
[x,fxnew,itback]=backtracking(rastrigin,x,fx,gx,d,tau,bet,alphainit);
delta=(fx-fxnew)/(itback+prod(size(x)))-deltagain;
fx=fxnew;
gx=rastrigingrad(x);
itgrad=itgrad+1;
itfct=itfct+itback;
end
xbest=x;fbest=fx;
endfunction
//
//-------------------------------------------------// clustering
//
function [Xclust,ind,Matr,Numb]=clustering(Xpar,Nclust);
[Npop,n2]=size(Xpar);n=n2-1;
bari=zeros(Nclust,n);
Matr=zeros(Nclust,Nclust);
Numb=zeros(1,Nclust);
Xclust=zeros(Nclust,n+1);
Xparclust=[Xpar,1+int(Nclust*rand(Npop,1))];
inon=1;

while inon==1;
inon=0;
for i=1:Nclust
index=find(Xparclust(:,$)==i);
if (length(index)>0) then
bari(i,:)=1/length(index)*sum(Xparclust(index,1:n),'r');
end
end
for i=1:Npop
dist0= norm(Xparclust(i,1:n)-bari(Xparclust(i,$),:));
for j=1:Nclust
dist(j)=norm(Xparclust(i,1:n)-bari(j,:));
if dist(j)<dist0 then
inon=1;
Xparclust(i,$)=j;dist0=dist(j);
end
end
end
end
for i=1:Nclust
index=find(Xparclust(:,$)==i);
[minval,indic]=mini(Xparclust(index,n+1));
ind(i)=index(indic(1));
if (length(index)>0) then
Xclust(i,:)=Xpar(index(indic(1)),:);
end
Numb(i)=length(index);
end
for i=1:Nclust
for j=1:Nclust
Matr(i,j)=norm(bari(i,:)-bari(j,:));
end
end
endfunction
//
//
//-------------------------------------------dynamic results displays
//
function result(Ntot,fmin,co)
nl=length(Ntot);
if (nl>1) then
xset('window',0);
plot2d([Ntot($-1),Ntot($)],[fmin($-1),fmin($)],co);
xtitle('convergence history','Neval','fmin');
end
endfunction
//
//----------------------------------------------------//----------------------------------------------------//---------main program ------------------------------//
xset('window',0);
// ---------------------------------------------------//
n=6;
// number of variables of the optimization
problem
Npop=40;
//parent number
//Ngen=80;
pc=0.9;
pm=0.6;

Nevalfmax=4000;
Nclustmax=4;
// maximal cluster number
itgradmax=10;
// maximal steepest descent iteration
valmin=3;
// minimal number of consecutive GA
iterations
CVRlim=1;
// coefficient of variation of the
population threshold
//
//
epsilongrad=1E-10;
// stopping criterion for gradient value
tau=0.3;
// backtraking coefficient
bet=1E-4;
// Armijo coefficient
alphainit=0.1;
// first step length of steepest descent
//
// ---------------------------------------------------//
xmin=-5.*ones(1,n);
xmax=5.*ones(1,n);
Xmin=ones(Npop,1)*xmin;
Xmax=ones(Npop,1)*xmax;
u=rand(Npop,n);
pop=Xmin+(Xmax-Xmin).*u; // random initialisation of the population
Xpar=[pop,rand(Npop,1)]; Xoldpar=Xpar;
Xoff=Xpar;Ntot=[];
mineval=0;newval=zeros(Npop,1);itfct=0;fmin=[];
CVold=1;valgen=1;Nevalg=0;Nevalf=0;gen=0;
//
Nclust=Nclustmax;
//---------------------------------------------------------//
MAIN LOOP : BEGINNING
//
//
while(Nevalf<Nevalfmax)
gen=gen+1;
//for gen=1:(Ngen-1)
//
Xpar=Xoff;
[Xpar,bestpar]=evalplus1elitism(Xpar,Xoldpar,Npop,gen); // evaluation
plus 1-elitism
Nevalf=Nevalf+Npop;
fmin=[fmin,bestpar($)];
Ntot=[Ntot,Nevalf];
result(Ntot,fmin,1);
// dynamic result display
if (gen==1) then Nold=Nevalf;oldmean=mean(Xoff(:,$));end
//
//-----------------------------------------------------------------------CVnew=mean(Xpar(:,$))/st_deviation(Xpar(:,$));
CVR=CVnew/CVold;
CVold=CVnew;
//
// ---------------------// shift to steepest descent method
-----------------------------//
if (CVR>CVRlim)&(Nclust>0)&(gen-valgen>valmin) then
newmean=mean(Xpar(:,$));Nnew=Nevalf;
deltagain=(oldmean-newmean)/(Nnew-Nold);
oldmean=newmean;Nold=Nnew;
//
[Xclust,ind,Matr,Numb]=clustering(Xpar,Nclust);
Xoffnew=[];

for i=1:Nclust
if (Numb(i)>0) then
[xnew,fnew,itfct,itgrad]=steepest(Xclust(i,1:n),Xclust(i,$),epsilongrad,itg
radmax,deltagain,tau,bet,alphainit);
Nevalf=Nevalf+itfct+itgrad*n;
Nevalg=Nevalg+itgrad;
Xpar(ind(i),:)=[xnew,fnew];
Xoffnew=[Xoffnew;xnew,fnew];
end
end
[bestpar,bestval]=thebest(Xoffnew);
// selection
lambda+mu
fmin=[fmin,bestval];
Ntot=[Ntot,Nevalf];
result(Ntot,fmin,2);
// dynamic result display
valgen=gen;
end
//
//------------------------------------------------------------------------//
Xoldpar=Xpar;
Xpar=rankselect(Xpar); // rank selection
for i=1:2:(Npop-1)
u1=int(Npop*rand())+1;u2=int(Npop*rand())+1;u3=rand();
xpar=[Xpar(u1,:);Xpar(u2,:)];Xoff(i:(i+1),:)=xpar;
if (u3<pc) then
xoff=blcross(xpar);
//crossover
Xoff(i:(i+1),:)=xoff;
for j=1:2
u4=rand();
if (u4<pm) then
xoffl=mutate(xoff(j,:),Nevalf,Nevalfmax,xmin,xmax);
Xoff(i+j-1,:)=xoffl;
end
end
end
end
//
//----------------------------------------------------------------------------//
//
MAIN LOOP : ENDING
//
end
//----------------------------------------------------//
//[Xpar,bestpar]=evalplus1elitism(Xoff,Xoldpar,Npop,gen); // evaluation
plus 1-elitism
fmin=[fmin,bestpar($)];
Ntot=[Ntot,Nevalf];
result(Ntot,fmin,1);
// dynamic result display
disp('minimum obtained:');disp(bestpar(1:n));
disp('corresponding value by f:');disp(fmin($));
disp('evaluation number:');disp(Nevalf);
//

//
//-----------------------------------------------------// hybrid method (ES + steepest descent) for Rastrigin
disp('hybrid method (ES + SD) for Rastrigin');
//
//
//----------------------------------------------------clear();
//
function y=rastrigin(x) // the function to optimize
n=prod(size(x));
y=n+sum(x.^2-cos(2*%pi*x));
endfunction
//
//----------------------------------------------------function y=rastrigingrad(x)
// the gradient of the
function to optimize
y=2*x+2*%pi*sin(2*%pi*x);
endfunction
//
//----------------------------------------------------//
//----------------------------------------------------function Xpar=evaluation(X); // evaluation of the whole population
Xpar=X;
[Npo,n]=size(X);n=n-1;
evalpop=zeros(Npo,1);
for i=1:Npo;
evalpop(i)=rastrigin(X(i,1:n));
end
Xpar(:,n+1)=evalpop;
endfunction
//
//----------------------------------------------------function Xparnew=selection(Xpar,Xoff);
[mu,n]=size(Xpar);
[lambda,n]=size(Xoff);
n=n-1;
Xtot=[Xpar;Xoff];
[Fsort,ind]=sort(-Xtot(:,n+1));
Xtot=Xtot(ind,:);
Xparnew=Xtot(1:mu,:);
endfunction
//
//-----------------------------------------------------//
function xoff=mutate(xpar,sigma) // normal mutation
[np,n]=size(xpar);n=n-1;
xoff=xpar;
u=rand(1,n,'n');
xoff(1,1:n)=xpar(1,1:n)+sigma*u;
endfunction
//
//----------------------------------------------------function z=dotprod(x,y);
// computes the dot product
of x and y
z=sum(x.*y);
endfunction
//----------------------------------------------------function d=descentdirection(f,x,fx,gx);
// descent direction:
gradient
d=-gx;

endfunction
//---------------------------------------------------function [xnew,fnew,itback]=backtracking(f,x,fx,gx,d,tau,bet,alphainit);//
line search by backtracking until Armijo condition
alpha=alphainit;xnew=x+alpha*d;
fnew=f(xnew);
itback=1;
while(fnew>fx+bet*alpha*dotprod(gx,d))
alpha=tau*alpha;
xnew=x+alpha*d;
fnew=f(xnew);
itback=itback+1;
end
endfunction
//------------------------------------------------//
// steepest descent method
with Armijo condition
function
[xbest,fbest,itfct,itgrad]=steepest(x,fx,epsilongrad,itermax,deltagain,tau,
bet,alphainit)
itgrad=0;itfct=0;
gx=rastrigingrad(x);itgrad=itgrad+1;
//
delta=1;
while (norm(gx)>epsilongrad)&(delta>0)&(itgrad<itermax)
d=descentdirection(rastrigin,x,fx,gx);
[x,fxnew,itback]=backtracking(rastrigin,x,fx,gx,d,tau,bet,alphainit);
delta=(fx-fxnew)/(itback+prod(size(x)))-deltagain;
fx=fxnew;
gx=rastrigingrad(x);
itgrad=itgrad+1;
itfct=itfct+itback;
end
xbest=x;fbest=fx;
endfunction
//
//-------------------------------------------------// clustering
//
function [Xclust,ind,Matr,Numb]=clustering(Xpar,Nclust);
[Npop,n2]=size(Xpar);n=n2-1;
bari=zeros(Nclust,n);
Matr=zeros(Nclust,Nclust);
Numb=zeros(1,Nclust);
Xclust=zeros(Nclust,n+1);
Xparclust=[Xpar,1+int(Nclust*rand(Npop,1))];
inon=1;
while inon==1;
inon=0;
for i=1:Nclust
index=find(Xparclust(:,$)==i);
if (length(index)>0) then
bari(i,:)=1/length(index)*sum(Xparclust(index,1:n),'r');
end
end
for i=1:Npop
dist0= norm(Xparclust(i,1:n)-bari(Xparclust(i,$),:));
for j=1:Nclust
dist(j)=norm(Xparclust(i,1:n)-bari(j,:));
if dist(j)<dist0 then
inon=1;

Xparclust(i,$)=j;dist0=dist(j);
end
end
end
end
for i=1:Nclust
index=find(Xparclust(:,$)==i);
[minval,indic]=mini(Xparclust(index,n+1));
ind(i)=index(indic(1));
if (length(index)>0) then
Xclust(i,:)=Xpar(index(indic(1)),:);
end
Numb(i)=length(index);
end
for i=1:Nclust
for j=1:Nclust
Matr(i,j)=norm(bari(i,:)-bari(j,:));
end
end
endfunction
//
//
//-------------------------------------------dynamic results displays
//
function result(Ntot,fmin,co)
nl=length(Ntot);
if (nl>1) then
xset('window',0);
plot2d([Ntot($-1),Ntot($)],[fmin($-1),fmin($)],co);
xtitle('convergence history','Neval','fmin');
end
endfunction
//
//----------------------------------------------------//----------------------------------------------------//---------main program ------------------------------//
xset('window',0);xbasc();
// ---------------------------------------------------//
n=6;
// number of variables of the optimization
problem
mu=5;
//parent number
lambda=35;
// offspring number
siginit=0.5;
// initial mutation strength
alpha=1.03;
// 1/5 rule coefficient
Ngen=60;
//
Nclustmax=0;
// maximal cluster number
itgradmax=10;
// maximal steepest descent iteration
valmin=3;
// minimal number of consecutive GA
iterations
CVRlim=1;
// coefficient of variation of the
population threshold
//
//
epsilongrad=1E-10;
// stopping criterion for gradient value
tau=0.3;
// backtraking coefficient
bet=1E-4;
// Armijo coefficient
alphainit=0.1;
// first step length of steepest descent

//
// ---------------------------------------------------//
xmin=-5.*ones(1,n);
xmax=5.*ones(1,n);
Xmin=ones(mu,1)*xmin;
Xmax=ones(mu,1)*xmax;
u=rand(mu,n);
pop=Xmin+(Xmax-Xmin).*u; // random initialisation of the population
Xpar=[pop,rand(mu,1)]; Xoldpar=Xpar;
Xoff=Xpar;Ntot=[];
mineval=0;newval=zeros(mu,1);itfct=0;fmin=[];
CVold=1;valgen=1;Nevalg=0;
//
//----------------------------------------------------//----------------------------------------------------//
sigma=siginit;
Xpar=evaluation(Xpar);
Nevalf=mu;
Nclust=Nclustmax;
gen=0;
//
//
MAIN LOOP : BEGINNING
//
//
for gen=1:Ngen
for i=1:lambda
u=int(mu*rand())+1;
xpar=Xpar(u,:);
xoff=mutate(xpar,sigma);
// ES mutation
Xoff(i,:)=xoff;
end
//
oldval=Xoff(:,$);
Xoff=evaluation(Xoff);
// evaluation of the offsprings
Nevalf=Nevalf+lambda;
//
if (gen==1) then Nold=Nevalf;oldmean=mean(Xoff(:,$));end
//
Xpar=selection(Xpar,Xoff);
// selection lambda+mu
bestpar=Xpar(1,1:n);
fmin=[fmin,Xpar(1,$)];
Ntot=[Ntot,Nevalf];
result(Ntot,fmin,1);
// dynamic result display
//
newval=Xoff(:,$);
ind=find(oldval>newval);
taux=length(ind)/lambda;
if (taux<0.2) then sigma=sigma/alpha; else sigma=alpha*sigma;end
//
// -------------------------------------------------------------------------//
CVnew=mean(Xoff(:,$))/st_deviation(Xoff(:,$));
CVR=CVnew/CVold;
CVold=CVnew;
//
if (CVR>CVRlim)&(Nclust>0)&(gen-valgen>valmin) then
// shift to
steepest descent method
newmean=mean(Xoff(:,$));Nnew=Nevalf;

deltagain=(oldmean-newmean)/(Nnew-Nold);
oldmean=newmean;Nold=Nnew;
//
[Xclust,ind,Matr,Numb]=clustering(Xoff,Nclust);
Xoffnew=[];
for i=1:Nclust
if (Numb(i)>0) then
[xnew,fnew,itfct,itgrad]=steepest(Xclust(i,1:n),Xclust(i,$),epsilongrad,itg
radmax,deltagain,tau,bet,alphainit);
Nevalf=Nevalf+itfct+itgrad*n;
Nevalg=Nevalg+itgrad;
Xoffnew=[Xoffnew;xnew,fnew];
end
end
Xpar=selection(Xpar,Xoffnew);
// selection lambda+mu
bestpar=Xpar(1,1:n);
fmin=[fmin,Xpar(1,$)];
Ntot=[Ntot,Nevalf];
result(Ntot,fmin,2);
// dynamic result display
valgen=gen;
end
//----------------------------------------------------------------------------//
//
MAIN LOOP : ENDING
//
end
//----------------------------------------------------//
//
//
disp('minimum obtained:');disp(bestpar(1:n));
disp('corresponding value by f:');disp(fmin($));
disp('evaluation number:');disp(Nevalf);
//

You might also like