class Algorithms::GeneticAlgorithm::PermutationSolution

Public Class Methods

new(array) click to toggle source
# File lib/algorithms/genetic_algorithm/solutions/permutation.rb, line 4
def initialize(array)
  @data = array
end

Public Instance Methods

crossover(other_solution) click to toggle source
# File lib/algorithms/genetic_algorithm/solutions/permutation.rb, line 12
def crossover(other_solution)
  sampled_indices = @data.each_index.to_a.sample(@data.size / 2)
  sampled_elements = sampled_indices.map { |i| @data[i] }
  elements_from_other = other_solution.data.select { |el| sampled_elements.include?(el) }
  new_data = @data.dup
  sampled_indices.zip(elements_from_other).each do |idx, el|
    new_data[idx] = el
  end
  create_new_solution(new_data)
end
mutate() click to toggle source
# File lib/algorithms/genetic_algorithm/solutions/permutation.rb, line 8
def mutate
  create_new_solution(@data.shuffle)
end