-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReinforcementLearning.py
331 lines (264 loc) · 11.8 KB
/
ReinforcementLearning.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import numpy as np
import time
import random
import matplotlib.pyplot as plt
from keras import Sequential
from keras.engine import InputLayer
from keras.layers import Dense
from keras.models import load_model
from matplotlib import animation
class Maze:
def __init__(self, name='5x5-v0'):
self.name = name
### positions
# [0 1 2 3 4]
# [5 6 7 8 9]
# [10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]
d = {
'5x5-v0': np.array([['S', '.', '.', '.', '.'],
['.', 'O', '.', 'O', '.'],
['.', '.', '.', '.', '.'],
['.', 'O', '.', 'O', '.'],
['.', '.', '.', '.', 'E']]),
'5x5-v1': np.array([['S', '.', 'O', '.', '.'],
['.', 'O', '.', 'O', '.'],
['.', '.', '.', '.', '.'],
['.', 'O', '.', 'O', '.'],
['.', '.', 'O', '.', 'E']]),
'10x10': np.array([['.', 'O', '.', '.', 'O', '.', '.', '.', '.', '.'],
['.', '.', 'O', '.', '.', '.', 'O', '.', 'S', '.'],
['.', '.', '.', 'O', '.', '.', 'O', '.', '.', '.'],
['.', '.', '.', '.', 'O', '.', 'O', 'O', 'O', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', 'O', '.', 'O', '.', '.', 'O'],
['.', 'O', 'O', 'O', '.', '.', '.', 'O', '.', '.'],
['.', '.', 'E', 'O', '.', '.', '.', '.', 'O', '.'],
['.', 'O', '.', 'O', '.', '.', '.', '.', '.', 'O'],
['O', '.', '.', '.', '.', '.', '.', '.', '.', '.']])
}
self.maze = d[self.name]
self.rows, self.columns = self.maze.shape
self.reset()
self.enlarge = 10
self.size = (self.rows * self.enlarge, self.columns * self.enlarge)
def show(self):
old_sign = self.get_sign_at_current_position()
self.maze[self.position // self.columns, self.position % self.columns] = '@' # current position
print(self.name)
[print(row) for row in self.maze]
print()
self.maze[self.position // self.columns, self.position % self.columns] = old_sign
def make_image(self, size):
img = np.zeros((*size, 3), dtype='uint8')
for i, row in enumerate(self.maze):
for j, cell in enumerate(row):
img[i*self.enlarge: (i+1)*self.enlarge-1, j*self.enlarge: (j+1)*self.enlarge-1] = self.coloured_part(cell)[-1, -1, :]
i = self.position // self.columns
j = self.position % self.columns
img[i*self.enlarge: (i+1)*self.enlarge-1, j*self.enlarge: (j+1)*self.enlarge-1] = self.coloured_part('@')[-1, -1, :]
img[0, :, :] = 0
img[:, 0, :] = 0
return img
def coloured_part(self, cell):
part = np.zeros((self.enlarge, self.enlarge, 3), dtype='uint8')
if cell == 'S' or cell == '.':
part[:, :, 1] = 200
elif cell == 'O':
part[:, :, 0] = 200
elif cell == 'E':
part[:, :, 0] = 250
part[:, :, 1] = 200
elif cell == '@':
part[:, :, 2] = 200
return part
def reset(self):
self.position = 0 if self.name.startswith('5') else 18
def get_sign_at_current_position(self):
return self.maze[self.position // self.columns, self.position % self.columns]
def check_start(self):
return self.get_sign_at_current_position() == 'S'
def check_end(self):
return self.get_sign_at_current_position() == 'E'
def check_holes(self):
return self.get_sign_at_current_position() == 'O'
def check_path(self):
return self.get_sign_at_current_position() == '.'
def make_move(self, direction):
if direction == 'up':
if self.position >= self.columns:
self.position -= self.columns
elif direction == 'right':
if self.position % self.columns != self.columns - 1:
self.position += 1
elif direction == 'down':
if self.position < (self.rows * self.columns) - self.columns:
self.position += self.columns
elif direction == 'left':
if self.position % self.columns != 0:
self.position -= 1
class ReinforcementLearning:
def __init__(self, name='5x5-v0', verbose=False):
self.name = name
self.verbose = verbose
self.maze = Maze(self.name)
self.set_parameters()
def set_parameters(self):
self.max_episodes = 1000
self.learning_rate = 0.1
self.max_steps = 100 # Max steps per episode
self.discount_rate = 0.95
# Exploration parameters
self.epsilon = 1.0 # Exploration rate
self.max_epsilon = 1.0 # Exploration probability at start
self.min_epsilon = 0.01 # Minimum exploration probability
self.decay_rate = 0.01 # Exponential decay rate for exploration prob
self.map2str = {
0: 'up',
1: 'right',
2: 'down',
3: 'left'
}
def check_position(self):
"""
:return: reward, done
"""
if self.maze.check_end():
print('EEEEENNNNNNNNNNNNNNNDDDDDD \n\n\n')
return 1000, True
elif self.maze.check_holes():
print('HHHHHOOOOOOLLLLLLLEEEEEEEE \n\n\n')
return -100, True
elif self.maze.check_path() or self.maze.check_start():
return -1, False
def reinforcement_learning(self):
action_size = len(self.map2str)
state_size = self.maze.rows * self.maze.columns
Q_table = np.zeros((state_size, action_size))
rewards = []
for episode in range(self.max_episodes):
done = False
total_reward = 0
self.maze.reset()
if self.verbose:
print("***********************************************")
print("EPISODE ", episode, '\n')
self.maze.show()
time.sleep(2)
for step in range(self.max_steps):
r = random.uniform(0, 1)
if r > self.epsilon: # exploitation (biggest value of current state)
action = np.argmax(Q_table[self.maze.position, :])
else: # exploration (random choice)
action = random.randint(0, action_size - 1)
previous_position = self.maze.position
self.maze.make_move(self.map2str[action])
reward, done = self.check_position()
# update Q_table
Q_table[previous_position, action] = Q_table[previous_position, action] + self.learning_rate * \
(reward + self.discount_rate * np.max(Q_table[self.maze.position, :]) -
Q_table[previous_position, action])
if self.verbose:
self.maze.show()
time.sleep(1.5)
total_reward += reward
if done:
break
rewards.append(total_reward)
# reduce epsilon (less exploration)
self.epsilon = self.min_epsilon + (self.max_epsilon - self.min_epsilon) * np.exp(-self.decay_rate * episode)
if self.verbose:
print(' up right down left')
for i, row in enumerate(Q_table):
print(i, row)
print('\n\n')
self.Q = Q_table
def play(self):
images = []
self.maze = Maze(self.name)
images.append([plt.imshow(self.maze.make_image(self.maze.size), animated=True)])
for step in range(self.max_steps):
# time.sleep(1.5) # for demo only
# Take the action (index) that have the maximum expected future reward given that state
action = np.argmax(self.Q[self.maze.position, :])
self.maze.make_move(self.map2str[action])
images.append([plt.imshow(self.maze.make_image(self.maze.size), animated=True)])
_, done = self.check_position()
if done:
break
anim = animation.ArtistAnimation(plt.figure(0), images, interval=1000, repeat=False, blit=True)
plt.show()
def create_model(self):
model = Sequential()
model.add(InputLayer(batch_input_shape=(1, self.maze.size[0] * self.maze.size[1])))
model.add(Dense(4, activation='sigmoid'))
model.add(Dense(len(self.map2str), activation='linear'))
model.compile(loss='mse', optimizer='adam', metrics=['mae'])
model.summary()
return model
@staticmethod
def flatten_image(img):
return np.ndarray.flatten(img)[np.newaxis, :]
@staticmethod
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
def deep_reinforcement_learning(self):
model = self.create_model()
action_size = len(self.map2str)
y = 0.95
eps = 0.5
decay_factor = 0.999
rewards_list = []
self.max_episodes = 500
self.max_steps = 50
for i in range(self.max_episodes):
print("Episode {} of {}".format(i + 1, self.max_episodes))
self.maze.reset()
eps *= decay_factor
done = False
reward_sum = 0
for step in range(self.max_steps):
if np.random.random() < eps: # exploration (random choice)
action = random.randint(0, action_size - 1)
else: # exploitation (biggest value of current state)
img = self.flatten_image(self.rgb2gray(self.maze.make_image(self.maze.size)))
action = np.argmax(model.predict(img))
self.maze.make_move(self.map2str[action])
reward, done = self.check_position()
img = self.flatten_image(self.rgb2gray(self.maze.make_image(self.maze.size)))
target = reward + y * np.max(model.predict(img))
target_vec = model.predict(img)[0]
target_vec[action] = target
model.fit(img, target_vec.reshape(-1, len(self.map2str)), epochs=1, verbose=0)
reward_sum += reward
if done:
break
# print('Reward sum: ', reward_sum, '\n')
rewards_list.append(reward_sum)
s = self.maze.name + '_dense4_ep' + str(self.max_episodes) + '_st' + str(self.max_steps) + '.h5'
model.save(s)
return model
def play_deep(self, model):
images = []
self.maze = Maze(self.name)
images.append([plt.imshow(self.rgb2gray(self.maze.make_image(self.maze.size)), animated=True, cmap='gray')])
for step in range(self.max_steps):
img = self.flatten_image(self.rgb2gray(self.maze.make_image(self.maze.size)))
action = np.argmax(model.predict(img))
print(self.map2str[action])
self.maze.make_move(self.map2str[action])
images.append([plt.imshow(self.rgb2gray(self.maze.make_image(self.maze.size)), animated=True, cmap='gray')])
_, done = self.check_position()
if done:
break
# anim = animation.ArtistAnimation(plt.figure(0), images, interval=1000, repeat=False, blit=True)
# plt.show()
if __name__ == '__main__':
np.set_printoptions(precision=2, suppress=True)
name = '5x5-v0'
rl = ReinforcementLearning(name, verbose=False)
# model = rl.deep_reinforcement_learning()
#
model = load_model('5x5-v0_dense4_ep500_st50.h5')
rl.play_deep(model)