gusucode.com > 遗传算法matlab源码程序 > 时间满意度遗传算法/feasible.m

    function feasibleKids=feasible(newKids,fixcost,demand,covermatrix,beta)
% input parameters: newKids,FitnessFcn,fixcost,demand,covermatrix,beta
kids=newKids;
[n,m]=size(covermatrix);%find the size of demand and service
TotalDemand=sum(demand);%total demand
%----------------------------------------------------------
% analysis if kids is feasible
covermat=zeros(n,m);%covered matrix under present solution
bbi=zeros(n,1);%initiate a vector
for i=1:n
for j=1:m
covermat(i,j)=covermatrix(i,j)*kids(j);
end
end
covered=zeros(n,1);%covered demand points
bbi=sum(covermat,2);
for i=1:n
if bbi(i)>=1
covered(i)=1;
else
covered(i)=0;
end
end
CovD=demand*covered;%total demand has been covered
if CovD>beta*TotalDemand
feasibleKids=kids;
else
feasibleKids = ffeasible(newKids,fixcost,demand,covermatrix,beta,bbi,covered);
end
%=====================================================================
function fKids = ffeasible(newKids,fixcost,demand,covermatrix,beta,bbi,covered)
%subfunction to add new facility locations and do some adjustment
kids=newKids;
[n,m]=size(covermatrix);%find the size of demand and service
faij=zeros(n,1);%demand set that covered by facility j in column vector
TotalDemand=sum(demand);%total demand
ksi=zeros(n,1);%the number of facilities that cover a demand node
uncover=ones(n,1);%initialize the uncovered set of demand node
rj=zeros(1,m);%the weighted uncovered facility set
%------------------------------------------------------
ksi=bbi;
uncover=~covered;
CovD=demand*covered;%total demand has been covered
while CovD<beta*TotalDemand
if sum(kids)==m
fKids=kids;
return;
end
unlocate=~kids;%unlocated facility cites set
for j=1:m
if unlocate(j)==1
rj(j)=(uncover.*demand')'*covermatrix(:,j)/fixcost(j);
else
rj(j)=0;
end
end
[mind,rjD]=max(rj);
kids(rjD)=1;
faij=covermatrix(:,rjD);%demand set covered by facility rjD
for i=1:n %calculate the number of facilities cover each demand node
if faij(i)==1
ksi(i)=ksi(i)+1;
if ksi(i)>=1
uncover(i)=0;
end
end
end
CovD=demand*~uncover;%total demand has been covered
end
%%----------------------------------------------------------
%% delete redundant facility
for j=1:m
if kids(j)==1
faij=covermatrix(:,j);%demand set covered by facility j in column vector
nksi=0;%the number of ksi who greater than 2 in set faij
for i=1:n
if faij(i)==1
if ksi(i)>=2
nksi=nksi+1;
end
end
end
if nksi==sum(faij)
kids(j)=0;%delet the jth facility from solution
for i=1:n
if faij(i)==1
ksi(i)=ksi(i)-1;
end
end
end
end
end
% ---------------------------------------------------------
% output solution
fKids=kids;
% =====================================================================
% end of main feasible function