function [children] = crossover(pop_keep, ma,pa, N_total_children, method, wfn, bounds) % CROSSOVER - Implements a couple of crossover algorithms % % Inputs: % pop_keep the population of most fit individuals % ma = indices into pop_keep for the "mother" chromosome % pa = indices into pop_keep for the "father" chromosome % each pairing produced two children, we repeatidly cycle through % the parents performing crossover until we have enough children % N_total_children = number of total children that we must generate % method = method used to perform crossover % (the first three methods are the same as thoes described in the chapter on binary genetic algorithms) % % 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_keep,N_vars] = size(pop_keep); nPairs = length(ma); children = zeros(N_total_children,N_vars); % initialize storage switch method case 1, % single point crossover crossover_points = ceil( (N_vars-1) * rand(1,N_total_children) ); child_number = 1; while( child_number 1.; children(child_number,inds) = 1.; child_number = child_number + 1; if( child_number<=N_total_children ) children(child_number,:) = [ (1.-beta)*father(indsL), beta*mother(indsR) ]; inds = children(child_number,:) < 0.; children(child_number,inds) = 0.; % respect variable boundaries inds = children(child_number,:) > 1.; children(child_number,inds) = 1.; child_number = child_number + 1; end end case 5, % blending method (3.13) or the books suggested method: we blend two variables at a specific point and cross all other variables alpha_locations = ceil( N_vars * rand(1,N_total_children) ); child_number = 1; while( child_number 1.0 ) nv = 1.0; end children(child_number,:) = [ mother(indsL), nv, father(indsR) ]; child_number = child_number + 1; if( child_number<=N_total_children ) nv = mother(indsC) + beta*(mother(indsC)-father(indsC)); if( nv < 0.0 ) nv = 0.0; end % restrict boundaries if( nv > 1.0 ) nv = 1.0; end children(child_number,:) = [ father(indsL), nv, mother(indsR) ]; child_number = child_number + 1; end end otherwise error('unknown value for method'); end