Class: GABenchmark::StrategiesDSL
- Inherits:
-
Object
- Object
- GABenchmark::StrategiesDSL
- Defined in:
- lib/charlie/gabenchmark.rb
Overview
Used in the GABenchmark#benchmark function.
Class Method Summary collapse
Instance Method Summary collapse
-
#get_tests ⇒ Object
Get all the tests.
-
#initialize ⇒ StrategiesDSL
constructor
A new instance of StrategiesDSL.
-
#setup(&b) ⇒ Object
Pass a block that does the setup.
-
#teardown(&b) ⇒ Object
Pass a block that does the setup.
-
#track_stat(&b) ⇒ Object
(also: #track_stats)
Pass a block that returns one or more statistics to track.
Constructor Details
#initialize ⇒ StrategiesDSL
Returns a new instance of StrategiesDSL.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/charlie/gabenchmark.rb', line 167 def initialize @repeat = 10 @population_size = 20 @generations = 50 @setup = @teardown = proc{} selection [] crossover [] mutator [] track_stat{|best| best.fitness } # tracks maximum fitness by default end |
Class Method Details
.attr_dsl(x) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/charlie/gabenchmark.rb', line 140 def attr_dsl(x) x = x.to_s attr_accessor x alias_method 'get_'+x, x # rename reader define_method(x) {|*args| # reader with 0 args, write with 1 arg return send('get_'+x) if args.empty? args.size > 1 ? send(x+'=',args) : send(x+'=',*args) } end |
Instance Method Details
#get_tests ⇒ Object
Get all the tests. Basically a cartesian product of all selection, crossover and mutation methods.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/charlie/gabenchmark.rb', line 201 def get_tests t = [] defmod = Module.new{self.name='default'} selection = [@selection].flatten ; selection = [defmod] if selection.empty? crossover = [@crossover].flatten ; crossover = [defmod] if crossover.empty? mutator = [@mutator].flatten ; mutator = [defmod] if mutator.empty? selection.each{|s| crossover.each{|c| mutator.each{|m| t << [s,c,m] } } } t end |
#setup(&b) ⇒ Object
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)
189 190 191 192 |
# File 'lib/charlie/gabenchmark.rb', line 189 def setup(&b) return @setup unless block_given? @setup = b end |
#teardown(&b) ⇒ Object
Pass a block that does the setup. Rarely needed. Called with the population as argument AFTER track_stats.
195 196 197 198 |
# File 'lib/charlie/gabenchmark.rb', line 195 def teardown(&b) return @teardown unless block_given? @teardown = b end |
#track_stat(&b) ⇒ Object Also known as: track_stats
Pass a block that returns one or more statistics to track. Block is passed the individual with the highest fitness after each run.
-
Can be used to track, for example, training error vs generalization error.
-
Default is fitness of the best solution.
-
When returning multiple values, <=> for arrays is used to determine the best individual in the info table (i.e. second elements only for tie-breaking), but min/max/avg/stddev stats are calculated independently for each component
182 183 184 185 |
# File 'lib/charlie/gabenchmark.rb', line 182 def track_stat(&b) return @track_stat unless block_given? @track_stat = b end |