Class: Wallace::Koza::KozaSpecies
- Defined in:
- lib/modules/koza/koza_species.rb
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#depth_limits ⇒ Object
readonly
Returns the value of attribute depth_limits.
-
#non_terminals ⇒ Object
readonly
Returns the value of attribute non_terminals.
-
#terminals ⇒ Object
readonly
Returns the value of attribute terminals.
Attributes inherited from Species
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ KozaSpecies
constructor
Constructs a new koza tree-based species.
-
#spawn(opts = {}) ⇒ Object
Creates a new koza tree based individual using the constraints of the species.
-
#valid?(individual) ⇒ Boolean
Check that a given individual meets the depth constraints of the species.
-
#valid_tree?(tree) ⇒ Boolean
Checks whether a given tree meets the depth constraints of the species.
Methods inherited from Species
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
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
5 6 7 |
# File 'lib/modules/koza/koza_species.rb', line 5 def builder @builder end |
#depth_limits ⇒ Object (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_terminals ⇒ Object (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 |
#terminals ⇒ Object (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.
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.
29 30 31 |
# File 'lib/modules/koza/koza_species.rb', line 29 def valid_tree?(tree) @depth_limits.cover?(tree.length) end |