-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathq_learning_player.rb
77 lines (63 loc) · 1.87 KB
/
q_learning_player.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
class QLearningPlayer
attr_accessor :x, :game
def initialize
@x = 0
@actions = [:left, :right]
@first_run = true
@learning_rate = 0.2
@discount = 0.9
@epsilon = 0.9
@r = Random.new
end
def initialize_q_table
# Initialize q_table states by actions
@q_table = Array.new(@game.map_size){ Array.new(@actions.length) }
# Initialize to random values
@game.map_size.times do |s|
@actions.length.times do |a|
@q_table[s][a] = @r.rand
end
end
end
def get_input
# Pause to make sure humans can follow along
sleep 0.05
if @first_run
# If this is first run initialize the Q-table
initialize_q_table
@first_run = false
else
# If this is not the first run
# Evaluate what happened on last action and update Q table
# Calculate reward
r = 0 # default is 0
if @old_score < @game.score
r = 1 # reward is 1 if our score increased
elsif @old_score > @game.score
r = -1 # reward is -1 if our score decreased
end
# Our new state is equal to the player position
@outcome_state = @x
@q_table[@old_state][@action_taken_index] = @q_table[@old_state][@action_taken_index] + @learning_rate * (r + @discount * @q_table[@outcome_state].max - @q_table[@old_state][@action_taken_index])
end
# Capture current state and score
@old_score = @game.score
@old_state = @x
# Chose action based on Q value estimates for state
if @r.rand > @epsilon
# Select random action
@action_taken_index = @r.rand(@actions.length).round
else
# Select based on Q table
s = @x
@action_taken_index = @q_table[s].each_with_index.max[1]
end
# Take action
return @actions[@action_taken_index]
end
def print_table
@q_table.length.times do |i|
puts @q_table[i].to_s
end
end
end