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

    function [x,fval,exitFlag,output,population,scores] = mytsbga (fixcost, demand, covermatrix, beta,
iniwithgreedy)
%% mytsbga, a genetic algorithm dedicated for the Time Satisfaction Based
%% covering location problems
%% fixcost is a row vector of facility cost in size m
%% demand is a row vector of demand node in size n
%% covermatrix is the matrixes of covering where element is 1 if a volumn
%% covers a row and 0 otherelse, which is a m-by-n matrix
%% beta is the percentage coverage
%% iniwithgreedy decide whether or not initialize population with greedy
%% algorithm.iniwithgreedy is a 3-by-n matrix.
%--------------------------------------------
% [X, FVAL] = mytsbga(fixcost, ...) returns FVAL, the value of the fitness
% function FITNESSFCN at the solution X.
% [X,FVAL,REASON] = matsbga(fixcost, ...) returns the REASON for stopping.
% [X,FVAL,REASON,OUTPUT] = mytsbga(fixcost, ...) returns OUTPUT information
% [X,FVAL,REASON,OUTPUT,POPULATION] = mytsbga(fixcost, ...) returns the final
% POPULATION at termination.
% [X,FVAL,REASON,OUTPUT,POPULATION,SCORES] = mytsbga(fixcost, ...) returns the
% SCORES of the final POPULATION.
%=====================================================================
% do some check on the input variables
msg = nargchk(1,5,nargin);
if ~isempty(msg)
error(msg);
end
valid = isreal(beta) && isscalar(beta) && (beta >= 0) && (beta <= 1);
if(~valid)
msg = sprintf('beta must contain a scalar on the interval (0,1)');
error(msg);
end
[n,m]=size(covermatrix);
mm=size(fixcost,2);
nn=size(demand,2);
if (nn~=n)|(mm~=m)
error('invalid input data');
end
%-------------------------------------------
covered=zeros(1,n);%covered demand points
bb=sum(covermatrix,2);
for i=1:n
if bb(i)>=1
covered(i)=1;
else
covered(i)=0;
end
end
coveD=covered*demand';
if coveD<beta*sum(demand)
error('this problem does not have a feasible solution')
end
%-------------------------------------------------------------------------
%% initiate some parameters
FitnessFcn={ @fitnessTSBCLP fixcost};
%% GenomeLength is the number of variables
GenomeLength=mm;
%% number of people
Popsize=ceil(1.2*GenomeLength);
%% initiate options structure
options = struct('PopulationSize', Popsize, ...
'EliteCount', 2, ...
'Generations', 1200, ...
'TimeLimit', 1000, ...
'FitnessLimit', 0, ...
'StallGenLimit', 450, ...
'StallTimeLimit', 320, ...
'PlotInterval',1, ...
'FitnessScalingFcn', { @fitscalingshiftlinear 2 }, ...
'SelectionFcn', { @selectiontournament max(4,ceil(Popsize/6)) }, ...
'CrossoverFcn',@corssoverTSBCLP, ...
'MutationFcn',{@mutationTSBCLP 0.05}, ...
'PlotFcns', {@gaplotbestf }, ...
'Display', 'final', ...
'OutputFcns',{ @gaoutputgen 10 }, ...
'CreationFcn', @gacreationTSBCLP);
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
%% Generate a initial population
IniPop=feval(options.CreationFcn,fixcost,demand,covermatrix,beta,Popsize);
if nargin==5 %decide whether a greedy gene be initialed
if size(iniwithgreedy,2)~=mm
error('wrong input of initial enhanced subpopulation');
else
nn1=size(iniwithgreedy,1);
end
IniPop([1:nn1],:)=iniwithgreedy;
end
%-------------------------------------------------------------------------
x =[];fval =[];exitFlag='';population=[];scores=[];
%Remember the random number states used
output.randstate = rand('state');
output.randnstate = randn('state');
output.generations = 0;
output.funccount = 0;
output.message = '';
%%------------------------------------------------------------------------
%Create initial state: population, scores, status data
state.Population = IniPop
state.Score = feval(FitnessFcn,state.Population,fixcost);
% a variety of data used in various places
state.Generation = 0; % current generation counter
state.StartTime = cputime; % start time
state.StopFlag = []; % reason for termination
state.LastImprovement = 1; % generation stall counter
state.LastImprovementTime = state.StartTime; % time stall counter
state.Selection = [];
%-------------------------------------------------------------------------
%Validate arguments
[GenomeLength,FitnessFcn,options] = myvalidate(GenomeLength,FitnessFcn,options);
%Give the plot/output Fcns a chance to do any initialization they need.
state = mygaplot(FitnessFcn,options,state,'init');
[state,options] = mygaoutput(FitnessFcn,options,state,'init');
%Print some diagnostic information if asked for
if strcmpi(options.Display,'diagnose')
gadiagnose(FitnessFcn,GenomeLength,options);
end
%Setup display header
if any(strcmpi(options.Display, {'iter','diagnose'}))
fprintf('\n Best Mean Stall\n');
fprintf('Generation f-count f(x) f(x) Generations\n');
end

exitFlag = '';
%-------------------------------------------------------------------------
% run the main loop until some termination condition becomes true
while isempty(exitFlag)
state.Generation = state.Generation + 1;
score = state.Score;
population = state.Population;
[score,population,state] = stepGA(score, population, options, state, GenomeLength,
FitnessFcn, fixcost, demand, covermatrix, beta);
state.Population = population;
state.Score = score;
% remember the best score
best = min(state.Score);
generation = state.Generation;
state.Best(generation) = best;

% keep track of improvement in the best
if((generation > 1) && finite(best))
if(state.Best(generation-1) > best)
state.LastImprovement = generation;
state.LastImprovementTime = cputime;
end
% update the Output
state = mygaplot(FitnessFcn,options,state,'iter');
[state,options] = mygaoutput(FitnessFcn,options,state,'iter');
% check to see if any stopping criteria have been met
exitFlag = isItTimeToStop(options,state);
end %End while loop
% find and return the best solution
[fval,best] = min(state.Score);
x = state.Population(best,:);
%Update output structure
output.generations = state.Generation;
output.message = exitFlag;
output.funccount = state.Generation*length(state.Score);
% load up outputs
if(nargout > 4)
population = state.Population;
if(nargout > 5)
scores = state.Score;
end
end
% give the Output functions a chance to finish up
mygaplot(FitnessFcn,options,state,'done');
mygaoutput(FitnessFcn,options,state,'done');

end