Class: Genotype
Overview
Base class. Inherit this if you’re starting from scratch
Instance Attribute Summary collapse
-
#fitness_cache ⇒ Object
Used by Genotype.cache_fitness.
-
#genes ⇒ Object
Returns the value of attribute genes.
Class Method Summary collapse
-
.cache_fitness ⇒ Object
Adds caching to the fitness function.
-
.from_genes(g) ⇒ Object
Creates a new instance of your genotype class given its genes.
-
.use(*mods) ⇒ Object
For each module passed: Includes the module if it has a mutate! method, and includes it in the metaclass otherwise.
Instance Method Summary collapse
- #<=>(b) ⇒ Object
- #clear_cache ⇒ Object
- #dup ⇒ Object
-
#fight(other) ⇒ Object
raises an exception when called.
-
#fitness ⇒ Object
raises an exception when called.
- #mutate ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#fitness_cache ⇒ Object
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 |
#genes ⇒ Object
Returns the value of attribute genes.
6 7 8 |
# File 'lib/charlie/genotype.rb', line 6 def genes @genes end |
Class Method Details
.cache_fitness ⇒ Object
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 .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_cache ⇒ Object
55 56 57 |
# File 'lib/charlie/genotype.rb', line 55 def clear_cache @fitness_cache = nil end |
#dup ⇒ Object
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
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 |
#fitness ⇒ Object
raises an exception when called
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 |
#mutate ⇒ Object
66 67 68 69 70 |
# File 'lib/charlie/genotype.rb', line 66 def mutate cp = dup cp.mutate! cp end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/charlie/genotype.rb', line 72 def to_s @genes.to_s end |