% % 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'); addpath('../Chapter3'); % Parameters used in the genetic algorithm % N_pop = 16; % the population size X_rate = 0.5; % selection rate mu = 0.2; % the mutation rate max_number_of_iterations = 20; % 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 % 4=> blending method (3.9) % 5=> blending method (3.13) or the books suggested method % crossover_method = 5; % a function to minimize (and its bounds) % funnum = 7; bounds = [ 0, +10 ; 0, +10 ]; % the functions domain funnum = 8; bounds = [ -10, +10 ; -10, +10 ]; % the functions domain %funnum = 11; %bounds = [ -1, +1 ; -1, +1 ]; % the functions domain %funnum = 16; %bounds = [ -20, +20 ; -20, +20 ]; % the functions domain wfn = @(x) testfunction(x,funnum); % our function handle can evaluate an entire population N_mc = 100; study_avg = 0; % look at average or best % The original (non hybrid method): % fn_values = zeros(N_mc,max_number_of_iterations); for ii = 1:N_mc randn('seed',ii); rand('seed',ii); [pop,pop_costs,best_fn_values,avg_fn_values] = continuous_GA( wfn, bounds, ... N_pop, X_rate, mu, max_number_of_iterations, parent_selection_method, crossover_method, 0 ); if( study_avg==1 ) fn_values(ii,:) = avg_fn_values; else fn_values(ii,:) = best_fn_values; end end m_fn_values_NH = mean( fn_values, 1 ); % NH = not hybrid s_fn_values_NH = std( fn_values, 1 ); ph_nh = plot( m_fn_values_NH, '-o' ); hold on; plot( m_fn_values_NH+2*s_fn_values_NH, '-' ); plot( m_fn_values_NH-2*s_fn_values_NH, '-' ); % The HYBRID genetic algorithm: % fn_values = zeros(N_mc,max_number_of_iterations); for ii = 1:N_mc randn('seed',ii); rand('seed',ii); [pop,pop_costs,best_fn_values,avg_fn_values] = continuous_GA( wfn, bounds, ... N_pop, X_rate, mu, max_number_of_iterations, parent_selection_method, crossover_method, 1 ); if( study_avg==1 ) fn_values(ii,:) = avg_fn_values; else fn_values(ii,:) = best_fn_values; end end m_fn_values_H = mean( fn_values, 1 ); % H = hybrid s_fn_values_H = std( fn_values, 1 ); ph_h = plot( m_fn_values_H, '-ko' ); plot( m_fn_values_H+2*s_fn_values_H, '-k' ); plot( m_fn_values_H-2*s_fn_values_H, '-k' ); legend( [ph_nh,ph_h], {'nonhybrid GA','hybrid GA'} ); xlabel('iteration number'); ylabel('population average cost'); %saveas( gcf, '../../WriteUp/Graphics/Chapter5/hybrid_F8_convergence.eps', 'epsc' );