This method generates reports comparing several
selection/crossover/mutation methods. Check the examples directory for
several examples. See the BENCHMARK documentation file for more
information.
7: def benchmark(genotype_class, html_outfile='report.html', csv_outfile=nil, &b)
8: start = Time.now
9:
10: dsl_obj = StrategiesDSL.new; dsl_obj.instance_eval(&b)
11: all_tests = dsl_obj.get_tests
12: generations = dsl_obj.generations
13: population_size = dsl_obj.population_size
14: repeat_tests = dsl_obj.repeat
15: setup_proc = dsl_obj.setup
16: teardown_proc = dsl_obj.teardown
17:
18: track_stat = dsl_obj.track_stat
19:
20: n_tests = all_tests.size
21: tests_done = 0
22: puts "#{n_tests} Total tests:"
23:
24: overall_best = [nil, -1.0 / 0.0]
25:
26: data = all_tests.map{|selection_module,crossover_module,mutator_module|
27: tests_done += 1
28: print "\nRunning test #{tests_done}/#{n_tests} : #{selection_module} / #{crossover_module} / #{mutator_module}\t"
29:
30: gclass = Class.new(genotype_class) { use selection_module,crossover_module,mutator_module }
31: start_test = Time.now
32:
33: test_stats = (0...repeat_tests).map{
34: print '.'; $stdout.flush
35:
36: population = Population.new(gclass,population_size)
37: setup_proc.call(population)
38: best = population.evolve_silent(generations).last
39:
40: stat = track_stat.call(best)
41: teardown_proc.call(population)
42:
43: overall_best = [best, stat] if overall_best[0].nil? || (overall_best[1] <=> stat) < 0
44: stat
45: }
46: [selection_module, crossover_module,mutator_module,
47: (Time.now-start_test) / repeat_tests, test_stats]
48: }
49:
50: html_output(html_outfile, data, genotype_class, Time.now-start, overall_best, dsl_obj)
51: csv_output(csv_outfile , data)
52:
53: puts '',table_details(data).to_s
54: return data
55: end