Class: EvoSynth::MetaOperators::ProportionalCombinedOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/evosynth/operators/meta_operators/proportional_combined_operator.rb

Overview

This operator is a container for other operators. When ProportionalCombinedOperator gets called with a method, that it does not implement, each operator has a probability to get called with that method. Most likely you will combine mutations and recombinations and therefore mutate() or recombine() will get called.

Instance Method Summary collapse

Constructor Details

#initialize(*ops) ⇒ ProportionalCombinedOperator

Returns a new instance of ProportionalCombinedOperator.



34
35
36
37
# File 'lib/evosynth/operators/meta_operators/proportional_combined_operator.rb', line 34

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/evosynth/operators/meta_operators/proportional_combined_operator.rb', line 47

def method_missing(method_name, *args)
  raise "no operator to call" if @operators.empty?

  operator = nil
  rand_value = EvoSynth.rand
  sum = 0.0
  @operators.each do |op|
    sum += op[1];
    operator = op[0];
    break if sum >= rand_value
  end

  operator.send(method_name, *args)
end

Instance Method Details

#add(operator, probability = 1.0 / (@operators.size > 0 ? @operators.size : 1.0)) ⇒ Object



40
41
42
43
44
# File 'lib/evosynth/operators/meta_operators/proportional_combined_operator.rb', line 40

def add(operator, probability = 1.0 / (@operators.size > 0 ? @operators.size : 1.0))
  @operators << [operator, probability]
  normalize_operator_probabilities
  self
end

#to_sObject



63
64
65
66
67
# File 'lib/evosynth/operators/meta_operators/proportional_combined_operator.rb', line 63

def to_s
  operators_to_s = []
  @operators.each { |op| operators_to_s << "#{op[0].to_s} (probability: #{op[1]})" }
  "proportional combinded operator <operators: #{operators_to_s.join(', ')}>"
end