Error programs,download from mathworks (matlab 2015a)

조회 수: 2 (최근 30일)
jixie zhuangbei
jixie zhuangbei 2015년 8월 4일
댓글: jixie zhuangbei 2015년 8월 30일
% SpeedyGA is a vectorized implementation of a Simple Genetic Algorithm in Matlab
% Version 1.3
% Copyright (C) 2007, 2008, 2009 Keki Burjorjee
% Created and tested under Matlab 7 (R14).
% Licensed under the Apache License, Version 2.0 (the "License"); you may
% not use this file except in compliance with the License. You may obtain
% a copy of the License at
%
%
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
% Acknowledgement of the author (Keki Burjorjee) is requested, but not required,
% in any publication that presents results obtained by using this script
% Without Sigma Scaling, Stochastic Universal Sampling, and the generation of mask
% repositories, SpeedyGA faithfully implements the specification of a simple genetic
% algorithm given on pages 10,11 of M. Mitchell's book An Introduction to
% Genetic Algorithms, MIT Press, 1996). Selection is fitness
% proportionate.
len=640 % The length of the genomes
popSize=500; % The size of the population (must be an even number)
maxGens=1000; % The maximum number of generations allowed in a run
probCrossover=1; % The probability of crossing over.
probMutation=0.003; % The mutation probability (per bit)
sigmaScalingFlag=1; % Sigma Scaling is described on pg 168 of M. Mitchell's
% GA book. It often improves GA performance.
sigmaScalingCoeff=1; % Higher values => less fitness pressure
SUSFlag=1; % 1 => Use Stochastic Universal Sampling (pg 168 of
% M. Mitchell's GA book)
% 0 => Do not use Stochastic Universal Sampling
% Stochastic Universal Sampling almost always
% improves performance
crossoverType=2; % 0 => no crossover
% 1 => 1pt crossover
% 2 => uniform crossover
visualizationFlag=1; % 0 => don't visualize bit frequencies
% 1 => visualize bit frequencies
verboseFlag=1; % 1 => display details of each generation
% 0 => run quietly
useMaskRepositoriesFlag=1; % 1 => draw uniform crossover and mutation masks from
% a pregenerated repository of randomly generated bits.
% Significantly improves the speed of the code with
% no apparent changes in the behavior of
% the SGA
% 0 => generate uniform crossover and mutation
% masks on the fly. Slower.
% crossover masks to use if crossoverType==0.
mutationOnlycrossmasks=false(popSize,len);
% pre-generate two 搑epositories?of random binary digits from which the
% the masks used in mutation and uniform crossover will be picked.
% maskReposFactor determines the size of these repositories.
maskReposFactor=5;
uniformCrossmaskRepos=rand(popSize/2,(len+1)*maskReposFactor)<0.5;
mutmaskRepos=rand(popSize,(len+1)*maskReposFactor)<probMutation;
% preallocate vectors for recording the average and maximum fitness in each
% generation
avgFitnessHist=zeros(1,maxGens+1);
maxFitnessHist=zeros(1,maxGens+1);
eliteIndiv=[];
eliteFitness=-realmax;
% the population is a popSize by len matrix of randomly generated boolean
% values
pop=rand(popSize,len)<.5;
for gen=0:maxGens
% evaluate the fitness of the population. The vector of fitness values
% returned must be of dimensions 1 x popSize.
fitnessVals=oneMax(pop);
[maxFitnessHist(1,gen+1),maxIndex]=max(fitnessVals);
avgFitnessHist(1,gen+1)=mean(fitnessVals);
if eliteFitness<maxFitnessHist(gen+1)
eliteFitness=maxFitnessHist(gen+1);
eliteIndiv=pop(maxIndex,:);
end
% display the generation number, the average Fitness of the population,
% and the maximum fitness of any individual in the population
if verboseFlag
display(['gen=' num2str(gen,'%.3d') ' avgFitness=' ...
num2str(avgFitnessHist(1,gen+1),'%3.3f') ' maxFitness=' ...
num2str(maxFitnessHist(1,gen+1),'%3.3f')]);
end
% Conditionally perform bit-frequency visualization
if visualizationFlag
figure(1)
set (gcf, 'color', 'w');
hold off
bitFreqs=sum(pop)/popSize;
plot(1:len,bitFreqs, '.');
axis([0 len 0 1]);
title(['Generation = ' num2str(gen) ', Average Fitness = ' sprintf('%0.3f', avgFitnessHist(1,gen+1))]);
ylabel('Frequency of the Bit 1');
xlabel('Locus');
drawnow;
end
% Conditionally perform sigma scaling
if sigmaScalingFlag
sigma=std(fitnessVals);
if sigma~=0;
fitnessVals=1+(fitnessVals-mean(fitnessVals))/...
(sigmaScalingCoeff*sigma);
fitnessVals(fitnessVals<=0)=0;
else
fitnessVals=ones(popSize,1);
end
end
% Normalize the fitness values and then create an array with the
% cumulative normalized fitness values (the last value in this array
% will be 1)
cumNormFitnessVals=cumsum(fitnessVals/sum(fitnessVals));
% Use fitness proportional selection with Stochastic Universal or Roulette
% Wheel Sampling to determine the indices of the parents
% of all crossover operations
if SUSFlag
markers=rand(1,1)+[1:popSize]/popSize;
markers(markers>1)=markers(markers>1)-1;
else
markers=rand(1,popSize);
end
[temp parentIndices]=histc(markers,[0 cumNormFitnessVals]);
parentIndices=parentIndices(randperm(popSize));
% deterimine the first parents of each mating pair
firstParents=pop(parentIndices(1:popSize/2),:);
% determine the second parents of each mating pair
secondParents=pop(parentIndices(popSize/2+1:end),:);
% create crossover masks
if crossoverType==0
masks=mutationOnlycrossmasks;
elseif crossoverType==1
masks=false(popSize/2, len);
temp=ceil(rand(popSize/2,1)*(len-1));
for i=1:popSize/2
masks(i,1:temp(i))=true;
end
else
if useMaskRepositoriesFlag
temp=floor(rand*len*(maskReposFactor-1));
masks=uniformCrossmaskRepos(:,temp+1:temp+len);
else
masks=rand(popSize/2, len)<.5;
end
end
% determine which parent pairs to leave uncrossed
reprodIndices=rand(popSize/2,1)<1-probCrossover;
masks(reprodIndices,:)=false;
% implement crossover
firstKids=firstParents;
firstKids(masks)=secondParents(masks);
secondKids=secondParents;
secondKids(masks)=firstParents(masks);
pop=[firstKids; secondKids];
% implement mutation
if useMaskRepositoriesFlag
temp=floor(rand*len*(maskReposFactor-1));
masks=mutmaskRepos(:,temp+1:temp+len);
else
masks=rand(popSize, len)<probMutation;
end
pop=xor(pop,masks);
end
if verboseFlag
figure(2)
%set(gcf,'Color','w');
hold off
plot([0:maxGens],avgFitnessHist,'k-');
hold on
plot([0:maxGens],maxFitnessHist,'c-');
title('Maximum and Average Fitness')
xlabel('Generation')
ylabel('Fitness')
end
The second:】
function [p_min, iter, f]=genetic_algorithm (func, numMaxInd, numF, numMaxGen, pRepr, pRecom, pMut, pImm, numVariabili, limLow, limUp, tolerance)
% A genetic algorithm is a search heuristic that mimics the process of
% natural evolution. It is used to generate useful solutions to
% optimization and search problems. Genetic algorithms belong to the larger
% class of evolutionary algorithms, which generate solutions to
% optimization problems using techniques inspired by natural evolution,
% such as inheritance, mutation, selection, and crossover.
%
% Output variables:
% - p_min: it's the minimum point of the objective function;
% - iter: it's the final iteration number;
% - f: it's the objective function value in the minimum point.
%
% Input variables:
% - func: it's the handle of the objective function to minimize (example: f_obj=@(x) function(x) where x is the variables vector);
% - numMaxInd: Number of individuals (number of initial points);
% - numF: Number of sons for each generation;
% - numMaxGen: Max number of generations (number of max iterations);
% - pRepr: Reproduction probability;
% - pRecom: Ricombination probability;
% - pMut: Mutation probability;
% - pImm: Immigration probability;
% - numVariabili: it's the number of the function variables.
% - limLow: it's the low bound for the initial points;
% - limUp: it's the high bound for the initial points;
% - tolerance: it's the tolerance error for the stop criterion.
fOb=func; % Objective Function (Function to minimize)
numInd=numMaxInd; % Number of individuals (number of initial points)
numIter=numMaxGen; % Number of generations (number of max iterations)
numFigliTot=numF; % Number of sons for each generation.
% The sum of the probabilities of the mutation operations must be 1.
pRep=pRepr; %Reproduction probability
pRec=pRecom; %Ricombination probability
pM=pMut; %Mutation probability
pI=pImm; %Immigration probability
numVar=numVariabili; %Number of function variables
low=limLow;
up=limUp;
% Initializes the initial population
ind=low+(up-low).*rand(numInd, numVar);
iter=1;
tolStop=tolerance;
stop=1;
while(iter<=numIter && stop>tolStop)
%Calculates the population ranking
sumValF=0;
for l=1:numInd
valF(l,1)=fOb(ind(l,:));
sumValF=sumValF+(valF(l,1))^-1;
end
[valFord,Ind]=sort(valF,1,'ascend');
indOrd=ind(Ind,:);
for l=1:numInd
rank(l,1)=(valFord(l,1))^-1/sumValF;
end
numFigli=1;
numFigli=numFigli+1;
while (numFigli<=numFigliTot)
p=rand(); % Probability to choose one genetic operation
if p>=0 && p<pRep
% disp('riprod')
% Reproduction
% Choose the parents with the wheel of fortune
gen=rand();
k=1;
r=0;
while(1)
r=r+rank(k,1);
if gen<=r;
break
else
k=k+1;
end
end
newGen(numFigli,:)=indOrd(k,:);
numFigli=numFigli+1;
else if p>=pRep && p<(pRec+pRep)
% Ricombination
% disp('ricomb')
minimo=low-1;
mass=up+1;
while(minimo<low || mass>up)
for h=1:2
gen=rand();
k=1;
r=0;
while(1)
r=r+rank(k,1);
if gen<=r;
break
else
k=k+1;
end
end
recomb(h,1)=k;
end
alpha=normrnd(0.8,0.5); % Calculates the alpha coefficient with a gaussian distribution with average = 0.8 and sigma = 0.5
newI(1,:)=alpha.*indOrd(recomb(1,1),:)+(1-alpha).*indOrd(recomb(2,1),:);
newI(2,:)=alpha.*indOrd(recomb(2,1),:)+(1-alpha).*indOrd(recomb(1,1),:);
minimo=min(min(newI(1,:)),min(newI(2,:)));
mass=max(max(newI(1,:)),max(newI(2,:)));
end
newGen(numFigli,:)=newI(1,:);
newGen(numFigli+1,:)=newI(2,:);
numFigli=numFigli+2;
else if p>=(pRec+pRep) && p<(pRec+pRep+pM)
% Mutation
% disp('mutaz')
minimo=low-1;
mass=up+1;
while(minimo<low || mass>up)
gen=rand();
k=1;
r=0;
while(1)
r=r+rank(k,1);
if gen<=r;
break
else
k=k+1;
end
end
newI=indOrd(k,:)+normrnd(0,0.8,1,numVar); %Add to genotype random values ??created by considering a Gaussian distribution with zero mean and variance 0.8
minimo=min(newI);
mass=max(newI);
end
newGen(numFigli,:)=newI(1,:);
numFigli=numFigli+1;
else if p>=(pRec+pRep+pM) && p<(pRec+pRep+pM+pI)
% Immigration
% disp('imm')
for l=1:50
newGen(numFigli,:)=low+(up-low).*rand(1, numVar);
numFigli=numFigli+1;
end
end
end
end
end
end
%Calculation of the best individuals that will be part of the new population
indTot=[ind;newGen];
for l=1:size(indTot,1)
valFtot(l,1)=fOb(indTot(l,:));
end
[valFnewOrd,IndNew]=sort(valFtot,1,'ascend');
indOrdNew=indTot(IndNew,:);
% disp('new gen')
ind=indOrdNew(1:numInd,:);
iter=iter+1;
%stop=abs(f(1)-f(numInd));
stop=valFnewOrd(1);
end
f=valFnewOrd(1:numInd,:);
p_min=ind;
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 8월 4일
What is the error message, and what arguments are you passing to the code when you invoke it?
jixie zhuangbei
jixie zhuangbei 2015년 8월 30일
Thanksyou

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by