-
Notifications
You must be signed in to change notification settings - Fork 2
/
takeoff.py
583 lines (500 loc) · 23.4 KB
/
takeoff.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
'''###########################################
CS221 Final Project: Takeoff Implementation -- Under Development
Authors:
Kongphop Wongpattananukul ([email protected])
Pouya Rezazadeh Kalehbasti ([email protected])
Dong Hee Song ([email protected])
###########################################'''
import sys, math
import numpy as np
from collections import deque
import random
import copy
import keras
from keras.models import Sequential
from keras.layers import Dense
import Box2D
from Box2D.b2 import (edgeShape, circleShape, fixtureDef, polygonShape, revoluteJointDef, contactListener)
import gym
from gym import spaces
from gym.utils import seeding, EzPickle
# Rocket trajectory optimization is a classic topic in Optimal Control.
#
# According to Pontryagin's maximum principle it's optimal to fire engine full throttle or
# turn it off. That's the reason this environment is OK to have discreet actions (engine on or off).
#
# Landing pad is always at coordinates (0,0). Coordinates are the first two numbers in state vector.
# Reward for moving from the top of the screen to landing pad and zero speed is about 100..140 points.
# If lander moves away from landing pad it loses reward back. Episode finishes if the lander crashes or
# comes to rest, receiving additional -100 or +100 points. Each leg ground contact is +10. Firing main
# engine is -0.3 points each frame. Firing side engine is -0.03 points each frame. Solved is 200 points.
#
# Landing outside landing pad is possible. Fuel is infinite, so an agent can learn to fly and then land
# on its first attempt. Please see source code for details.
#
# To see heuristic landing, run:
#
# python gym/envs/box2d/lunar_lander.py
#
# To play yourself, run:
#
# python examples/agents/keyboard_agent.py LunarLander-v2
#
# Created by Oleg Klimov. Licensed on the same terms as the rest of OpenAI Gym.
FPS = 50
SCALE = 30.0 # affects how fast-paced the game is, forces should be adjusted as well
lander_density = 5.0# * 3
MAIN_ENGINE_POWER = 13.0# * 3
SIDE_ENGINE_POWER = 0.6# * 3
INITIAL_RANDOM = 1000.0 # Set 1500 to make game harder
LANDER_POLY =[
(-14,+17), (-17,0), (-17,-10),
(+17,-10), (+17,0), (+14,+17)
]
LEG_AWAY = 20
LEG_DOWN = 18
LEG_W, LEG_H = 2, 8
LEG_SPRING_TORQUE = 40*3
SIDE_ENGINE_HEIGHT = 14.0
SIDE_ENGINE_AWAY = 12.0
VIEWPORT_W = 600
VIEWPORT_H = 400
class ContactDetector(contactListener):
def __init__(self, env):
contactListener.__init__(self)
self.env = env
def BeginContact(self, contact):
if self.env.lander==contact.fixtureA.body or self.env.lander==contact.fixtureB.body:
self.env.game_over = True
for i in range(2):
if self.env.legs[i] in [contact.fixtureA.body, contact.fixtureB.body]:
self.env.legs[i].ground_contact = True
def EndContact(self, contact):
for i in range(2):
if self.env.legs[i] in [contact.fixtureA.body, contact.fixtureB.body]:
self.env.legs[i].ground_contact = False
class LunarLander(gym.Env, EzPickle):
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second' : FPS
}
continuous = False
def __init__(self):
EzPickle.__init__(self)
self.seed()
self.viewer = None
self.world = Box2D.b2World()
self.moon = None
self.lander = None
self.particles = []
self.prev_reward = None
# useful range is -1 .. +1, but spikes can be higher
self.observation_space = spaces.Box(-np.inf, np.inf, shape=(8,), dtype=np.float32)
if self.continuous:
# Action is two floats [main engine, left-right engines].
# Main engine: -1..0 off, 0..+1 throttle from 50% to 100% power. Engine can't work with less than 50% power.
# Left-right: -1.0..-0.5 fire left engine, +0.5..+1.0 fire right engine, -0.5..0.5 off
self.action_space = spaces.Box(-1, +1, (2,), dtype=np.float32)
else:
# Nop, fire left engine, main engine, right engine
self.action_space = spaces.Discrete(4)
self.reset()
def seed(self, seed=1):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def _destroy(self):
if not self.moon: return
self.world.contactListener = None
self._clean_particles(True)
self.world.DestroyBody(self.moon)
self.moon = None
self.world.DestroyBody(self.lander)
self.lander = None
self.world.DestroyBody(self.legs[0])
self.world.DestroyBody(self.legs[1])
def reset(self):
self._destroy()
self.world.contactListener_keepref = ContactDetector(self)
self.world.contactListener = self.world.contactListener_keepref
self.game_over = False
self.prev_shaping = None
W = VIEWPORT_W/SCALE
H = VIEWPORT_H/SCALE
# terrain
CHUNKS = 11
height = self.np_random.uniform(0, H/2, size=(CHUNKS+1,) )
chunk_x = [W/(CHUNKS-1)*i for i in range(CHUNKS)]
# Take-off area ## ADDED -- MODIFIED
height[0] = 0
height[1] = 0
height[2] = 0
height[3] = 0
self.helipad_x1 = chunk_x[CHUNKS-4] #chunk_x[CHUNKS//2-1] ## MODIFIED
self.helipad_x2 = chunk_x[CHUNKS-2] # chunk_x[CHUNKS//2+1] ## MODIFIED
self.helipad_y = 1/2*H ## MODIFIED
height[CHUNKS-5] = self.helipad_y## MODIFIED
height[CHUNKS-4] = self.helipad_y## MODIFIED
height[CHUNKS-3] = self.helipad_y## MODIFIED
height[CHUNKS-2] = self.helipad_y## MODIFIED
height[CHUNKS-1] = self.helipad_y## MODIFIED
# height[CHUNKS//2-2] = self.helipad_y## MODIFIED
# height[CHUNKS//2-1] = self.helipad_y## MODIFIED
# height[CHUNKS//2+0] = self.helipad_y## MODIFIED
# height[CHUNKS//2+1] = self.helipad_y## MODIFIED
# height[CHUNKS//2+2] = self.helipad_y## MODIFIED
smooth_y = [0.33*(height[i-1] + height[i+0] + height[i+1]) for i in range(CHUNKS)]
self.moon = self.world.CreateStaticBody( shapes=edgeShape(vertices=[(0, 0), (W, 0)]) )
self.sky_polys = []
for i in range(CHUNKS-1):
p1 = (chunk_x[i], smooth_y[i])
p2 = (chunk_x[i+1], smooth_y[i+1])
self.moon.CreateEdgeFixture(
vertices=[p1,p2],
density=0,
friction=0.1)
self.sky_polys.append( [p1, p2, (p2[0],H), (p1[0],H)] )
self.moon.color1 = (0.0,0.0,0.0)
self.moon.color2 = (0.0,0.0,0.0)
initial_y = 1.0*LEG_DOWN/SCALE#VIEWPORT_H/SCALE/2 # position = (VIEWPORT_W/SCALE/2, initial_y), ## MODIFIED
initial_x = np.mean([chunk_x[1], chunk_x[2]]) #2*LEG_AWAY/SCALE # MODIFIED
self.lander = self.world.CreateDynamicBody(
position = (initial_x, initial_y), ## MODIFIED
angle=0.0,
fixtures = fixtureDef(
shape=polygonShape(vertices=[ (x/SCALE,y/SCALE) for x,y in LANDER_POLY ]),
density=lander_density,
friction=0.1,
categoryBits=0x0010,
maskBits=0x001, # collide only with ground
restitution=0.0) # 0.99 bouncy
)
self.lander.color1 = (0.5,0.4,0.9)
self.lander.color2 = (0.3,0.3,0.5)
# self.lander.ApplyForceToCenter( (
# self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM),
# self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM)
# ), True)
self.legs = []
for i in [-1,+1]:
leg = self.world.CreateDynamicBody(
position = (initial_x - i*LEG_AWAY/SCALE, initial_y), ## MODIFIED
angle = (i*0.05),
fixtures = fixtureDef(
shape=polygonShape(box=(LEG_W/SCALE, LEG_H/SCALE)),
density=1.0,
restitution=0.0,
categoryBits=0x0020,
maskBits=0x001)
)
leg.ground_contact = False
leg.color1 = (0.5,0.4,0.9)
leg.color2 = (0.3,0.3,0.5)
rjd = revoluteJointDef(
bodyA=self.lander,
bodyB=leg,
localAnchorA=(0, 0),
localAnchorB=(i*LEG_AWAY/SCALE, LEG_DOWN/SCALE),
enableMotor=True,
enableLimit=True,
maxMotorTorque=LEG_SPRING_TORQUE,
motorSpeed=+0.3*i # low enough not to jump back into the sky
)
if i==-1:
rjd.lowerAngle = +0.9 - 0.5 # Yes, the most esoteric numbers here, angles legs have freedom to travel within
rjd.upperAngle = +0.9
else:
rjd.lowerAngle = -0.9
rjd.upperAngle = -0.9 + 0.5
leg.joint = self.world.CreateJoint(rjd)
self.legs.append(leg)
self.drawlist = [self.lander] + self.legs
return self.step(np.array([0,0]) if self.continuous else 0)[0]
def _create_particle(self, mass, x, y, ttl):
p = self.world.CreateDynamicBody(
position = (x,y),
angle=0.0,
fixtures = fixtureDef(
shape=circleShape(radius=2/SCALE, pos=(0,0)),
density=mass,
friction=0.1,
categoryBits=0x0100,
maskBits=0x001, # collide only with ground
restitution=0.3)
)
p.ttl = ttl
self.particles.append(p)
self._clean_particles(False)
return p
def _clean_particles(self, all):
while self.particles and (all or self.particles[0].ttl<0):
self.world.DestroyBody(self.particles.pop(0))
def step(self, action):
if self.continuous:
action = np.clip(action, -1, +1).astype(np.float32)
else:
assert self.action_space.contains(action), "%r (%s) invalid " % (action, type(action))
# Engines
tip = (math.sin(self.lander.angle), math.cos(self.lander.angle))
side = (-tip[1], tip[0]);
dispersion = [self.np_random.uniform(-1.0, +1.0) / SCALE for _ in range(2)]
m_power = 0.0
if (self.continuous and action[0] > 0.0) or (not self.continuous and action==2):
# Main engine
if self.continuous:
m_power = (np.clip(action[0], 0.0,1.0) + 1.0)*0.5 # 0.5..1.0
assert m_power>=0.5 and m_power <= 1.0
else:
m_power = 1.0
ox = tip[0]*(4/SCALE + 2*dispersion[0]) + side[0]*dispersion[1] # 4 is move a bit downwards, +-2 for randomness
oy = -tip[1]*(4/SCALE + 2*dispersion[0]) - side[1]*dispersion[1]
impulse_pos = (self.lander.position[0] + ox, self.lander.position[1] + oy)
p = self._create_particle(3.5, impulse_pos[0], impulse_pos[1], m_power) # particles are just a decoration, 3.5 is here to make particle speed adequate
p.ApplyLinearImpulse( ( ox*MAIN_ENGINE_POWER*m_power, oy*MAIN_ENGINE_POWER*m_power), impulse_pos, True)
self.lander.ApplyLinearImpulse( (-ox*MAIN_ENGINE_POWER*m_power, -oy*MAIN_ENGINE_POWER*m_power), impulse_pos, True)
s_power = 0.0
if (self.continuous and np.abs(action[1]) > 0.5) or (not self.continuous and action in [1,3]):
# Orientation engines
if self.continuous:
direction = np.sign(action[1])
s_power = np.clip(np.abs(action[1]), 0.5,1.0)
assert s_power>=0.5 and s_power <= 1.0
else:
direction = action-2
s_power = 1.0
ox = tip[0]*dispersion[0] + side[0]*(3*dispersion[1]+direction*SIDE_ENGINE_AWAY/SCALE)
oy = -tip[1]*dispersion[0] - side[1]*(3*dispersion[1]+direction*SIDE_ENGINE_AWAY/SCALE)
impulse_pos = (self.lander.position[0] + ox - tip[0]*17/SCALE, self.lander.position[1] + oy + tip[1]*SIDE_ENGINE_HEIGHT/SCALE)
p = self._create_particle(0.7, impulse_pos[0], impulse_pos[1], s_power)
p.ApplyLinearImpulse( ( ox*SIDE_ENGINE_POWER*s_power, oy*SIDE_ENGINE_POWER*s_power), impulse_pos, True)
self.lander.ApplyLinearImpulse( (-ox*SIDE_ENGINE_POWER*s_power, -oy*SIDE_ENGINE_POWER*s_power), impulse_pos, True)
self.world.Step(1.0/FPS, 6*30, 2*30)
pos = self.lander.position
vel = self.lander.linearVelocity
state = [
(pos.x - VIEWPORT_W/SCALE/2) / (VIEWPORT_W/SCALE/2),
(pos.y - (self.helipad_y+LEG_DOWN/SCALE)) / (VIEWPORT_H/SCALE/2),
vel.x*(VIEWPORT_W/SCALE/2)/FPS,
vel.y*(VIEWPORT_H/SCALE/2)/FPS,
self.lander.angle,
20.0*self.lander.angularVelocity/FPS,
1.0 if self.legs[0].ground_contact else 0.0,
1.0 if self.legs[1].ground_contact else 0.0
]
assert len(state)==8
#################
# W = VIEWPORT_W/SCALE
# H = VIEWPORT_H/SCALE
# # terrain
# CHUNKS = 11
# height = self.np_random.uniform(0, H/2, size=(CHUNKS+1,) )
# chunk_x = [W/(CHUNKS-1)*i for i in range(CHUNKS)]
# # Take-off area ## ADDED -- MODIFIED
# height[0] = 0
# height[1] = 0
# height[2] = 0
# height[3] = 0
# self.helipad_x1 = chunk_x[CHUNKS-4] #chunk_x[CHUNKS//2-1] ## MODIFIED
# self.helipad_x2 = chunk_x[CHUNKS-2] # chunk_x[CHUNKS//2+1] ## MODIFIED
# self.helipad_y = 3/4*H ## MODIFIED
##############
reward = 0 ## MODIFIED
shaping = \
- 100*np.sqrt((state[0]-np.mean([self.helipad_x1, self.helipad_x2]))**2 + (state[1]-self.helipad_y)**2) \
- 100*np.sqrt(state[2]*state[2] + state[3]*state[3]) \
- 100*abs(state[4]) + 10*state[6] + 10*state[7] # And ten points for legs contact, the idea is if you
# lose contact again after landing, you get negative reward
if self.prev_shaping is not None:
reward = shaping - self.prev_shaping
self.prev_shaping = shaping
reward -= m_power*0.30 # less fuel spent is better, about -30 for heurisic landing
reward -= s_power*0.03
done = False
if self.game_over or abs(state[0]) >= 1.0:
done = True
reward = -100
if not self.lander.awake:
done = True
reward = +100
return np.array(state, dtype=np.float32), reward, done, {}
def render(self, mode='human'):
from gym.envs.classic_control import rendering
if self.viewer is None:
self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H)
self.viewer.set_bounds(0, VIEWPORT_W/SCALE, 0, VIEWPORT_H/SCALE)
for obj in self.particles:
obj.ttl -= 0.15
obj.color1 = (max(0.2,0.2+obj.ttl), max(0.2,0.5*obj.ttl), max(0.2,0.5*obj.ttl))
obj.color2 = (max(0.2,0.2+obj.ttl), max(0.2,0.5*obj.ttl), max(0.2,0.5*obj.ttl))
self._clean_particles(False)
for p in self.sky_polys:
self.viewer.draw_polygon(p, color=(0,0,0))
for obj in self.particles + self.drawlist:
for f in obj.fixtures:
trans = f.body.transform
if type(f.shape) is circleShape:
t = rendering.Transform(translation=trans*f.shape.pos)
self.viewer.draw_circle(f.shape.radius, 20, color=obj.color1).add_attr(t)
self.viewer.draw_circle(f.shape.radius, 20, color=obj.color2, filled=False, linewidth=2).add_attr(t)
else:
path = [trans*v for v in f.shape.vertices]
self.viewer.draw_polygon(path, color=obj.color1)
path.append(path[0])
self.viewer.draw_polyline(path, color=obj.color2, linewidth=2)
for x in [self.helipad_x1, self.helipad_x2]:
flagy1 = self.helipad_y
flagy2 = flagy1 + 50/SCALE
self.viewer.draw_polyline( [(x, flagy1), (x, flagy2)], color=(1,1,1) )
self.viewer.draw_polygon( [(x, flagy2), (x, flagy2-10/SCALE), (x+25/SCALE, flagy2-5/SCALE)], color=(0.8,0.8,0) )
return self.viewer.render(return_rgb_array = mode=='rgb_array')
def close(self):
if self.viewer is not None:
self.viewer.close()
self.viewer = None
class LunarLanderContinuous(LunarLander):
continuous = False # True
############################################################
class QLearningAlgorithm():
def __init__(self, actions, discount, weights, explorationProb=0.2, exploreProbDecay=0.99, explorationProbMin=0.01, batchSize=32):
self.actions = actions
self.discount = discount
self.explorationProb = explorationProb
self.exploreProbDecay = exploreProbDecay
self.explorationProbMin = explorationProbMin
self.weights = weights
self.numIters = 0
self.model = NeuralNetwork(batchSize, weights)
self.cache = deque(maxlen=1000000)
# This algorithm will produce an action given a state.
# Here we use the epsilon-greedy algorithm: with probability
# |explorationProb|, take a random action.
def getAction(self, state):
if np.random.rand() < self.explorationProb:
return random.choice(self.actions)
else:
predScores = self.model.predict(state)[0]
return np.argmax(predScores)
# We will call this function with (s, a, r, s'), which you should use to update |weights|.
# Note that if s is a terminal state, then s' will be None. Remember to check for this.
# You should update the weights using self.getStepSize(); use
# self.getQ() to compute the current estimate of the parameters.
def incorporateFeedback(self, states, actions, rewards, newStates, dones):
# initialize variable
states = np.squeeze(states)
newStates = np.squeeze(newStates)
X = states
y = self.model.predict(states)
# calculate gradient
targets = rewards + self.discount*(np.amax(self.model.predict(newStates), axis=1))*(1-dones)
ind = np.array([i for i in range(len(states))])
y[[ind], [actions]] = targets
# update weight
self.model.fit(X, y)
def updateCache(self, state, action, reward, newState, done):
self.cache.append((state, action, reward, newState, done))
# neural network
class NeuralNetwork():
def __init__(self, batchSize = 32, weights=None):
self.model = Sequential()
self.model.add(Dense(150, input_dim=8, activation='relu'))
self.model.add(Dense(120, activation='relu'))
self.model.add(Dense(4, activation='linear'))
adam = keras.optimizers.adam(lr=0.001)
self.model.compile(loss='mse', optimizer=adam)
if isinstance(weights, str):
self.model.load_weights(weights)
def predict(self, state):
return self.model.predict_on_batch(state)
def fit(self, X, y):
self.model.fit(X, y, epochs=1, verbose=0)
def save(self, weights):
self.model.save_weights(weights)
# Perform |numTrials| of the following:
# On each trial, take the MDP |mdp| and an RLAlgorithm |rl| and simulates the
# RL algorithm according to the dynamics of the MDP.
# Each trial will run for at most |maxIterations|.
# Return the list of rewards that we get for each trial.
def simulate(env, rl, numTrials=10, train=False, verbose=False,
trialDemoInterval=10, batchSize=32):
totalRewards = [] # The rewards we get on each trial
for trial in range(numTrials):
state = np.reshape(env.reset(), (1,8))
totalReward = 0
iteration = 0
while iteration <= 3000:
# while True:
action = rl.getAction(state)
newState, reward, done, info = env.step(action)
newState = np.reshape(newState, (1,8))
# Appending the new results to the deque
rl.updateCache(state, action, reward, newState, done)
# update
totalReward += reward
state = newState
iteration += 1
if verbose == True and trial % trialDemoInterval == 0:
still_open = env.render()
if still_open == False: break
# Conducting memory replay
if len(rl.cache) < batchSize: # Waiting till memory size is larger than batch size
continue
else:
batch = random.sample(rl.cache, batchSize)
states = np.array([sample[0] for sample in batch])
actions = np.array([sample[1] for sample in batch])
rewards = np.array([sample[2] for sample in batch])
newStates = np.array([sample[3] for sample in batch])
dones = np.array([sample[4] for sample in batch])
if train:
rl.incorporateFeedback(states, actions, rewards, newStates,
dones)
rl.explorationProb = max(rl.exploreProbDecay * rl.explorationProb,
rl.explorationProbMin)
if done:
break
totalRewards.append(totalReward)
if verbose:
print(('Trial {} Total Reward: {}'.format(trial, totalReward)))
print(('Mean(last 10 total rewards): {}'.format(np.mean(totalRewards[-10:]))))
return totalRewards
## Main variables
# np.random.seed(0)
numEpochs = 300
numTrials = 1 #30000
numTestTrials = 1000
trialDemoInterval = numTrials/2
discountFactor = 0.99
explorProbInit = 1.0
exploreProbDecay = 0.996
explorationProbMin = 0.01
batchSize = 64
if __name__ == '__main__':
# Initiate weights
# Cold start weights
weights = None
# Warm start weights
# weights = 'weights_heavy_ship_cold_start.h5'
# TRAIN
print('\n++++++++++++ TRAINING +++++++++++++')
rl = QLearningAlgorithm([0, 1, 2, 3], discountFactor, weights,
explorProbInit, exploreProbDecay,
explorationProbMin, batchSize)
# env = gym.make('LunarLander-v2')
env = LunarLander()
# env.seed(0)
for i in range(numEpochs):
totalRewards = simulate(env, rl, numTrials=numTrials, train=True, verbose=False,
trialDemoInterval=trialDemoInterval, batchSize=batchSize)
print('Average Total Reward in Trial {}/{}: {}'.format(i, numEpochs, np.mean(totalRewards)))
env.close()
# Save Weights
rl.model.save('lift_off.h5')
# TEST
print('\n\n++++++++++++++ TESTING +++++++++++++++')
weights = 'lift_off.h5'
# env = gym.make('LunarLander-v2')
env = LunarLander()
# env.seed(3)
rl = QLearningAlgorithm([0, 1, 2, 3], discountFactor, weights, 0.0, 0.0, 0.0, batchSize)
totalRewards = simulate(env, rl, numTrials=numTestTrials, train=False, verbose=True, trialDemoInterval=trialDemoInterval)
env.close()
print('Average Total Testing Reward: {}'.format(np.mean(totalRewards)))