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 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 to maximize, minimize, or equalize some numeric value.

A population's goal value can be most easily assigned via an argument to Evolvable::Population#evolve like this: 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.


25
26
27
# File 'lib/evolvable/evaluation.rb', line 25

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

Instance Attribute Details

#goalObject

Returns the value of attribute goal.


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

def goal
  @goal
end

Instance Method Details

#best_evolvable(population) ⇒ Object


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

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

#call(population) ⇒ Object


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

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

#met_goal?(population) ⇒ Boolean

Returns:

  • (Boolean)

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

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