| Class | GABenchmark::StrategiesDSL |
| In: |
lib/charlie/gabenchmark.rb
|
| Parent: | Object |
Used in the GABenchmark#benchmark function.
| mutator | -> | mutation |
| mutator= | -> | mutation= |
# File lib/charlie/gabenchmark.rb, line 141
141: def attr_dsl(x)
142: x = x.to_s
143: attr_accessor x
144: alias_method 'get_'+x, x # rename reader
145: define_method(x) {|*args| # reader with 0 args, write with 1 arg
146: return send('get_'+x) if args.empty?
147: args.size > 1 ? send(x+'=',args) : send(x+'=',*args)
148: }
149: end
# File lib/charlie/gabenchmark.rb, line 168
168: def initialize
169: @repeat = 10
170: @population_size = 20
171: @generations = 50
172: @setup = @teardown = proc{}
173: selection []
174: crossover []
175: mutator []
176: track_stat{|best| best.fitness } # tracks maximum fitness by default
177: end
Get all the tests. Basically a cartesian product of all selection, crossover and mutation methods.
# File lib/charlie/gabenchmark.rb, line 202
202: def get_tests
203: t = []
204:
205: defmod = Module.new{self.name='default'}
206:
207: selection = [@selection].flatten ; selection = [defmod] if selection.empty?
208: crossover = [@crossover].flatten ; crossover = [defmod] if crossover.empty?
209: mutator = [@mutator].flatten ; mutator = [defmod] if mutator.empty?
210:
211: selection.each{|s|
212: crossover.each{|c|
213: mutator.each{|m|
214: t << [s,c,m]
215: }
216: }
217: }
218: t
219: end
Pass a block that does the setup. Rarely needed. The proc is passes the population before each test (i.e. between Population.new and evolve_silent)
# File lib/charlie/gabenchmark.rb, line 190
190: def setup(&b)
191: return @setup unless block_given?
192: @setup = b
193: end
Pass a block that does the setup. Rarely needed. Called with the population as argument AFTER track_stats.
# File lib/charlie/gabenchmark.rb, line 196
196: def teardown(&b)
197: return @teardown unless block_given?
198: @teardown = b
199: end
Pass a block that returns one or more statistics to track. Block is passed the individual with the highest fitness after each run.
# File lib/charlie/gabenchmark.rb, line 183
183: def track_stat(&b)
184: return @track_stat unless block_given?
185: @track_stat = b
186: end