Class: EvoSynth::MetaOperators::SequentialCombinedOperator
- Inherits:
-
Object
- Object
- EvoSynth::MetaOperators::SequentialCombinedOperator
- Defined in:
- lib/evosynth/operators/meta_operators/sequential_combined_operator.rb
Overview
This operator is a container for other operators. When SequentialCombinedOperator gets called with a method, that it does not implement, each operator has a probability to get called with that method. But in contrast to the ProportionalCombinedOperator all operators will get called. All Operators should be of the same kind, otherwise you will most likely receive a exception.
Most likely you will combine mutations and recombinations and therefore mutate() or recombine() will get called.
Instance Method Summary collapse
-
#add(operator, probability = 1.0) ⇒ Object
default probability is 1.0.
-
#initialize(*ops) ⇒ SequentialCombinedOperator
constructor
A new instance of SequentialCombinedOperator.
- #method_missing(method_name, *args) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*ops) ⇒ SequentialCombinedOperator
Returns a new instance of SequentialCombinedOperator.
37 38 39 40 |
# File 'lib/evosynth/operators/meta_operators/sequential_combined_operator.rb', line 37 def initialize(*ops) @operators = [] ops.each { |operator| add(operator) } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/evosynth/operators/meta_operators/sequential_combined_operator.rb', line 52 def method_missing(method_name, *args) raise "no operator to call" if @operators.empty? result = args @operators.each do |operator| result = operator[0].send(method_name, *result) if EvoSynth.rand < operator[1] end result end |
Instance Method Details
#add(operator, probability = 1.0) ⇒ Object
default probability is 1.0
45 46 47 48 49 |
# File 'lib/evosynth/operators/meta_operators/sequential_combined_operator.rb', line 45 def add(operator, probability = 1.0) probability = 1.0 if probability > 1.0 @operators << [operator, probability] self end |
#to_s ⇒ Object
64 65 66 67 68 |
# File 'lib/evosynth/operators/meta_operators/sequential_combined_operator.rb', line 64 def to_s operators_to_s = [] @operators.each { |op| operators_to_s << "#{op[0].to_s} (probability: #{op[1]})" } "sequential combinded operator <operators: #{operators_to_s.join(', ')}>" end |