%evaluate all the individuals in the population p, and return its values of
%the fitness function, and the degree of the inequality and equality
%constraint violation
function [fit,g,h] = fitness(p,problem,delta,A,normalization)
%% input parameters
% p -- the population evaluated
% problem -- the problem tested
% delta -- the tolerance value
% A -- the array of parameters p,q,r
% normalization -- the flag to indicate the way that will be selected
%% output parameters
% fit -- values of fitness functions and degree of constraint violation
% g -- degree of the inequality constraint violations
% h -- degree of the equality constraint violations
%compute the size of the population p
popsize=size(p,1);
%initialize the set fit
fit=zeros(popsize,2);
%initialize the set of degree of equality constraint violations
h=[];
%initialize the set of degree of inequality constraint violations
g=[];
%choose the problem
switch problem
case 1
%calculate the degree of inequality constraint violations
g(:,1)=2*p(:,1)+2*p(:,2)+p(:,10)+p(:,11)-10;
g(:,2)=2*p(:,1)+2*p(:,3)+p(:,10)+p(:,12)-10;
g(:,3)=2*p(:,2)+2*p(:,3)+p(:,11)+p(:,12)-10;
g(:,4)=-8*p(:,1)+p(:,10);
g(:,5)=-8*p(:,2)+p(:,11);
g(:,6)=-8*p(:,3)+p(:,12);
g(:,7)=-2*p(:,4)-p(:,5)+p(:,10);
g(:,8)=-2*p(:,6)-p(:,7)+p(:,11);
g(:,9)=-2*p(:,8)-p(:,9)+p(:,12);
%compute the value of the objective function
f1=5*sum(p(:,1:4),2)-5*sum(p(:,1:4).^2,2)-sum(p(:,5:13),2);
%compute the degree of constraint violations according to 'normalization'
if normalization==1
f2=sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)./size(g,2);
else
f2=sum(max(0,g),2);
end
case 2
%calculate the degree of inequality constraint violations
g(:,1)=0.75-prod(p,2);
g(:,2)=sum(p,2)-7.5*size(p,2);
%compute the value of the objective function
f1=-abs(sum((cos(p).^4),2)-2*prod((cos(p).^2),2))./(sqrt(sum(((ones(size(p,1),1)*[1:size(p,2)]).*(p.^2)),2))+1e-30);
%compute the degree of constraint violations according to 'normalization'
if normalization==1
f2=sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)./size(g,2);
else
f2=sum(max(0,g),2);
end
case 3
%calculate the degree of equality constraint violations
h(:,1)=sum(p.^2,2)-1;
%compute the value of the objective function
f1=-(10.^0.5)^10*prod(p,2);
%compute the degree of constraint violations according to 'normalization'
if normalization==1
f2=sum(max(0,abs(h)-delta)./(ones(popsize,1)*(max(max(0,abs(h)-delta))+1E-30)),2)./(size(h,2));
else
f2=sum(max(0,abs(h)-delta),2);
end
case 4
%calculate the degree of inequality constraint violations
g(:,1)=+85.334407+0.0056858*p(:,2).*p(:,5)+0.0006262*p(:,1).*p(:,4)-0.0022053*p(:,3).*p(:,5)-92;
g(:,2)=-85.334407-0.0056858*p(:,2).*p(:,5)-0.0006262*p(:,1).*p(:,4)+0.0022053*p(:,3).*p(:,5);
g(:,3)=+80.51249+0.0071317*p(:,2).*p(:,5)+0.0029955*p(:,1).*p(:,2)+0.0021813*p(:,3).^2-110;
g(:,4)=-80.51249-0.0071317*p(:,2).*p(:,5)-0.0029955*p(:,1).*p(:,2)-0.0021813*p(:,3).^2+90;
g(:,5)=+9.300961+0.0047026*p(:,3).*p(:,5)+0.0012547*p(:,1).*p(:,3)+0.0019085*p(:,3).*p(:,4)-25;
g(:,6)=-9.300961-0.0047026*p(:,3).*p(:,5)-0.0012547*p(:,1).*p(:,3)-0.0019085*p(:,3).*p(:,4)+20;
%compute the value of the objective function
f1=5.3578547*p(:,3).^2+0.8356891*p(:,1).*p(:,5)+37.293239*p(:,1)-40792.141;
%compute the degree of constraint violations according to 'normalization'
if normalization==1
f2=sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)./size(g,2);
else
f2=sum(max(0,g),2);
end
case 5
%calculate the degree of inequality constraint violations
g(:,1)=-p(:,4)+p(:,3)-0.55;
g(:,2)=-p(:,3)+p(:,4)-0.55;
%calculate the degree of equality constraint violations
h(:,1)=1000*sin(-p(:,3)-0.25)+1000*sin(-p(:,4)-0.25)+894.8-p(:,1);
h(:,2)=1000*sin(p(:,3)-0.25)+1000*sin(p(:,3)-p(:,4)-0.25)+894.8-p(:,2);
h(:,3)=1000*sin(p(:,4)-0.25)+1000*sin(p(:,4)-p(:,3)-0.25)+1294.8;
%compute the value of the objective function
f1=3*p(:,1)+0.000001*p(:,1).^3+2*p(:,2)+0.000002/3*p(:,2).^3;
%求最大值
% f1 = -( 3*p(:,1)+0.000001*p(:,1).^3+2*p(:,2)+0.000002/3*p(:,2).^3 ) ;
%compute the degree of constraint violations according to 'normalization'
if normalization==1
f2=(sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)+...
sum(max(0,abs(h)-delta)./(ones(popsize,1)*(max(max(0,abs(h)-delta))+1E-30)),2))./(size(g,2)+size(h,2));
else
f2=sum(max(0,g),2)+sum(max(0,abs(h)-delta),2);
end
case 6
%calculate the degree of inequality constraint violations
g(:,1)=-(p(:,1)-5).^2-(p(:,2)-5).^2+100;
g(:,2)=(p(:,1)-6).^2+(p(:,2)-5).^2-82.81;
%compute the value of the objective function
f1=(p(:,1)-10).^3+(p(:,2)-20).^3;
%compute the degree of constraint violations according to
%'normalization'
if normalization==1
f2=sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)./size(g,2);
else
f2=sum(max(0,g),2);
end
case 7
%calculate the degree of inequality constraint violations
g(:,1)=-105+4*p(:,1)+5*p(:,2)-3*p(:,7)+9*p(:,8);
g(:,2)=10*p(:,1)-8*p(:,2)-17*p(:,7)+2*p(:,8);
g(:,3)=-8*p(:,1)+2*p(:,2)+5*p(:,9)-2*p(:,10)-12;
g(:,4)=3*(p(:,1)-2).^2+4*(p(:,2)-3).^2+2*p(:,3).^2-7*p(:,4)-120;
g(:,5)=5*p(:,1).^2+8*p(:,2)+(p(:,3)-6).^2-2*p(:,4)-40;
g(:,6)=p(:,1).^2+2*(p(:,2)-2).^2-2*p(:,1).*p(:,2)+14*p(:,5)-6*p(:,6);
g(:,7)=0.5*(p(:,1)-8).^2+2*(p(:,2)-4).^2+3*p(:,5).^2-p(:,6)-30;
g(:,8)=-3*p(:,1)+6*p(:,2)+12*(p(:,9)-8).^2-7*p(:,10);
%compute the value of the objective function
f1=p(:,1).^2+p(:,2).^2+p(:,1).*p(:,2)-14*p(:,1)-16*p(:,2)+(p(:,3)-10).^2+4*(p(:,4)-5).^2+(p(:,5)-3).^2+2*(p(:,6)-1).^2+5*p(:,7).^2+7*(p(:,8)-11).^2+2*(p(:,9)-10).^2+(p(:,10)-7).^2+45;
%compute the degree of constraint violations according to
%'normalization'
if normalization==1
f2=sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)./size(g,2);
else
f2=sum(max(0,g),2);
end
case 8
%calculate the degree of inequality constraint violations
g(:,1)=p(:,1).^2-p(:,2)+1;
g(:,2)=1-p(:,1)+(p(:,2)-4).^2;
%compute the value of the objective function
f1=-(sin(2*pi*p(:,1)).^3).*sin(2*pi*p(:,2))./((p(:,1).^3.*(p(:,1)+p(:,2)))+1e-17);
%compute the degree of constraint violations according to
%'normalization'
if normalization==1
f2=sum(max(0,g)./(ones(popsize,1)*(max(max(0,g))+1E-30)),2)./size(g,2);
else
f2=sum(max(0,g),2);
end
case 9
%calculate the degree of inequality constraint violations
g(:,1)=-127+2*p(:,1).^2+3*p(:,2).^4+p(:,3)+4*p(:,4).^2+5*p(:,5);
g(:,2)=-282+7*p(:,1)+3*p(:,2)+10*p(:,3).^2+p(:,4)-p(:,5);
g(:,3)=-196+23*p(:,1)+p(:,2).^2+6*p(:,6).^2-8*p(:,7);
g(:,4)=4*p(:,1).^2+p(:,2).^2-3*p(:,1).*p(:,2)+2*p(:,3).^2+5*p(:,6)-11*p(:,7);
%compute the value of the objective function
f1=(p(:,1)-10).^2+5*(p(:,2)-12).^2+p(:,3).^4+3*(p(:,4)-11).^2+10*p(:,5).^6+7*p(:,6).^2+p(:,7).^4-4*p(:,6).*p(:,7)-10*p(:,6)-8*p(:,7);
%compute the degree of constraint violations according