drp 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +6 -0
- data/CHANGES +0 -0
- data/INTRO +309 -0
- data/LICENSE +281 -0
- data/README +77 -0
- data/Rakefile +99 -0
- data/TODO +13 -0
- data/examples/intro/README +2 -0
- data/examples/intro/canvas_example.rb +123 -0
- data/examples/intro/max_depths_example.rb +34 -0
- data/examples/intro/max_depths_example_2.rb +30 -0
- data/examples/intro/odds_and_ends.rb +96 -0
- data/examples/intro/parameterization_example.rb +32 -0
- data/examples/intro/toy_example.rb +22 -0
- data/examples/intro/weight_fcd_example.rb +44 -0
- data/examples/intro/weights_example.rb +33 -0
- data/examples/intro/weights_example_2.rb +30 -0
- data/examples/symbolic_regression.rb +127 -0
- data/lib/defaults.rb +30 -0
- data/lib/drp.rb +30 -0
- data/lib/error.rb +69 -0
- data/lib/info.rb +30 -0
- data/lib/instance_methods.rb +154 -0
- data/lib/pso.rb +178 -0
- data/lib/rule_engine.rb +324 -0
- data/lib/utils.rb +67 -0
- data/lib/weights_and_max_depths.rb +264 -0
- data/test/tc_instance_methods.rb +219 -0
- data/test/tc_max_depths.rb +98 -0
- data/test/tc_utils.rb +60 -0
- data/test/tc_weights.rb +372 -0
- data/test/ts_drp.rb +28 -0
- metadata +80 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'drp'
|
4
|
+
|
5
|
+
class ParameterizationExample
|
6
|
+
|
7
|
+
extend DRP::RuleEngine
|
8
|
+
|
9
|
+
begin_rules
|
10
|
+
|
11
|
+
max_depth 3
|
12
|
+
|
13
|
+
def foo n
|
14
|
+
"[" + foo(n + 1) + "]"
|
15
|
+
end
|
16
|
+
def foo n
|
17
|
+
"<#{n}>" + foo(n)
|
18
|
+
end
|
19
|
+
|
20
|
+
end_rules
|
21
|
+
|
22
|
+
def default_rule_method n
|
23
|
+
"<<#{n * 2}>>!!"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
pe = ParameterizationExample.new
|
29
|
+
3.times do
|
30
|
+
puts pe.foo(0)
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'drp'
|
3
|
+
|
4
|
+
class ToyExample
|
5
|
+
|
6
|
+
extend DRP::RuleEngine
|
7
|
+
|
8
|
+
begin_rules
|
9
|
+
|
10
|
+
def foobar
|
11
|
+
"foo #{foobar}"
|
12
|
+
end
|
13
|
+
def foobar
|
14
|
+
"bar!"
|
15
|
+
end
|
16
|
+
|
17
|
+
end_rules
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
toy = ToyExample.new
|
22
|
+
puts Array.new(3) { toy.foobar } # -> [ 'foo foo foo bar!', 'bar!', 'foo bar!' ]
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'drp'
|
3
|
+
|
4
|
+
class WeightFromCurrentDepthExample
|
5
|
+
|
6
|
+
extend DRP::RuleEngine
|
7
|
+
|
8
|
+
begin_rules
|
9
|
+
|
10
|
+
max_depth 25
|
11
|
+
weight_fcd 20..0.1
|
12
|
+
|
13
|
+
def foo
|
14
|
+
"foo #{foo}"
|
15
|
+
end
|
16
|
+
|
17
|
+
weight 1
|
18
|
+
|
19
|
+
def foo
|
20
|
+
"bar!"
|
21
|
+
end
|
22
|
+
|
23
|
+
def mama
|
24
|
+
"mama #{mama}"
|
25
|
+
end
|
26
|
+
def mama
|
27
|
+
"mia!"
|
28
|
+
end
|
29
|
+
|
30
|
+
end_rules
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
3.times do
|
35
|
+
wfcde = WeightFromCurrentDepthExample.new
|
36
|
+
3.times do
|
37
|
+
puts wfcde.foo
|
38
|
+
end
|
39
|
+
puts
|
40
|
+
3.times do
|
41
|
+
puts wfcde.mama
|
42
|
+
end
|
43
|
+
puts
|
44
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'drp'
|
3
|
+
|
4
|
+
class WeightsExample
|
5
|
+
|
6
|
+
extend DRP::RuleEngine
|
7
|
+
|
8
|
+
begin_rules
|
9
|
+
|
10
|
+
max_depth 3
|
11
|
+
weight 1
|
12
|
+
|
13
|
+
def foo
|
14
|
+
"foo1 #{foo}"
|
15
|
+
end
|
16
|
+
|
17
|
+
weight 8
|
18
|
+
|
19
|
+
def foo
|
20
|
+
"foo2 #{foo}"
|
21
|
+
end
|
22
|
+
|
23
|
+
end_rules
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
3.times do
|
28
|
+
we = WeightsExample.new
|
29
|
+
3.times do
|
30
|
+
puts we.foo
|
31
|
+
end
|
32
|
+
puts
|
33
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'drp'
|
3
|
+
|
4
|
+
class WeightsExample2
|
5
|
+
|
6
|
+
extend DRP::RuleEngine
|
7
|
+
|
8
|
+
begin_rules
|
9
|
+
|
10
|
+
max_depth 2
|
11
|
+
weight 0..1
|
12
|
+
|
13
|
+
def foo
|
14
|
+
"foo1 #{foo}"
|
15
|
+
end
|
16
|
+
def foo
|
17
|
+
"foo2 #{foo}"
|
18
|
+
end
|
19
|
+
|
20
|
+
end_rules
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
3.times do
|
25
|
+
we2 = WeightsExample2.new
|
26
|
+
3.times do
|
27
|
+
puts we2.foo
|
28
|
+
end
|
29
|
+
puts
|
30
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
DRP, Genetic Programming + Grammatical Evolution = Directed Ruby Programming
|
5
|
+
Copyright (C) 2006, Christophe McKeon
|
6
|
+
|
7
|
+
This program is free software; you can redistribute it and/or
|
8
|
+
modify it under the terms of the GNU General Public License
|
9
|
+
as published by the Free Software Foundation; either version 2
|
10
|
+
of the License, or (at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU General Public License
|
18
|
+
along with this program; if not, write to the Free Softwar Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require 'drp'
|
24
|
+
|
25
|
+
class SymbolicRegressionExample
|
26
|
+
|
27
|
+
extend DRP::RuleEngine
|
28
|
+
|
29
|
+
GREATEST_FLOAT_SMALLER_THAN_1 = 1 - Float::EPSILON
|
30
|
+
|
31
|
+
def initialize codons
|
32
|
+
@codons = codons
|
33
|
+
@num_codons = codons.size
|
34
|
+
@index = 0
|
35
|
+
end
|
36
|
+
|
37
|
+
def next_codon
|
38
|
+
res = @codons[@index]
|
39
|
+
@index += 1
|
40
|
+
@index = 0 if @index == @num_codons
|
41
|
+
clamp res
|
42
|
+
end
|
43
|
+
|
44
|
+
def clamp float
|
45
|
+
return GREATEST_FLOAT_SMALLER_THAN_1 if float > GREATEST_FLOAT_SMALLER_THAN_1
|
46
|
+
return 0.0 if float < 0.0
|
47
|
+
float
|
48
|
+
end
|
49
|
+
|
50
|
+
def test value
|
51
|
+
@index = 0
|
52
|
+
@input = value
|
53
|
+
expr
|
54
|
+
end
|
55
|
+
|
56
|
+
begin_rules
|
57
|
+
|
58
|
+
max_depth 2..4
|
59
|
+
|
60
|
+
def op x, y
|
61
|
+
x + y
|
62
|
+
end
|
63
|
+
def op x, y
|
64
|
+
x * y
|
65
|
+
end
|
66
|
+
def op x, y
|
67
|
+
x - y
|
68
|
+
end
|
69
|
+
def op x, y
|
70
|
+
if y == 0
|
71
|
+
1
|
72
|
+
else
|
73
|
+
x / y
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def expr
|
78
|
+
#puts "op(expr,expr)"
|
79
|
+
op(expr, expr)
|
80
|
+
end
|
81
|
+
def expr
|
82
|
+
#puts "@input"
|
83
|
+
@input
|
84
|
+
end
|
85
|
+
def expr
|
86
|
+
#puts "map"
|
87
|
+
map -5..5, :i_lin
|
88
|
+
end
|
89
|
+
|
90
|
+
end_rules
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
#############################################
|
95
|
+
|
96
|
+
NUM_TEST_VALUES = 10
|
97
|
+
SWARM_SIZE = 250
|
98
|
+
VECTOR_SIZE = 16
|
99
|
+
NUM_ITER = 1000
|
100
|
+
REBIRTH = 0.0
|
101
|
+
|
102
|
+
pso_class = DRP::SearchAlgorithms::PSO::ParticleSwarmOptimizer
|
103
|
+
pso = pso_class.new SWARM_SIZE, VECTOR_SIZE, REBIRTH
|
104
|
+
|
105
|
+
expr = proc { |x| x**2 + 1 }
|
106
|
+
values = Array.new(NUM_TEST_VALUES) { rand * 2 - 1 }
|
107
|
+
solutions = values.collect &expr
|
108
|
+
|
109
|
+
NUM_ITER.times do |iter|
|
110
|
+
pso.each do |codons|
|
111
|
+
error = 0
|
112
|
+
sr = SymbolicRegressionExample.new codons
|
113
|
+
values.each_with_index do |val, i|
|
114
|
+
error += (sr.test(val) - solutions[i]).abs
|
115
|
+
end
|
116
|
+
error
|
117
|
+
end
|
118
|
+
gbe = pso.global_best_error
|
119
|
+
puts "iter #{iter+1}, best error: #{gbe}"
|
120
|
+
break if gbe < 0.000001
|
121
|
+
end
|
122
|
+
|
123
|
+
winner = SymbolicRegressionExample.new(pso.global_best_vector)
|
124
|
+
puts "winning error: #{pso.global_best_error}"
|
125
|
+
NUM_TEST_VALUES.times do |i|
|
126
|
+
puts "#{values[i]} -> #{winner.test(values[i])} [#{solutions[i]}]"
|
127
|
+
end
|
data/lib/defaults.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
DRP, Genetic Programming + Grammatical Evolution = Directed Ruby Programming
|
5
|
+
Copyright (C) 2006, Christophe McKeon
|
6
|
+
|
7
|
+
This program is free software; you can redistribute it and/or
|
8
|
+
modify it under the terms of the GNU General Public License
|
9
|
+
as published by the Free Software Foundation; either version 2
|
10
|
+
of the License, or (at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU General Public License
|
18
|
+
along with this program; if not, write to the Free Softwar Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
module DRP
|
23
|
+
|
24
|
+
DEFAULT = {
|
25
|
+
:max_depth => 10,
|
26
|
+
:weight => 1.0,
|
27
|
+
:test_for_extend_name_clashes => true
|
28
|
+
}
|
29
|
+
|
30
|
+
end
|
data/lib/drp.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
DRP, Genetic Programming + Grammatical Evolution = Directed Ruby Programming
|
5
|
+
Copyright (C) 2006, Christophe McKeon
|
6
|
+
|
7
|
+
This program is free software; you can redistribute it and/or
|
8
|
+
modify it under the terms of the GNU General Public License
|
9
|
+
as published by the Free Software Foundation; either version 2
|
10
|
+
of the License, or (at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU General Public License
|
18
|
+
along with this program; if not, write to the Free Softwar Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
require 'defaults'
|
23
|
+
require 'info'
|
24
|
+
require 'error'
|
25
|
+
require 'pso'
|
26
|
+
require 'utils'
|
27
|
+
require 'weights_and_max_depths'
|
28
|
+
require 'instance_methods'
|
29
|
+
require 'rule_engine'
|
30
|
+
|
data/lib/error.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
DRP, Genetic Programming + Grammatical Evolution = Directed Ruby Programming
|
5
|
+
Copyright (C) 2006, Christophe McKeon
|
6
|
+
|
7
|
+
This program is free software; you can redistribute it and/or
|
8
|
+
modify it under the terms of the GNU General Public License
|
9
|
+
as published by the Free Software Foundation; either version 2
|
10
|
+
of the License, or (at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU General Public License
|
18
|
+
along with this program; if not, write to the Free Softwar Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
module DRP
|
23
|
+
|
24
|
+
class DRPError < StandardError
|
25
|
+
end
|
26
|
+
|
27
|
+
class DRPNameClashError < DRPError
|
28
|
+
|
29
|
+
CLASHING_INSTANCE_METHODS = %w{
|
30
|
+
# default_rule_method
|
31
|
+
# next_codon
|
32
|
+
# next_meta_codon
|
33
|
+
depth
|
34
|
+
max_depth
|
35
|
+
map
|
36
|
+
}
|
37
|
+
CLASHING_CLASS_METHODS = %w{
|
38
|
+
begin_rules
|
39
|
+
end_rules
|
40
|
+
max_depth
|
41
|
+
weight
|
42
|
+
weight_fcd
|
43
|
+
}
|
44
|
+
|
45
|
+
def self.test extended_klass
|
46
|
+
k = extended_klass
|
47
|
+
CLASHING_INSTANCE_METHODS.each do |m|
|
48
|
+
if k.method_defined?(m) or k.protected_method_defined?(m) or k.private_method_defined?(m)
|
49
|
+
raise(
|
50
|
+
self,
|
51
|
+
"the class you are extending already defines instance method '#{m}'",
|
52
|
+
caller
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
class_methods = k.methods + k.protected_methods + k.private_methods
|
57
|
+
CLASHING_CLASS_METHODS.each do |name|
|
58
|
+
if class_methods.include? name
|
59
|
+
raise(
|
60
|
+
self,
|
61
|
+
"the class you are extending already defines class method #{name}",
|
62
|
+
caller
|
63
|
+
)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end # DRPNameClashError
|
68
|
+
|
69
|
+
end # module DRP
|
data/lib/info.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
=begin
|
3
|
+
|
4
|
+
DRP, Genetic Programming + Grammatical Evolution = Directed Ruby Programming
|
5
|
+
Copyright (C) 2006, Christophe McKeon
|
6
|
+
|
7
|
+
This program is free software; you can redistribute it and/or
|
8
|
+
modify it under the terms of the GNU General Public License
|
9
|
+
as published by the Free Software Foundation; either version 2
|
10
|
+
of the License, or (at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU General Public License
|
18
|
+
along with this program; if not, write to the Free Softwar Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
module DRP
|
23
|
+
|
24
|
+
VERSION_MAJOR = 0
|
25
|
+
VERSION_MINOR = 0
|
26
|
+
VERSION_TWEAK = 6
|
27
|
+
Version = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_TWEAK}"
|
28
|
+
Copyright = 'Copyright (c) 2006 by Chritophe McKeon <polypus@yahoo.com>'
|
29
|
+
|
30
|
+
end
|