-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
389 lines (331 loc) · 14.1 KB
/
main.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
'''
Deep Q-learning approach to the Mountain Car problem
using OpenAI's gym environment.
As part of the basic series on reinforcement learning @
https://github.com/vmayoral/basic_reinforcement_learning
This code implements the algorithm described at:
Mnih, V., Kavukcuoglu, K., Silver, D., Rusu, A. A., Veness, J., Bellemare, M. G., ... & Petersen,
S. (2015). Human-level control through deep reinforcement learning. Nature, 518(7540), 529-533.
Code based on @wingedsheep's work at https://gist.github.com/wingedsheep/4199594b02138dd427c22a540d6d6b8d
@author: Victor Mayoral Vilches <victor@erlerobotics.com>
'''
import random
import gym
import numpy as np
from keras import optimizers
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.normalization import BatchNormalization
from keras.models import Sequential
from keras.regularizers import l2
# import os
# os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32"
# import theano
class Memory:
"""
This class provides an abstraction to store the [s, a, r, a'] elements of each iteration.
Instead of using tuples (as other implementations do), the information is stored in lists
that get returned as another list of dictionaries with each key corresponding to either
"state", "action", "reward", "nextState" or "isFinal".
"""
def __init__(self, size):
self.size = size
self.currentPosition = 0
self.states = []
self.actions = []
self.rewards = []
self.newStates = []
self.finals = []
def getMiniBatch(self, size):
indices = random.sample(np.arange(len(self.states)), min(size, len(self.states)))
miniBatch = []
for index in indices:
miniBatch.append({'state': self.states[index], 'action': self.actions[index], 'reward': self.rewards[index],
'newState': self.newStates[index], 'isFinal': self.finals[index]})
return miniBatch
def getCurrentSize(self):
return len(self.states)
def getMemory(self, index):
return {'state': self.states[index], 'action': self.actions[index], 'reward': self.rewards[index],
'newState': self.newStates[index], 'isFinal': self.finals[index]}
def addMemory(self, state, action, reward, newState, isFinal):
if (self.currentPosition >= self.size - 1):
self.currentPosition = 0
if (len(self.states) > self.size):
self.states[self.currentPosition] = state
self.actions[self.currentPosition] = action
self.rewards[self.currentPosition] = reward
self.newStates[self.currentPosition] = newState
self.finals[self.currentPosition] = isFinal
else:
self.states.append(state)
self.actions.append(action)
self.rewards.append(reward)
self.newStates.append(newState)
self.finals.append(isFinal)
self.currentPosition += 1
class DeepQ:
"""
DQN abstraction.
As a quick reminder:
traditional Q-learning:
Q(s, a) += alpha * (reward(s,a) + gamma * max(Q(s') - Q(s,a))
DQN:
target = reward(s,a) + gamma * max(Q(s')
"""
def __init__(self, inputs, outputs, memorySize, discountFactor, learningRate, learnStart):
"""
Parameters:
- inputs: input size
- outputs: output size
- memorySize: size of the memory that will store each state
- discountFactor: the discount factor (gamma)
- learningRate: learning rate
- learnStart: steps to happen before for learning. Set to 128
"""
self.input_size = inputs
self.output_size = outputs
self.memory = Memory(memorySize)
self.discountFactor = discountFactor
self.learnStart = learnStart
self.learningRate = learningRate
def initNetworks(self, hiddenLayers):
model = self.createModel(self.input_size, self.output_size, hiddenLayers, "relu", self.learningRate)
self.model = model
targetModel = self.createModel(self.input_size, self.output_size, hiddenLayers, "relu", self.learningRate)
self.targetModel = targetModel
def createRegularizedModel(self, inputs, outputs, hiddenLayers, activationType, learningRate):
bias = True
dropout = 0
regularizationFactor = 0.01
model = Sequential()
if len(hiddenLayers) == 0:
model.add(Dense(self.output_size, input_shape=(self.input_size,), init='lecun_uniform', bias=bias))
model.add(Activation("linear"))
else:
if regularizationFactor > 0:
model.add(Dense(hiddenLayers[0], input_shape=(self.input_size,), init='lecun_uniform',
W_regularizer=l2(regularizationFactor), bias=bias))
else:
model.add(Dense(hiddenLayers[0], input_shape=(self.input_size,), init='lecun_uniform', bias=bias))
if (activationType == "LeakyReLU"):
model.add(LeakyReLU(alpha=0.01))
else:
model.add(Activation(activationType))
for index in range(1, len(hiddenLayers)):
layerSize = hiddenLayers[index]
if regularizationFactor > 0:
model.add(Dense(layerSize, init='lecun_uniform', W_regularizer=l2(regularizationFactor), bias=bias))
else:
model.add(Dense(layerSize, init='lecun_uniform', bias=bias))
if (activationType == "LeakyReLU"):
model.add(LeakyReLU(alpha=0.01))
else:
model.add(Activation(activationType))
if dropout > 0:
model.add(Dropout(dropout))
model.add(Dense(self.output_size, init='lecun_uniform', bias=bias))
model.add(Activation("linear"))
optimizer = optimizers.RMSprop(lr=learningRate, rho=0.9, epsilon=1e-06)
model.compile(loss="mse", optimizer=optimizer)
model.summary()
return model
def createModel(self, inputs, outputs, hiddenLayers, activationType, learningRate):
model = Sequential()
if len(hiddenLayers) == 0:
model.add(Dense(self.output_size, input_shape=(self.input_size,), init='lecun_uniform'))
model.add(Activation("linear"))
else:
model.add(Dense(hiddenLayers[0], input_shape=(self.input_size,), init='lecun_uniform'))
if (activationType == "LeakyReLU"):
model.add(LeakyReLU(alpha=0.01))
else:
model.add(Activation(activationType))
for index in range(1, len(hiddenLayers)):
# print("adding layer "+str(index))
layerSize = hiddenLayers[index]
model.add(Dense(layerSize, init='lecun_uniform'))
if (activationType == "LeakyReLU"):
model.add(LeakyReLU(alpha=0.01))
else:
model.add(Activation(activationType))
model.add(Dense(self.output_size, init='lecun_uniform'))
model.add(Activation("linear"))
optimizer = optimizers.RMSprop(lr=learningRate, rho=0.9, epsilon=1e-06)
model.compile(loss="mse", optimizer=optimizer)
model.summary()
return model
def printNetwork(self):
i = 0
for layer in self.model.layers:
weights = layer.get_weights()
print
"layer ", i, ": ", weights
i += 1
def backupNetwork(self, model, backup):
weightMatrix = []
for layer in model.layers:
weights = layer.get_weights()
weightMatrix.append(weights)
i = 0
for layer in backup.layers:
weights = weightMatrix[i]
layer.set_weights(weights)
i += 1
def updateTargetNetwork(self):
self.backupNetwork(self.model, self.targetModel)
# predict Q values for all the actions
def getQValues(self, state):
predicted = self.model.predict(state.reshape(1, len(state)))
return predicted[0]
def getTargetQValues(self, state):
predicted = self.targetModel.predict(state.reshape(1, len(state)))
return predicted[0]
def getMaxQ(self, qValues):
return np.max(qValues)
def getMaxIndex(self, qValues):
return np.argmax(qValues)
# calculate the target function
def calculateTarget(self, qValuesNewState, reward, isFinal):
"""
target = reward(s,a) + gamma * max(Q(s')
"""
if isFinal:
return reward
else:
return reward + self.discountFactor * self.getMaxQ(qValuesNewState)
# select the action with the highest Q value
def selectAction(self, qValues, explorationRate):
rand = random.random()
if rand < explorationRate:
action = np.random.randint(0, self.output_size)
else:
action = self.getMaxIndex(qValues)
return action
def selectActionByProbability(self, qValues, bias):
qValueSum = 0
shiftBy = 0
for value in qValues:
if value + shiftBy < 0:
shiftBy = - (value + shiftBy)
shiftBy += 1e-06
for value in qValues:
qValueSum += (value + shiftBy) ** bias
probabilitySum = 0
qValueProbabilities = []
for value in qValues:
probability = ((value + shiftBy) ** bias) / float(qValueSum)
qValueProbabilities.append(probability + probabilitySum)
probabilitySum += probability
qValueProbabilities[len(qValueProbabilities) - 1] = 1
rand = random.random()
i = 0
for value in qValueProbabilities:
if (rand <= value):
return i
i += 1
def addMemory(self, state, action, reward, newState, isFinal):
self.memory.addMemory(state, action, reward, newState, isFinal)
def learnOnLastState(self):
if self.memory.getCurrentSize() >= 1:
return self.memory.getMemory(self.memory.getCurrentSize() - 1)
def learnOnMiniBatch(self, miniBatchSize, useTargetNetwork=True):
# Do not learn until we've got self.learnStart samples
if self.memory.getCurrentSize() > self.learnStart:
# learn in batches of 128
miniBatch = self.memory.getMiniBatch(miniBatchSize)
X_batch = np.empty((0, self.input_size), dtype=np.float64)
Y_batch = np.empty((0, self.output_size), dtype=np.float64)
for sample in miniBatch:
isFinal = sample['isFinal']
state = sample['state']
action = sample['action']
reward = sample['reward']
newState = sample['newState']
qValues = self.getQValues(state)
if useTargetNetwork:
qValuesNewState = self.getTargetQValues(newState)
else:
qValuesNewState = self.getQValues(newState)
targetValue = self.calculateTarget(qValuesNewState, reward, isFinal)
X_batch = np.append(X_batch, np.array([state.copy()]), axis=0)
Y_sample = qValues.copy()
Y_sample[action] = targetValue
Y_batch = np.append(Y_batch, np.array([Y_sample]), axis=0)
if isFinal:
X_batch = np.append(X_batch, np.array([newState.copy()]), axis=0)
Y_batch = np.append(Y_batch, np.array([[reward] * self.output_size]), axis=0)
self.model.fit(X_batch, Y_batch, batch_size=len(miniBatch), nb_epoch=1, verbose=0)
env = gym.make('MountainCar-v0')
# env.monitor.start('/tmp/mountaincar-experiment-1', force=True)
# Exploring the new environment observations and actions:
#
# >>> import gym
# env = gym.make('MountainCar-v0')>>> env = gym.make('MountainCar-v0')
# [2016-06-19 17:37:12,780] Making new env: MountainCar-v0
# >>> print env.observation_space
# Box(2,)
# >>> print env.action_space
# Discrete(3)
epochs = 1000
steps = 100000
updateTargetNetwork = 10000
explorationRate = 1
minibatch_size = 128
learnStart = 128
learningRate = 0.00025
discountFactor = 0.99
memorySize = 1000000
last100Scores = [0] * 100
last100ScoresIndex = 0
last100Filled = False
deepQ = DeepQ(2, 3, memorySize, discountFactor, learningRate, learnStart)
# deepQ.initNetworks([30,30,30])
deepQ.initNetworks([30, 30])
# deepQ.initNetworks([300,300])
stepCounter = 0
# number of reruns
for epoch in range(epochs):
observation = env.reset()
print
explorationRate
# number of timesteps
for t in range(steps):
#env.render()
qValues = deepQ.getQValues(observation)
action = deepQ.selectAction(qValues, explorationRate)
newObservation, reward, done, info = env.step(action)
if (t >= 199):
print("Failed. Time out")
done = True
# reward = 200
if done and t < 199:
print("Sucess!")
# reward -= 200
deepQ.addMemory(observation, action, reward, newObservation, done)
if stepCounter >= learnStart:
if stepCounter <= updateTargetNetwork:
deepQ.learnOnMiniBatch(minibatch_size, False)
else:
deepQ.learnOnMiniBatch(minibatch_size, True)
observation = newObservation
if done:
last100Scores[last100ScoresIndex] = t
last100ScoresIndex += 1
if last100ScoresIndex >= 100:
last100Filled = True
last100ScoresIndex = 0
if not last100Filled:
print("Episode ", epoch, " finished after {} timesteps".format(t + 1))
else:
print("Episode ", epoch, " finished after {} timesteps".format(t + 1), " last 100 average: ", (
sum(last100Scores) / len(last100Scores)))
break
stepCounter += 1
if stepCounter % updateTargetNetwork == 0:
deepQ.updateTargetNetwork()
print( "updating target network")
explorationRate *= 0.995
# explorationRate -= (2.0/epochs)
explorationRate = max(0.05, explorationRate)
# env.monitor.close()