Class: Evopop::Dna

Inherits:
Object
  • Object
show all
Defined in:
lib/evopop/dna.rb

Overview

Represents a Dna structure, like an array of floating point values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_range, max_range, min_mutation, max_mutation, dna_len) ⇒ Dna

Returns a new instance of Dna.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/evopop/dna.rb', line 8

def initialize(min_range, max_range, min_mutation, max_mutation, dna_len)
  # TODO: Extract these to a DnaProperties class or something so we don't
  # have to couple parameter passsing so much.
  @min_range = min_range
  @max_range = max_range
  @min_mutation = min_mutation
  @max_mutation = max_mutation
  @dna = []
  @dna_len = dna_len

  dna_len_range.each do
    @dna << random_dna_val
  end
end

Instance Attribute Details

#dnaObject

Returns the value of attribute dna.



6
7
8
# File 'lib/evopop/dna.rb', line 6

def dna
  @dna
end

#dna_lenObject

Returns the value of attribute dna_len.



6
7
8
# File 'lib/evopop/dna.rb', line 6

def dna_len
  @dna_len
end

#max_mutationObject

Returns the value of attribute max_mutation.



6
7
8
# File 'lib/evopop/dna.rb', line 6

def max_mutation
  @max_mutation
end

#max_rangeObject

Returns the value of attribute max_range.



6
7
8
# File 'lib/evopop/dna.rb', line 6

def max_range
  @max_range
end

#min_mutationObject

Returns the value of attribute min_mutation.



6
7
8
# File 'lib/evopop/dna.rb', line 6

def min_mutation
  @min_mutation
end

#min_rangeObject

Returns the value of attribute min_range.



6
7
8
# File 'lib/evopop/dna.rb', line 6

def min_range
  @min_range
end

Class Method Details

.create(min_range, max_range, min_mutation, max_mutation, dna) ⇒ Object



23
24
25
26
27
# File 'lib/evopop/dna.rb', line 23

def self.create(min_range, max_range, min_mutation, max_mutation, dna)
  new_dna = new(min_range, max_range, min_mutation, max_mutation, dna.size)
  new_dna.dna = dna
  new_dna
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
# File 'lib/evopop/dna.rb', line 65

def ==(other)
  @dna == other.dna
end

#[](key) ⇒ Object



57
58
59
# File 'lib/evopop/dna.rb', line 57

def [](key)
  @dna[key]
end

#[]=(key, value) ⇒ Object



61
62
63
# File 'lib/evopop/dna.rb', line 61

def []=(key, value)
  @dna[key] = value
end

#dna_len_rangeObject



29
30
31
# File 'lib/evopop/dna.rb', line 29

def dna_len_range
  0...@dna_len
end

#drop(ordinal) ⇒ Object



53
54
55
# File 'lib/evopop/dna.rb', line 53

def drop(ordinal)
  @dna.drop(ordinal)
end

#lengthObject



45
46
47
# File 'lib/evopop/dna.rb', line 45

def length
  @dna.length
end

#mutate(i) ⇒ Object



41
42
43
# File 'lib/evopop/dna.rb', line 41

def mutate(i)
  @dna[i] += [random_mutation_val, -1 * random_mutation_val].sample
end

#random_dna_valObject



33
34
35
# File 'lib/evopop/dna.rb', line 33

def random_dna_val
  Random.rand(@min_range...@max_range)
end

#random_mutation_valObject



37
38
39
# File 'lib/evopop/dna.rb', line 37

def random_mutation_val
  Random.rand(@min_mutation...@max_mutation)
end

#take(num) ⇒ Object



49
50
51
# File 'lib/evopop/dna.rb', line 49

def take(num)
  @dna.take(num)
end

#to_sObject



69
70
71
# File 'lib/evopop/dna.rb', line 69

def to_s
  @dna.to_s
end