Class: EvoSynth::Evolvers::BalancedCoevolutionary

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

Overview

based on “Coevolution, Memory and Balance” from Paredis, 1999

evolves two populations (problems, solutions / predator, prey) in a balanced manner

Constant Summary collapse

DEFAULT_PAIRING_RUNS =
20
DEFAULT_SELECTION =
EvoSynth::Selections::FitnessProportionalSelection.new
DEFAULT_RECOMBINATION =
EvoSynth::Recombinations::KPointCrossover.new(2)
DEFAULT_RECOMBINATION_PROBABILITY =
0.75

Instance Attribute Summary collapse

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) ⇒ BalancedCoevolutionary

Returns a new instance of BalancedCoevolutionary.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 42

def initialize(profile)
	init_profile :population,
		:problems,
	    :evaluator,
		:mutation,
	    :problem_mutation,
		:recombination => DEFAULT_RECOMBINATION,
	    :problem_recombination => DEFAULT_RECOMBINATION,
		:parent_selection => DEFAULT_SELECTION,
		:enviromental_selection => DEFAULT_SELECTION,
		:pairing_runs => DEFAULT_PAIRING_RUNS

	use_profile profile

	# intialize fitnesses?! FIXME: find a better way to do this
	solution = @parent_selection.select(@population, 1).first
	@problems.each do |problem|
		@evaluator.encounter(problem, solution)
	end

	problem = @parent_selection.select(@problems, 1).first
	@population.each do |solution|
		@evaluator.encounter(problem, solution)
	end
end

Instance Attribute Details

#solution_successObject (readonly)

Returns the value of attribute solution_success.



35
36
37
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 35

def solution_success
  @solution_success
end

Instance Method Details

#best_solutionObject



72
73
74
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 72

def best_solution
	@population.best
end

#next_generationObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 84

def next_generation
	problem_wins, solution_wins = 0, 0

	@pairing_runs.times do
		problem = @parent_selection.select(@problems, 1).first
		solution = @parent_selection.select(@population, 1).first
		winner = @evaluator.encounter(problem, solution)
		winner == problem ? problem_wins += 1 : solution_wins += 1
	end

	@solution_success = solution_wins / @pairing_runs.to_f

	if EvoSynth.rand < @solution_success
		# select, recombine and mutate solution child
		evolve_offspring(@population, @problems, @recombination,
			@mutation) { |child, enemy| @evaluator.encounter(enemy, child, false, true) }
	else
		# select, recombine and mutate problem child
		evolve_offspring(@problems, @population, @problem_recombination,
			@problem_mutation) { |child, enemy| @evaluator.encounter(child, enemy, true, false) }
	end				
end

#return_resultObject



80
81
82
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 80

def return_result
	@population
end

#to_sObject



68
69
70
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 68

def to_s
	"C-M-B coevolutionary algorithm <mutation: #{@mutation}, recombination: #{@recombination}, parent selection: #{@parent_selection}, enviromental selection: #{@enviromental_selection}>"
end

#worst_solutionObject



76
77
78
# File 'lib/evosynth/evolvers/coevolutionary/balanced_coevolutionary.rb', line 76

def worst_solution
	@population.worst
end