Class: EvoSynth::Evolvers::GeneticAlgorithm

Inherits:
Object
  • Object
show all
Includes:
Evolver
Defined in:
lib/evosynth/evolvers/basic/genetic_algorithm.rb

Overview

GENETISCHER-ALGORITHMUS (Weicker Page 85)

Constant Summary collapse

DEFAULT_SELECTION =
EvoSynth::Selections::FitnessProportionalSelection.new
DEFAULT_RECOMBINATION =
EvoSynth::Recombinations::OnePointCrossover.new
DEFAULT_RECOMBINATION_PROBABILITY =
0.75
DEFAULT_CHILD_FACTOR =
0.5

Instance Attribute Summary

Attributes included from RunnableEvolver

#generations_computed

Instance Method Summary collapse

Methods included from ProfileUsingEvolver

#init_profile, #use_profile

Methods included from RunnableEvolver

#run_until, #run_until_fitness_reached, #run_until_generations_reached

Constructor Details

#initialize(profile) ⇒ GeneticAlgorithm

Returns a new instance of GeneticAlgorithm.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/evosynth/evolvers/basic/genetic_algorithm.rb', line 38

def initialize(profile)
	init_profile :population, 
	    :evaluator,
	    :mutation,
	    :parent_selection			=> DEFAULT_SELECTION,
	    :recombination				=> DEFAULT_RECOMBINATION,
	    :recombination_probability	=> DEFAULT_RECOMBINATION_PROBABILITY,
		:child_factor				=> DEFAULT_CHILD_FACTOR

	use_profile profile

	@population.each { |individual| @evaluator.calculate_and_set_initial_fitness(individual) }
end

Instance Method Details

#best_solutionObject



56
57
58
# File 'lib/evosynth/evolvers/basic/genetic_algorithm.rb', line 56

def best_solution
	@population.best
end

#next_generationObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/evosynth/evolvers/basic/genetic_algorithm.rb', line 68

def next_generation
	selected_pop = @parent_selection.select(@population, @population.size * @child_factor)
	@population.clear

	selected_pop.each_index do |index_one|
		index_two = (index_one + 1) % selected_pop.size

		if EvoSynth.rand < @recombination_probability
			child_one, child_two = @recombination.recombine(selected_pop[index_one], selected_pop[index_two])
		else
			child_one = selected_pop[index_one]
			child_two = selected_pop[index_two]
		end

		@population.add(@mutation.mutate(child_one))
		@population.add(@mutation.mutate(child_two))
	end

	@population.each { |individual| @evaluator.calculate_and_set_fitness(individual) }
end

#return_resultObject



64
65
66
# File 'lib/evosynth/evolvers/basic/genetic_algorithm.rb', line 64

def return_result
	@population
end

#to_sObject



52
53
54
# File 'lib/evosynth/evolvers/basic/genetic_algorithm.rb', line 52

def to_s
	"basic genetic algoritm <mutation: #{@mutation}, parent selection: #{@parent_selection}, recombination: #{@recombination}>"
end

#worst_solutionObject



60
61
62
# File 'lib/evosynth/evolvers/basic/genetic_algorithm.rb', line 60

def worst_solution
	@population.worst
end