Class: Evolvable::Evaluation

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

Overview

For selection to be effective in the context of evolution, there needs to be a way to compare evolvables. In the genetic algorithm, this is often referred to as the "fitness function".

The Evolvable::Evaluation object expects evolvable instances to define a #value method that returns some numeric value. Values are used to evaluate instances relative to each other and with regards to some goal. Out of the box, the goal can be set to maximize, minimize, or equalize numeric values.

Examples:

# TODO: Show how to add/change population's evaluation object

# The goal value can also be assigned via as argument to `Evolvable::Population#evolve`
population.evolve(goal_value: 1000)

Constant Summary collapse

GOALS =
{ maximize: Evolvable::Goal::Maximize.new,
minimize: Evolvable::Goal::Minimize.new,
equalize: Evolvable::Goal::Equalize.new }.freeze
DEFAULT_GOAL_TYPE =
:maximize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(goal = DEFAULT_GOAL_TYPE) ⇒ Evaluation

Returns a new instance of Evaluation.



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

def initialize(goal = DEFAULT_GOAL_TYPE)
  @goal = normalize_goal(goal)
end

Instance Attribute Details

#goalObject

Returns the value of attribute goal.



32
33
34
# File 'lib/evolvable/evaluation.rb', line 32

def goal
  @goal
end

Instance Method Details

#best_evolvable(population) ⇒ Object



38
39
40
# File 'lib/evolvable/evaluation.rb', line 38

def best_evolvable(population)
  population.evolvables.max_by { |evolvable| goal.evaluate(evolvable) }
end

#call(population) ⇒ Object



34
35
36
# File 'lib/evolvable/evaluation.rb', line 34

def call(population)
  population.evolvables.sort_by! { |evolvable| goal.evaluate(evolvable) }
end

#met_goal?(population) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/evolvable/evaluation.rb', line 42

def met_goal?(population)
  goal.met?(population.evolvables.last)
end