Class: Evolvable::GeneCombination

Inherits:
Object
  • Object
show all
Defined in:
lib/evolvable/gene_combination.rb

Overview

Combination is the process of creating new evolvables by mixing the genes of selected parents. This step drives the creation of the next generation by recombining traits in novel ways.

You can choose from several built-in combination strategies or implement your own. By default, Evolvable uses Evolvable::GeneCombination, which delegates gene-level behavior to individual gene classes.

To define custom combination logic for a gene type, implement:

YourGeneClass.combine(parent_1_gene, parent_2_gene)

Instance Method Summary collapse

Instance Method Details

#call(population) ⇒ Evolvable::Population

Performs the combination operation on the population. Creates new evolvables by combining parent genomes.

Parameters:

Returns:



28
29
30
31
# File 'lib/evolvable/gene_combination.rb', line 28

def call(population)
  new_evolvables(population, population.size)
  population
end

#combine_genomes(genome_1, genome_2) ⇒ Evolvable::Genome

Combines two parent genomes to create a new genome. For each gene key, combines the count genes and individual genes.

Parameters:

Returns:



58
59
60
61
62
63
64
65
66
67
# File 'lib/evolvable/gene_combination.rb', line 58

def combine_genomes(genome_1, genome_2)
  new_config = {}
  genome_1.each do |gene_key, gene_config_1|
    gene_config_2 = genome_2.config[gene_key]
    count_gene = combine_count_genes(gene_config_1, gene_config_2)
    genes = combine_genes(count_gene.count, gene_config_1, gene_config_2)
    new_config[gene_key] = { count_gene: count_gene, genes: genes }
  end
  Genome.new(config: new_config)
end

#new_evolvables(population, count) ⇒ Array<Evolvable>

Creates new evolvable instances by combining parent genomes. For each new evolvable, selects parent genomes and combines them.

Parameters:

  • population (Evolvable::Population)

    The population containing parent evolvables

  • count (Integer)

    The number of new evolvables to create

Returns:

  • (Array<Evolvable>)

    The newly created evolvables



41
42
43
44
45
46
47
48
# File 'lib/evolvable/gene_combination.rb', line 41

def new_evolvables(population, count)
  parent_genome_cycle = population.new_parent_genome_cycle
  Array.new(count) do
    genome_1, genome_2 = parent_genome_cycle.next
    genome = combine_genomes(genome_1, genome_2)
    population.new_evolvable(genome: genome)
  end
end