-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.rb
94 lines (79 loc) · 1.87 KB
/
ga.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require "eval.rb"
class Generator
def initialize(population,maxsize)
@e=Evaluator.new
@p=Array.new
population.each do |w|
@p << [w, self.calculate_fitness(w)]
end
@maxsize=maxsize
self.sort_population
end
def population
@p
end
def eval_population
a=Array.new
@p.each do |k|
if k[1]>0 and @e.eval_domain_free(k[0])
a << k[0]
end
a
end
end
def sort_population
@p.sort! do |a,b|
a[1]<=>b[1]
end
end
def calculate_fitness(word)
@e.eval(word)
end
def generate_population(psize)
while @p.size < psize
@p << self.generate_random()
end
end
def mutate(s)
a="abcdefghijklmnopqrstuvwxyz"
word="#{s}"
p=rand(word.size)
word[p]=a[rand(a.size)]
word
end
def crossover(s1,s2)
cut1=rand(s1.size)
cut2=rand(s2.size)
w=s1[0,cut1]+s2[0,cut2]
if w.size>15
w=w[0,15]
end
w
end
def generation
size=@p.size
(size/2).to_i.times do
w1=@p[rand(size)]
w2=@p[rand(size)]
w = self.crossover(w1[0], w2[0])
if w.size>4
@p << [w,self.calculate_fitness(w)]
end
end
# (size/10).to_i.times do
# w = self.mutate(@p[rand(size)][0])
# @p << [w,self.calculate_fitness(w)]
# end
self.sort_population
if @p.size > @maxsize
@p = @p[@p.size-@maxsize,@p.size-1]
end
end
end
# should contain all characters, this is the starting population
a=Generator.new( ['technology','science','business','think','forward','pushing','solutions','services','ultimate','universal','network','max','base','knowledge','intelligence','inter','networking','expert','reasoning','distribute','balance','advanced','essential','connecting','source','trust','architect','digital','experience','high','brain','mind','value','ability','gate','peak','experience','clever','smart','pure','skill','robust','artful','profound','constan','value'], 300 )
15.times do |generation|
puts generation
a.generation
end
print a.eval_population