Class: NEAT::Critter

Inherits:
NeatOb show all
Defined in:
lib/rubyneat/critter.rb,
lib/rubyneat/reporting.rb

Overview

Critter Reporting

The reporting functionality for critters are represented here, since this is only tangenial to the actual functionality of the critters themselves.

Defined Under Namespace

Classes: Genotype, Phenotype

Instance Attribute Summary collapse

Attributes inherited from NeatOb

#controller, #name

Instance Method Summary collapse

Methods inherited from NeatOb

attr_neat, log, #log, #to_s

Constructor Details

#initialize(pop, mating = false, &block) ⇒ Critter

Critter construction. We construct the genotype. The phenotype will be constructed by the Expressor operator.



20
21
22
23
24
25
# File 'lib/rubyneat/critter.rb', line 20

def initialize(pop, mating = false, &block)
  super pop.controller
  @population = pop
  @genotype = Genotype.new(self, mating)
  block.(self) unless block.nil?
end

Instance Attribute Details

#fitnessObject

Ratings assigned by Evaluator



16
17
18
# File 'lib/rubyneat/critter.rb', line 16

def fitness
  @fitness
end

#genotypeObject

Returns the value of attribute genotype.



13
14
15
# File 'lib/rubyneat/critter.rb', line 13

def genotype
  @genotype
end

#noveltyObject

Ratings assigned by Evaluator



16
17
18
# File 'lib/rubyneat/critter.rb', line 16

def novelty
  @novelty
end

#phenotypeObject

Returns the value of attribute phenotype.



13
14
15
# File 'lib/rubyneat/critter.rb', line 13

def phenotype
  @phenotype
end

#populationObject (readonly)

Returns the value of attribute population.



12
13
14
# File 'lib/rubyneat/critter.rb', line 12

def population
  @population
end

Instance Method Details

#compare(oc) ⇒ Object

Compare ourselves against another critter for compability.

The function to be used here is: distance = c1*E + c2*D + c3*W

Where: E, D - The number of excess and disjoint genes repesctively. N - The number of genes in the largest genome. W - The sum of absolute weight differences.

This is a variation of the formulation suggested by the Stanley paper, which normalizes the E and D terms by N.



333
334
335
336
337
338
339
340
341
# File 'lib/rubyneat/critter.rb', line 333

def compare(oc)
  c1 = @controller.parms.excess_coefficient
  c2 = @controller.parms.disjoint_coefficient
  c3 = @controller.parms.weight_coefficient
  e = excess(oc)
  d = disjoint(oc)
  w = weight_diff(oc)
  return c1 * e + c2 * d + c3 * w
end

#dump_sObject

Critter print



344
345
346
# File 'lib/rubyneat/critter.rb', line 344

def dump_s
  to_s + @genotype.dump_s + "\n" + @phenotype.to_s + "\n"
end

#evaluate!Object

A single evaluation step. Evaluate and generate fitness, novelty, etc. Returns the result.



50
51
52
# File 'lib/rubyneat/critter.rb', line 50

def evaluate!
  @controller.evaluator.evaluate! self
end

#express!Object

Exoress this critter using the Expressor plugin.



36
37
38
# File 'lib/rubyneat/critter.rb', line 36

def express!
  @controller.expressor.express! self
end

#initialize_neurons!Object

This initializes neurons in preparation for recurrence. Note that the Critter should already have expressed its genotype before this is called.



43
44
45
# File 'lib/rubyneat/critter.rb', line 43

def initialize_neurons!
  @phenotype.initialize_neurons
end

#ready_for_expression!Object

Get the Critter ready for the Expressor to express the geneotype.



29
30
31
32
33
# File 'lib/rubyneat/critter.rb', line 29

def ready_for_expression!
  @genotype.wire!
  @phenotype = NEAT::Critter::Phenotype[self]
  @phenotype
end

#reportObject



105
106
107
108
109
110
111
# File 'lib/rubyneat/reporting.rb', line 105

def report
  {
      genotype: report_genotype,
      phenotype: report_phenotype,
      neuron_types: report_neuron_types
  }
end

#report_genotypeObject



97
98
99
# File 'lib/rubyneat/reporting.rb', line 97

def report_genotype
  genotype.genes.map{|innov, gene| {in: gene.in_neuron, out: gene.out_neuron, innov: innov}}
end

#report_neuron_typesObject



89
90
91
92
93
94
95
# File 'lib/rubyneat/reporting.rb', line 89

def report_neuron_types
  {
      input:  population.input_neurons.map {|n| n.name},
      output: population.output_neurons.map{|n| n.name},
      hidden: population.hidden_neurons.map{|n| n.name}
  }
end

#report_phenotypeObject



101
102
103
# File 'lib/rubyneat/reporting.rb', line 101

def report_phenotype
  phenotype.code
end