dino_game_v2.rb
# Perform crossover process, mate the two fittest individuals

Perform crossover process, mate the two fittest individuals
Perform crossover process mate the two fittest individuals
def crossover(dinos, parents, least_fit_of_pop)
	fittest_parent_chromosome = dinos[parents[0]].chromosome
	second_fittest_parent_chromosome = dinos[parents[1]].chromosome
	slice_point = rand(0...4.0).floor 
	first_child_chromosome = Array.new
	second_child_chromosome = Array.new
	i = 0
	while i < 4
		if i < slice_point
			first_child_chromosome << fittest_parent_chromosome[i]
			second_child_chromosome << second_fittest_parent_chromosome[i]
		else
			first_child_chromosome << second_fittest_parent_chromosome[i]
			second_child_chromosome << fittest_parent_chromosome[i]
		end
		i += 1
	end
	dinos[least_fit_of_pop[0]].chromosome = first_child_chromosome
	dinos[least_fit_of_pop[1]].chromosome = second_child_chromosome
	return dinos, least_fit_of_pop[0], least_fit_of_pop[1]
end
def crossover ( dinos , parents , least_fit_of_pop ) fittest_parent_chromosome = dinos [ parents [ 0 ] ] . chromosome second_fittest_parent_chromosome = dinos [ parents [ 1 ] ] . chromosome slice_point = rand ( 0 ... 4.0 ) . floor first_child_chromosome = Array . new second_child_chromosome = Array . new i = 0 while i < 4 if i < slice_point first_child_chromosome << fittest_parent_chromosome [ i ] second_child_chromosome << second_fittest_parent_chromosome [ i ] else first_child_chromosome << second_fittest_parent_chromosome [ i ] second_child_chromosome << fittest_parent_chromosome [ i ] end i += 1 end dinos [ least_fit_of_pop [ 0 ] ] . chromosome = first_child_chromosome dinos [ least_fit_of_pop [ 1 ] ] . chromosome = second_child_chromosome return dinos , least_fit_of_pop [ 0 ] , least_fit_of_pop [ 1 ] end
Perform crossover process, mate the two fittest individuals
Perform crossover process mate the two fittest individuals
# Also known as the cross over point for genetic algorithm
 # Now we perform crossover operation on the children
 # Replace the two least fit individuals with the two new offspring

dinos
<not_specific>
parents
<not_specific>
least_fit_of_pop
<not_specific>