charlie 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/History.txt +14 -0
  2. data/Manifest.txt +13 -22
  3. data/README.txt +3 -3
  4. data/Rakefile +1 -1
  5. data/TODO.txt +11 -21
  6. data/data/BENCHMARK +25 -23
  7. data/data/CROSSOVER +5 -1
  8. data/data/GENOTYPE +6 -6
  9. data/data/MUTATION +19 -7
  10. data/data/SELECTION +2 -1
  11. data/data/template.html +2 -1
  12. data/examples/EXAMPLES_README.txt +70 -0
  13. data/examples/bitstring.rb +72 -0
  14. data/examples/{gladiatorial_sunburn.rb → coevolution.rb} +80 -22
  15. data/examples/function_optimization.rb +113 -0
  16. data/examples/output/{royalroad1_report.html → bitstring_royalroad.html} +822 -655
  17. data/examples/output/function_optimization_sombrero.html +2289 -0
  18. data/examples/output/function_optimization_twopeak.csv +210 -0
  19. data/examples/output/function_optimization_twopeak.html +2477 -0
  20. data/examples/output/string_weasel.html +513 -0
  21. data/examples/output/tsp.html +633 -882
  22. data/examples/{money.rb → permutation.rb} +20 -8
  23. data/examples/string.rb +98 -0
  24. data/examples/tree.rb +37 -12
  25. data/examples/tsp.rb +34 -22
  26. data/lib/charlie.rb +5 -1
  27. data/lib/charlie/1.9fixes.rb +46 -0
  28. data/lib/charlie/crossover.rb +31 -14
  29. data/lib/charlie/etc/minireport.rb +5 -4
  30. data/lib/charlie/etc/monkey.rb +11 -8
  31. data/lib/charlie/gabenchmark.rb +230 -0
  32. data/lib/charlie/genotype.rb +4 -0
  33. data/lib/charlie/list/list_crossover.rb +25 -5
  34. data/lib/charlie/mutate.rb +34 -7
  35. data/lib/charlie/permutation/permutation.rb +34 -6
  36. data/lib/charlie/population.rb +12 -122
  37. data/lib/charlie/selection.rb +1 -0
  38. data/lib/charlie/tree/tree.rb +179 -17
  39. data/test/t_common.rb +1 -1
  40. data/test/test_benchmark.rb +19 -5
  41. data/test/test_cross.rb +23 -1
  42. data/test/test_evolve.rb +14 -1
  43. data/test/test_mutator.rb +28 -2
  44. data/test/test_permutation.rb +23 -1
  45. data/test/test_sel.rb +3 -1
  46. data/test/test_tree.rb +63 -1
  47. metadata +17 -25
  48. data/examples/bit.rb +0 -10
  49. data/examples/function_opt_2peak.rb +0 -24
  50. data/examples/function_opt_sombero.rb +0 -38
  51. data/examples/gladiatorial_simple.rb +0 -17
  52. data/examples/gridwalk.rb +0 -29
  53. data/examples/output/flattened_sombero.html +0 -6400
  54. data/examples/output/flattened_sombero2_.html +0 -3576
  55. data/examples/output/fopt1_dblopt.html +0 -2160
  56. data/examples/output/hill10.html +0 -5816
  57. data/examples/output/hill2.csv +0 -24
  58. data/examples/output/hill2.html +0 -384
  59. data/examples/output/royalroad2_report.html +0 -1076
  60. data/examples/output/royalroadquick_report.html +0 -504
  61. data/examples/output/weasel1_report.html +0 -1076
  62. data/examples/output/weasel2_report.html +0 -240
  63. data/examples/royalroad.rb +0 -26
  64. data/examples/royalroad2.rb +0 -18
  65. data/examples/simple_climb_hill2.rb +0 -47
  66. data/examples/weasel.rb +0 -36
@@ -1,3 +1,17 @@
1
+ == 0.7.0 / 2008-01-11
2
+ * Added 1.9fixes.rb which removes all calls to super to avoid bug #16493. Now 100% 1.9-compatible.
3
+ * N-point crossovers for lists.
4
+ * Improved/fixed minor bug in PCross, PMutate.
5
+ * Added PCrossN, PMutateN
6
+ * Option check_every added for evolve_until.
7
+ * Moved benchmark methods to GABenchmark, cleaned up code, added support for multiple stats.
8
+ * Save & output overall best solution in benchmark. Also output version information.
9
+ * Renamed PermutationMutator->TranspositionMutator
10
+ * Added InsertionMutator, RotationMutator
11
+ * Cleaned up examples, merged some, removed duplicates, added readme.
12
+ * Changed order of unary/binary in TreeGenotype, removed defaults.
13
+ * Extended tree mutator options.
14
+
1
15
  == 0.6.0 / 2007-12-26
2
16
  * Some fixes for some changes in Ruby 1.9
3
17
  * Edge recombination crossover added.
@@ -9,35 +9,26 @@ data/GENOTYPE
9
9
  data/MUTATION
10
10
  data/SELECTION
11
11
  data/template.html
12
- examples/bit.rb
13
- examples/function_opt_2peak.rb
14
- examples/function_opt_sombero.rb
15
- examples/gladiatorial_simple.rb
16
- examples/gladiatorial_sunburn.rb
17
- examples/gridwalk.rb
18
- examples/money.rb
19
- examples/output/flattened_sombero.html
20
- examples/output/flattened_sombero2_.html
21
- examples/output/fopt1_dblopt.html
22
- examples/output/hill10.html
23
- examples/output/hill2.csv
24
- examples/output/hill2.html
25
- examples/output/royalroad1_report.html
26
- examples/output/royalroad2_report.html
27
- examples/output/royalroadquick_report.html
12
+ examples/EXAMPLES_README.txt
13
+ examples/bitstring.rb
14
+ examples/coevolution.rb
15
+ examples/function_optimization.rb
16
+ examples/output/bitstring_royalroad.html
17
+ examples/output/function_optimization_sombrero.html
18
+ examples/output/function_optimization_twopeak.csv
19
+ examples/output/function_optimization_twopeak.html
20
+ examples/output/string_weasel.html
28
21
  examples/output/tsp.html
29
- examples/output/weasel1_report.html
30
- examples/output/weasel2_report.html
31
- examples/royalroad.rb
32
- examples/royalroad2.rb
33
- examples/simple_climb_hill2.rb
22
+ examples/permutation.rb
23
+ examples/string.rb
34
24
  examples/tree.rb
35
25
  examples/tsp.rb
36
- examples/weasel.rb
37
26
  lib/charlie.rb
27
+ lib/charlie/1.9fixes.rb
38
28
  lib/charlie/crossover.rb
39
29
  lib/charlie/etc/minireport.rb
40
30
  lib/charlie/etc/monkey.rb
31
+ lib/charlie/gabenchmark.rb
41
32
  lib/charlie/genotype.rb
42
33
  lib/charlie/list/list_crossover.rb
43
34
  lib/charlie/list/list_genotype.rb
data/README.txt CHANGED
@@ -1,4 +1,4 @@
1
- == Charlie version 0.6.0
1
+ == Charlie version 0.7.0
2
2
  * http://rubyforge.org/projects/charlie/
3
3
  * http://charlie.rubyforge.org
4
4
  * mailto:sander.land+ruby@gmail.com
@@ -45,7 +45,7 @@ This example solves a TSP problem (also quiz #142):
45
45
  Population.new(TSP,20).evolve_on_console(50)
46
46
 
47
47
  This example finds a polynomial which approximates cos(x)
48
- class Cos < TreeGenotype([proc{3*rand-1.5},:x], [:+,:*,:-], [:-@])
48
+ class Cos < TreeGenotype([proc{3*rand-1.5},:x], [:-@], [:+,:*,:-])
49
49
  def fitness
50
50
  -[0,0.33,0.66,1].map{|x| (eval_genes(:x=>x) - Math.cos(x)).abs }.max
51
51
  end
@@ -58,7 +58,7 @@ This example finds a polynomial which approximates cos(x)
58
58
 
59
59
  (The MIT License)
60
60
 
61
- Copyright (c) 2007 Sander Land
61
+ Copyright (c) 2007, 2008 Sander Land
62
62
 
63
63
  Permission is hereby granted, free of charge, to any person obtaining
64
64
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ h = Hoe.new('charlie', Charlie::VERSION) do |p|
14
14
  p.email = 'sander.land+ruby@gmail.com'
15
15
 
16
16
  p.summary = 'A genetic algorithms library for Ruby.'
17
- p.description = [1,2,6,7].map{|n| p.paragraphs_of('README.txt', n)}.join("\n\n")
17
+ p.description = [1,2,3,6,7].map{|n| p.paragraphs_of('README.txt', n)}.join("\n\n")
18
18
 
19
19
  p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
20
20
 
data/TODO.txt CHANGED
@@ -1,30 +1,20 @@
1
1
  This is my todo list. Contributions and suggestions don't have to be restricted to these things in any way.
2
2
 
3
3
  === Small stuff
4
- * clean up PCross, PMutate
5
- * n-point crossover for lists
6
- * external elitism (or whatever this is called). apply elitism but do not allow the elite to reproduce.
7
- * move benchmark method(s) to separate class(?)
4
+ * Arithmetic crossovers, Brood crossover?
8
5
  === Bigger stuff
9
6
  * More builtin genotypes:
10
- 1. Tree-based, extending into genetic programming. (partially done, mutation options to be extended)
11
- 2. Graphs / adjacency matrix. -- or multidimensional arrays in general?
12
- 3. Neural networks, Perceptron
13
- * Method for population to do several shorter runs until convergence.
14
- * Method for population to do several shorter runs, save some the best in each to generate a new population, and evolve this. is this useful?
15
- * More selection algorithms. Multiple populations w migration? niching?
7
+ * Tree-based, extending into genetic programming. mostly done. add option for ADFs?
8
+ * Graphs / adjacency matrix. Multidimensional arrays in general?
9
+ * Neural networks.
10
+ * Method for population to do several shorter runs until convergence (cf. gridwalk example).
11
+ * Method for population to do several shorter runs, save some the best in each to generate a new population, and evolve this. Is this useful?
12
+ * More selection algorithms. Multiple populations with migration? Niching? gladiatorial/tournaments (+Prisoner's dilemma example)?
16
13
  * Extend benchmarking options:
17
- 1. Extend dsl to have options for manual test cases given by sel/cross/mut/etc options, or even free code blocks.
18
- 2. Option to compare several generation/population size settings. Especially both of them together while keeping the number of fitness evaluations roughly constant.
19
- 3. Detailed stats for use in comparing a small number of strategies? Including population stats for every generation and run, graphs?
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
14
+ * Extend dsl to have options for manual test cases given by sel/cross/mut/etc options, or even free code blocks.
15
+ * Option to compare several generation/population size settings. Especially both of them together while keeping the number of fitness evaluations roughly constant.
16
+ * Instead of (2), constraint on time, or # of fitness evals instead of generations. (?)
17
+ * Detailed stats for use in comparing a small number of strategies? Including population stats for every generation and run, graphs?
28
18
 
29
19
 
30
20
 
@@ -1,53 +1,55 @@
1
1
  == Benchmark documentation
2
- Population#benchmark can compare several selection, crossover and mutation methods and give a report comparing their
2
+ GABenchmark.benchmark can compare several selection, crossover and mutation methods and give a report comparing their
3
3
  performance, both for convergence and speed.
4
4
 
5
- The call to the function is:
5
+ An example for a call to the function is:
6
6
 
7
- Population.benchmark(genotype_class,html_output_file,csv_output_file=nil) {
7
+ GABenchmark.benchmark(YourGenotypeClass,'html_output.html','csv_output.csv') {
8
8
  selection RandomSelection, TournamentSelection(3)
9
9
  crossover SinglePointCrossover
10
- mutator ListMutator(
10
+ mutator ListMutator(:single_point,:uniform[0.25]), ListMutator(:expected_n[2],:gaussian[0.2])
11
+ repeat 5
12
+ population_size 27
11
13
  }
12
14
 
13
- * +genotype_class+ is simply your genotype class.
14
- * +html_output_file+ is an output file where a large report with several tables of statistics will be written. Pass nil for no output file of this type.
15
- * +csv_output_file+ is an output file where the raw data (the maximum fitness value for each run) will be written. Pass nil or omit parameter for no output file of this type.
16
-
15
+ * YourGenotypeClass is the class which has Genotype as an ancestor and which has fitness function.
16
+ * <tt>html_output.html</tt> is an output file where a large report with several tables of statistics will be written. Pass nil for no output file of this type.
17
+ * <tt>csv_output.csv</tt> is an output file where the raw data (the maximum fitness value for each run) will be written. Pass nil or omit parameter for no output file of this type.
17
18
 
18
19
 
19
20
  In the block you can call several functions to specify the parameters of the benchmark.
20
21
  This is sometimes refered to as a DSL. As inaccurate as that term may be, I'll still use it here.
21
22
 
22
23
 
23
- === Benchmark DSL documentation (also see StrategiesDSL)
24
- In the block you must call the following functions:
24
+ === Benchmark DSL documentation (also see GABenchmark::StrategiesDSL)
25
+ In the block you can call the following functions:
25
26
 
26
- ==== selection(*s_args)
27
+ ==== <tt>selection(*s_args)</tt>
27
28
  Will cause each of the selection modules in +s_args+ to be tested.
28
- ==== crossover(*c_args)
29
+ ==== <tt>crossover(*c_args)</tt>
29
30
  Will cause each of the crossover modules in +c_args+ to be tested.
30
- ==== mutator(*m_args)
31
+ ==== <tt>mutator(*m_args)</tt> or <tt>mutation(*m_args)</tt>
31
32
  Will cause each of the mutation modules in +m_args+ to be tested.
32
33
 
33
-
34
- If you want to test the module that is already included without repeating it, just pass Module.new as one of the arguments (or Module.new{self.name='default'} for something more descriptive).
35
-
36
-
37
- You can also call the following functions to change some other settings:
38
-
39
- ==== self.generations = g
34
+ ==== <tt>generations g</tt>
40
35
  Changes the number of generations in each test to +g+. Default is 50.
41
36
 
42
- ==== self.population_size = s
37
+ ==== <tt>population_size s</tt>
43
38
  Changes the population size in each test to +g+. Default is 20.
44
39
 
45
- ==== self.repeat = r
40
+ ==== <tt>repeat r</tt>
46
41
  Changes the number of times each test will be repeated to +r+. Default is 10, a higher number here will take longer to
47
42
  complete, but will improve the accuracy of the results.
48
43
 
44
+ ==== <tt>track_stat{|best| best.fitness }</tt> or <tt>track_stats{|best| [best.fitness, best.some_other_stat] }</tt>
45
+ * Changes the statistic you want to track, the individual with the highest fitness at the end of a run is yielded to the block.
46
+ * Default is the fitness itself.
47
+ * This can be an array, if you want to track multiple stats at once (for example: tracking both training and generalization error).
49
48
 
50
-
49
+ ==== Remarks
50
+ * If you want to test the module that is already included without repeating it, just pass Module.new as one of the arguments (or Module.new{self.name='default'} for something more descriptive). This is also the default, so <tt>GABenchmark.benchmark(klass){}</tt> will simply test the strategies already included.
51
+ * The methods <tt>selection, crossover, mutator, generations, population_size, repeat</tt> are defined using metaprogramming as <tt>attr_dsl</tt>. Passing no arguments to the function will return the current value, passing more will assign the value. This means you can do things like <tt>selection << TournamentSelection(3)</tt> in the block.
52
+ * You can also use calls like <tt>self.repeat = 10</tt>, which are aliases.
51
53
 
52
54
 
53
55
 
@@ -29,6 +29,9 @@ Standard single point crossover. Returns two children.
29
29
  === UniformCrossover
30
30
  Standard uniform crossover.Returns two children.
31
31
 
32
+ === NPointCrossover(n=2)
33
+ N-point crossover. Returns two children.
34
+
32
35
 
33
36
  == Specialized Crossovers
34
37
  === For PermutationGenotype
@@ -50,5 +53,6 @@ Applies an arbitrary crossover and returns a random child.
50
53
  === PCross(p,crossover,othercrossover=NullCrossover)
51
54
  Applies an arbitrary crossover with probability p, and another crossover with probability 1-p.
52
55
 
53
-
56
+ === PCrossN(hash of module=>probability pairs)
57
+ Version of PCross for more than two crossover operators. If sum(probability) < 1, NullCrossover will be used for the remaining probability.
54
58
 
@@ -1,5 +1,5 @@
1
1
  == Genotype documentation
2
- The genotype, aka genome, aka chromosomes is a simple representation of a solution to your problem.
2
+ The genotype, aka genome is a simple representation of a solution to your problem.
3
3
  This is usually an array of numbers or bits.
4
4
 
5
5
  === Creating your own.
@@ -23,7 +23,7 @@ They are all generated dynamically by functions, and should be used like:
23
23
 
24
24
 
25
25
  ==== FloatListGenotype(n,range=0..1)
26
- Genotype of +n+ floats in the range +range+.
26
+ Genotype of +n+ floats initialized in the range +range+. Mutations can take the floats out of this range.
27
27
 
28
28
  Includes <tt>ListMutator()</tt> (with default parameters :expected_n[3], :uniform[ 0.25 ]) and <tt>SinglePointCrossover</tt> by default.
29
29
 
@@ -41,11 +41,11 @@ Includes <tt>ListMutator(:expected_n[2],:replace[*elements]), SinglePointCrossov
41
41
  These genotypes have their own crossover and mutation operators.
42
42
 
43
43
  ==== PermutationGenotype(n,elements=0...n) (file)[link:files/lib/charlie/permutation/permutation_rb.html]
44
- Genotype for permutations. Includes PermutationMutator and PermutationCrossover by default.
44
+ Genotype for permutations. Includes TranspositionMutator and PermutationCrossover by default.
45
45
 
46
- ==== TreeGenotype(terminals = [proc{rand},:x], binary_ops = [:+,:*,:-,:/], unary_ops = nil) (file)[link:files/lib/charlie/tree/tree_rb.html]
47
- * Genotype for trees. Useful for genetic programming. Includes TreeMutator and TreeCrossover by default.
48
- * Functions included: eval_genes(terminal_values_hash), size, module GPTreeHelper
46
+ ==== TreeGenotype(terminals, unary_ops, binary_ops) (file)[link:files/lib/charlie/tree/tree_rb.html]
47
+ * Genotype for trees. Useful for genetic programming. Includes PMutate(0.5,TreeReplaceMutator) and PCross(0.7,TreeCrossover) by default.
48
+ * Functions included: eval_genes(terminal_values_hash), size, depth, module GPTreeHelper
49
49
  * Check examples/tree.rb for usage.
50
50
 
51
51
 
@@ -3,7 +3,7 @@ A mutation operator mutates a single instance in place.
3
3
 
4
4
 
5
5
 
6
- The mutation operator should be defined as a +mutate!+ method in your genotype class.
6
+ The mutation operator should be defined as a <tt>mutate!</tt> method in your genotype class.
7
7
  class Example < Genotype
8
8
  def mutate!
9
9
  # apply mutation here.
@@ -31,12 +31,22 @@ Both of these parameters use Symbol#[] in the examples. <tt>:replace['a','b']</t
31
31
 
32
32
  == Specialized Mutators
33
33
  === For PermutationGenotype
34
- PermutationMutator is a transposition mutator for permutations.
35
-
36
- InversionMutator is another mutator for permutations. It inverses a part of the genes array.
37
-
38
- === For TreeGenotype
39
- TreeMutator replaces a randomly chosen subtree with a new, randomly generated, subtree of depth <= 2.
34
+ All of these will probably work on other array/string-based genotypes as well, but this is untested.
35
+
36
+ * TranspositionMutator is a transposition mutator for permutations. It interchanges two elements and leaves the remaining elements in their original positions.
37
+ * InversionMutator is another mutator for permutations. It inverses a part of the genes array.
38
+ * InsertionMutator takes a random element of the permutation, and inserts it at a random position.
39
+ * RotationMutator takes a random element, and rotates the permutation such that this is the first element, i.e. it effectively does nothing if the permutation represents a cycle.
40
+
41
+ === For TreeGenotype (file)[link:files/lib/charlie/tree/tree_rb.html]
42
+ * TreeReplaceMutator(depth,type) replaces a randomly chosen subtree with a new, randomly generated, subtree.
43
+ * TreePruneMutator replaces a randomly chosen subtree with a randomly generated terminal.
44
+ * TreeRemoveNodeMutator replaces a random node by one of its children.
45
+ * TreeChopMutator replaces the root by one of its children.
46
+ * TreeInsertNodeMutator replaces a random node x by a new operator node having x as one of its children.
47
+ * TreeTerminalMutator replaces a random terminal by a new one.
48
+ * TreeNumTerminalMutator(mutate=:uniform[ 0.1 ]) mutates a random numeric terminal.
49
+ * TreeEvalMutator(values_hash=Hash{0}) replaces a random terminal by the result of its evaluation.
40
50
 
41
51
  == Meta-mutators
42
52
  These functions take one or more mutator modules and generate a new mutator module.
@@ -44,5 +54,7 @@ These functions take one or more mutator modules and generate a new mutator modu
44
54
  === PMutate(p,mutator,othermutator=NullMutator)
45
55
  Applies an arbitrary mutator with probability p, and another mutator with probability 1-p.
46
56
 
57
+ === PMutateN(hash of module=>probability pairs)
58
+ Version of PMutate for more than two mutators. If sum(probability) < 1, NullMutator will be used for the remaining probability.
47
59
 
48
60
 
@@ -6,7 +6,8 @@ The selection strategy should be defined as a +next_generation+ method in the me
6
6
  ...
7
7
  class << self
8
8
  def next_generation(population)
9
- # select parents and yield(parent1,parent2) them to the crossover and mutation operator. Create an array from several of these calls to get the next generation.
9
+ # select parents and yield(parent1,parent2) them to the crossover and mutation operator.
10
+ # Create an array from several of these calls to get the next generation.
10
11
  end
11
12
  end
12
13
  use RouletteSelection # or include some builtin operator
@@ -8,6 +8,7 @@
8
8
  table{
9
9
  background: #ccc;
10
10
  font-family: Verdana, Helvetica, sans-serif;
11
+ font-size: small;
11
12
  }
12
13
  th {
13
14
  background: #666;
@@ -18,7 +19,7 @@
18
19
  td {
19
20
  color: #333;
20
21
  background: #e2e2e2;
21
- padding: 2px;
22
+ padding: 2px 3px;
22
23
  text-align: center;
23
24
  }
24
25
  </style>
@@ -0,0 +1,70 @@
1
+ In this directory are several examples of how to use the library.
2
+ This file serves as an index, which can be used to find an example for a specific function.
3
+
4
+ ==== function_optimization.rb ====
5
+ - ruby function_optimization.rb hill
6
+ Description: Function optimization example in many dimensions, hill climbing.
7
+ Uses: FloatListGenotype, TournamentSelection, UniformCrossover, ListMutator
8
+
9
+ - ruby function_optimization.rb twopeak
10
+ Description: Function optimization example in 1 dimension, avoiding a local maximum.
11
+ Uses: FloatListGenotype (single float), NullCrossover, GABenchmark
12
+
13
+ - ruby function_optimization.rb sombrero
14
+ Description: Function optimization example in 2 dimensions, avoiding many maxima.
15
+ Uses: FloatListGenotype, GABenchmark
16
+
17
+ ==== bitstring.rb ====
18
+ - ruby bitstring.rb 512
19
+ Description: Very simple example that finds the binary representation of 512.
20
+ Uses: BitStringGenotype
21
+
22
+ - ruby bitstring.rb royalroad
23
+ Description: Example for convergence with limited feedback from the fitness function.
24
+ Uses: BitStringGenotype, TruncationSelection, UniformCrossover, ListMutator, cache_fitness, GABenchmark
25
+
26
+ ==== string.rb ====
27
+ - ruby string.rb weasel
28
+ Description: Simple target string evolver.
29
+ Uses: StringGenotype, BestOnlySelection, UniformCrossover, ListMutator, GABenchmark
30
+
31
+ - ruby string.rb gridwalk
32
+ Description: Example of using strings of symbols other than chars to evolve a route through a grid.
33
+ Uses: StringGenotype, TournamentSelection, evolve_until
34
+
35
+ ==== tsp.rb ====
36
+ - ruby tsp.rb
37
+ Description: Travelling salesperson problem.
38
+ Uses: PermutationGenotype, PCross, PMutateN, evolve_until, TournamentSelection,
39
+ EdgeRecombinationCrossover, InversionMutator ,InsertionMutator, GABenchmark
40
+
41
+ ==== permutation.rb ====
42
+ - ruby permutation.rb
43
+ Description: send+more=money problem with PermutationGenotype.
44
+ Uses: PermutationGenotype
45
+
46
+ ==== coevolution.rb ====
47
+ - ruby coevolution.rb simple
48
+ Description: Simple co-evolutionary example. Evolves a two-element array to be [large,small]
49
+ Uses: GladiatorialSelection, FloatListGenotype, UniformCrossover, ListMutator
50
+
51
+ - ruby coevolution.rb sunburn
52
+ Description: More elaborate example of co-evolution. Evolves weapon systems on space ships (like Ashlock (2004), ch.4).
53
+ Uses: GladiatorialSelection, StringGenotype, custom mutation/crossover, UniformCrossover
54
+
55
+ ==== tree.rb ====
56
+ - ruby tree.rb cos
57
+ Description: Example for TreeGenotype/GP. Evolves a polynomial that approximates cos(x).
58
+ Uses: TreeGenotype, ...
59
+
60
+ - ruby tree.rb pors [n=31]
61
+ Description: Plus One Recall Store example. Generate a number n using a minimal # of +,1,STO,RCL operations.
62
+ Uses: TreeGenotype, passing proc{}'s to node values at evaluation, custom unary operators.
63
+
64
+ - ruby tree.rb porsx [n=31]
65
+ Description: Same idea as PORS, but generate a tree that evaluates to n*T where T can only be recalled once.
66
+
67
+ - ruby tree.rb bloat
68
+ Description: Just grows large trees.
69
+
70
+
@@ -0,0 +1,72 @@
1
+ begin
2
+ require '../lib/charlie'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'charlie'
6
+ end
7
+
8
+ if ARGV[0].nil?
9
+ puts "Several examples for bit string genotypes."
10
+ puts "Usage: ruby bitstring.rb 512|royalroad"
11
+
12
+
13
+ elsif ARGV[0].downcase.gsub(/[^0-9]/,'') == '512'
14
+
15
+
16
+ puts "Simple test with a bit string. Find the binary representation of 512."
17
+
18
+ class Test < BitStringGenotype(10)
19
+ def fitness
20
+ -(genes.map(&:to_s).join.to_i(2) - 512).abs # or just to_s.to_i(2)
21
+ end
22
+ end
23
+ Population.new(Test).evolve_on_console
24
+
25
+
26
+
27
+ elsif ARGV[0].downcase.gsub(/[^a-z]/,'') == 'royalroad'
28
+
29
+
30
+
31
+ puts "The Royal Road problem."
32
+ puts "Find a bit string of 64 ones, while only getting a fitness bonus for groups of 8 ones."
33
+ puts "Useful as a test for convergence in situations where there is a lack of feedback."
34
+
35
+
36
+ class RoyalRoad < BitStringGenotype(64) # Royal Road problem
37
+ def fitness
38
+ 1 + genes.enum_slice(8).find_all{|e| e.all?{|x|x==1} }.size # +1 to avoid all fitness 0 for roulette
39
+ end
40
+ use TruncationSelection(0.3), UniformCrossover, ListMutator([:expected_n, 4],:flip)
41
+ cache_fitness
42
+ end
43
+
44
+ Population.new(RoyalRoad).evolve_on_console(100)
45
+
46
+
47
+
48
+ output_file = 'output/bitstring_royalroad.html';
49
+
50
+ puts "Running benchmark. Takes about 3 minutes on Ruby 1.9. Output in #{output_file}"
51
+
52
+ puts "Press [Enter] to continue, or Ctrl-C to abort."
53
+ STDIN.gets
54
+
55
+ GABenchmark.benchmark(RoyalRoad,output_file){
56
+ selection TruncationSelection(0.3),
57
+ Elitism(ScaledRouletteSelection),
58
+ Elitism(RouletteSelection),
59
+ BestOnlySelection,
60
+ RouletteSelection
61
+
62
+ crossover NullCrossover,
63
+ SinglePointCrossover,
64
+ UniformCrossover
65
+
66
+ mutator *(1..6).map{|n| ListMutator(:expected_n[n],:flip) }
67
+ mutator << NullMutator
68
+ generations 100
69
+ }
70
+
71
+
72
+ end