Sei sulla pagina 1di 4

function [BestGen,BestFitness,gx]=HarmonySearch

% This code has been written with Matlab 7.0


% You can modify the simple constraint handlening method using more efficient
% methods. A good review of these methods can be found in:
% Carlos A. Coello Coello,"Theoretical and numerical constraint-handling techniques
used with evolutionary algorithms: a survey of the state of the art"

clc
clear;
global NVAR NG NH MaxItr HMS HMCR PARmin PARmax bwmin bwmax;
global HM NCHV fitness PVB BW gx;
global BestIndex WorstIndex BestFit WorstFit BestGen currentIteration;

NVAR=1; %number of variables


NG=0; %number of ineguality constraints
NH=0; %number of eguality constraints
MaxItr=40; % maximum number of iterations
HMS=100; % harmony memory size
HMCR=0.8; % harmony consideration rate 0< HMCR <1
PARmin=0.4; % minumum pitch adjusting rate
PARmax=0.9; % maximum pitch adjusting rate
bwmin=0.0001; % minumum bandwidth
bwmax=1.0; % maxiumum bandwidth
PVB=[0 100]; % range of variables

% /**** Initiate Matrix ****/


HM=zeros(HMS,NVAR);
NCHV=zeros(1,NVAR);
BestGen=zeros(1,NVAR);
fitness=zeros(1,HMS);
BW=zeros(1,NVAR);
gx=zeros(1,NG);
% warning off MATLAB:m_warning_end_without_block

MainHarmony;

% /**********************************************/

function sum =Fitness(sol)


sum(1)=(sol*sol);
sum(2)=(sol-2)^2;

end
% /*********************************************/

function initialize
% randomly initialize the HM
for i=1:HMS
for j=1:NVAR
HM(i,j)=randval(PVB(j,1),PVB(j,2));
end
raw = Fitness(HM(i,:));
rr(i,1)=raw(1,1);
rr(i,2)=raw(1,2);
fitness=rr;
end
end
HM;
%/*******************************************/

function MainHarmony
% global NVAR NG NH MaxItr HMS HMCR PARmin PARmax bwmin bwmax;
% global HM NCHV fitness PVB BW gx currentIteration;

initialize;
currentIteration = 0;
dom=ones(1,HMS);
for i5=1:HMS
for i6=1:HMS
if i5~=i6
ch1(1)=(fitness(i5,1)>=fitness(i6,1));
ch1(2)=(fitness(i5,2)>=fitness(i6,2));
ch2(1)=(fitness(i5,1)>fitness(i6,1));
ch2(2)=(fitness(i5,2)>fitness(i6,2));
if ((all(ch1))&&(any(ch2)))
dom(i5)=1;
break;
else
dom(i5)=0;
end
end
end
end
i8=0;
temp=0;
for i7=1:HMS
if dom(i7)==0
i8=i8+1;
v2(i8,1)=fitness(i7,1);
v2(i8,2)=fitness(i7,2);
temp=1;
end
end
v2
dom2=ones(1,HMS);
for i10=1:HMS
if dom(i10)==0
dom2(i10)=0;
end
end

if temp==1
scatter(v2(:,1),v2(:,2))
hold on;
drawnow
pause(0.001);
end
while(StopCondition(currentIteration))
currentIteration
i3=currentIteration+1;
PAR=(PARmax-PARmin)/(MaxItr)*currentIteration+PARmin;
coef=log(bwmin/bwmax)/MaxItr;
for pp =1:NVAR
BW(pp)=bwmax*exp(coef*currentIteration);
end
% improvise a new harmony vector
for jjj=1:HMS
for i =1:NVAR
ran = rand(1);
if( ran < HMCR ) % memory consideration
index = randint(1,HMS);
index;
NCHV(i) = HM(index,i);
NCHV;
pvbRan = rand(1);
if( pvbRan < PAR) % pitch adjusting
pvbRan1 = rand(1);
result = NCHV(i);
if( pvbRan1 < 0.5)
result =result+ rand(1) * BW(i);
if( result < PVB(i,2))
NCHV(i) = result;
end
else
result =result- rand(1) * BW(i);
if( result > PVB(i,1))
NCHV(i) = result;
end
end
end
else
NCHV(i) = randval( PVB(i,1), PVB(i,2) ); % random selection
end
end
NCHV;
raw = Fitness(NCHV);
rr(jjj,1)=raw(1,1);
rr(jjj,2)=raw(1,2);
end
v2=[v2;rr];
ss=size(v2);
sss=ss(1,1);
sss;
for i5=1:sss
for i6 = 1:sss
if i5~=i6
ch1(1)=(v2(i5,1)>=v2(i6,1));
ch1(2)=(v2(i5,2)>=v2(i6,2));
ch2(1)=(v2(i5,1)>v2(i6,1));
ch2(2)=(v2(i5,2)>v2(i6,2));
if ((all(ch1))&&(any(ch2)))
dom(i5)=1;
break;
else
dom(i5)=0;
end
end
end
end

for i7=1:sss
if dom(i7)==0
i8=i8+1;
v3(i8,1)=v2(i7,1);
v3(i8,2)=v2(i7,2);
temp=1;
end
end
dom2=ones(1,sss);
for i10=1:sss
if dom(i10)==0
dom2(i10)=0;
end
end
if temp==1
scatter(v3(:,1),v3(:,2))
hold on;
drawnow
p(i3)=getframe;
pause(0.001);
end
currentIteration=currentIteration+1;
end

end
end %function

% /*****************************************/
function val1=randval(Maxv,Minv)
val1=rand(1)*(Maxv-Minv)+Minv;
end

function val2=randint(Maxv,Minv)
val2=round(rand(1)*(Maxv-Minv)+Minv);
end
% /*******************************************/

function val=StopCondition(Itr)
global MaxItr;
val=1;
if(Itr>MaxItr)
val=0;
end
end

% /*******************************************/

Potrebbero piacerti anche