gusucode.com > 遗传算法matlab源码程序 > 时间满意度遗传算法/初始种群生成.txt

    function Population = gacreationTSBCLP(fixcost,demand,covermatrix,beta,options)
% Population = gacreationTSBCLP(fixcost,demand,covermatrix,beta)
% gacreationTSBCLP Creates the initial population for the genetic
% algorithm of TSBCLP model.
%============================================================
totalpopulation = options.PopulationSize;
Population=[];
i=1;
while i<=totalpopulation %popsize
rowgene=rgenerater(fixcost,demand,covermatrix,beta);
Population=[Population;rowgene];
i=i+1;
end
if all(isnan(Population))
msg = sprintf(['Initial population contains NaN;','PopInitRange is possibly too big.']);
error(msg);
elseif all(isinf(Population))
msg = sprintf(['Initial population contains Inf;','PopInitRange is possibly too big.']);
error(msg);
end
%%====================================================================
% generate a single solution for population
function rowgene=rgenerater(fixcost,demand,covermatrix,beta)
[n,m]=size(covermatrix);%find the size of demand and service
rowgene=zeros(1,m);%solution set initialization
faii=zeros(1,m);%facility set that cover demand node i in row vector
faij=zeros(n,1);%demand set that covered by facility j in column vector
ksi=zeros(1,n);%the number of facilities that cover a demand node
udfcost=1./fixcost;%the updown fixcost of facility
uncover=ones(1,n);%initialize the uncovered set of demand node
TotalDemand=sum(demand);%total demand
%%--------------------------------------------------------
%% find a solution with weighted cost and demand randomely
CovD=0;
while CovD<beta*TotalDemand
UncDemand=uncover.*demand;%uncovered demand vector
TUnD=sum(UncDemand);%total uncovered demand
wheel1=cumsum(UncDemand)/TUnD;
r=rand;
i=1;
while i<=length(wheel1)
if r<wheel1(i)
if ksi(i)<1
newCD=i;%found a new covered demand node
i=length(wheel1)+1;%end while
end
end
i=i+1;
end
faii=covermatrix(newCD,:);%facility set cover demand i
unlocate=~rowgene;%unlocated facility set
UnLFC=faii.*udfcost.*unlocate;%unlocated facility udfcost vector
TUnLFC=sum(UnLFC);%total unlocated facility udfcost
if TUnLFC==0
rowgene=ones(1,m);
ksi=sum(covermatrix,2)';
break;
end
wheel2=cumsum(UnLFC)/TUnLFC;
r=rand;
j=1;
while j<=length(wheel2)
if r<wheel2(j)
if unlocate(j)==1
newLF=j;%found a new facility node
j=length(wheel2)+1;%end while
end
end
j=j+1;
end
rowgene(newLF)=1;%add new located facility
faij=covermatrix(:,newLF);%demand set covered by facility newLF in column vector
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=sum(~uncover.*demand);%total demand has been covered
end
%%---------------------------------------------------------
%% delete redundant facility without weighted cost randomely
deleset=rowgene;% define a initial set of un lean solution
Nds=sum(deleset);%number of facilities in deleset
while Nds>0
wheel3=cumsum(deleset)/Nds;
r=rand;
j=1;
while j<=length(wheel3)
if r<wheel3(j)
if deleset(j)==1
dej=j;%foun a delete facility node
j=length(wheel3)+1;%end while
end
end
j=j+1;
end
deleset(dej)=0;
faij=covermatrix(:,dej);%demand set covered by facility dej 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)
rowgene(dej)=0;%delet the dejth facility from solution
for i=1:n
if faij(i)==1
ksi(i)=ksi(i)-1;
end
end
end
Nds=sum(deleset);
end%%end of generating a single solution
%%====================================================================