function [pop] = mutate(pop,mu,nElite) % MUTATE - Implements mutation on the genetic population % % Inputs: % pop % mu % nElite = number of "elite" chromosomes (population members we will not mutate) % % Outputs: % % Written by: % -- % John L. Weatherwax 2006-08-28 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- [N_pop,N_var] = size(pop); if nargin<3 nElite = 1; else nElite = min(nElite,N_pop-1); % we mutate at least *one* chromosome end num_of_mutations = ceil( mu * ( N_pop - nElite ) * N_var ); % -nElite => we don't mutate nElite of our best chromosome mrow = ceil( rand(1,num_of_mutations)*( N_pop - nElite ) ) + nElite; ncol = ceil( rand(1,num_of_mutations)*N_var ); pop(mrow,ncol) = rand(num_of_mutations,num_of_mutations); % replace with a new random variable