forked from tejaspradhan/Q-Learning-Maze-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QLearning.py
159 lines (107 loc) · 3.79 KB
/
QLearning.py
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
import time
import numpy as np
import pandas as pd
p = pd.DataFrame((np.arange(16).reshape(4,4)), columns = [0,1,2,3] )
np.random.seed(2)
n_states= 16
action = ['up', 'down', 'left', 'right'] # available actions
epsilon = 0.9 # greedy police
alpha = 0.1 # learning rate
gamma = 0.9 # discount factor
max_episode = 20 # maximum episodes
fresh_time = 0.3 # fresh time for one move
def build_qtable(n_states, action):
table= pd.DataFrame(np.zeros((n_states, len(action))),columns = action)
return table
def act(state, q_table):
# This is how to choose an action
state_actions = q_table.iloc[state, :]
if (np.random.uniform() > epsilon) or ((state_actions == 0).all()): # act non-greedy or state-action have no value
action_name = np.random.choice(action)
else: # act greedy
action_name = state_actions.idxmax() # replace argmax to idxmax as argmax means a different function in newer version of pandas
return action_name
def change_env(s, a):
s_=s
reward =0
if a == 'up':
if s[0]==0:
s_[0]=0
else:
s_[0]= s[0]-1
elif a== 'down':
if s[0]==2 and s[1]==3:
s_ = [3, 3]
reward = 1
elif s[0]==3:
s_[0]=3
else:
s_[0]= s[0]+1
elif a == 'left':
if s[1]==0:
s_[1]=0
else:
s_[1]=s[1]-1
elif a == 'right':
if s[0]==3 and s[1]==2:
s_ = [3,3]
reward = 5
elif s[1]==3:
s_[1]=3
else:
s_[1]= s[1]+1
if s_[0]==2 and s_[1]==3:
reward=-2
return s_, reward
def update_env(s, episode, step_counter):
# This is how environment be updated
l = [['-']*4,['-']*4,['-']*4,['-']*4]
l[3][3] = 'T'
l[2][3] ='X'
if (s[0] == 3 and s[1]==3) or (s[0]==2 and s[1]==3):
interaction = 'Episode %s: total_steps = %s' % (episode+1, step_counter)
print('\r{}'.format(interaction), end='')
time.sleep(2)
print('\r ', end='')
else:
l[s[0]][s[1]]='o'
for i in l:
for j in i:
print(j,end='')
print()
time.sleep(fresh_time)
l[s[0]][s[1]]='-'
def rl():
q_table = build_qtable(n_states, action)
for episode in range(max_episode):
step_counter = 0
s = [0, 0]
is_terminated = True
update_env(s, episode, step_counter)
while is_terminated:
S = p.iloc[s[0], s[1]]
A = act(S, q_table)
print(A)
s_, R = change_env(s, A)
# take action & get next state and reward
S_ = p.iloc[s_[0], s_[1]]
print(s_)
q_predict = q_table.loc[S, A]
if (s_[0]==3 and s_[1] ==3):
q_target = R # next state is terminal
is_terminated = False
elif (s_[0]==2 and s_[1] ==3):
q_target = R # next state is terminal
is_terminated = False
# next state is not terminal
else:
q_target = R + gamma * q_table.iloc[S_, :].max() # terminate this episode
q_table.loc[S, A] += alpha * (q_target - q_predict) # update
s = s_ # move to next state
update_env(s, episode, step_counter+1)
step_counter += 1
return q_table
if __name__ == "__main__":
q_table = rl()
print('\r\nQ-table:\n')
print(q_table)