gene 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/Manifest +46 -0
  2. data/Rakefile +12 -9
  3. data/TODO +7 -0
  4. data/gene.gemspec +34 -0
  5. data/initializers/functional_extensions.rb +44 -0
  6. data/initializers/module_extensions.rb +14 -0
  7. data/initializers/object_extensions.rb +25 -0
  8. data/initializers/range_extensions.rb +9 -0
  9. data/initializers/runner.rb +15 -0
  10. data/initializers/symbol_extensions.rb +11 -0
  11. data/initializers/unbound_method_extensions.rb +3 -0
  12. data/lib/aligner.rb +34 -0
  13. data/lib/calculator.rb +42 -0
  14. data/lib/cell.rb +41 -0
  15. data/lib/color.rb +9 -0
  16. data/lib/dsl.rb +16 -0
  17. data/lib/gene.rb +67 -3
  18. data/lib/generator.rb +83 -0
  19. data/lib/geometry.rb +64 -0
  20. data/lib/hungarian.rb +205 -0
  21. data/lib/imagine.rb +22 -0
  22. data/lib/petri.rb +85 -0
  23. data/lib/point.rb +1 -0
  24. data/lib/trait.rb +60 -0
  25. data/tasks/test.rake +23 -0
  26. data/test/assets/Nova.jpg +0 -0
  27. data/test/assets/Rex.jpg +0 -0
  28. data/test/assets/Squares.jpg +0 -0
  29. data/test/test_helper.rb +6 -0
  30. data/test/unit/aligner_test.rb +91 -0
  31. data/test/unit/calculator_test.rb +100 -0
  32. data/test/unit/cell_test.rb +64 -0
  33. data/test/unit/color_test.rb +23 -0
  34. data/test/unit/dsl_test.rb +45 -0
  35. data/test/unit/functionals_extensions_test.rb +51 -0
  36. data/test/unit/gene_test.rb +76 -0
  37. data/test/unit/generator_test.rb +76 -0
  38. data/test/unit/geometry_test.rb +57 -0
  39. data/test/unit/hungarian_test.rb +196 -0
  40. data/test/unit/imagine_test.rb +54 -0
  41. data/test/unit/module_extensions_test.rb +40 -0
  42. data/test/unit/object_extensions_test.rb +34 -0
  43. data/test/unit/petri_test.rb +87 -0
  44. data/test/unit/range_extensions_test.rb +29 -0
  45. data/test/unit/symbol_extensions_test.rb +18 -0
  46. data/test/unit/trait_test.rb +97 -0
  47. data/test/unit/unbound_method_extensions_test.rb +11 -0
  48. metadata +118 -30
  49. data/History.txt +0 -6
  50. data/Manifest.txt +0 -7
  51. data/README.txt +0 -48
  52. data/bin/gene +0 -3
  53. data/test/test_gene.rb +0 -8
@@ -0,0 +1,76 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper.rb")
2
+
3
+ class GeneTest < Test::Unit::TestCase
4
+ def setup
5
+ Petri.stubs(:image_dimensions).returns(Point.new(640, 480))
6
+ Petri.stubs(:num_points).returns(3)
7
+
8
+ @gene = Gene.new
9
+ end
10
+
11
+ def test_true
12
+ assert true
13
+ end
14
+
15
+ def test_polygon_has_extra_methods
16
+ assert @gene.polygon.respond_to?(:num_points)
17
+ assert @gene.polygon.respond_to?(:points)
18
+ end
19
+
20
+ def test_polygon_has_correct_setup
21
+ @gene.polygon.points.each do |point|
22
+ assert point.class == Point
23
+ assert point.x.class == Trait
24
+ assert point.y.class == Trait
25
+ end
26
+ end
27
+
28
+ def test_color_has_correct_setup
29
+ [:r, :g, :b, :a].each do |channel|
30
+ assert_equal Trait, @gene.color.send(channel).class
31
+ end
32
+ end
33
+
34
+ def test_initialize__no_default_values
35
+ image_dimensions = Point.new(640, 480)
36
+
37
+ assert_equal 3, @gene.polygon.num_points
38
+
39
+ @gene.polygon.points.each do |point|
40
+ assert((0...image_dimensions.x) === point.x.value)
41
+ assert((0...image_dimensions.y) === point.y.value)
42
+ end
43
+
44
+ @gene.color.rgb.each do |channel|
45
+ assert((0.0..1.0) === channel.value)
46
+ end
47
+ end
48
+
49
+ def test_initialize__with_block
50
+ gene = Gene.new do
51
+ point_1_x { set_value 100 }
52
+
53
+ point_2 100, 100
54
+
55
+ trait_r { set_value 100 }
56
+ end
57
+
58
+ lambda do |index|
59
+ assert gene.polygon[index].x.is_a?(Trait)
60
+ assert gene.polygon[index].y.is_a?(Trait)
61
+ end | Petri.num_points.times
62
+
63
+ lambda do |trait_name|
64
+ assert gene.color.send(trait_name).is_a?(Trait)
65
+ end | %w[r g b a]
66
+
67
+ assert_equal 100, gene.polygon[1].x.value
68
+ assert_equal 100, gene.polygon[2].x.value
69
+ assert_equal 100, gene.polygon[2].y.value
70
+ assert_equal 100, gene.color.r.value
71
+
72
+ assert_raise NoMethodError do
73
+ Gene.new(3, Point.new(640, 480)) { rawr! }
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,76 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper.rb")
2
+
3
+ class GeneratorTest < Test::Unit::TestCase
4
+ def setup
5
+ Petri.stubs(:image_dimensions).returns(Point.new(640, 480))
6
+ Petri.stubs(:num_genes).returns(5)
7
+ Petri.stubs(:num_points).returns(3)
8
+
9
+ @cell_1 = Cell.new
10
+ @cell_2 = Cell.new
11
+ [@cell_1, @cell_2].each { |cell| cell.fitness = 0.5 }
12
+
13
+ @generator = Generator.new(@cell_1, @cell_2)
14
+ end
15
+
16
+ def test_true
17
+ assert true
18
+ end
19
+
20
+ def test_initialize__with_block
21
+ assert_equal Generator::DEFAULT_XOVER_FREQ, @generator.xover_freq
22
+ assert_equal Generator::DEFAULT_MUTATION_FREQ, @generator.mutation_freq
23
+
24
+ generator = Generator.new(@cell_1, @cell_2) do
25
+ set_xover_freq 1.0
26
+ set_mutation_freq 1.0
27
+ end
28
+
29
+ assert_equal 1.0, generator.xover_freq
30
+ assert_equal 1.0, generator.mutation_freq
31
+
32
+ assert_raise NoMethodError do
33
+ Generator.new(@cell_1, @cell_2) do
34
+ rawr!
35
+ end
36
+ end
37
+ end
38
+
39
+ def test_combine
40
+ assert_equal Cell, @generator.combine.class
41
+ end
42
+
43
+ def test_mutate__returns_normal_trait_value
44
+ @generator.instance_variable_set(:@mutation_freq, 0)
45
+
46
+ trait = mock
47
+ trait.expects(:value)
48
+
49
+ @generator.send(:mutate, trait)
50
+ end
51
+
52
+ def test_mutate__returns_mutated_trait_value
53
+ @generator.instance_variable_set(:@mutation_freq, 1)
54
+
55
+ trait = mock
56
+ trait.expects(:mutated_value)
57
+
58
+ @generator.send(:mutate, trait)
59
+ end
60
+
61
+ def test_read_sequence
62
+ @generator.instance_variable_set(:@current_sequence, 0)
63
+ @generator.instance_variable_set(:@xover_freq, 1)
64
+
65
+ assert_equal 1, @generator.send(:read_sequence)
66
+ assert_equal 0, @generator.send(:read_sequence)
67
+
68
+ @generator.instance_variable_set(:@xover_freq, 0)
69
+
70
+ assert_equal 0, @generator.send(:read_sequence)
71
+
72
+ @generator.instance_variable_set(:@current_sequence, 1)
73
+
74
+ assert_equal 1, @generator.send(:read_sequence)
75
+ end
76
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper.rb")
2
+
3
+ class GeometryTest < Test::Unit::TestCase
4
+ class TestClass
5
+ include Geometry
6
+
7
+ attr_accessor :polygon
8
+ end
9
+
10
+ def setup
11
+ @test_class = TestClass.new
12
+ end
13
+
14
+ def test_true
15
+ assert true
16
+ end
17
+
18
+ def test_hull__less_than_three_points
19
+ point_lists = [[], [create_point(0, 0)], [create_point(0, 0), create_point(0, 0)]]
20
+
21
+ point_lists.each do |point_list|
22
+ assert_raise ArgumentError do
23
+ @test_class.polygon = point_list
24
+ @test_class.hulled_sequence
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_hull_simple_case
30
+ @test_class.polygon = [create_point(0, 0), create_point(50, 0), create_point(50, 50)]
31
+ assert_equal [0, 0, 50, 50, 50, 0], @test_class.hulled_sequence
32
+ end
33
+
34
+ def test_hull__one_point_inside
35
+ @test_class.polygon = [create_point(0, 0), create_point(50, 0), create_point(50, 50), create_point(10, 5)]
36
+ assert_equal [0, 0, 50, 50, 50, 0], @test_class.hulled_sequence
37
+ end
38
+
39
+ def test_hull__three_points_on_a_line
40
+ @test_class.polygon = [create_point(0, 0), create_point(25, 0), create_point(50, 0), create_point(50, 50)]
41
+ assert_equal [0, 0, 50, 50, 50, 0], @test_class.hulled_sequence
42
+ end
43
+
44
+ def test_hull__three_points_on_a_line_with_one_point_inside
45
+ @test_class.polygon = [create_point(0, 0), create_point(25, 0), create_point(50, 0), create_point(50, 50), create_point(10, 5)]
46
+ assert_equal [0, 0, 50, 50, 50, 0], @test_class.hulled_sequence
47
+ end
48
+
49
+ protected
50
+
51
+ def create_point(x, y)
52
+ Point.new(
53
+ Trait.new(0...100) { set_value x },
54
+ Trait.new(0...100) { set_value y }
55
+ )
56
+ end
57
+ end
@@ -0,0 +1,196 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper.rb")
2
+
3
+ class TraitTest < Test::Unit::TestCase
4
+ def setup
5
+ @hungarian = Hungarian.new([[1, 0], [0, 1]])
6
+ end
7
+
8
+ def test_true
9
+ assert true
10
+ end
11
+
12
+ def test_minimize_rows
13
+ @hungarian.instance_variable_set(:@matrix, [[1, 2, 3], [1, 0, 1], [2, 2, 2]])
14
+ @hungarian.send(:minimize_rows)
15
+ assert_equal [[0, 1, 2], [1, 0, 1], [0, 0, 0]], @hungarian.instance_variable_get(:@matrix)
16
+ end
17
+
18
+ def test_star_zeroes
19
+ @hungarian.instance_variable_set(:@matrix, [[0, 0], [0, 0]])
20
+ @hungarian.expects(:reset_covered_hash)
21
+
22
+ assert_equal [[Hungarian::EMPTY, Hungarian::EMPTY], [Hungarian::EMPTY, Hungarian::EMPTY]], @hungarian.instance_variable_get(:@mask)
23
+ expected_hash = {:rows => [false, false], :columns => [false, false]}
24
+ assert_equal expected_hash, @hungarian.instance_variable_get(:@covered)
25
+
26
+ @hungarian.send(:star_zeroes)
27
+
28
+ assert_equal [[Hungarian::STAR, Hungarian::EMPTY], [Hungarian::EMPTY, Hungarian::STAR]], @hungarian.instance_variable_get(:@mask)
29
+ expected_hash = {:rows => [true, true], :columns => [true, true]}
30
+ assert_equal expected_hash, @hungarian.instance_variable_get(:@covered)
31
+ end
32
+
33
+ def test_mask_columns__not_all_columns_covered
34
+ @hungarian.expects(:index_range).returns((0..1))
35
+ @hungarian.expects(:column_mask_values_for).twice.returns(stub(:any? => true), stub(:any? => false))
36
+ assert_equal :prime_zeroes, @hungarian.send(:mask_columns)
37
+ end
38
+
39
+ def test_mask_columns__all_columns_covered
40
+ @hungarian.expects(:index_range).returns((0..1))
41
+ @hungarian.expects(:column_mask_values_for).twice.returns(stub(:any? => true))
42
+ assert_equal :finished, @hungarian.send(:mask_columns)
43
+ end
44
+
45
+ def test_prime_zeroes__breaks_when_no_uncovered_zeroes
46
+ @hungarian.expects(:find_uncovered_zero).returns([-1, -1])
47
+ assert_equal :adjust_matrix, @hungarian.send(:prime_zeroes)
48
+ end
49
+
50
+ def test_prime_zeroes__star_in_row
51
+ @hungarian.instance_variable_set(:@covered, {:rows => [nil, nil], :columns => [nil, nil]})
52
+
53
+ @hungarian.expects(:find_uncovered_zero).twice.returns([0, 0], [-1, -1])
54
+ @hungarian.expects(:row_mask_values_for).with(0).returns(stub(:index => 0))
55
+ assert_equal :adjust_matrix, @hungarian.send(:prime_zeroes)
56
+
57
+ expected_hash = {:rows => [true, nil], :columns => [false, nil]}
58
+ assert_equal expected_hash, @hungarian.instance_variable_get(:@covered)
59
+ end
60
+
61
+ def test_prime_zeroes__star_not_in_row
62
+ @hungarian.instance_variable_set(:@covered, {:rows => [nil, nil], :columns => [nil, nil]})
63
+
64
+ @hungarian.expects(:find_uncovered_zero).returns([0, 0])
65
+ @hungarian.expects(:row_mask_values_for).with(0).returns(stub(:index => nil))
66
+ assert_equal [:augment_path, 0, 0], @hungarian.send(:prime_zeroes)
67
+
68
+ expected_hash = {:rows => [nil, nil], :columns => [nil, nil]}
69
+ assert_equal expected_hash, @hungarian.instance_variable_get(:@covered)
70
+ end
71
+
72
+ def test_augment_path__break_out_on_first_loop
73
+ path = [0, 0]
74
+ @hungarian.expects(:column_mask_values_for).with(path[1]).returns(stub(:index => nil))
75
+
76
+ @hungarian.expects(:update_elements_in).with([path])
77
+ @hungarian.expects(:traverse_indices)
78
+ @hungarian.expects(:reset_covered_hash)
79
+
80
+ assert_equal :mask_columns, @hungarian.send(:augment_path, *path)
81
+ end
82
+
83
+ def test_augment_path__loop_once_then_break
84
+ path = [0, 0]
85
+ @hungarian.expects(:column_mask_values_for).twice.with(anything).returns(stub(:index => 1), stub(:index => nil))
86
+ @hungarian.expects(:row_mask_values_for).with(anything).returns(stub(:index => 1))
87
+
88
+ @hungarian.expects(:update_elements_in).with([path, [1, 0], [1, 1]])
89
+ @hungarian.expects(:traverse_indices)
90
+ @hungarian.expects(:reset_covered_hash)
91
+
92
+ assert_equal :mask_columns, @hungarian.send(:augment_path, *path)
93
+ end
94
+
95
+ def test_adjust_matrix
96
+ @hungarian.instance_variable_set(:@matrix, [[1, 2], [3, 4]])
97
+ @hungarian.instance_variable_set(:@covered, {:rows => [false, true], :columns => [false, true]})
98
+
99
+ expected_matrix = [[0, 2], [3, 5]]
100
+ assert_equal :prime_zeroes, @hungarian.send(:adjust_matrix)
101
+ assert_equal expected_matrix, @hungarian.instance_variable_get(:@matrix)
102
+ end
103
+
104
+ def test_assignment
105
+ @hungarian.instance_variable_set(:@mask, [[Hungarian::STAR, Hungarian::EMPTY], [Hungarian::EMPTY, Hungarian::STAR]])
106
+ assert_equal [[0, 0], [1, 1]], @hungarian.send(:assignment)
107
+
108
+ @hungarian.instance_variable_set(:@mask, [[Hungarian::EMPTY, Hungarian::STAR], [Hungarian::STAR, Hungarian::EMPTY]])
109
+ assert_equal [[0, 1], [1, 0]], @hungarian.send(:assignment)
110
+ end
111
+
112
+ def test_update_elements_in_path
113
+ @hungarian.instance_variable_set(:@mask, [[Hungarian::STAR, Hungarian::EMPTY], [Hungarian::EMPTY, Hungarian::PRIME]])
114
+ @hungarian.send(:update_elements_in, [[0, 0], [1, 1]])
115
+ assert_equal [[Hungarian::EMPTY, Hungarian::EMPTY], [Hungarian::EMPTY, Hungarian::STAR]], @hungarian.instance_variable_get(:@mask)
116
+ end
117
+
118
+ def test_find_uncovered_zero
119
+ hungarian = Hungarian.new([[0, 0], [0, 0]])
120
+ hungarian.instance_variable_set(:@covered, {:rows => [false, true], :columns => [false, true]})
121
+ assert_equal [0, 0], hungarian.send(:find_uncovered_zero)
122
+
123
+ hungarian = Hungarian.new([[0, 0], [0, 0]])
124
+ hungarian.instance_variable_set(:@covered, {:rows => [true, false], :columns => [true, false]})
125
+ assert_equal [1, 1], hungarian.send(:find_uncovered_zero)
126
+ end
127
+
128
+ def test_find_uncovered_zero__cell_skipped_if_value_nonzero__default_returned
129
+ hungarian = Hungarian.new([[1, 1], [1, 1]])
130
+ assert_equal [-1, -1], hungarian.send(:find_uncovered_zero)
131
+ end
132
+
133
+ def test_cover_cell
134
+ @hungarian.instance_variable_set(:@covered, {:rows => [false, false], :columns => [false, false]})
135
+ @hungarian.send(:cover_cell, 0, 0)
136
+
137
+ expected_hash = {:rows => [true, false], :columns => [true, false]}
138
+ assert_equal expected_hash, @hungarian.instance_variable_get(:@covered)
139
+ end
140
+
141
+ def test_reset_covered_hash
142
+ @hungarian.instance_variable_set(:@covered, {:rows => [true, false], :columns => [true, false]})
143
+ @hungarian.send(:reset_covered_hash)
144
+
145
+ expected_hash = {:rows => [false, false], :columns => [false, false]}
146
+ assert_equal expected_hash, @hungarian.instance_variable_get(:@covered)
147
+ end
148
+
149
+ def test_location_covered
150
+ @hungarian.instance_variable_set(:@covered, {:rows => [false], :columns => [false]})
151
+ assert_equal false, @hungarian.send(:location_covered?, 0, 0)
152
+
153
+ @hungarian.instance_variable_set(:@covered, {:rows => [true], :columns => [false]})
154
+ assert_equal true, @hungarian.send(:location_covered?, 0, 0)
155
+
156
+ @hungarian.instance_variable_set(:@covered, {:rows => [false], :columns => [true]})
157
+ assert_equal true, @hungarian.send(:location_covered?, 0, 0)
158
+
159
+ @hungarian.instance_variable_set(:@covered, {:rows => [true], :columns => [true]})
160
+ assert_equal true, @hungarian.send(:location_covered?, 0, 0)
161
+ end
162
+
163
+ def test_row_mask_values_for
164
+ @hungarian.instance_variable_set(:@mask, [[0, 1], [2, 3]])
165
+ assert_equal [0, 1], @hungarian.send(:row_mask_values_for, 0)
166
+ assert_equal [2, 3], @hungarian.send(:row_mask_values_for, 1)
167
+ end
168
+
169
+ def test_column_mask_values_for
170
+ @hungarian.instance_variable_set(:@mask, [[0, 1], [2, 3]])
171
+ assert_equal [0, 2], @hungarian.send(:column_mask_values_for, 0)
172
+ assert_equal [1, 3], @hungarian.send(:column_mask_values_for, 1)
173
+ end
174
+
175
+ def test_indices_of_covered_rows
176
+ @hungarian.instance_variable_set(:@length, 3)
177
+ @hungarian.instance_variable_set(:@covered, {:rows => [true, false, true], :columns => [true, false, true]})
178
+ assert_equal [0, 2], @hungarian.send(:indices_of_covered_rows)
179
+ end
180
+
181
+ def test_indices_of_uncovered_columns
182
+ @hungarian.instance_variable_set(:@length, 3)
183
+ @hungarian.instance_variable_set(:@covered, {:rows => [false, true, false], :columns => [false, true, false]})
184
+ assert_equal [0, 2], @hungarian.send(:indices_of_uncovered_columns)
185
+ end
186
+
187
+ def test_traverse_indices
188
+ traversal = []
189
+ @hungarian.send(:traverse_indices) { |row, column| traversal << [row, column] }
190
+ assert_equal [[0, 0], [0, 1], [1, 0], [1, 1]], traversal
191
+ end
192
+
193
+ def test_index_range
194
+ assert_equal 0...2, @hungarian.send(:index_range)
195
+ end
196
+ end
@@ -0,0 +1,54 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper.rb")
2
+
3
+ class ImagineTest < Test::Unit::TestCase
4
+ RGB = Struct.new(:red, :green, :blue)
5
+
6
+ class TestClass
7
+ include Imagine
8
+
9
+ def image_dimensions
10
+ Point.new(2, 2)
11
+ end
12
+
13
+ def each_pixel
14
+ [
15
+ [RGB.new(0, 0, 0), 0, 0],
16
+ [RGB.new(0, 0, 0), 0, 1],
17
+ [RGB.new(0, 0, 0), 1, 0],
18
+ [RGB.new(0, 0, 0), 1, 1]
19
+ ].each { |params| yield *params }
20
+ end
21
+ end
22
+
23
+ def setup
24
+ Magick.send(:remove_const, :MaxRGB)
25
+ Magick.const_set(:MaxRGB, 1)
26
+
27
+ @test_class = TestClass.new
28
+ @test_class.stubs(:target_image).returns(@test_class)
29
+ end
30
+
31
+ def test_true
32
+ assert true
33
+ end
34
+
35
+ def test_compare_image_to__identical_returns_1
36
+ assert_in_delta 1.0, @test_class.compare_image_to(image_of_color(RGB.new(0, 0, 0))), 1e-5
37
+ end
38
+
39
+ def test_compare_image_to__opposite_returns_0
40
+ assert_in_delta 0.0, @test_class.compare_image_to(image_of_color(RGB.new(1, 1, 1))), 1e-5
41
+ end
42
+
43
+ def test_compare_image_to__50_percent
44
+ assert_in_delta 0.5, @test_class.compare_image_to(image_of_color(RGB.new(0.5, 0.5, 0.5))), 1e-5
45
+ end
46
+
47
+ private
48
+
49
+ def image_of_color(color)
50
+ returning(stub) do |image|
51
+ 2.times { |x| 2.times { |y| image.stubs(:pixel_color).with(x, y).returns(color) } }
52
+ end
53
+ end
54
+ end