Class: PermutationCheckStrategy

Inherits:
CheckStrategy show all
Defined in:
lib/gimuby/genetic/solution/check_strategy/permutation_check_strategy.rb

Overview

Permutation goes represented as permutation from [0, … l] indexes

Instance Method Summary collapse

Instance Method Details

#check(solution_representation) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gimuby/genetic/solution/check_strategy/permutation_check_strategy.rb', line 7

def check(solution_representation)
  permutation = solution_representation
  expected_elements = *(0..permutation.length - 1)
  duplicate = []
  missing = []
  expected_elements.each do |element|
    match = permutation.select do |concreteElement|
      concreteElement == element
    end
    case match.length <=> 1
      when -1 then
        missing.push(element)
      when 1 then
        duplicate.push(element)
      else
        # do nothing
    end
  end
  missing.shuffle!
  duplicate.each do |to_remove|
    to_insert = missing.pop()
    ind = permutation.index(to_remove)
    permutation[ind] = to_insert
  end
  unless missing.empty?
    solution_representation = check(solution_representation)
  end
  solution_representation
end