gga4r 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +12 -15
- data/examples/ga_fitness_all_1s.rb +2 -2
- data/lib/gga4r/gga4r_main.rb +28 -6
- data/lib/gga4r/version.rb +1 -1
- metadata +6 -5
data/README.txt
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
README for gga4r
|
2
|
-
|
1
|
+
=README for gga4r
|
2
|
+
|
3
|
+
|
4
|
+
==Introduction
|
3
5
|
|
4
|
-
Introduction
|
5
|
-
************
|
6
6
|
General Genetic Algorithm for ruby is a Ruby Genetic Algorithm so simple to use:
|
7
7
|
|
8
8
|
1) Take a class to evolve it and define fitness, recombine and mutate methods.
|
@@ -13,8 +13,8 @@ General Genetic Algorithm for ruby is a Ruby Genetic Algorithm so simple to use:
|
|
13
13
|
|
14
14
|
def recombine(c2)
|
15
15
|
cross_point = (rand * c2.size).to_i
|
16
|
-
c1_a, c1_b = self.
|
17
|
-
c2_a, c2_b = c2.
|
16
|
+
c1_a, c1_b = self.separate(cross_point)
|
17
|
+
c2_a, c2_b = c2.separate(cross_point)
|
18
18
|
[StringPopulation.new(c1_a + c2_b), StringPopulation.new(c2_a + c1_b)]
|
19
19
|
end
|
20
20
|
|
@@ -40,8 +40,7 @@ General Genetic Algorithm for ruby is a Ruby Genetic Algorithm so simple to use:
|
|
40
40
|
100.times { |i| ga.evolve }
|
41
41
|
p ga.best_fit[0]
|
42
42
|
|
43
|
-
Install
|
44
|
-
*******
|
43
|
+
==Install
|
45
44
|
|
46
45
|
1) Execute:
|
47
46
|
gem install gga4r
|
@@ -51,20 +50,18 @@ Install
|
|
51
50
|
require "gga4r"
|
52
51
|
|
53
52
|
|
54
|
-
Attention
|
55
|
-
|
56
|
-
Please note that Gga4r adds shuffle!, each_pair and
|
53
|
+
==Attention
|
54
|
+
|
55
|
+
Please note that Gga4r adds shuffle!, each_pair and separate methods to the Array class.
|
57
56
|
|
58
|
-
Documentation
|
59
|
-
*************
|
57
|
+
==Documentation
|
60
58
|
|
61
59
|
Documentation can be generated using rdoc tool under the source code with:
|
62
60
|
|
63
61
|
rdoc README lib
|
64
62
|
|
65
63
|
|
66
|
-
Copying
|
67
|
-
*******
|
64
|
+
==Copying
|
68
65
|
|
69
66
|
This work is developed by Sergio Espeja ( www.upf.edu/pdi/iula/sergio.espeja, sergio.espeja at gmail.com )
|
70
67
|
mainly in Institut Universitari de Lingüística Aplicada of Universitat Pompeu Fabra ( www.iula.upf.es ),
|
@@ -7,8 +7,8 @@ class StringPopulation < Array
|
|
7
7
|
|
8
8
|
def recombine(c2)
|
9
9
|
cross_point = (rand * c2.size).to_i
|
10
|
-
c1_a, c1_b = self.
|
11
|
-
c2_a, c2_b = c2.
|
10
|
+
c1_a, c1_b = self.split(cross_point)
|
11
|
+
c2_a, c2_b = c2.split(cross_point)
|
12
12
|
[StringPopulation.new(c1_a + c2_b), StringPopulation.new(c2_a + c1_b)]
|
13
13
|
end
|
14
14
|
|
data/lib/gga4r/gga4r_main.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "yaml"
|
2
2
|
require "logger"
|
3
|
+
require "active_support"
|
3
4
|
|
4
5
|
class GeneticAlgorithm
|
5
6
|
attr_reader :generations, :p_combination, :p_mutation
|
@@ -22,7 +23,7 @@ class GeneticAlgorithm
|
|
22
23
|
@p_mutation = prop[:p_mutation] || 0.01
|
23
24
|
@max_population = prop[:max_population]
|
24
25
|
@logger = prop[:logger] if prop[:logger]
|
25
|
-
|
26
|
+
@use_threads = prop[:use_threads] if prop[:use_threads]
|
26
27
|
# mean_fitness
|
27
28
|
end
|
28
29
|
|
@@ -77,7 +78,7 @@ class GeneticAlgorithm
|
|
77
78
|
# Evolves the actual generation num_steps steps (1 by default).
|
78
79
|
def evolve(num_steps = 1)
|
79
80
|
num_steps.times do
|
80
|
-
@generations <<
|
81
|
+
@generations << evaluation_with_threads(@generations[-1])
|
81
82
|
selection!
|
82
83
|
recombination!
|
83
84
|
mutation!
|
@@ -97,6 +98,25 @@ class GeneticAlgorithm
|
|
97
98
|
end
|
98
99
|
end
|
99
100
|
|
101
|
+
# Prepares given generation for evaluation ( evaluates its fitness ),
|
102
|
+
# using Threads
|
103
|
+
def evaluation_with_threads(g)
|
104
|
+
@logger.debug "Evaluation " + g.size.to_s + " chromosomes." if @logger
|
105
|
+
threads = []
|
106
|
+
i = 0
|
107
|
+
g.each do |chromosome|
|
108
|
+
i += 1
|
109
|
+
@logger.debug "Evaluating chromosome #{i}:" if @logger
|
110
|
+
@logger.debug "#{chromosome.stats.join("\n")}" if @logger
|
111
|
+
threads << Thread.new(chromosome) do |t_chromosome|
|
112
|
+
t_chromosome.fitness
|
113
|
+
puts "Thread finished #{Thread.current.id} - #{Thread.current.status}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
# Wait for threads for finish
|
117
|
+
threads.each {|thread| puts "#{thread.status}"; thread.join; puts "#{thread.status}"}
|
118
|
+
return g
|
119
|
+
end
|
100
120
|
|
101
121
|
|
102
122
|
# Selects population to survive and recombine
|
@@ -113,10 +133,10 @@ class GeneticAlgorithm
|
|
113
133
|
new_generation = g.dup.shuffle!
|
114
134
|
@logger.debug "Shuffled!" if @logger
|
115
135
|
new_childs = []
|
116
|
-
new_generation.
|
136
|
+
new_generation.in_groups_of(2) do |chromosome1, chromosome2|
|
117
137
|
if rand > (1 - @p_combination)
|
118
138
|
@logger.debug "Recombining" if @logger
|
119
|
-
new_childs
|
139
|
+
new_childs += chromosome1.recombine(chromosome2)
|
120
140
|
end
|
121
141
|
end
|
122
142
|
new_generation + new_childs
|
@@ -142,8 +162,10 @@ class GeneticAlgorithm
|
|
142
162
|
new_generation = []
|
143
163
|
g.each do |chromosome|
|
144
164
|
num_rep = 0
|
145
|
-
|
146
|
-
|
165
|
+
if chromosome.fitness > 0
|
166
|
+
num_rep += (chromosome.fitness.to_f/mean_fitness).to_i
|
167
|
+
num_rep += 1 if rand > (1 - (chromosome.fitness/mean_fitness)%1)
|
168
|
+
end
|
147
169
|
new_generation = new_generation + ([chromosome] * num_rep)
|
148
170
|
end
|
149
171
|
new_generation
|
data/lib/gga4r/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.4
|
3
3
|
specification_version: 1
|
4
4
|
name: gga4r
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.9.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.9.1
|
7
|
+
date: 2007-11-22 00:00:00 +01:00
|
8
8
|
summary: "A Ruby Genetic Algorithm. So simple to use: 1, take a class to evolve it and define fitness, recombine and mutate methods. 2, create a GeneticAlgorithm object with the population. 3, call evolve method as many times as you want.description of gem"
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
+
- test
|
11
12
|
email: sergio.espeja@gmail.com
|
12
13
|
homepage: http://gga4r.rubyforge.org
|
13
14
|
rubyforge_project: gga4r
|
@@ -41,8 +42,8 @@ files:
|
|
41
42
|
- test/test_helper.rb
|
42
43
|
- test/gga4r_test.rb
|
43
44
|
- examples/ga_fitness_all_1s.rb
|
44
|
-
test_files:
|
45
|
-
|
45
|
+
test_files: []
|
46
|
+
|
46
47
|
rdoc_options: []
|
47
48
|
|
48
49
|
extra_rdoc_files: []
|