Class: Evolvable::Evaluation

Inherits:
Object
  • Object
show all
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

  1. Your evolvable class defines a #fitness method that returns a Comparable object.

    • Preferably a numeric value like an integer or float.
  2. During evolution, evolvables are sorted by your goal's fitness interpretation

    • The default goal type is :maximize, see goal types below for other options
  3. 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

See Also:

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.

Returns:

{ 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.

Returns:

  • (Symbol)

    The default goal type (maximize)

:maximize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(goal = DEFAULT_GOAL_TYPE) ⇒ Evaluation

Initializes a new evaluation object.

Parameters:

  • goal (Symbol, Hash, Evolvable::Goal) (defaults to: DEFAULT_GOAL_TYPE)

    The goal type (:maximize, :minimize, :equalize), a hash specifying goal type and value, or a custom goal 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

#goalEvolvable::Goal

The goal object used for evaluation.

Returns:



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.

Parameters:

Returns:

  • (Evolvable)

    The best evolvable based on the current 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.

Parameters:

Returns:

  • (Array<Evolvable>)

    The sorted evolvables



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.

Parameters:

Returns:

  • (Boolean)

    True if the goal has been met, false otherwise



116
117
118
# File 'lib/evolvable/evaluation.rb', line 116

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