% % 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. % %----- clear; close all; %clc; addpath('../Chapter1'); % Parameters used in the genetic algorithm % N_gene = 50; % the discreteness of the floating point representations X_rate = 0.5; % selection rate max_number_of_iterations = 5; % method to select mates: % 1=> pairing from top to bottom % 2=> random pairing % 3=> random pairing (rank weighting) % 4=> weighted random pairing (cost weighting) % 5=> tournament selection % parent_selection_method = 3; % method used to do crossover: % 1=> single point crossover % 2=> double point crossover % 3=> uniform crossover % crossover_method = 1; % a function to minimize (and its bounds) % funnum = 8; bounds = [ -10, +10 ; -10, +10 ]; % the functions domain wfn = @(x) testfunction(x,funnum); % our function handle can evaluate an entire population % % STUDY THE CHANGE IN THE EFFECT OF MU: % N_pop = 50; % a very small the population size but fixed nMCSamples = 100; % the number of monte carlo samples % Consider various values of mu muArray = linspace( 0.01, 0.5, 20 ); avg_min_fn = zeros(1,length(muArray)); std_min_fn = zeros(1,length(muArray)); for mui = 1:length(muArray) fprintf('working on %6d monte carlo runs for mu= %6.4f\n',nMCSamples,muArray(mui)); f_min_sample = zeros(1,nMCSamples); for ii=1:nMCSamples, % a random draw [pop,pop_costs,best_fn_values,avg_fn_values] = binary_GA( wfn, bounds, N_pop, N_gene, X_rate, muArray(mui), max_number_of_iterations, parent_selection_method, crossover_method ); f_min_sample(ii) = best_fn_values( max_number_of_iterations ); end % compute statistics over this minium: avg_min_fn(mui) = mean( f_min_sample ); std_min_fn(mui) = std( f_min_sample ); end figure; av = plot( muArray, avg_min_fn, '-rx' ); hold on; avm = plot( muArray, avg_min_fn-std_min_fn, '-g' ); avp = plot( muArray, avg_min_fn+std_min_fn, '-g' ); legend( [av,avp,avm], {'average','lower 1 sigma CI','upper 1 sigma CI'}, 'location', 'best' ); xlabel('value of mu'); ylabel('average of the smallest function value'); %saveas(gcf,'../../WriteUp/Graphics/Chapter2/variations_in_mu_MC.eps','epsc'); % % STUDY THE CHANGE IN THE EFFECT OF N_pop: % mu = 0.02; % the fixed mutation rate N_pop = 50; % a very small the population size nMCSamples = 100; % the number of monte carlo samples % Consider various values of N_pop NArray = 50:50:1000; avg_min_fn = zeros(1,length(NArray)); std_min_fn = zeros(1,length(NArray)); for ni = 1:length(NArray) fprintf('working on %6d monte carlo runs for N_pop= %6d\n',nMCSamples,NArray(ni)); f_min_sample = zeros(1,nMCSamples); for ii=1:nMCSamples, % a random draw [pop,pop_costs,best_fn_values,avg_fn_values] = binary_GA( wfn, bounds, NArray(ni), N_gene, X_rate, mu, max_number_of_iterations, parent_selection_method, crossover_method ); f_min_sample(ii) = best_fn_values( max_number_of_iterations ); end % compute statistics over this minium: avg_min_fn(ni) = mean( f_min_sample ); std_min_fn(ni) = std( f_min_sample ); end figure; av = plot( NArray, avg_min_fn, '-rx' ); hold on; avm = plot( NArray, avg_min_fn-std_min_fn, '-g' ); avp = plot( NArray, avg_min_fn+std_min_fn, '-g' ); legend( [av,avp,avm], {'average','lower 1 sigma CI','upper 1 sigma CI'}, 'location', 'best' ); xlabel('value of N_{pop}'); ylabel('average of the smallest function value'); %saveas(gcf,'../../WriteUp/Graphics/Chapter2/variations_in_N_pop.eps','epsc');