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

    function [nextScore,nextPopulation,state] = mystepGA(thisScore, thisPopulation, options,state,GenomeLength, FitnessFcn, fixcost, demand, covermatrix, beta)
% MYSTEPGA Moves the genetic algorithm forward by one generation
% This function is private to MYTSBGA.
%=====================================================================
% how many crossover offspring will there be from each source?
nEliteKids = options.EliteCount;
% how many parents will we need to complete the population?
nParents = 2;
% decide who will contribute to the next generation
% fitness scaling
state.Expectation = feval(options.FitnessScalingFcn, thisScore,nParents, options.FitnessScaling…
FcnArgs{:});
% selection. parents are indicies into thispopulation
parents =
feval(options.SelectionFcn,state.Expectation,nParents,options,options.SelectionFcnArgs{:});
%parentCode
parentCode=thisPopulation(parents,:);
[unused,k] = sort(thisScore);
% Everyones parents are stored here for genealogy display
state.Selection = [k(1:options.EliteCount);parents'];

% here we make all of the members of the next generation
eliteKids = thisPopulation(k(1:options.EliteCount),:);
% choose a kids to be replaced
otherKids = thisPopulation(k(options.EliteCount+1:end),:);
otherScore = thisScore(k(options.EliteCount+1:end));
totalScore=sum(otherScore);
wheel=cumsum(otherScore)/totalScore;
r=rand;
i=1;
while i<=length(wheel)
if r<wheel(i)
repNo=i;%found a new covered demand node
i=length(wheel)+1;%end while
end
i=i+1;
end
otherKids(repNo,:)=[];
% find the minfitness parent to crossover
fitnessk=feval(FitnessFcn,parentCode,fixcost);
[unusedd,xovk]=max(fitnessk);
% generate a new child by corossover and mutation operation
if any(xor(parentCode(1,:),parentCode(2,:)))
newKids=feval(options.CrossoverFcn,parentCode,GenomeLength,fitnessk);
state.cross=state.cross+1;

if (any(xor(parentCode(1,:),newKids)))&(any(xor(parentCode(2,:),newKids)))
newKids=feasible(newKids,fixcost,demand,covermatrix,beta);
if ~((any(xor(parentCode(1,:),newKids)))&(any(xor(parentCode(2,:),newKids))))
state.mutate=state.mutate+1;
newKids=feval(options.MutationFcn,parentCode(xovk,:),GenomeLength,options.MutationFcnArgs{:});
newKids=feasible(newKids,fixcost,demand,covermatrix,beta);
end
else
state.mutate=state.mutate+1;
newKids=feval(options.MutationFcn,parentCode(xovk,:),GenomeLength,options.MutationFcnArgs{:});
newKids=feasible(newKids,fixcost,demand,covermatrix,beta);
end
else
state.mutate=state.mutate+1;
newKids=feval(options.MutationFcn,parentCode(1,:),GenomeLength,options.MutationFcnArgs{:});
newKids=feasible(newKids,fixcost,demand,covermatrix,beta);
end
% group them into the next generation
nextPopulation = [ eliteKids ; newKids ; otherKids ];
% score the population
nextScore = feval(FitnessFcn,nextPopulation,fixcost);