Cross Entropy with Matlab

function [xopt,fopt]= cecoba(N,rho,alpha,mu,sig)

% cross entropy for function minimization

%N: population size

%rho: portion of elite sample

%alpha -weight

%mu- initial vector for mean

%sig-initial vector for std dev
it=0;
x=[100 200];
has=[];
while abs(sum(std(x)))> 10e-9
x=repmat(mu,N,1)+randn(N,2).*repmat(sig,N,1);
for i=1:N
f(i)=himel(x(i,:));
end
[fu,idk]=sort(f);
elit=ceil(rho*N);
xe=x(idk(1:elit),:);
xb=mean(xe);
sd=std(xe);
mu=alpha.*xb+(1-alpha).*mu;
sig=alpha.*sd+(1-alpha).*sig;
it=it+1;
has=[has ; it mu himel(mu)];
end
xopt=mu;
fopt=himel(mu);
has

function f=himel(x)
f=(x(1)^2+x(2)-11)^2+(x(1)+x(2)^2-7)^2;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s