Class: Wallace::Koza::KozaSpecies

Inherits:
Species
  • Object
show all
Defined in:
lib/modules/koza/koza_species.rb

Instance Attribute Summary collapse

Attributes inherited from Species

#id

Instance Method Summary collapse

Methods inherited from Species

#finish!

Constructor Details

#initialize(opts = {}) ⇒ KozaSpecies

Constructs a new koza tree-based species.

Parameters:

  • opts, hash of keyword options used by this method. -> id, the unique identifier for this species. -> terminals, the set of terminal nodes. -> non_terminals, the set of non-terminal nodes. -> builder, the build method used to generate new trees and sub-trees. -> depth_limits, the range of depths trees in this species may have.



19
20
21
22
23
24
25
26
# File 'lib/modules/koza/koza_species.rb', line 19

def initialize(opts = {})
  super(opts)
  @terminals = Wallace::Koza::TerminalSet.new(opts[:terminals])
  @non_terminals = Wallace::Koza::NonTerminalSet.new(opts[:non_terminals])
  @builder = opts[:builder] || Wallace::Koza::Builder::HalfBuilder.new
  @depth_limits = opts[:depth_limits] || (0..6)
  @builder.prepare(@terminals, @non_terminals, @depth_limits)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



5
6
7
# File 'lib/modules/koza/koza_species.rb', line 5

def builder
  @builder
end

#depth_limitsObject (readonly)

Returns the value of attribute depth_limits.



5
6
7
# File 'lib/modules/koza/koza_species.rb', line 5

def depth_limits
  @depth_limits
end

#non_terminalsObject (readonly)

Returns the value of attribute non_terminals.



5
6
7
# File 'lib/modules/koza/koza_species.rb', line 5

def non_terminals
  @non_terminals
end

#terminalsObject (readonly)

Returns the value of attribute terminals.



5
6
7
# File 'lib/modules/koza/koza_species.rb', line 5

def terminals
  @terminals
end

Instance Method Details

#spawn(opts = {}) ⇒ Object

Creates a new koza tree based individual using the constraints of the species.

Parameters:

  • opts, hash of keyword options used by this method. -> random, the RNG to use when spawning a new individual.



44
45
46
# File 'lib/modules/koza/koza_species.rb', line 44

def spawn(opts = {})
  Wallace::Individual.new(self, @builder.build_tree(random: opts[:random] || Random.new))
end

#valid?(individual) ⇒ Boolean

Check that a given individual meets the depth constraints of the species.

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/modules/koza/koza_species.rb', line 34

def valid?(individual)
  return false unless super(individual)
  return false unless valid_tree?(individual.data)
end

#valid_tree?(tree) ⇒ Boolean

Checks whether a given tree meets the depth constraints of the species.

Returns:

  • (Boolean)


29
30
31
# File 'lib/modules/koza/koza_species.rb', line 29

def valid_tree?(tree)
  @depth_limits.cover?(tree.length)
end