Class: Genotype

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/charlie/genotype.rb

Overview

Base class. Inherit this if you’re starting from scratch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fitness_cacheObject

Used by Genotype.cache_fitness. This accessor can be used to clear the cache. Also could be used by niche selection, etc. as a place to change the effective fitness w/o changing the actual selection algorithms.



60
61
62
# File 'lib/charlie/genotype.rb', line 60

def fitness_cache
  @fitness_cache
end

#genesObject

Returns the value of attribute genes.



6
7
8
# File 'lib/charlie/genotype.rb', line 6

def genes
  @genes
end

Class Method Details

.cache_fitnessObject

Adds caching to the fitness function. Only call AFTER defining your fitness function. Never clears the cache. This should work most of the time because crossovers/from_genes create new instances.



46
47
48
49
50
51
52
# File 'lib/charlie/genotype.rb', line 46

def cache_fitness
  alias_method :real_fitness, :fitness
  define_method(:fitness) {
    @fitness_cache ||= send(:real_fitness)
  }
  self
end

.from_genes(g) ⇒ Object

Creates a new instance of your genotype class given its genes.



38
39
40
41
42
# File 'lib/charlie/genotype.rb', line 38

def from_genes(g)
  r = allocate
  r.genes = g
  r
end

.use(*mods) ⇒ Object

For each module passed: Includes the module if it has a mutate! method, and includes it in the metaclass otherwise. Used to include selection/crossover/mutation modules in one line, or just to avoid all the class<<self;include …;end

class Example < Genotype
  use RandomSelection, NullCrossover, NullMutator
end


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/charlie/genotype.rb', line 25

def use(*mods)
  mods.each{|mod|
   if mod.instance_methods(true).find { |m| m.to_s == 'mutate!'} # ruby1.8 returns strings, 1.9 symbols  so include? doesn't work
     include mod
   else
     metaclass.class_eval{
       include mod
     }
   end
  }
end

Instance Method Details

#<=>(b) ⇒ Object



89
90
91
# File 'lib/charlie/genotype.rb', line 89

def <=>(b)
  fitness <=> b.fitness
end

#clear_cacheObject



55
56
57
# File 'lib/charlie/genotype.rb', line 55

def clear_cache
  @fitness_cache = nil
end

#dupObject



62
63
64
# File 'lib/charlie/genotype.rb', line 62

def dup
  self.class.from_genes(genes.dup)
end

#fight(other) ⇒ Object

raises an exception when called

Raises:

  • (NoMethodError)


13
14
15
# File 'lib/charlie/genotype.rb', line 13

def fight(other)
  raise NoMethodError, "You need to define a fight(other) function in your genotype class to use this selection strategy!"
end

#fitnessObject

raises an exception when called

Raises:

  • (NoMethodError)


9
10
11
# File 'lib/charlie/genotype.rb', line 9

def fitness
  raise NoMethodError, "You need to define a fitness function in your genotype class!"
end

#mutateObject



66
67
68
69
70
# File 'lib/charlie/genotype.rb', line 66

def mutate
  cp = dup
  cp.mutate!
  cp
end

#to_sObject



72
73
74
# File 'lib/charlie/genotype.rb', line 72

def to_s
  @genes.to_s
end