-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTreasure.py
More file actions
173 lines (128 loc) · 3.91 KB
/
Copy pathTreasure.py
File metadata and controls
173 lines (128 loc) · 3.91 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import numpy as np
from pandas import DataFrame
from time import sleep
TRAIN_ROUNDS = 8
NB_STATE = 4
NB_ACTION = 2
EPSILON = 0.1
ALPHA = 0.01 # learning rate
GAMMA = 0.9 # discount factor
TRACE_DECAY = 0.9 # SarsaLambda Decay Factor
def q_table_init(nb_state, nb_action):
return DataFrame(np.zeros((nb_state, nb_action)))
def choose_action(S, Q):
actions = Q.loc[S, :]
# actions.sum() == 0, explore for the first time
if np.random.uniform() < EPSILON or actions.sum() == 0:
# explore with 10% chance
A = np.random.choice(NB_ACTION)
else:
# use
A = actions.argmax()
return A
# take action , and get feedback
def take_action(S, A):
# 0 1 2 3 4, with NB_STATE = 5
if A == 0:
# go left
R = 0
if S == 0:
S_ = 0
else:
S_ = S - 1
else:
# go right
S_ = S + 1
if S == NB_STATE - 2:
R = 1
else:
R = 0
return S_, R
def update_env(S):
s = '\r'
for i in range(NB_STATE):
s += 'o' if i == S else '-'
print(s, end="")
def QLearning():
Q = q_table_init(NB_STATE, NB_ACTION)
for r in range(TRAIN_ROUNDS):
print("\n***Q-Learning Round {}***".format(r+1))
is_terminated = False
S = 0
steps = 0
update_env(S)
while not is_terminated:
sleep(0.2)
steps += 1
A = choose_action(S, Q)
S_, R = take_action(S, A)
is_terminated = (S_ == NB_STATE - 1)
# Q-Learning, take next max
Q.loc[S, A] += ALPHA * (R + GAMMA * Q.loc[S_, :].max() - Q.loc[S, A])
# assign next status, not action
S = S_
update_env(S)
print('\ntake {} steps'.format(steps))
def Sarsa():
Q = q_table_init(NB_STATE, NB_ACTION)
for r in range(TRAIN_ROUNDS):
print("\n***Sarsa Round {}***".format(r+1))
is_terminated = False
S = 0
steps = 0
update_env(S)
# Sarsa choose init A(ction) here
A = choose_action(S, Q)
while not is_terminated:
sleep(0.2)
steps += 1
S_, R = take_action(S, A)
is_terminated = (S_ == NB_STATE - 1)
# Sarsa, specify next A_(ction)
A_ = choose_action(S_, Q)
Q.loc[S, A] += ALPHA * (R + GAMMA * Q.loc[S_, A_] - Q.loc[S, A])
# just assign the next S_ and A_
S = S_
A = A_
update_env(S)
print('\ntake {} steps'.format(steps))
def SarsaLambda():
Q = q_table_init(NB_STATE, NB_ACTION)
eligibility_trace = Q.copy()
for r in range(TRAIN_ROUNDS):
print("\n***SarsaLambda Round {}***".format(r+1))
is_terminated = False
S = 0
steps = 0
update_env(S)
# Sarsa choose init A(ction) here
A = choose_action(S, Q)
while not is_terminated:
sleep(0.2)
steps += 1
S_, R = take_action(S, A)
is_terminated = (S_ == NB_STATE - 1)
# based on Sarsa, specify next A_(ction)
A_ = choose_action(S_, Q)
#Q.loc[S, A] += ALPHA * (R + GAMMA * Q.loc[S_, A_] - Q.loc[S, A])
error = R + GAMMA * Q.loc[S_, A_] - Q.loc[S, A]
# Method 1
#eligibility_trace.loc[S, A] += 1
# Method 2
eligibility_trace.loc[S, :] *= 0
eligibility_trace.loc[S, A] = 1
# number * number * matrix
Q += ALPHA * error * eligibility_trace
# decay every time, the far, the less impact
eligibility_trace *= TRACE_DECAY
# just assign the next S_ and A_
S = S_
A = A_
update_env(S)
print('\ntake {} steps'.format(steps))
def main():
# Q = QLearning()
#Q = Sarsa()
Q = SarsaLambda()
if __name__ == '__main__':
main()