wallace 0.0.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 (99) hide show
  1. data/Gemfile +14 -0
  2. data/README.md +12 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/bin/.gitkeep +0 -0
  6. data/lib/analysers/fitness_distribution_analyser.rb +28 -0
  7. data/lib/analysers/init.rb +1 -0
  8. data/lib/core/analyser.rb +63 -0
  9. data/lib/core/breeder.rb +68 -0
  10. data/lib/core/breeding_graph.rb +59 -0
  11. data/lib/core/breeding_graph/init.rb +3 -0
  12. data/lib/core/breeding_graph/input_node.rb +55 -0
  13. data/lib/core/breeding_graph/node.rb +68 -0
  14. data/lib/core/breeding_graph/node_input.rb +6 -0
  15. data/lib/core/evaluator.rb +52 -0
  16. data/lib/core/evolver.rb +47 -0
  17. data/lib/core/exceptions.rb +6 -0
  18. data/lib/core/experiment.rb +22 -0
  19. data/lib/core/fitness.rb +8 -0
  20. data/lib/core/fraction.rb +7 -0
  21. data/lib/core/individual.rb +65 -0
  22. data/lib/core/logger.rb +5 -0
  23. data/lib/core/migrator.rb +6 -0
  24. data/lib/core/operator.rb +54 -0
  25. data/lib/core/population.rb +56 -0
  26. data/lib/core/selector.rb +43 -0
  27. data/lib/core/species.rb +52 -0
  28. data/lib/core/state.rb +29 -0
  29. data/lib/core/subpopulation.rb +53 -0
  30. data/lib/core/termination.rb +39 -0
  31. data/lib/distributions/gaussian_distribution.rb +60 -0
  32. data/lib/distributions/init.rb +3 -0
  33. data/lib/fitness/init.rb +1 -0
  34. data/lib/fitness/raw_fitness.rb +30 -0
  35. data/lib/loggers/csv_logger.rb +20 -0
  36. data/lib/loggers/init.rb +1 -0
  37. data/lib/loggers/mongo_logger.rb +5 -0
  38. data/lib/loggers/sqlite_logger.rb +5 -0
  39. data/lib/modules/ge/backus_naur_form.rb +125 -0
  40. data/lib/modules/ge/grammar_derivation.rb +30 -0
  41. data/lib/modules/ge/grammar_species.rb +29 -0
  42. data/lib/modules/ge/init.rb +5 -0
  43. data/lib/modules/init.rb +2 -0
  44. data/lib/modules/koza/builder.rb +48 -0
  45. data/lib/modules/koza/builder/full_builder.rb +92 -0
  46. data/lib/modules/koza/builder/grow_builder.rb +103 -0
  47. data/lib/modules/koza/builder/half_builder.rb +70 -0
  48. data/lib/modules/koza/builder/init.rb +3 -0
  49. data/lib/modules/koza/ephemeral.rb +33 -0
  50. data/lib/modules/koza/init.rb +12 -0
  51. data/lib/modules/koza/koza_node.rb +108 -0
  52. data/lib/modules/koza/koza_node_value.rb +72 -0
  53. data/lib/modules/koza/koza_node_value_set.rb +51 -0
  54. data/lib/modules/koza/koza_species.rb +48 -0
  55. data/lib/modules/koza/koza_tree.rb +159 -0
  56. data/lib/modules/koza/operators/init.rb +4 -0
  57. data/lib/modules/koza/operators/subtree_crossover_operation.rb +43 -0
  58. data/lib/modules/koza/operators/subtree_mutation_operation.rb +32 -0
  59. data/lib/operators/bit_flip_mutation_operation.rb +29 -0
  60. data/lib/operators/boundary_mutation_operation.rb +28 -0
  61. data/lib/operators/cycle_crossover_operation.rb +77 -0
  62. data/lib/operators/gaussian_mutation_operation.rb +39 -0
  63. data/lib/operators/half_uniform_crossover_operation.rb +24 -0
  64. data/lib/operators/init.rb +26 -0
  65. data/lib/operators/merging_crossover_operation.rb +27 -0
  66. data/lib/operators/one_point_crossover_operation.rb +29 -0
  67. data/lib/operators/order_crossover_operation.rb +38 -0
  68. data/lib/operators/partially_mapped_crossover_operation.rb +44 -0
  69. data/lib/operators/point_mutation_operation.rb +31 -0
  70. data/lib/operators/position_crossover_operation.rb +50 -0
  71. data/lib/operators/reverse_sequence_mutation_operation.rb +13 -0
  72. data/lib/operators/shuffle_mutation_operation.rb +17 -0
  73. data/lib/operators/splice_crossover_operation.rb +42 -0
  74. data/lib/operators/subtour_exchange_crossover_operation.rb +54 -0
  75. data/lib/operators/swap_mutation_operation.rb +29 -0
  76. data/lib/operators/three_parent_crossover_operation.rb +16 -0
  77. data/lib/operators/two_point_crossover_operation.rb +31 -0
  78. data/lib/operators/twors_mutation_operation.rb +18 -0
  79. data/lib/operators/uniform_crossover_operation.rb +30 -0
  80. data/lib/operators/uniform_mutation_operation.rb +31 -0
  81. data/lib/operators/variable_one_point_crossover_operation.rb +80 -0
  82. data/lib/patches/enumerable.rb +85 -0
  83. data/lib/patches/init.rb +5 -0
  84. data/lib/patches/range.rb +13 -0
  85. data/lib/selectors/init.rb +5 -0
  86. data/lib/selectors/random_selector.rb +14 -0
  87. data/lib/selectors/roulette_selector.rb +23 -0
  88. data/lib/selectors/tournament_selector.rb +36 -0
  89. data/lib/species/array_species.rb +40 -0
  90. data/lib/species/bit_string_species.rb +18 -0
  91. data/lib/species/init.rb +4 -0
  92. data/lib/species/permutation_species.rb +22 -0
  93. data/lib/species/string_species.rb +29 -0
  94. data/lib/utility/init.rb +4 -0
  95. data/lib/utility/scaled_array.rb +88 -0
  96. data/lib/utility/sorted_array.rb +39 -0
  97. data/lib/wallace.rb +40 -0
  98. data/test/.gitkeep +0 -0
  99. metadata +248 -0
@@ -0,0 +1,18 @@
1
+ # Bit-strings are implemented as a specialised form of array species that are
2
+ # confined to only containing 0 and 1 integers.
3
+ class Wallace::Species::BitStringSpecies < Wallace::Species::ArraySpecies
4
+
5
+ name = :bit_string
6
+
7
+ # Constructs a new bit-string based species.
8
+ #
9
+ # *Parameters:*
10
+ # * opts, hash of keyword options used by this method.
11
+ # -> id, the unique identifier for this species.
12
+ # -> length, the length constraints imposed on individuals of this species.
13
+ def initialize(opts = {})
14
+ opts[:length] = [0, 1]
15
+ super(opts)
16
+ end
17
+
18
+ end
@@ -0,0 +1,4 @@
1
+ require_relative 'array_species.rb'
2
+ require_relative 'string_species.rb'
3
+ require_relative 'bit_string_species.rb'
4
+ require_relative 'permutation_species.rb'
@@ -0,0 +1,22 @@
1
+ class Wallace::Species::PermutationSpecies < Wallace::Species
2
+
3
+ # Constructs a new permutation species.
4
+ #
5
+ # *Parameters:*
6
+ # * opts, hash of keyword options used by this method.
7
+ # -> id, the unique identifier for this species.
8
+ # -> values, the list of values to be permuted.
9
+ def initialize(opts = {})
10
+ super(opts)
11
+ @values = opts[:values]
12
+ end
13
+
14
+ # Spawns a new permutation of this species list of values.
15
+ #
16
+ # *Parameters:*
17
+ # * rng, random number generation to use during process.
18
+ def spawn(rng)
19
+ Wallace::Individual.new(self, @values.shuffle(random: rng))
20
+ end
21
+
22
+ end
@@ -0,0 +1,29 @@
1
+ # String-based species represent their members as strings. Species may impose
2
+ # constraints on the length and alphabet of these strings. If more constraints
3
+ # are desired then the species can be easily extended or sub-classed to
4
+ # implement those constraints.
5
+ class Wallace::Species::StringSpecies < Wallace::Species::ArraySpecies
6
+
7
+ name = :string
8
+
9
+ # Constructs a new string-based species.
10
+ #
11
+ # *Parameters:*
12
+ # * opts, hash of keyword options used by this method.
13
+ # -> id, the unique identifier for this species.
14
+ # -> length, the length constraints imposed on individuals of this species.
15
+ # -> alphabet, the alphabet used to generate strings.
16
+ def initialize(opts = {})
17
+ opts[:values] = opts.delete(:alphabet)
18
+ super(opts)
19
+ end
20
+
21
+ # Creates a new string-based individual using the alphabet and length
22
+ # constraints of this species.
23
+ def spawn(rng)
24
+ ind = super(rng)
25
+ ind.data = ind.data.join
26
+ return ind
27
+ end
28
+
29
+ end
@@ -0,0 +1,4 @@
1
+ module Wallace::Utility; end
2
+
3
+ require_relative "scaled_array"
4
+ require_relative "sorted_array"
@@ -0,0 +1,88 @@
1
+ class Wallace::Utility::ScaledArray
2
+
3
+ def initialize(items, probabilities)
4
+ @items = items
5
+ @cprob = []
6
+
7
+ # Convert the probabilities into cumulative probabilities.
8
+ sum = 0.0
9
+ for p in probabilities
10
+ @cprob << sum
11
+ sum += p
12
+ end
13
+
14
+ end
15
+
16
+ def sample
17
+ point = Random.rand
18
+ return self.select(point)
19
+ end
20
+
21
+ def select(point)
22
+
23
+ # Initialise the lower and upper bounds of the binary search
24
+ # at either end of the distribution.
25
+ lb = 0
26
+ ub = @items.length - 1
27
+
28
+ # Keep searching until the upper and lower bound converge.
29
+ until lb == ub
30
+
31
+ # Select the point in between the lower and upper bound
32
+ # as the pivot.
33
+ pivot = lb + ((ub - lb) / 2)
34
+
35
+ # If the point is less than the start of the area for
36
+ # the pivot, move the upper bound back one.
37
+ if point < @cprob[pivot]
38
+ ub = pivot - 1
39
+
40
+ # Check if the point lies within the bounds of the pivot.
41
+ elsif pivot == ub or point < @cprob[pivot+1]
42
+ lb = ub = pivot
43
+
44
+ # If it doesn't, then move the lower bound forward.
45
+ else
46
+ lb = pivot + 1
47
+ end
48
+
49
+
50
+ end
51
+
52
+ return @items[lb]
53
+
54
+ end
55
+
56
+
57
+ def select_old(point)
58
+
59
+ # If there is only a single item in the distribution,
60
+ # then return that item.
61
+ return @items[0] if @items.length == 1
62
+
63
+ # Initialise the lower and upper bounds of the binary search
64
+ # at either end of the distribution.
65
+ lb = 0
66
+ ub = @items.length - 1
67
+
68
+ # Keep searching until the upper and lower bound converge.
69
+ until lb == ub
70
+
71
+ # Select the point in between the lower and upper bound
72
+ # as the pivot.
73
+ pivot = lb + ((ub - lb) / 2)
74
+
75
+ if point <= @cprob[pivot]
76
+ ub = pivot
77
+ else
78
+ lb = pivot + 1
79
+ end
80
+
81
+
82
+ end
83
+
84
+ return @items[pivot]
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,39 @@
1
+ class Wallace::Utility::SortedArray < Array
2
+
3
+ def initialize(array = nil)
4
+ super(array.sort) if array != nil
5
+ end
6
+
7
+ def set(values)
8
+
9
+ end
10
+
11
+ # Inserts a value into the array at its correct sorted position.
12
+ def <<(value)
13
+ insert(calculate_index(value), value)
14
+ end
15
+
16
+ def push(*values)
17
+ values.each{|v| self << v}
18
+ return self
19
+ end
20
+
21
+ # Calculates the correct index to insert the value into the
22
+ # array at.
23
+ #
24
+ # \*TODO:*
25
+ # * This is a rubbish O(n) implementation.
26
+ # | A RB-binary tree or a better insertion sort method should
27
+ # | be implemented.
28
+ def calculate_index(value)
29
+ i = 0
30
+ self.each { |v|
31
+ if v > value
32
+ break
33
+ end
34
+ i += 1
35
+ }
36
+ return i
37
+ end
38
+
39
+ end
@@ -0,0 +1,40 @@
1
+ module Wallace; end
2
+
3
+ # Load Peach if we're using JRuby.
4
+ require 'peach' if defined?(RUBY_VERSION) and RUBY_ENGINE == 'jruby'
5
+
6
+ # Load all monkey patches.
7
+ require_relative 'patches/init.rb'
8
+
9
+ # Load core files.
10
+ require_relative 'core/state.rb'
11
+ require_relative 'core/analyser.rb'
12
+ require_relative 'core/operator.rb'
13
+ require_relative 'core/selector.rb'
14
+ require_relative 'core/fitness.rb'
15
+ require_relative 'core/evaluator.rb'
16
+ require_relative 'core/species.rb'
17
+ require_relative 'core/breeder.rb'
18
+ require_relative 'core/migrator.rb'
19
+ require_relative 'core/termination.rb'
20
+ require_relative 'core/population.rb'
21
+ require_relative 'core/subpopulation.rb'
22
+ require_relative 'core/evolver.rb'
23
+ require_relative 'core/individual.rb'
24
+ require_relative 'core/fraction.rb'
25
+ require_relative 'core/exceptions.rb'
26
+ require_relative 'core/breeding_graph.rb'
27
+ require_relative 'core/breeding_graph/init.rb'
28
+
29
+ # Load all default selectors, species, etc.
30
+ require_relative 'distributions/init.rb'
31
+ require_relative 'selectors/init.rb'
32
+ require_relative 'species/init.rb'
33
+ require_relative 'operators/init.rb'
34
+ require_relative 'fitness/init.rb'
35
+
36
+ # Load all modules.
37
+ require_relative 'modules/init.rb'
38
+
39
+ # Load utilities.
40
+ require_relative 'utility/init.rb'
File without changes
metadata ADDED
@@ -0,0 +1,248 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wallace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Timperley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: peach
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.7
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: peach
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.5.1
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.5.1
110
+ description: ! " Wallace is a powerful, flexible and modular toolkit for running
111
+ Evolutionary Algorithms in Ruby.\n Out of the box it provides support for steady-state
112
+ EAs, genetic algorithms, genetic programming,\n grammatical evolution and other
113
+ types of EAs.\n"
114
+ email: christimperley@gmail.com
115
+ executables:
116
+ - .gitkeep
117
+ extensions: []
118
+ extra_rdoc_files:
119
+ - README.md
120
+ files:
121
+ - Gemfile
122
+ - README.md
123
+ - Rakefile
124
+ - VERSION
125
+ - bin/.gitkeep
126
+ - lib/analysers/fitness_distribution_analyser.rb
127
+ - lib/analysers/init.rb
128
+ - lib/core/analyser.rb
129
+ - lib/core/breeder.rb
130
+ - lib/core/breeding_graph.rb
131
+ - lib/core/breeding_graph/init.rb
132
+ - lib/core/breeding_graph/input_node.rb
133
+ - lib/core/breeding_graph/node.rb
134
+ - lib/core/breeding_graph/node_input.rb
135
+ - lib/core/evaluator.rb
136
+ - lib/core/evolver.rb
137
+ - lib/core/exceptions.rb
138
+ - lib/core/experiment.rb
139
+ - lib/core/fitness.rb
140
+ - lib/core/fraction.rb
141
+ - lib/core/individual.rb
142
+ - lib/core/logger.rb
143
+ - lib/core/migrator.rb
144
+ - lib/core/operator.rb
145
+ - lib/core/population.rb
146
+ - lib/core/selector.rb
147
+ - lib/core/species.rb
148
+ - lib/core/state.rb
149
+ - lib/core/subpopulation.rb
150
+ - lib/core/termination.rb
151
+ - lib/distributions/gaussian_distribution.rb
152
+ - lib/distributions/init.rb
153
+ - lib/fitness/init.rb
154
+ - lib/fitness/raw_fitness.rb
155
+ - lib/loggers/csv_logger.rb
156
+ - lib/loggers/init.rb
157
+ - lib/loggers/mongo_logger.rb
158
+ - lib/loggers/sqlite_logger.rb
159
+ - lib/modules/ge/backus_naur_form.rb
160
+ - lib/modules/ge/grammar_derivation.rb
161
+ - lib/modules/ge/grammar_species.rb
162
+ - lib/modules/ge/init.rb
163
+ - lib/modules/init.rb
164
+ - lib/modules/koza/builder.rb
165
+ - lib/modules/koza/builder/full_builder.rb
166
+ - lib/modules/koza/builder/grow_builder.rb
167
+ - lib/modules/koza/builder/half_builder.rb
168
+ - lib/modules/koza/builder/init.rb
169
+ - lib/modules/koza/ephemeral.rb
170
+ - lib/modules/koza/init.rb
171
+ - lib/modules/koza/koza_node.rb
172
+ - lib/modules/koza/koza_node_value.rb
173
+ - lib/modules/koza/koza_node_value_set.rb
174
+ - lib/modules/koza/koza_species.rb
175
+ - lib/modules/koza/koza_tree.rb
176
+ - lib/modules/koza/operators/init.rb
177
+ - lib/modules/koza/operators/subtree_crossover_operation.rb
178
+ - lib/modules/koza/operators/subtree_mutation_operation.rb
179
+ - lib/operators/bit_flip_mutation_operation.rb
180
+ - lib/operators/boundary_mutation_operation.rb
181
+ - lib/operators/cycle_crossover_operation.rb
182
+ - lib/operators/gaussian_mutation_operation.rb
183
+ - lib/operators/half_uniform_crossover_operation.rb
184
+ - lib/operators/init.rb
185
+ - lib/operators/merging_crossover_operation.rb
186
+ - lib/operators/one_point_crossover_operation.rb
187
+ - lib/operators/order_crossover_operation.rb
188
+ - lib/operators/partially_mapped_crossover_operation.rb
189
+ - lib/operators/point_mutation_operation.rb
190
+ - lib/operators/position_crossover_operation.rb
191
+ - lib/operators/reverse_sequence_mutation_operation.rb
192
+ - lib/operators/shuffle_mutation_operation.rb
193
+ - lib/operators/splice_crossover_operation.rb
194
+ - lib/operators/subtour_exchange_crossover_operation.rb
195
+ - lib/operators/swap_mutation_operation.rb
196
+ - lib/operators/three_parent_crossover_operation.rb
197
+ - lib/operators/two_point_crossover_operation.rb
198
+ - lib/operators/twors_mutation_operation.rb
199
+ - lib/operators/uniform_crossover_operation.rb
200
+ - lib/operators/uniform_mutation_operation.rb
201
+ - lib/operators/variable_one_point_crossover_operation.rb
202
+ - lib/patches/enumerable.rb
203
+ - lib/patches/init.rb
204
+ - lib/patches/range.rb
205
+ - lib/selectors/init.rb
206
+ - lib/selectors/random_selector.rb
207
+ - lib/selectors/roulette_selector.rb
208
+ - lib/selectors/tournament_selector.rb
209
+ - lib/species/array_species.rb
210
+ - lib/species/bit_string_species.rb
211
+ - lib/species/init.rb
212
+ - lib/species/permutation_species.rb
213
+ - lib/species/string_species.rb
214
+ - lib/utility/init.rb
215
+ - lib/utility/scaled_array.rb
216
+ - lib/utility/sorted_array.rb
217
+ - lib/wallace.rb
218
+ - test/.gitkeep
219
+ homepage: http://github.com/ChrisTimperley/Wallace.rb
220
+ licenses:
221
+ - MIT
222
+ post_install_message:
223
+ rdoc_options: []
224
+ require_paths:
225
+ - lib
226
+ required_ruby_version: !ruby/object:Gem::Requirement
227
+ none: false
228
+ requirements:
229
+ - - ! '>='
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ segments:
233
+ - 0
234
+ hash: -521889391
235
+ required_rubygems_version: !ruby/object:Gem::Requirement
236
+ none: false
237
+ requirements:
238
+ - - ! '>='
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 1.8.28
244
+ signing_key:
245
+ specification_version: 3
246
+ summary: A powerful, flexible and modular toolkit for running Evolutionary Algorithms
247
+ in Ruby
248
+ test_files: []