-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq-learning-9.3.1.py
289 lines (241 loc) · 8.48 KB
/
q-learning-9.3.1.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""Q Learning to solve a simple world model
Simple deterministic MDP is made of 6 grids (states)
---------------------------------
| | | |
| Start | | Goal |
| | | |
---------------------------------
| | | |
| | | Hole |
| | | |
---------------------------------
"""
from collections import deque
import numpy as np
import argparse
import os
import time
from termcolor import colored
class QWorld():
def __init__(self):
# 4 actions
# 0 - Left, 1 - Down, 2 - Right, 3 - Up
self.col = 4
# 6 states
self.row = 6
# setup the environment
self.q_table = np.zeros([self.row, self.col])
self.init_transition_table()
self.init_reward_table()
# discount factor
self.gamma = 0.9
# 90% exploration, 10% exploitation
self.epsilon = 0.9
# exploration decays by this factor every episode
self.epsilon_decay = 0.9
# in the long run, 10% exploration, 90% exploitation
self.epsilon_min = 0.1
# reset the environment
self.reset()
self.is_explore = True
# start of episode
def reset(self):
self.state = 0
return self.state
# agent wins when the goal is reached
def is_in_win_state(self):
return self.state == 2
def init_reward_table(self):
"""
0 - Left, 1 - Down, 2 - Right, 3 - Up
----------------
| 0 | 0 | 100 |
----------------
| 0 | 0 | -100 |
----------------
"""
self.reward_table = np.zeros([self.row, self.col])
self.reward_table[1, 2] = 100.
self.reward_table[4, 2] = -100.
def init_transition_table(self):
"""
0 - Left, 1 - Down, 2 - Right, 3 - Up
-------------
| 0 | 1 | 2 |
-------------
| 3 | 4 | 5 |
-------------
"""
self.transition_table = np.zeros([self.row, self.col], dtype=int)
self.transition_table[0, 0] = 0
self.transition_table[0, 1] = 3
self.transition_table[0, 2] = 1
self.transition_table[0, 3] = 0
self.transition_table[1, 0] = 0
self.transition_table[1, 1] = 4
self.transition_table[1, 2] = 2
self.transition_table[1, 3] = 1
# terminal Goal state
self.transition_table[2, 0] = 2
self.transition_table[2, 1] = 2
self.transition_table[2, 2] = 2
self.transition_table[2, 3] = 2
self.transition_table[3, 0] = 3
self.transition_table[3, 1] = 3
self.transition_table[3, 2] = 4
self.transition_table[3, 3] = 0
self.transition_table[4, 0] = 3
self.transition_table[4, 1] = 4
self.transition_table[4, 2] = 5
self.transition_table[4, 3] = 1
# terminal Hole state
self.transition_table[5, 0] = 5
self.transition_table[5, 1] = 5
self.transition_table[5, 2] = 5
self.transition_table[5, 3] = 5
# execute the action on the environment
def step(self, action):
# determine the next_state given state and action
next_state = self.transition_table[self.state, action]
# done is True if next_state is Goal or Hole
done = next_state == 2 or next_state == 5
# reward given the state and action
reward = self.reward_table[self.state, action]
# the enviroment is now in new state
self.state = next_state
return next_state, reward, done
# determine the next action
def act(self):
# 0 - Left, 1 - Down, 2 - Right, 3 - Up
# action is from exploration
if np.random.rand() <= self.epsilon:
# explore - do random action
self.is_explore = True
return np.random.choice(4,1)[0]
# or action is from exploitation
# exploit - choose action with max Q-value
self.is_explore = False
return np.argmax(self.q_table[self.state])
# Q-Learning - update the Q Table using Q(s, a)
def update_q_table(self, state, action, reward, next_state):
# Q(s, a) = reward + gamma * max_a' Q(s', a')
q_value = self.gamma * np.amax(self.q_table[next_state])
q_value += reward
self.q_table[state, action] = q_value
# UI to dump Q Table contents
def print_q_table(self):
print("Q-Table (Epsilon: %0.2f)" % self.epsilon)
print(self.q_table)
# update Exploration-Exploitation mix
def update_epsilon(self):
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
# UI to display agent moving on the grid
def print_cell(self, row=0):
print("")
for i in range(13):
j = i - 2
if j in [0, 4, 8]:
if j == 8:
if self.state == 2 and row == 0:
marker = "\033[4mG\033[0m"
elif self.state == 5 and row == 1:
marker = "\033[4mH\033[0m"
else:
marker = 'G' if row == 0 else 'H'
color = self.state == 2 and row == 0
color = color or (self.state == 5 and row == 1)
color = 'red' if color else 'blue'
print(colored(marker, color), end='')
elif self.state in [0, 1, 3, 4]:
cell = [(0, 0, 0), (1, 0, 4), (3, 1, 0), (4, 1, 4)]
marker = '_' if (self.state, row, j) in cell else ' '
print(colored(marker, 'red'), end='')
else:
print(' ', end='')
elif i % 4 == 0:
print('|', end='')
else:
print(' ', end='')
print("")
# UI to display mode and action of agent
def print_world(self, action, step):
actions = { 0: "(Left)", 1: "(Down)", 2: "(Right)", 3: "(Up)" }
explore = "Explore" if self.is_explore else "Exploit"
print("Step", step, ":", explore, actions[action])
for _ in range(13):
print('-', end='')
self.print_cell()
for _ in range(13):
print('-', end='')
self.print_cell(row=1)
for _ in range(13):
print('-', end='')
print("")
# UI to display episode count
def print_episode(episode, delay=1):
os.system('clear')
for _ in range(13):
print('=', end='')
print("")
print("Episode ", episode)
for _ in range(13):
print('=', end='')
print("")
time.sleep(delay)
# UI to display the world, delay of 1 sec for ease of understanding
def print_status(q_world, done, step, delay=1):
os.system('clear')
q_world.print_world(action, step)
q_world.print_q_table()
if done:
print("-------EPISODE DONE--------")
delay *= 2
time.sleep(delay)
# main loop of Q-Learning
if __name__ == '__main__':
parser = argparse.ArgumentParser()
help_ = "Trains and show final Q Table"
parser.add_argument("-t",
"--train",
help=help_,
action='store_true')
args = parser.parse_args()
if args.train:
maxwins = 2000
delay = 0
else:
maxwins = 10
delay = 1
wins = 0
episode_count = 10 * maxwins
# scores (max number of steps bef goal) - good indicator of learning
scores = deque(maxlen=maxwins)
q_world = QWorld()
step = 1
# state, action, reward, next state iteration
for episode in range(episode_count):
state = q_world.reset()
done = False
print_episode(episode, delay=delay)
while not done:
action = q_world.act()
next_state, reward, done = q_world.step(action)
q_world.update_q_table(state, action, reward, next_state)
print_status(q_world, done, step, delay=delay)
state = next_state
# if episode is done, perform housekeeping
if done:
if q_world.is_in_win_state():
wins += 1
scores.append(step)
if wins > maxwins:
print(scores)
exit(0)
# Exploration-Exploitation is updated every episode
q_world.update_epsilon()
step = 1
else:
step += 1
print(scores)
q_world.print_q_table()