Class: Evolvable::Goal

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

Overview

Custom Goals

You can create custom goals by subclassing Evolvable::Goal and implementing:

  • evaluate(evolvable): Return a value that for sorting evolvables
  • met?(evolvable): Returns true when the goal value is reached

Examples:

Example goal implementation that prioritizes evolvables with fitness values within a specific range

class YourRangeGoal < Evolvable::Goal
  def value
    @value ||= 0..100
   end

  def evaluate(evolvable)
    return 1 if value.include?(evolvable.fitness)

    min, max = value.minmax
    -[(min - evolvable.fitness).abs, (max - evolvable.fitness).abs].min
  end

  def met?(evolvable)
    value.include?(evolvable.fitness)
  end
end

See Also:

Direct Known Subclasses

EqualizeGoal, MaximizeGoal, MinimizeGoal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value: nil) ⇒ Goal

Returns a new instance of Goal.



33
34
35
# File 'lib/evolvable/goal.rb', line 33

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

Instance Attribute Details

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#evaluate(_evolvable) ⇒ Object

Raises:



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

def evaluate(_evolvable)
  raise Error, "Undefined method: #{self.class.name}##{__method__}"
end

#met?(_evolvable) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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

def met?(_evolvable)
  raise Error, "Undefined method: #{self.class.name}##{__method__}"
end