Class: Ai::Neat::Creature

Inherits:
Object
  • Object
show all
Defined in:
lib/ai/neat/creature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models) ⇒ Creature

Returns a new instance of Creature.



8
9
10
11
12
# File 'lib/ai/neat/creature.rb', line 8

def initialize(models)
  @network = Network.new(models)
  @fitness = 0
  @score = 0
end

Instance Attribute Details

#fitnessObject

Returns the value of attribute fitness.



6
7
8
# File 'lib/ai/neat/creature.rb', line 6

def fitness
  @fitness
end

#networkObject

Returns the value of attribute network.



6
7
8
# File 'lib/ai/neat/creature.rb', line 6

def network
  @network
end

#scoreObject

Returns the value of attribute score.



6
7
8
# File 'lib/ai/neat/creature.rb', line 6

def score
  @score
end

Instance Method Details

#decisionObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ai/neat/creature.rb', line 14

def decision
  index = -1
  max = -Float::INFINITY

  (0..(@network.layers.last.nodes.count - 1)).each do |i|
    if @network.layers.last.nodes[i].value > max
      max = @network.layers.last.nodes[i].value
      index = i
    end
  end

  index
end

#feed_forwardObject



28
29
30
# File 'lib/ai/neat/creature.rb', line 28

def feed_forward
  @network.feed_forward
end

#flatten_genesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ai/neat/creature.rb', line 32

def flatten_genes
  genes = []

  (0..(@network.layers.count - 2)).each do |i|
    @network.layers[i].nodes.each do |node|
      node.weights.each do |weight|
        genes.push(weight)
      end
    end

    @network.layers[i].bias.weights.each do |weight|
      genes.push(weight)
    end
  end

  genes
end

#flatten_genes=(genes) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ai/neat/creature.rb', line 50

def flatten_genes=(genes)
  (0..(@network.layers.count - 2)).each do |i|
    (0..@network.layers[i].nodes.count - 1).each do |w|
      (0..@network.layers[i].nodes[w].weights.count - 1).each do |e|
        @network.layers[i].nodes[w].weights[e] = genes.first
        genes.shift
      end
    end
    
    (0..@network.layers[i].bias.weights.count - 1).each do |w|
      @network.layers[i].bias.weights[w] = genes.first
      genes.shift
    end
  end
end

#inputsObject



66
67
68
# File 'lib/ai/neat/creature.rb', line 66

def inputs
  @network.layers.first.values
end

#inputs=(values) ⇒ Object



70
71
72
# File 'lib/ai/neat/creature.rb', line 70

def inputs=(values)
  @network.layers.first.values = values
end