Class: Evolvable::Goal

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

Overview

The goal for a population can be specified via assignment - population.goal = Evolvable::Goal::Equalize.new - or by passing an evaluation object when initializing a population.

You can intialize the Evolvable::Evaluation object with any goal object like this:

You can implement custom goal object like so:

or more succinctly like this:

Examples:

goal_object = SomeGoal.new(value: 100)
Evolvable::Evaluation.new(goal_object)
Evolvable::Evaluation.new(:maximize) # Uses default goal value of   Float::INFINITY
Evolvable::Evaluation.new(maximize: 50) # Sets goal value to 50
Evolvable::Evaluation.new(:minimize) # Uses default goal value of   -Float::INFINITY
Evolvable::Evaluation.new(minimize: 100) # Sets goal value to 100
Evolvable::Evaluation.new(:equalize) # Uses default goal value of 0
Evolvable::Evaluation.new(equalize: 1000) # Sets goal value to 1000
class CustomGoal < Evolvable::Goal
  def evaluate(instance)
    # Required by Evolvable::Evaluation in order to sort instances in preparation for selection.
  end

  def met?(instance)
    # Used by Evolvable::Population#evolve to stop evolving when the goal value has been reached.
  end
end

Direct Known Subclasses

EqualizeGoal, MaximizeGoal, MinimizeGoal

Defined Under Namespace

Classes: Equalize, Maximize, Minimize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value: nil) ⇒ Goal

Returns a new instance of Goal.



38
39
40
# File 'lib/evolvable/goal.rb', line 38

def initialize(value: nil)
  @value = value if value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



42
43
44
# File 'lib/evolvable/goal.rb', line 42

def value
  @value
end

Instance Method Details

#evaluate(_evolvable) ⇒ Object



44
45
46
# File 'lib/evolvable/goal.rb', line 44

def evaluate(_evolvable)
  raise Errors::UndefinedMethod, "#{self.class.name}##{__method__}"
end

#met?(_evolvable) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



48
49
50
# File 'lib/evolvable/goal.rb', line 48

def met?(_evolvable)
  raise Errors::UndefinedMethod, "#{self.class.name}##{__method__}"
end