-
Notifications
You must be signed in to change notification settings - Fork 1
/
ga.rb
93 lines (84 loc) · 1.92 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
#!/usr/bin/ruby
VARS = %w(wall_below piece_below wall_on_left piece_on_left wall_on_right piece_on_right space_filled blank_space_below)
TRIALS = 3
def score(candidate)
options = {}
VARS.each_with_index do |var,i|
options[var] = candidate[i]
end
query_string = options.collect{|pair| pair.join('=')}.join('&')
score = 0
TRIALS.times do |i|
score += `ruby mod.rb http://localhost/tetris/bot.php?#{query_string}`.to_i
end
score /= TRIALS
return score
end
def random_candidate
a = []
range = 10
VARS.length.times {|i| a.push((rand(range) - range/2).to_f)}
return a
end
def merge(mother, father)
#puts "Merging #{mother} and #{father}."
child = []
VARS.length.times do |i|
child .push((mother[i] + father[i]) / 2)
end
#Random mutation
#child[rand VARS.length] += (rand(6) - 3)
child = child.collect{|var| var += (rand(3) - 1)}
#puts "#{child}"
return child
end
def test(candidate)
total = 0
10.times do |i|
score = score candidate
total += score
puts "#{score}"
end
puts "#{total / 10}"
end
def go
mother = random_candidate
father = random_candidate
child = []
generation = 0
start = Time.now.to_f
while true
new1 = []
new1score = -1
new2 = []
new2score = -1
scores = []
puts "---------------"
puts "Generation #{generation}"
generation += 1
10.times do |i|
child = merge(mother,father)
child_score = score child
scores << child_score
puts "Child's score is #{child_score}."
if child_score > new1score or new1score == -1
new2 = new1
new2score = new1score
new1 = child
new1score = child_score
#puts "New1 bumped"
elsif child_score > new2score or new2score == -1
new2 = child
new2score = child_score
#puts "New2 bumped"
end
end
mother = new1
father = new2
puts mother.join(' ')
puts "Average: #{scores.inject(0){|sum,score| sum + score}/scores.length}"
end
puts child
puts "#{Time.now.to_f - start} seconds."
end
test [1,1,1,1,1,1,7,-6]