Class: Evolvable::Evaluation
- Inherits:
-
Object
- Object
- Evolvable::Evaluation
- 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.
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
-
#goal ⇒ Object
Returns the value of attribute goal.
Instance Method Summary collapse
- #best_evolvable(population) ⇒ Object
- #call(population) ⇒ Object
-
#initialize(goal = DEFAULT_GOAL_TYPE) ⇒ Evaluation
constructor
A new instance of Evaluation.
- #met_goal?(population) ⇒ Boolean
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
#goal ⇒ Object
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
42 43 44 |
# File 'lib/evolvable/evaluation.rb', line 42 def met_goal?(population) goal.met?(population.evolvables.last) end |