Using additionally a mutation operator every cycle. The mutation is applied to the strings which have already received a crossover operation.
//************************************************************* // All Functions Unified // // p := number of parameters between strings left and right (actually p=5) // l := length of strings ( actually l=5) // n := number of elements in POP (actually n=4) // POP := a predefined population (can be done automatically) // run := number of cycles // MThreshold := number of cycles which have to be waited until the mutation operators will be applied function[POP,FITNESS_ALLLOG]=gasimple(POP,l,p,n,run, MThreshold) MutationCount = 0 FITNESS_ALLLOG=[] for cyc = 1:run [M]= mshow(POP,n,l+p); for j=1:n, v=POP(j,:), [POP(j,l+1)]= vec2dec(v,l) end for i=1:n,[POP(i,l+2)]= fitness1(POP(i,l+1)) end [FITNESS_ALL]=fitness(POP,l) FITNESS_ALLLOG(cyc)=FITNESS_ALL [MFITNESS]=maxfitness(POP,l) [POP,AFITNESS]=rfitness(POP,l,FITNESS_ALL) [POP]=newMember(POP,l,n) [POP]=newPop(POP,l,p,n) [POP]= crossoverPrep(POP,l,p,n) [POP]= crossover(POP,l,p,n) printf('Mutationcount = %d\n', MutationCount) if(MutationCount > MThreshold) then [POP]= mutation(POP,l,p,n), MutationCount=0, end MutationCount = MutationCount + 1 end //for == run // Compute the maximal value and the percentage of success [MAXFIT]=maxfit02(l,n) FITNESS_ALL_PERC=[] for i=1:run, FITNESS_ALL_PERC(i) = FITNESS_ALLLOG(i)/(MAXFIT/100), end // Show graphical results clf(), xdel, //plot2d([1:1:run], FITNESS_ALLLOG) plot2d([1:1:run], FITNESS_ALL_PERC) endfunction