Module: Evolvable::Gene

Included in:
CountGene, RigidCountGene
Defined in:
lib/evolvable/gene.rb

Overview

For evolution to be effective, an evolvable's genes must be able to influence its behavior. Evolvables are composed of genes that can be used to run simple functions or orchestrate complex interactions. The level of abstraction is up to you.

Defining gene classes requires encapsulating some "sample space" and returning a sample outcome when a gene attribute is accessed. For evolution to proceed in a non-random way, the same sample outcome should be returned every time a particular gene is accessed with a particular set of parameters. Memoization is a useful technique for doing just this. The memo_wise gem may be useful for more complex memoizations.

Examples:

# This gene generates a random hexidecimal color code for use by evolvables.

require 'securerandom'

class ColorGene
  include Evolvable::Gene

  def hex_code
    @hex_code ||= SecureRandom.hex(3)
  end
end

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#evolvableObject

Returns the value of attribute evolvable.



59
60
61
# File 'lib/evolvable/gene.rb', line 59

def evolvable
  @evolvable
end

Class Method Details

.included(base) ⇒ Object



34
35
36
# File 'lib/evolvable/gene.rb', line 34

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#keyObject



61
62
63
# File 'lib/evolvable/gene.rb', line 61

def key
  self.class.key
end