lib/individual.rb
# Invert gene between the two indices.
# For example, if ary = [0,1,2,3,4,5] and indices are 2 and 4
# then the inversion result is [0,1,4,3,2,5].

Invert gene between the two indices.
Invert gene between the two indices .
def inversion_mutation(gene_ary)
    idx1 = rand(gene_ary.size)
    idx2 = idx1 
    while (idx1 == idx2)
      idx2 = rand(gene_ary.size)
    end
    if idx1 > idx2
      tmp_idx = idx1
      idx1 = idx2
      idx2 = tmp_idx
    end
    tmp = gene_ary.slice!(idx1, (idx2 - idx1 + 1))
    tmp.reverse!
    gene_ary.insert(idx1, tmp).flatten!
    return gene_ary
  end
def inversion_mutation ( gene_ary ) idx1 = rand ( gene_ary . size ) idx2 = idx1 while ( idx1 == idx2 ) idx2 = rand ( gene_ary . size ) end if idx1 > idx2 tmp_idx = idx1 idx1 = idx2 idx2 = tmp_idx end tmp = gene_ary . slice! ( idx1 , ( idx2 - idx1 + 1 ) ) tmp . reverse! gene_ary . insert ( idx1 , tmp ) . flatten! return gene_ary end
Invert gene between the two indices.
Invert gene between the two indices .
gene_ary
<not_specific>