Class: Evolvable::Evaluation

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

Overview

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