answer-factory 0.1.3.6 → 0.1.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/answer-factory.gemspec +4 -1
- data/lib/answer-factory.rb +2 -0
- data/lib/machines/point_crossover.rb +48 -0
- data/spec/machines/point_crossover_spec.rb +80 -0
- metadata +6 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.3.
|
1
|
+
0.1.3.7
|
data/answer-factory.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{answer-factory}
|
8
|
-
s.version = "0.1.3.
|
8
|
+
s.version = "0.1.3.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Bill Tozier", "Trek Glowacki", "Jesse Sielaff"]
|
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
|
|
38
38
|
"lib/machines/infrastructure.rb",
|
39
39
|
"lib/machines/mutate_codeblock.rb",
|
40
40
|
"lib/machines/mutate_footnotes.rb",
|
41
|
+
"lib/machines/point_crossover.rb",
|
41
42
|
"lib/machines/select_by_summed_rank.rb",
|
42
43
|
"lib/machines/select_nondominated.rb",
|
43
44
|
"readme.md",
|
@@ -54,6 +55,7 @@ Gem::Specification.new do |s|
|
|
54
55
|
"spec/machines/infrastructure_spec.rb",
|
55
56
|
"spec/machines/mutate_codeblock_spec.rb",
|
56
57
|
"spec/machines/mutate_footnotes_spec.rb",
|
58
|
+
"spec/machines/point_crossover_spec.rb",
|
57
59
|
"spec/machines/select_by_summed_rank_spec.rb",
|
58
60
|
"spec/machines/select_nondominated_spec.rb",
|
59
61
|
"spec/spec_helper.rb",
|
@@ -79,6 +81,7 @@ Gem::Specification.new do |s|
|
|
79
81
|
"spec/machines/infrastructure_spec.rb",
|
80
82
|
"spec/machines/mutate_codeblock_spec.rb",
|
81
83
|
"spec/machines/mutate_footnotes_spec.rb",
|
84
|
+
"spec/machines/point_crossover_spec.rb",
|
82
85
|
"spec/machines/select_by_summed_rank_spec.rb",
|
83
86
|
"spec/machines/select_nondominated_spec.rb",
|
84
87
|
"spec/spec_helper.rb"
|
data/lib/answer-factory.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
module AnswerFactory
|
3
|
+
module Machines
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
class PointCrossover < Machine
|
9
|
+
|
10
|
+
def build(batch, overridden_options={})
|
11
|
+
raise ArgumentError, "PointCrossover#build cannot process a #{batch.class}" unless
|
12
|
+
batch.kind_of?(Batch)
|
13
|
+
|
14
|
+
all_options = @options.merge(overridden_options)
|
15
|
+
|
16
|
+
replicates = all_options[:replicates] || 1
|
17
|
+
|
18
|
+
result = Batch.new
|
19
|
+
number_of_parents = batch.length
|
20
|
+
|
21
|
+
batch.each do |answer|
|
22
|
+
replicates.times do
|
23
|
+
parents = batch.sample(2)
|
24
|
+
mom = parents[0]
|
25
|
+
dad = parents[1]
|
26
|
+
|
27
|
+
mom_point = Kernel.rand(mom.points)+1
|
28
|
+
|
29
|
+
dad_point = Kernel.rand(dad.points)+1
|
30
|
+
dad_code = dad.program[dad_point].blueprint
|
31
|
+
|
32
|
+
baby = mom.replace_point_or_clone(mom_point,dad_code)
|
33
|
+
baby_progress = [dad.progress, mom.progress].max + 1
|
34
|
+
|
35
|
+
result << Answer.new(baby, progress:baby_progress)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
return result
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
alias :generate :build
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), "./../spec_helper")
|
3
|
+
|
4
|
+
describe "Machines::PointCrossover" do
|
5
|
+
before(:each) do
|
6
|
+
@mater = Machines::PointCrossover.new
|
7
|
+
@two = Batch.[](
|
8
|
+
Answer.new("block { do a do b do c do d}"),
|
9
|
+
Answer.new("block { ref a ref b ref c ref d ref e}"))
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
describe "#build method" do
|
14
|
+
it "should respond to :build" do
|
15
|
+
@mater.should respond_to(:build)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should respond to :generate as an alias of :build" do
|
19
|
+
@mater.should respond_to(:generate)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise an error if the argument isn't a Batch" do
|
23
|
+
lambda{@mater.build(@two)}.should_not raise_error
|
24
|
+
lambda{@mater.build(99)}.should raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should produce a Batch" do
|
28
|
+
@mater.build(@two).should be_a_kind_of(Batch)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should produce a Batch with a new object_id" do
|
32
|
+
@mater.build(@two).object_id.should_not == @two.object_id
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not modify or include any Answer object from its argument Batch" do
|
36
|
+
original_ids = @two.collect {|a| a.object_id}
|
37
|
+
result_ids = @mater.build(@two).collect {|a| a.object_id}
|
38
|
+
(original_ids & result_ids).length.should == 0
|
39
|
+
end
|
40
|
+
|
41
|
+
describe ":replicates option" do
|
42
|
+
it "should produce :replicates offspring for each parent" do
|
43
|
+
@mater.build(@two, replicates:2).length.should == 2 * @two.length
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should use the initialization :replicates option if there isn't a call option" do
|
47
|
+
threefer = Machines::PointCrossover.new(replicates:3)
|
48
|
+
threefer.build(@two).length.should == 3*@two.length
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should default to replicates:1 if none was set as an option" do
|
52
|
+
@mater.build(@two).length.should == @two.length
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should increment the :progress attribute of the derived Answers, regardless of other settings" do
|
57
|
+
@mater.build(@two).each {|a| a.progress.should == 1}
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "crossing over code" do
|
61
|
+
it "should work by selecting random pairs of parents (without replacement)" do
|
62
|
+
@two.stub!(:sample).and_return([@two[0],@two[1]])
|
63
|
+
@mater.build(@two)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should pick the exchanged points with uniform probability" do
|
67
|
+
@two.stub!(:sample).and_return([@two[0],@two[1]])
|
68
|
+
Kernel.should_receive(:rand).exactly(2).times.with(5).and_return(1)
|
69
|
+
Kernel.should_receive(:rand).exactly(2).times.with(6).and_return(2)
|
70
|
+
@mater.build(@two)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should move code from one parent to the other, for each offspring made" do
|
74
|
+
Kernel.stub!(:rand).and_return(1)
|
75
|
+
babies = @mater.build(@two)
|
76
|
+
babies.each {|b| (b.blueprint.should include("ref")) && (b.blueprint.should include("do"))}
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: answer-factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 69
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
9
|
- 3
|
10
|
-
-
|
11
|
-
version: 0.1.3.
|
10
|
+
- 7
|
11
|
+
version: 0.1.3.7
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Bill Tozier
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- lib/machines/infrastructure.rb
|
160
160
|
- lib/machines/mutate_codeblock.rb
|
161
161
|
- lib/machines/mutate_footnotes.rb
|
162
|
+
- lib/machines/point_crossover.rb
|
162
163
|
- lib/machines/select_by_summed_rank.rb
|
163
164
|
- lib/machines/select_nondominated.rb
|
164
165
|
- readme.md
|
@@ -175,6 +176,7 @@ files:
|
|
175
176
|
- spec/machines/infrastructure_spec.rb
|
176
177
|
- spec/machines/mutate_codeblock_spec.rb
|
177
178
|
- spec/machines/mutate_footnotes_spec.rb
|
179
|
+
- spec/machines/point_crossover_spec.rb
|
178
180
|
- spec/machines/select_by_summed_rank_spec.rb
|
179
181
|
- spec/machines/select_nondominated_spec.rb
|
180
182
|
- spec/spec_helper.rb
|
@@ -229,6 +231,7 @@ test_files:
|
|
229
231
|
- spec/machines/infrastructure_spec.rb
|
230
232
|
- spec/machines/mutate_codeblock_spec.rb
|
231
233
|
- spec/machines/mutate_footnotes_spec.rb
|
234
|
+
- spec/machines/point_crossover_spec.rb
|
232
235
|
- spec/machines/select_by_summed_rank_spec.rb
|
233
236
|
- spec/machines/select_nondominated_spec.rb
|
234
237
|
- spec/spec_helper.rb
|