Code covered by the BSD License

# Genetic Packman

### Hanan Kavitz (view profile)

A simple demonstration of Genetic Algorithm using all times favorite game.

### Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

mutate_permutation(parents ,options,NVARS, ...
```function mutationChildren = mutate_permutation(parents ,options,NVARS, ...
FitnessFcn, state, thisScore,thisPopulation,mutationRate)
%   MUTATE_PERMUTATION Custom mutation function for traveling salesman.
%   MUTATIONCHILDREN = MUTATE_PERMUTATION(PARENTS,OPTIONS,NVARS, ...
%   FITNESSFCN,STATE,THISSCORE,THISPOPULATION,MUTATIONRATE) mutate the
%   PARENTS to produce mutated children MUTATIONCHILDREN.
%
%   The arguments to the function are
%     PARENTS: Parents chosen by the selection function
%     OPTIONS: Options structure created from GAOPTIMSET
%     NVARS: Number of variables
%     FITNESSFCN: Fitness function
%     STATE: State structure used by the GA solver
%     THISSCORE: Vector of scores of the current population
%     THISPOPULATION: Matrix of individuals in the current population
%     MUTATIONRATE: Rate of mutation

%   Copyright 2004 The MathWorks, Inc.
%   \$Revision: 1.1.6.1 \$  \$Date: 2009/08/29 08:28:05 \$

% Here we swap two elements of the permutation
mutationChildren = zeros(length(parents),NVARS);
if nargin<8
mutationNum=2;
else
mutationNum=nnz(rand(1,NVARS)<mutationRate)+1;
end

for i=1:length(parents)
parent = thisPopulation(parents(i),:);
p = ceil(length(parent) * rand(1,mutationNum));
mutationChildren(i,:)=parent;
p=[p p(1)];
for j=1:length(p)-1
mutationChildren(i,p(j+1)) = parent(p(j));
end

end
```

Contact us