Class: Evolvable::Evaluation
- Inherits:
-
Object
- Object
- Evolvable::Evaluation
- Defined in:
- lib/evolvable/evaluation.rb
Overview
Evaluation sorts evolvables based on their fitness and provides mechanisms to change the goal type and value (fitness goal). Goals define the success criteria for evolution. They allow you to specify what your population is evolving toward, whether it's maximizing a value, minimizing a value, or seeking a specific value.
How It Works
Your evolvable class defines a
#fitness
method that returns a Comparable object.- Preferably a numeric value like an integer or float.
During evolution, evolvables are sorted by your goal's fitness interpretation
- The default goal type is
:maximize
, see goal types below for other options
- The default goal type is
If a goal value is specified, evolution will stop when it is met
Goal Types
- Maximize (higher is better)
robots = Robot.new_population(evaluation: :maximize) # Defaults to infinity
robots.evolve_to_goal(100) # Evolve until fitness reaches 100+
# Same as above
Robot.new_population(evaluation: { maximize: 100 }).evolve_to_goal
- Minimize (lower is better)
errors = ErrorModel.new_population(evaluation: :minimize) # Defaults to -infinity
errors.evolve_to_goal(0.01) # Evolve until error rate reaches 0.01 or less
# Same as above
ErrorModel.new_population(evaluation: { minimize: 0.01 }).evolve_to_goal
- Equalize (closer to target is better)
targets = TargetMatcher.new_population(evaluation: :equalize) # Defaults to 0
targets.evolve_to_goal(42) # Evolve until we match the target value
# Same as above
TargetMatcher.new_population(evaluation: { equalize: 42 }).evolve_to_goal
Constant Summary collapse
- GOALS =
Mapping of goal type symbols to their corresponding goal objects. See the readme section above for details on each goal type.
{ maximize: Evolvable::MaximizeGoal.new, minimize: Evolvable::MinimizeGoal.new, equalize: Evolvable::EqualizeGoal.new }.freeze
- DEFAULT_GOAL_TYPE =
The default goal type used if none is specified.
:maximize
Instance Attribute Summary collapse
-
#goal ⇒ Evolvable::Goal
The goal object used for evaluation.
Instance Method Summary collapse
-
#best_evolvable(population) ⇒ Evolvable
Returns the best evolvable in the population according to the goal.
-
#call(population) ⇒ Array<Evolvable>
Evaluates and sorts all evolvables in the population according to the goal.
-
#initialize(goal = DEFAULT_GOAL_TYPE) ⇒ Evaluation
constructor
Initializes a new evaluation object.
-
#met_goal?(population) ⇒ Boolean
Checks if the goal has been met by any evolvable in the population.
Constructor Details
#initialize(goal = DEFAULT_GOAL_TYPE) ⇒ Evaluation
Initializes a new evaluation object.
80 81 82 |
# File 'lib/evolvable/evaluation.rb', line 80 def initialize(goal = DEFAULT_GOAL_TYPE) @goal = normalize_goal(goal) end |
Instance Attribute Details
#goal ⇒ Evolvable::Goal
The goal object used for evaluation.
88 89 90 |
# File 'lib/evolvable/evaluation.rb', line 88 def goal @goal end |
Instance Method Details
#best_evolvable(population) ⇒ Evolvable
Returns the best evolvable in the population according to the goal.
106 107 108 |
# File 'lib/evolvable/evaluation.rb', line 106 def best_evolvable(population) population.evolvables.max_by { |evolvable| goal.evaluate(evolvable) } end |
#call(population) ⇒ Array<Evolvable>
Evaluates and sorts all evolvables in the population according to the goal.
96 97 98 |
# File 'lib/evolvable/evaluation.rb', line 96 def call(population) population.evolvables.sort_by! { |evolvable| goal.evaluate(evolvable) } end |
#met_goal?(population) ⇒ Boolean
Checks if the goal has been met by any evolvable in the population.
116 117 118 |
# File 'lib/evolvable/evaluation.rb', line 116 def met_goal?(population) goal.met?(population.evolvables.last) end |