Module: EvoSynth::Decoder

Defined in:
lib/evosynth/decoder.rb,
lib/evosynth/decoder/gray.rb,
lib/evosynth/decoder/binary_to_real.rb

Overview

This module contains all genome decoders (genotype <-> phenotype conversion).

Class Method Summary collapse

Class Method Details

.binary_to_gray(binary) ⇒ Object

Converts a standard binary bitstring (either 1/0 or true/false) into a gray code bitstring.



42
43
44
45
46
47
48
49
50
# File 'lib/evosynth/decoder/gray.rb', line 42

def Decoder.binary_to_gray(binary)
  gray = Array.new(binary)

  popped = gray.pop
  gray.unshift(popped ^ popped)
  gray.each_index { |index| gray[index] = binary[index] ^ gray[index]}

  gray
end

.binary_to_real(binary, min_bound = -1.0,, max_bound = 1.0) ⇒ Object

:call-seq: Decoder.binary_to_real() -> Float (between -1.0 and 1.0) Decoder.binary_to_real(, 0.0) -> Float (between 0.0 and 1.0) Decoder.binary_to_real(, -5.12, 5.12) -> Float (between -5.12 and 5.12)

This converts a given array of binary values (either 1/0 or true/false) into a Float in a given range.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/evosynth/decoder/binary_to_real.rb', line 36

def Decoder.binary_to_real(binary, min_bound = -1.0, max_bound = 1.0)
  binary = binary.map do |bin|
    case bin
      when false then 0
      when true then 1
      else bin
    end
  end

  max_bound, min_bound = min_bound, max_bound if min_bound > max_bound

  range_size = max_bound.to_f - min_bound.to_f
  integer = Integer("0b#{binary.join}")
  max_integer = Integer("0b" + ("1" * binary.size))

  float = integer * (range_size / max_integer) + min_bound;
  float
end

.gray_to_binary(gray) ⇒ Object

Converts a binary gray code bitstring (either 1/0 or true/false) into a standard binary bitstring.



30
31
32
33
34
35
36
37
38
# File 'lib/evosynth/decoder/gray.rb', line 30

def Decoder.gray_to_binary(gray)
  rev_stdbin = Array.new(gray.size)
  gray.reverse!

  rev_stdbin[gray.size - 1] = gray[gray.size - 1]
  (gray.size - 2).downto(0) { |index| rev_stdbin[index] = rev_stdbin[index + 1] ^ gray[index] }

  rev_stdbin.reverse
end