Class: MHL::RechenbergController
- Inherits:
-
Object
- Object
- MHL::RechenbergController
- Defined in:
- lib/mhl/rechenberg_controller.rb
Constant Summary collapse
- DEFAULT_THRESHOLD =
1.0/5.0
- TAU =
1.10
- P_M_MAX =
0.99
- P_M_MIN =
0.01
Instance Attribute Summary collapse
-
#generations ⇒ Object
readonly
Returns the value of attribute generations.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
- #call(solver, best) ⇒ Object
-
#initialize(generations = 5, threshold = DEFAULT_THRESHOLD, logger = nil) ⇒ RechenbergController
constructor
A new instance of RechenbergController.
Constructor Details
#initialize(generations = 5, threshold = DEFAULT_THRESHOLD, logger = nil) ⇒ RechenbergController
Returns a new instance of RechenbergController.
11 12 13 14 15 16 17 18 19 |
# File 'lib/mhl/rechenberg_controller.rb', line 11 def initialize(generations=5, threshold=DEFAULT_THRESHOLD, logger=nil) unless threshold > 0.0 and threshold < 1.0 raise ArgumentError, "The threshold parameter must be in the (0.0,1.0) range!" end @generations = generations @threshold = threshold @logger = logger @history = [] end |
Instance Attribute Details
#generations ⇒ Object (readonly)
Returns the value of attribute generations.
9 10 11 |
# File 'lib/mhl/rechenberg_controller.rb', line 9 def generations @generations end |
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
9 10 11 |
# File 'lib/mhl/rechenberg_controller.rb', line 9 def threshold @threshold end |
Instance Method Details
#call(solver, best) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mhl/rechenberg_controller.rb', line 21 def call(solver, best) @history << best if @history.size > @generations # calculate improvement ratio res = @history.each_cons(2).inject(0) {|s,x| s += 1 if x[1][:fitness] > x[0][:fitness]; s } / (@history.size - 1).to_f if res > @threshold # we had enough improvements - decrease impact of mutation # increase mutation probability by 5% or to P_M_MAX old_p_m = solver.mutation_probability new_p_m = [ old_p_m * TAU, P_M_MAX ].min @logger.info "increasing mutation_probability from #{old_p_m} to #{new_p_m}" if @logger solver.mutation_probability = new_p_m else # we didn't have enough improvements - increase impact of mutation # decrease mutation probability by 5% or to P_M_MAX old_p_m = solver.mutation_probability new_p_m = [ old_p_m / TAU, P_M_MIN ].max @logger.info "decreasing mutation_probability from #{old_p_m} to #{new_p_m}" if @logger solver.mutation_probability = new_p_m end # reset @history.shift(@history.size-1) end end |