% SpeedyGA is a vectorized implementation of a Simple Genetic Algorithm in Matlab % version 1.2.1 % Copyright (C) 2007,2008 Keki Burjorjee % 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 % % http://www.apache.org/licenses/LICENSE-2.0 % % 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. % Created and tested under Matlab 7 (R14). % If a parameter is passed to the speedyGA function, it is used to set the % seed of the random number generator. Otherwise, the seed is made to be % dependent upon the system clock. % Sample usage: speedyGA(), speedyGA(27463) function speedyGA(varargin) len=640 % The length of bitstrings popSize=500 % The size of the population maxGens=500 % The maximum number of generations allowed for a run probCrossover=1 % The probability of crossing over. % (Note: 1-probCrossover is the probability of % reproduction without crossover) probMutation=1/popSize % 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 crossoverType=1 % 0 => no crossover % 1 => 1pt crossover % 2 => uniform crossover visualizationFlag=1 % 0 => don't visualize bit frequencies % 1 => visualize bit frequencies makeVideoFlag=1 aviFileName=['speedyGArun_crossoverType=' num2str(crossoverType)]; %pregenerate crossover and mutation masks maskReposFactor=5; crossmasks=[]; if crossoverType==0 crossmasks=false(popSize,len); elseif crossoverType==2 crossmasks=rand(popSize,(len+1)*maskReposFactor)<0.5; end mutmasks=rand(popSize,(len+1)*maskReposFactor)1; SUSMarkers(gt1)=SUSMarkers(gt1)-1; [temp newPopIndices]=histc(SUSMarkers,[0 cumNormFitnessVals]); pop=pop(newPopIndices,:); %%%%%%%%%%%%%%% Perform uniform recombination and reproduction % generate pairs of mating indices matingIndices=ceil(rand(popSize,2)*popSize); % create a new population using the first mate from each mating pair newPop=pop(matingIndices(:,1),:); % create a new population using the second mate from each mating pair secondMate(:,:)=pop(matingIndices(:,2),:); % create crossover masks for the entire population if crossoverType==0 masks=crossmasks; elseif crossoverType==1 masks=zeros(popSize, len); temp=ceil(rand(popSize,1)*(len-1)); for i=1:popSize masks(i,1:temp(i))=1; end else temp=floor(rand*len*(maskReposFactor-1)); masks=crossmasks(:,temp+1:temp+len); end % generate an array of booleans indicating which first mates to leave % untouched reprodIndices=rand(popSize,1)<1-probCrossover; masks(reprodIndices,:)=false; % implement uniform recombination with reproduction newPop(logical(masks))=secondMate(logical(masks)); % implement mutation temp=floor(rand*len*(maskReposFactor-1)); masks=mutmasks(:,temp+1:temp+len); pop=xor(newPop,masks); end figure(2) hold off plot([0:maxGens],avgFitnessHist,'k-'); hold on plot([0:maxGens],maxFitnessHist,'c-'); title('Average and Maximum Fitness Per Generation') xlabel('Generations') ylabel('Fitness') if makeVideoFlag==1 aviObj=close(aviObj) end % onemax function fitness=oneMax(pop) fitness=sum(pop,2)'; % The royal roads function. The chromosome length (i.e. len) % should be a multiple of 8 function fitness=royalRoads(pop) [popSize len]=size(pop); fitness=zeros(popSize,1); for i=1:8:len temp=sum(pop(:,i:i+7),2); temp=double(temp==8); fitness=fitness+temp; end fitness=fitness';