-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethinks_meta.rb
More file actions
99 lines (76 loc) · 1.94 KB
/
methinks_meta.rb
File metadata and controls
99 lines (76 loc) · 1.94 KB
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
95
96
97
98
99
#!/usr/bin/env ruby
# methinks_meta.rb
require 'methinks'
class Hash
def get_child()
new_hash = {}
each_pair do |k,v|
new_hash[k] = (rand(v) + (v/2))
end
new_hash[:display_filter] = 5
return new_hash
end
end # Hash
###
class Meta_Mutator
NEW_TARGET = 'ruby'
MAX_ATTEMPTS = 2
TARGET = NEW_TARGET || String::TARGET
def initialize()
@params_by_number_of_mutations = {}
end
def mutate_mutations!(
params,
did_no_better_count=0
)
return if did_no_better_count > MAX_ATTEMPTS
num = update_params_by_number_of_mutations!(params)
return mutate_mutations!(
@params_by_number_of_mutations[best_num],
get_no_better_count(num, did_no_better_count)
)
end
def report()
@params_by_number_of_mutations.sort.each do |pair|
num, params = pair
puts sprintf("%0#{digits_needed}d", num) +
" generations with #{params.inspect}"
end
end
private
def best_num()
@params_by_number_of_mutations.keys.sort[0] || nil
end
def digits_needed()
@params_by_number_of_mutations.keys.max.to_s.size
end
def get_children(params, number_of_children = 10)
(0..number_of_children).to_a.map do |i|
params.get_child()
end
end
def get_no_better_count(num, did_no_better_count)
return 0 if (num == best_num)
did_no_better_count + 1
end
def update_params_by_number_of_mutations!(params)
children = get_children(params)
number_of_mutations = nil
children.each do |params|
candidate = String.new.scramble!(TARGET)
number_of_mutations = candidate.mutate_until_matches!(TARGET, params)
@params_by_number_of_mutations[number_of_mutations] = params.dup
end
return number_of_mutations
end
end # Meta_Mutator
###
params = {
:generation_size => 200,
:mutation_rate => 30,
:display_filter => 5,
:mutation_amp => 7
}
mm = Meta_Mutator.new()
mm.mutate_mutations!(params)
mm.report()