Class: Gargor
- Inherits:
-
Object
show all
- Defined in:
- lib/gargor.rb,
lib/gargor/version.rb,
lib/gargor/parameter.rb,
lib/gargor/individual.rb
Defined Under Namespace
Classes: Individual, Parameter
Constant Summary
collapse
- GLOBAL_OPTS =
["population","max_generations","target_nodes",
"attack_cmd","elite","mutation","target_cooking_cmd",
"attack_node","fitness_precision","attack_result"]
- VERSION =
"0.0.3"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.crossover(a, b) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/gargor.rb', line 58
def crossover a,b
return a.clone if a.params == b.params
puts "crossover: #{a} #{b}"
total = a.fitness + b.fitness
c = Individual.new
c.params = a.params.clone
c.params.each { |name,param|
cur = float_rand(total)
c.params[name] = b.params[name] if b.fitness > cur
}
puts "#{a.to_s} + #{b.to_s} \n => #{c.to_s}"
c
end
|
.float_rand(f, p = @@fitness_precision) ⇒ Object
51
52
53
54
55
56
|
# File 'lib/gargor.rb', line 51
def float_rand(f,p = @@fitness_precision)
f *= @@fitness_precision
i = f.to_i
f = rand(i)
f / @@fitness_precision.to_f
end
|
.individuals ⇒ Object
136
137
138
|
# File 'lib/gargor.rb', line 136
def individuals
@@individuals
end
|
.load_dsl(params_file) ⇒ Object
34
35
36
37
38
|
# File 'lib/gargor.rb', line 34
def load_dsl(params_file)
contents = File.open(params_file, 'rb'){ |f| f.read }
new.instance_eval(contents)
raise "POPULATION == 0" if @@population == 0
end
|
.mutation ⇒ Object
40
41
42
43
44
45
46
47
48
|
# File 'lib/gargor.rb', line 40
def mutation
individual = Individual.new
@@param_procs.each { |name,proc|
param = Parameter.new(name)
param.instance_eval(&proc)
individual.params[name] = param
}
individual
end
|
.next_generation ⇒ Object
126
127
128
129
130
131
132
133
134
|
# File 'lib/gargor.rb', line 126
def next_generation
puts "<== end generation #{@@generation}"
@@generation += 1
return false if @@generation > @@max_generations
puts "==> next generation #{@@generation}"
@@prev_generation = @@individuals
true
end
|
.opt(name) ⇒ Object
140
141
142
|
# File 'lib/gargor.rb', line 140
def opt name
Gargor.class_variable_get("@@#{name}")
end
|
.populate ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/gargor.rb', line 83
def populate
@@individuals = []
unless @@prev_generation
@@individuals << mutation.load_now
loop{
break if @@individuals.length >= @@population
@@individuals << mutation
}
return @@individuals
end
prev_count = @@prev_generation.select { |i| i.fitness > 0 }.count
if prev_count < 2
raise "***** EXTERMINATION ******"
end
puts "population: #{@@prev_generation.length}"
@@individuals = @@prev_generation.sort{ |a,b| a.fitness<=>b.fitness }.last(@@elite) if @@elite > 0
loop{
break if @@individuals.length >= @@population
if rand <= @@mutation
i = mutation
puts "mutation #{i}"
else
a = selection @@prev_generation
b = selection @@prev_generation
i = crossover(a,b)
end
if i and !@@individuals.find { |ii| ii.params == i.params }
@@individuals << i
end
}
puts "poulate: #{@@individuals}"
@@individuals
end
|
.selection(g) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/gargor.rb', line 73
def selection g
total = g.inject(0) { |sum,i| sum += i.fitness }
cur = float_rand(total)
g.each { |i|
return i if i.fitness > cur
cur -= i.fitness
}
raise "error selection"
end
|
.start ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/gargor.rb', line 19
def start
@@fitness_precision = 100000000
@@prev_generation = nil
@@individuals = []
@@param_procs = {}
@@population = 0
@@max_generations = 1
@@generation = 1
@@elite = 0
@@attack_cmd = "false"
@@attack_proc = nil
@@evaluate_proc = Proc.new { 0 }
@@target_nodes = []
end
|
Instance Method Details
#attack(cmd, &block) ⇒ Object
149
150
151
152
|
# File 'lib/gargor.rb', line 149
def attack cmd,&block
@@attack_cmd = cmd
@@attack_proc = block
end
|
#evaluate(&block) ⇒ Object
154
155
156
|
# File 'lib/gargor.rb', line 154
def evaluate &block
@@evaluate_proc = block
end
|
#param(name, &block) ⇒ Object
145
146
147
|
# File 'lib/gargor.rb', line 145
def param name,&block
@@param_procs[name] = block
end
|