-
Notifications
You must be signed in to change notification settings - Fork 0
/
environments.py
682 lines (588 loc) · 25.4 KB
/
environments.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# The following code is modified from openai/gym (https://github.com/openai/gym) under the MIT License.
# Modifications Copyright (c) 2019 Uber Technologies, Inc.
import sys
import math
import numpy as np
import Box2D
from Box2D.b2 import (edgeShape, circleShape, fixtureDef,
polygonShape, revoluteJointDef, contactListener)
import gym
from gym import spaces
from gym.utils import colorize, seeding
from collections import namedtuple
# This is simple 4-joints walker robot environment.
#
# There are two versions:
#
# - Normal, with slightly uneven terrain.
#
# - Hardcore with ladders, stumps, pitfalls.
#
# Reward is given for moving forward, total 300+ points up to the far end. If the robot falls,
# it gets -100. Applying motor torque costs a small amount of points, more optimal agent
# will get better score.
#
# Heuristic is provided for testing, it's also useful to get demonstrations to
# learn from. To run heuristic:
#
# python gym/envs/box2d/bipedal_walker.py
#
# State consists of hull angle speed, angular velocity, horizontal speed, vertical speed,
# position of joints and joints angular speed, legs contact with ground, and 10 lidar
# rangefinder measurements to help to deal with the hardcore version. There's no coordinates
# in the state vector. Lidar is less useful in normal version, but it works.
#
# To solve the game you need to get 300 points in 1600 time steps.
#
# To solve hardcore version you need 300 points in 2000 time steps.
#
# Created by Oleg Klimov. Licensed on the same terms as the rest of OpenAI Gym.
Env_config = namedtuple('Env_config', [
'name',
'ground_roughness',
'pit_gap',
'stump_width', 'stump_height', 'stump_float',
'stair_height', 'stair_width', 'stair_steps'
])
FPS = 50
SCALE = 30.0 # affects how fast-paced the game is, forces should be adjusted as well
MOTORS_TORQUE = 80
SPEED_HIP = 4
SPEED_KNEE = 6
LIDAR_RANGE = 160 / SCALE
INITIAL_RANDOM = 5
HULL_POLY = [
(-30, +9), (+6, +9), (+34, +1),
(+34, -8), (-30, -8)
]
LEG_DOWN = -8 / SCALE
LEG_W, LEG_H = 8 / SCALE, 34 / SCALE
VIEWPORT_W = 600
VIEWPORT_H = 400
TERRAIN_STEP = 14 / SCALE
TERRAIN_LENGTH = 200 # in steps
TERRAIN_HEIGHT = VIEWPORT_H / SCALE / 4
TERRAIN_GRASS = 10 # low long are grass spots, in steps
TERRAIN_STARTPAD = 20 # in steps
FRICTION = 2.5
HULL_FD = fixtureDef(
shape=polygonShape(vertices=[(x / SCALE, y / SCALE)
for x, y in HULL_POLY]),
density=5.0,
friction=0.1,
categoryBits=0x0020,
maskBits=0x001, # collide only with ground
restitution=0.0) # 0.99 bouncy
class ContactDetector(contactListener):
def __init__(self, env):
contactListener.__init__(self)
self.env = env
def BeginContact(self, contact):
if self.env.hull == contact.fixtureA.body or self.env.hull == contact.fixtureB.body:
self.env.game_over = True
for leg in [self.env.legs[1], self.env.legs[3]]:
if leg in [contact.fixtureA.body, contact.fixtureB.body]:
leg.ground_contact = True
def EndContact(self, contact):
for leg in [self.env.legs[1], self.env.legs[3]]:
if leg in [contact.fixtureA.body, contact.fixtureB.body]:
leg.ground_contact = False
class BipedalWalkerCustom(gym.Env):
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second': FPS
}
def __init__(self, env_config):
self.scale_vector = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.float)
self.spec = None
self.set_env_config(env_config)
self.env_seed = None
self._seed()
self.viewer = None
self.world = Box2D.b2World()
self.terrain = None
self.hull = None
self.prev_shaping = None
self.fd_polygon = fixtureDef(
shape=polygonShape(vertices=[(0, 0),
(1, 0),
(1, -1),
(0, -1)]),
friction=FRICTION)
self.fd_edge = fixtureDef(
shape=edgeShape(vertices=[(0, 0),
(1, 1)]),
friction=FRICTION,
categoryBits=0x0001,
)
self.reset()
high = np.array([np.inf] * 24)
self.action_space = spaces.Box(
np.array([-1, -1, -1, -1]), np.array([+1, +1, +1, +1]))
self.observation_space = spaces.Box(-high, high)
def set_env_config(self, env_config):
self.config = env_config
def _set_terrain_number(self):
self.hardcore = False
self.GRASS = 0
self.STUMP, self.STAIRS, self.PIT = -1, -1, -1
self._STATES_ = 1
if self.config.stump_width and self.config.stump_height and self.config.stump_float:
# STUMP exist
self.STUMP = self._STATES_
self._STATES_ += 1
if self.config.stair_height and self.config.stair_width and self.config.stair_steps:
# STAIRS exist
self.STAIRS = self._STATES_
self._STATES_ += 1
if self.config.pit_gap:
# PIT exist
self.PIT = self._STATES_
self._STATES_ += 1
if self._STATES_ > 1:
self.hardcore = True
def save_env_def(self, filename):
import json
a = {'config': self.config._asdict(), 'seed': self.env_seed}
with open(filename, 'w') as f:
json.dump(a, f)
def seed(self, seed=None):
return self._seed(seed)
def _seed(self, seed=None):
self.env_seed = seed
self.np_random, seed = seeding.np_random(seed)
return [seed]
def _destroy(self):
if not self.terrain:
return
self.world.contactListener = None
for t in self.terrain:
self.world.DestroyBody(t)
self.terrain = []
self.world.DestroyBody(self.hull)
self.hull = None
for leg in self.legs:
self.world.DestroyBody(leg)
self.legs = []
self.joints = []
self.world = None
def _generate_terrain(self, hardcore):
#GRASS, STUMP, STAIRS, PIT, _STATES_ = range(5)
state = self.GRASS
velocity = 0.0
y = TERRAIN_HEIGHT
counter = TERRAIN_STARTPAD
oneshot = False
self.terrain = []
self.terrain_x = []
self.terrain_y = []
pit_diff = 0
for i in range(TERRAIN_LENGTH):
x = i * TERRAIN_STEP
self.terrain_x.append(x)
if state == self.GRASS and not oneshot:
velocity = 0.8 * velocity + 0.01 * np.sign(TERRAIN_HEIGHT - y)
if i > TERRAIN_STARTPAD:
velocity += self.np_random.uniform(-1, 1) / SCALE # 1
# input parameter: ground_roughness
#ground_roughness = 1
y += self.config.ground_roughness * velocity
elif state == self.PIT and oneshot:
# input parameter: pit_gap
# pit_gap = self.np_random.randint(3, 5) #counter is the control of the GAP distance
#counter = pit_gap
#counter = self.np_random.randint(*self.config.pit_gap)
pit_gap = 1.0 + self.np_random.uniform(*self.config.pit_gap)
counter = np.ceil(pit_gap)
pit_diff = counter - pit_gap
poly = [
(x, y),
(x + TERRAIN_STEP, y),
(x + TERRAIN_STEP, y - 4 * TERRAIN_STEP),
(x, y - 4 * TERRAIN_STEP),
]
self.fd_polygon.shape.vertices = poly
t = self.world.CreateStaticBody(
fixtures=self.fd_polygon)
t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
self.terrain.append(t)
self.fd_polygon.shape.vertices = [
(p[0] + TERRAIN_STEP * pit_gap, p[1]) for p in poly]
t = self.world.CreateStaticBody(
fixtures=self.fd_polygon)
t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
self.terrain.append(t)
counter += 2
original_y = y
elif state == self.PIT and not oneshot:
y = original_y
if counter > 1:
y -= 4 * TERRAIN_STEP
if counter == 1:
self.terrain_x[-1] = self.terrain_x[-1] - pit_diff * TERRAIN_STEP
pit_diff = 0
elif state == self.STUMP and oneshot:
# input parameter stump_width, stump_height, stump_float
#stump_width = self.np_random.uniform(*self.config.stump_width)
stump_width = self.np_random.randint(*self.config.stump_width)
stump_height = self.np_random.uniform(
*self.config.stump_height)
stump_float = self.np_random.randint(*self.config.stump_float)
#counter = np.ceil(stump_width)
counter = stump_width
countery = stump_height
poly = [
(x, y + stump_float * TERRAIN_STEP),
(x + stump_width * TERRAIN_STEP, y + stump_float * TERRAIN_STEP),
(x + stump_width * TERRAIN_STEP, y + countery *
TERRAIN_STEP + stump_float * TERRAIN_STEP),
(x, y + countery *
TERRAIN_STEP + stump_float * TERRAIN_STEP),
]
self.fd_polygon.shape.vertices = poly
t = self.world.CreateStaticBody(
fixtures=self.fd_polygon)
t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
self.terrain.append(t)
elif state == self.STAIRS and oneshot:
# input parameters: stair_height, stair_width, stair_steps
stair_height = self.np_random.uniform(
*self.config.stair_height)
stair_slope = 1 if self.np_random.rand() > 0.5 else -1
stair_width = self.np_random.randint(*self.config.stair_width)
stair_steps = self.np_random.randint(*self.config.stair_steps)
original_y = y
for s in range(stair_steps):
poly = [
(x + (s * stair_width) * TERRAIN_STEP, y +
(s * stair_height * stair_slope) * TERRAIN_STEP),
(x + ((1 + s) * stair_width) * TERRAIN_STEP, y +
(s * stair_height * stair_slope) * TERRAIN_STEP),
(x + ((1 + s) * stair_width) * TERRAIN_STEP, y +
(-stair_height + s * stair_height * stair_slope) * TERRAIN_STEP),
(x + (s * stair_width) * TERRAIN_STEP, y + (-stair_height +
s * stair_height * stair_slope) * TERRAIN_STEP),
]
self.fd_polygon.shape.vertices = poly
t = self.world.CreateStaticBody(
fixtures=self.fd_polygon)
t.color1, t.color2 = (1, 1, 1), (0.6, 0.6, 0.6)
self.terrain.append(t)
counter = stair_steps * stair_width + 1
elif state == self.STAIRS and not oneshot:
s = stair_steps * stair_width - counter
n = s // stair_width
y = original_y + (n * stair_height * stair_slope) * TERRAIN_STEP - \
(stair_height if stair_slope == -1 else 0) * TERRAIN_STEP
oneshot = False
self.terrain_y.append(y)
counter -= 1
if counter == 0:
counter = self.np_random.randint(
TERRAIN_GRASS / 2, TERRAIN_GRASS)
if state == self.GRASS and hardcore:
state = self.np_random.randint(1, self._STATES_)
oneshot = True
else:
state = self.GRASS
oneshot = True
self.terrain_poly = []
for i in range(TERRAIN_LENGTH - 1):
poly = [
(self.terrain_x[i], self.terrain_y[i]),
(self.terrain_x[i + 1], self.terrain_y[i + 1])
]
self.fd_edge.shape.vertices = poly
t = self.world.CreateStaticBody(
fixtures=self.fd_edge)
color = (0.3, 1.0 if i % 2 == 0 else 0.8, 0.3)
t.color1 = color
t.color2 = color
self.terrain.append(t)
color = (0.4, 0.6, 0.3)
poly += [(poly[1][0], 0), (poly[0][0], 0)]
self.terrain_poly.append((poly, color))
self.terrain.reverse()
def _generate_clouds(self):
# Sorry for the clouds, couldn't resist
self.cloud_poly = []
for i in range(TERRAIN_LENGTH // 20):
x = self.np_random.uniform(0, TERRAIN_LENGTH) * TERRAIN_STEP
y = VIEWPORT_H / SCALE * 3 / 4
poly = [
(x + 15 * TERRAIN_STEP * math.sin(3.14 * 2 * a / 5) + self.np_random.uniform(0, 5 * TERRAIN_STEP),
y + 5 * TERRAIN_STEP * math.cos(3.14 * 2 * a / 5) + self.np_random.uniform(0, 5 * TERRAIN_STEP))
for a in range(5)]
x1 = min([p[0] for p in poly])
x2 = max([p[0] for p in poly])
self.cloud_poly.append((poly, x1, x2))
####
def augment_env(self, scale_vector):
self.scale_vector = np.copy(np.array(scale_vector, dtype=np.float))
def reset(self):
self._destroy()
self.world = Box2D.b2World()
self.world.contactListener_bug_workaround = ContactDetector(self)
self.world.contactListener = self.world.contactListener_bug_workaround
self.game_over = False
self.prev_shaping = None
self.scroll = 0.0
self.lidar_render = 0
W = VIEWPORT_W/SCALE
H = VIEWPORT_H/SCALE
self._set_terrain_number()
self._generate_terrain(self.hardcore)
self._generate_clouds()
# orig parameters
# LEG_DOWN = -8/SCALE
# LEG_W, LEG_H = 8/SCALE, 34/SCALE
# new parameters
U = 1.0 / SCALE
LEG_DOWN = -8 * U
#LEG_W = 1.0*8/SCALE
#LEG_H = 1.0*34/SCALE # maybe make one for each leg?
def calculate_total_area(x):
return x[0]*x[1]+x[2]*x[3]+x[4]*x[5]+x[6]*x[7]
def calculate_height(x): # returns height of shorter leg
return np.minimum(x[1]+x[3], x[5]+x[7])
body_param = [8.0, 34.0, 6.4, 34.0, 8.0, 34.0, 6.4, 34.0]
for i in range(len(body_param)):
body_param[i] = body_param[i]*self.scale_vector[i]
leg1_w_top = body_param[0]*U
leg1_h_top = body_param[1]*U
leg1_w_bot = body_param[2]*U
leg1_h_bot = body_param[3]*U
leg2_w_top = body_param[4]*U
leg2_h_top = body_param[5]*U
leg2_w_bot = body_param[6]*U
leg2_h_bot = body_param[7]*U
init_x = TERRAIN_STEP*TERRAIN_STARTPAD/2
init_y = TERRAIN_HEIGHT+np.maximum(leg1_h_top+leg1_h_bot, leg2_h_top+leg2_h_bot)
self.hull = self.world.CreateDynamicBody(
position = (init_x, init_y),
fixtures = HULL_FD
)
self.hull.color1 = (0.5,0.4,0.9)
self.hull.color2 = (0.3,0.3,0.5)
self.hull.ApplyForceToCenter((self.np_random.uniform(-INITIAL_RANDOM, INITIAL_RANDOM), 0), True)
self.legs = []
self.joints = []
for i in [-1,+1]:
if i == -1:
leg_w_top = leg1_w_top
leg_w_bot = leg1_w_bot
leg_h_top = leg1_h_top
leg_h_bot = leg1_h_bot
else:
leg_w_top = leg2_w_top
leg_w_bot = leg2_w_bot
leg_h_top = leg2_h_top
leg_h_bot = leg2_h_bot
leg = self.world.CreateDynamicBody(
position = (init_x, init_y - leg_h_top/2 - LEG_DOWN),
angle = (i*0.05),
fixtures = fixtureDef(
shape=polygonShape(box=(leg_w_top/2, leg_h_top/2)),
density=1.0,
restitution=0.0,
categoryBits=0x0020,
maskBits=0x001)
)
leg.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.)
leg.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.)
rjd = revoluteJointDef(
bodyA=self.hull,
bodyB=leg,
localAnchorA=(0, LEG_DOWN),
localAnchorB=(0, leg_h_top/2),
enableMotor=True,
enableLimit=True,
maxMotorTorque=MOTORS_TORQUE,
motorSpeed = i,
lowerAngle = -0.8,
upperAngle = 1.1,
)
self.legs.append(leg)
self.joints.append(self.world.CreateJoint(rjd))
lower = self.world.CreateDynamicBody(
position = (init_x, init_y - leg_h_top - leg_h_bot/2 - LEG_DOWN),
angle = (i*0.05),
fixtures = fixtureDef(
shape=polygonShape(box=(leg_w_bot/2, leg_h_bot/2)),
density=1.0,
restitution=0.0,
categoryBits=0x0020,
maskBits=0x001)
)
lower.color1 = (0.6-i/10., 0.3-i/10., 0.5-i/10.)
lower.color2 = (0.4-i/10., 0.2-i/10., 0.3-i/10.)
rjd = revoluteJointDef(
bodyA=leg,
bodyB=lower,
localAnchorA=(0, -leg_h_top/2),
localAnchorB=(0, leg_h_bot/2),
enableMotor=True,
enableLimit=True,
maxMotorTorque=MOTORS_TORQUE,
motorSpeed = 1,
lowerAngle = -1.6,
upperAngle = -0.1,
)
lower.ground_contact = False
self.legs.append(lower)
self.joints.append(self.world.CreateJoint(rjd))
self.drawlist = self.terrain + self.legs + [self.hull]
class LidarCallback(Box2D.b2.rayCastCallback):
def ReportFixture(self, fixture, point, normal, fraction):
if (fixture.filterData.categoryBits & 1) == 0:
return 1
self.p2 = point
self.fraction = fraction
return 0
self.lidar = [LidarCallback() for _ in range(10)]
return self._step(np.array([0,0,0,0]))[0]
####
def step(self, action):
return self._step(action)
def _step(self, action):
# self.hull.ApplyForceToCenter((0, 20), True) -- Uncomment this to receive a bit of stability help
control_speed = False # Should be easier as well
if control_speed:
self.joints[0].motorSpeed = float(
SPEED_HIP * np.clip(action[0], -1, 1))
self.joints[1].motorSpeed = float(
SPEED_KNEE * np.clip(action[1], -1, 1))
self.joints[2].motorSpeed = float(
SPEED_HIP * np.clip(action[2], -1, 1))
self.joints[3].motorSpeed = float(
SPEED_KNEE * np.clip(action[3], -1, 1))
else:
self.joints[0].motorSpeed = float(SPEED_HIP * np.sign(action[0]))
self.joints[0].maxMotorTorque = float(
MOTORS_TORQUE * np.clip(np.abs(action[0]), 0, 1))
self.joints[1].motorSpeed = float(SPEED_KNEE * np.sign(action[1]))
self.joints[1].maxMotorTorque = float(
MOTORS_TORQUE * np.clip(np.abs(action[1]), 0, 1))
self.joints[2].motorSpeed = float(SPEED_HIP * np.sign(action[2]))
self.joints[2].maxMotorTorque = float(
MOTORS_TORQUE * np.clip(np.abs(action[2]), 0, 1))
self.joints[3].motorSpeed = float(SPEED_KNEE * np.sign(action[3]))
self.joints[3].maxMotorTorque = float(
MOTORS_TORQUE * np.clip(np.abs(action[3]), 0, 1))
self.world.Step(1.0 / FPS, 6 * 30, 2 * 30)
pos = self.hull.position
vel = self.hull.linearVelocity
for i in range(10):
self.lidar[i].fraction = 1.0
self.lidar[i].p1 = pos
self.lidar[i].p2 = (
pos[0] + math.sin(1.5 * i / 10.0) * LIDAR_RANGE,
pos[1] - math.cos(1.5 * i / 10.0) * LIDAR_RANGE)
self.world.RayCast(
self.lidar[i], self.lidar[i].p1, self.lidar[i].p2)
state = [
# Normal angles up to 0.5 here, but sure more is possible.
self.hull.angle,
2.0 * self.hull.angularVelocity / FPS,
# Normalized to get -1..1 range
0.3 * vel.x * (VIEWPORT_W / SCALE) / FPS,
0.3 * vel.y * (VIEWPORT_H / SCALE) / FPS,
# This will give 1.1 on high up, but it's still OK (and there should be spikes on hiting the ground, that's normal too)
self.joints[0].angle,
self.joints[0].speed / SPEED_HIP,
self.joints[1].angle + 1.0,
self.joints[1].speed / SPEED_KNEE,
1.0 if self.legs[1].ground_contact else 0.0,
self.joints[2].angle,
self.joints[2].speed / SPEED_HIP,
self.joints[3].angle + 1.0,
self.joints[3].speed / SPEED_KNEE,
1.0 if self.legs[3].ground_contact else 0.0
]
state += [l.fraction for l in self.lidar]
assert len(state) == 24
self.scroll = pos.x - VIEWPORT_W / SCALE / 5
# moving forward is a way to receive reward (normalized to get 300 on completion)
shaping = 130 * pos[0] / SCALE
# keep head straight, other than that and falling, any behavior is unpunished
shaping -= 5.0 * abs(state[0])
reward = 0
if self.prev_shaping is not None:
reward = shaping - self.prev_shaping
self.prev_shaping = shaping
for a in action:
reward -= 0.00035 * MOTORS_TORQUE * np.clip(np.abs(a), 0, 1)
# normalized to about -50.0 using heuristic, more optimal agent should spend less
done = False
finish = False
if self.game_over or pos[0] < 0:
reward = -100
done = True
if pos[0] > (TERRAIN_LENGTH - TERRAIN_GRASS) * TERRAIN_STEP:
done = True
finish = True
return np.array(state), reward, done, {"finish": finish}
def render(self, *args, **kwargs):
return self._render(*args, **kwargs)
def _render(self, mode='human', close=False):
if close:
if self.viewer is not None:
self.viewer.close()
self.viewer = None
return
from gym.envs.classic_control import rendering
if self.viewer is None:
self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H)
self.viewer.set_bounds(self.scroll, VIEWPORT_W /
SCALE + self.scroll, 0, VIEWPORT_H / SCALE)
self.viewer.draw_polygon([
(self.scroll, 0),
(self.scroll + VIEWPORT_W / SCALE, 0),
(self.scroll + VIEWPORT_W / SCALE, VIEWPORT_H / SCALE),
(self.scroll, VIEWPORT_H / SCALE),
], color=(0.9, 0.9, 1.0))
for poly, x1, x2 in self.cloud_poly:
if x2 < self.scroll / 2:
continue
if x1 > self.scroll / 2 + VIEWPORT_W / SCALE:
continue
self.viewer.draw_polygon(
[(p[0] + self.scroll / 2, p[1]) for p in poly], color=(1, 1, 1))
for poly, color in self.terrain_poly:
if poly[1][0] < self.scroll:
continue
if poly[0][0] > self.scroll + VIEWPORT_W / SCALE:
continue
self.viewer.draw_polygon(poly, color=color)
self.lidar_render = (self.lidar_render + 1) % 100
i = self.lidar_render
if i < 2 * len(self.lidar):
l = self.lidar[i] if i < len(
self.lidar) else self.lidar[len(self.lidar) - i - 1]
self.viewer.draw_polyline(
[l.p1, l.p2], color=(1, 0, 0), linewidth=1)
for obj in 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, 30, color=obj.color1).add_attr(t)
self.viewer.draw_circle(
f.shape.radius, 30, 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)
flagy1 = TERRAIN_HEIGHT
flagy2 = flagy1 + 50 / SCALE
x = TERRAIN_STEP * 3
self.viewer.draw_polyline(
[(x, flagy1), (x, flagy2)], color=(0, 0, 0), linewidth=2)
f = [(x, flagy2), (x, flagy2 - 10 / SCALE),
(x + 25 / SCALE, flagy2 - 5 / SCALE)]
self.viewer.draw_polygon(f, color=(0.9, 0.2, 0))
self.viewer.draw_polyline(f + [f[0]], color=(0, 0, 0), linewidth=2)
return self.viewer.render(return_rgb_array=mode == 'rgb_array')