Module: Darwinning

Extended by:
Config
Defined in:
lib/darwinning.rb,
lib/darwinning/gene.rb,
lib/darwinning/config.rb,
lib/darwinning/version.rb,
lib/darwinning/organism.rb,
lib/darwinning/population.rb,
lib/darwinning/evolution_types.rb,
lib/darwinning/evolution_types/mutation.rb,
lib/darwinning/evolution_types/reproduction.rb

Defined Under Namespace

Modules: Config, EvolutionTypes Classes: Gene, Organism, Population

Constant Summary collapse

VERSION =
"0.1.0"

Constants included from Config

Config::CROSSOVER_METHOD

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/darwinning.rb', line 11

def self.included(base)
  def base.genes
    gene_ranges.map { |k,v| Gene.new(name: k, value_range: v) }
  end

  def base.build_population(fitness_goal, population_size = 10, generations_limit = 100, 
                       evolution_types = Population::DEFAULT_EVOLUTION_TYPES)
    Population.new(organism: self, population_size: population_size,
                   generations_limit: generations_limit, fitness_goal: fitness_goal,
                   evolution_types: evolution_types)
  end

  def base.is_evolveable?
    has_gene_ranges? &&
    gene_ranges.is_a?(Hash) &&
    gene_ranges.any? &&
    valid_genes? &&
    has_fitness_method?
  end

  private

  def base.has_gene_ranges?
    self.constants.include?(:GENE_RANGES)
  end

  def base.has_fitness_method?
    self.instance_methods.include?(:fitness)
  end

  def base.gene_ranges
    self::GENE_RANGES
  end

  def base.valid_genes?
    genes.each do |gene|
      return false unless self.method_defined? gene.name
    end
    true
  end
end

Instance Method Details

#genesObject



53
54
55
# File 'lib/darwinning.rb', line 53

def genes
  self.class.genes
end

#genotypesObject



57
58
59
60
61
62
63
# File 'lib/darwinning.rb', line 57

def genotypes
  gt = {}
  genes.each do |gene|
    gt[gene] = self.send(gene.name)
  end
  gt
end