<commit_before>#    Copyright (C) 2009, 2010 <NAME> <<EMAIL>>
#
#    This file is part of EvoSynth, a framework for rapid prototyping of
#    evolutionary and genetic algorithms.
#
#    EvoSynth is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    EvoSynth is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with EvoSynth.  If not, see <http://www.gnu.org/licenses/>.


module EvoSynth
	module Recombinations

		# EIN-PUNKT-CROSSOVER (Weicker Page 84)
		# FIXME: refactor, improve, test me!

		class OnePointCrossover

			def recombine(individual_one, individual_two)
				child_one = individual_one.deep_clone
				child_two = individual_two.deep_clone
				shorter = EvoSynth::Recombinations.individual_with_shorter_genome(individual_one, individual_two)

				crossover_point = rand(shorter.genome.size)

				crossover_point.times do |index|
					child_one.genome[index] = individual_one.genome[index]
					child_two.genome[index] = individual_two.genome[index]
				end

				crossover_point.upto(shorter.genome.size - 1) do |index|
					child_one.genome[index] = individual_two.genome[index]
					child_two.genome[index] = individual_one.genome[index]
				end

				[child_one, child_two]
			end

			def to_s
				"1-point crossover"
			end

		end

	end
end<commit_msg>improved performance a lot by using ranges instead of loops<commit_after>#    Copyright (C) 2009, 2010 <NAME> <<EMAIL>>
#
#    This file is part of EvoSynth, a framework for rapid prototyping of
#    evolutionary and genetic algorithms.
#
#    EvoSynth is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    EvoSynth is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with EvoSynth.  If not, see <http://www.gnu.org/licenses/>.


module EvoSynth
	module Recombinations

		# EIN-PUNKT-CROSSOVER (Weicker Page 84)

		class OnePointCrossover

			def recombine(parent_one, parent_two)
				child_one = parent_one.deep_clone
				child_two = parent_two.deep_clone

				shorter = EvoSynth::Recombinations.individual_with_shorter_genome(parent_one, parent_two)
				crossover_point = rand(shorter.genome.size)

				first_range = 0..crossover_point
				child_one.genome[first_range] =  parent_one.genome[first_range]
				child_two.genome[first_range] =  parent_two.genome[first_range]

				second_range = (crossover_point + 1)..(shorter.genome.size - 1)
				child_one.genome[second_range] = parent_two.genome[second_range]
				child_two.genome[second_range] = parent_one.genome[second_range]

				[child_one, child_two]
			end

			def to_s
				"one-point crossover"
			end

		end

	end
end
{'fraction_non_alphanumeric': 0.07228229937393284, 'fraction_numerical': 0.007683551508252703, 'mean_word_length': 3.7628726287262872, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4262437', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1061, 'n_words': 354}