Class: Darwinning::Organism
- Inherits:
-
Object
- Object
- Darwinning::Organism
- Includes:
- ClassLevelInheritableAttributes
- Defined in:
- lib/darwinning/organism.rb
Instance Attribute Summary collapse
-
#fitness ⇒ Object
Returns the value of attribute fitness.
-
#genes ⇒ Object
Returns the value of attribute genes.
-
#genotypes ⇒ Object
Returns the value of attribute genotypes.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(genotypes = []) ⇒ Organism
constructor
A new instance of Organism.
-
#mutate! ⇒ Object
Selects a random genotype from the organism and rexpresses its gene.
- #nice_print ⇒ Object
Methods included from ClassLevelInheritableAttributes
Constructor Details
#initialize(genotypes = []) ⇒ Organism
Returns a new instance of Organism.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/darwinning/organism.rb', line 38 def initialize(genotypes = []) #TODO: catch errors if genotype.length != @genotypes.length # catch if genotype[x] is not a valid value for @gene[x] if genotypes == [] # fill genotypes with expressed Genes @genotypes = self.class.genes.map { |g| g.express } # Gene expressions else @genotypes = genotypes end @fitness = -1 end |
Instance Attribute Details
#fitness ⇒ Object
Returns the value of attribute fitness.
33 34 35 |
# File 'lib/darwinning/organism.rb', line 33 def fitness @fitness end |
#genes ⇒ Object
Returns the value of attribute genes.
33 34 35 |
# File 'lib/darwinning/organism.rb', line 33 def genes @genes end |
#genotypes ⇒ Object
Returns the value of attribute genotypes.
33 34 35 |
# File 'lib/darwinning/organism.rb', line 33 def genotypes @genotypes end |
#name ⇒ Object
Returns the value of attribute name.
33 34 35 |
# File 'lib/darwinning/organism.rb', line 33 def name @name end |
Instance Method Details
#mutate! ⇒ Object
Selects a random genotype from the organism and rexpresses its gene
52 53 54 55 56 |
# File 'lib/darwinning/organism.rb', line 52 def mutate! random_index = (0..@genotypes.length-1).to_a.sample @genotypes[random_index] = self.class.genes[random_index].express self end |
#nice_print ⇒ Object
58 59 60 61 62 |
# File 'lib/darwinning/organism.rb', line 58 def nice_print puts self.class.name == "" ? "[no name]" : self.class.name self.class.genes.to_enum.each_with_index { |g, i| puts " #{g.name}: #{@genotypes[i]} #{g.units}" } puts " fitness: #{fitness}" end |