Module: DSL

Includes:
NEAT, NEAT::BasicNeuronTypes
Defined in:
lib/rubyneat/dsl.rb

Overview

RubyNEAT DSL

DSL is a doman-specific language for RubyNEAT to allow you to configure the NEAT engine for various evolutionary projects.

Constant Summary

Constant Summary

Constants included from NEAT

NEAT::STIMULUS

Instance Method Summary (collapse)

Methods included from NEAT

controller, controller=, create_controller, dpp, gaussian, new_innovation, random_name_generator

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(m, *args, &block)

This is used to handle the details of our DSL.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubyneat/dsl.rb', line 82

def method_missing(m, *args, &block)
  # we want to catch parameters settings here.    
  if NEAT::controller.parms.respond_to? (assignment = (m.to_s + '=').to_sym)
    raise NeatException.new("Missing value(s) to %s" % m) if args.empty?
    val = (args.size == 1) ? args[0] : args
    $log.debug { "Caught method %s with parameter of %s" % [assignment, val] }
    NEAT::controller.parms.send(assignment, val)
  else
    super
  end
end

Instance Method Details

- (Object) condition_boolean_vector(vec)

Helper function to Condition boolean vectors to be +1 if true, -1 if false



57
58
59
# File 'lib/rubyneat/dsl.rb', line 57

def condition_boolean_vector(vec)
  vec.map{|b| b ? 1 : -1}
end

- (Object) define(name = NEAT.random_name_generator, &block)

DSL – Define defines the parameters to the controller.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubyneat/dsl.rb', line 14

def define(name = NEAT.random_name_generator, &block)
  [
   :inputs, 
   :outputs, 
   :hidden  # we really don't care about mapping hidden neurons, but we'll ignore them later.
  ].each do |iometh|
    instance_eval %Q[
 def #{iometh}(nodes = nil, &block)
 neui = unless nodes.nil?
          nodes
        else
          block.()
        end
 NEAT::controller.neural_#{iometh} = if neui.kind_of? Hash
                                       neui
                                     else
                                       Hash[neui.map{|n| [NEAT::random_name_generator, n]}]
                                     end
 end]
  end  
  block.(NEAT::controller)
end

- (Object) evolve(&block)

DSL – Run evolution



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubyneat/dsl.rb', line 38

def evolve(&block)
  # Query function is called with the sequence (time evalution) number,
  # and returns an array or hash of parameters that will be given
  # to the input nodes. In the case of hash, the keys in the hash
  # shall correspond to the names given to the input neurons.
  def query(&block)
    NEAT::controller.query_func = block
  end
  
  # fitness function calls the block with 2 vectors or two hashes, input and output
  # vectors of the critter being evaluated for fitness, as well as a sequence
  # number that can be used to index what the actual output should be.
  # |vin, vout, seq|
  def fitness(&block)
    NEAT::controller.fitness_func = block
  end

  # Helper function to
  # Condition boolean vectors to be +1 if true, -1 if false
  def condition_boolean_vector(vec)
    vec.map{|b| b ? 1 : -1}
  end

  # Helper function to
  # Uncondition boolean vectors to be +1 if true, -1 if false
  def uncondition_boolean_vector(vec)
    vec.map{|o| o > 0.0 ? true : false}
  end


  block.(NEAT::controller)
end

- (Object) fitness(&block)

fitness function calls the block with 2 vectors or two hashes, input and output vectors of the critter being evaluated for fitness, as well as a sequence number that can be used to index what the actual output should be. |vin, vout, seq|



51
52
53
# File 'lib/rubyneat/dsl.rb', line 51

def fitness(&block)
  NEAT::controller.fitness_func = block
end

- (Object) query(&block)

Query function is called with the sequence (time evalution) number, and returns an array or hash of parameters that will be given to the input nodes. In the case of hash, the keys in the hash shall correspond to the names given to the input neurons.



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

def query(&block)
  NEAT::controller.query_func = block
end

- (Object) report(&block)

Report on evaluations



72
73
# File 'lib/rubyneat/dsl.rb', line 72

def report(&block)
end

- (Object) run_engine(&block)

Run the engine. The block is called on each generation.



76
77
78
79
# File 'lib/rubyneat/dsl.rb', line 76

def run_engine(&block)
  NEAT::controller.end_run_func = block
  NEAT::controller.run
end

- (Object) uncondition_boolean_vector(vec)

Helper function to Uncondition boolean vectors to be +1 if true, -1 if false



63
64
65
# File 'lib/rubyneat/dsl.rb', line 63

def uncondition_boolean_vector(vec)
  vec.map{|o| o > 0.0 ? true : false}
end