% % 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; randn('seed',0); rand('seed',0); addpath('../Chapter1'); % Parameters used in the genetic algorithm % N_pop = 300; % the population size N_gene = 100; % the discreteness of the floating point representations X_rate = 0.5; % selection rate mu = 0.02; % 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 % crossover_method = 1; % 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 [pop,pop_costs,best_fn_values,avg_fn_values] = binary_GA( wfn, bounds, ... N_pop, N_gene, X_rate, mu, max_number_of_iterations, parent_selection_method, crossover_method ); disp('top 5 solution x values'); pop(1:5,:) disp('top 5 f(x) values'); pop_costs(1:5,:) figure; bh = plot( 1:length(best_fn_values), best_fn_values, '-b' ); hold on; ah = plot( 1:length(avg_fn_values), avg_fn_values, '-r' ); legend( [bh,ah], {'best f(x)','average f(x)'} ); xlabel('iteration number'); %saveas(gcf,'../../WriteUp/Graphics/Chapter2/ga_convergence.eps','epsc');