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.
-
#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.
159 160 161 162 163 164 165 166 167 |
# File 'lib/charlie/gabenchmark.rb', line 159 def initialize @repeat = 10 @population_size = 20 @generations = 50 selection [] crossover [] mutator [] track_stat{|best| best.fitness } # tracks maximum fitness by default end |
Class Method Details
.attr_dsl(x) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/charlie/gabenchmark.rb', line 132 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.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/charlie/gabenchmark.rb', line 180 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 |
#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
173 174 175 176 |
# File 'lib/charlie/gabenchmark.rb', line 173 def track_stat(&b) return @track_stat unless block_given? @track_stat = b end |