-
Notifications
You must be signed in to change notification settings - Fork 1
/
gym_torcs_docker.py
249 lines (189 loc) · 7.47 KB
/
gym_torcs_docker.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
# -*- coding: utf-8 -*-
"""
rl_torcs.gym_torcs_docker
~~~~~~~~~~~~~~~~~~~~~~~~~
DESCRIPTION
gym torcs environment using docker. allows for parallel execution.
Based on https://github.com/ugo-nama-kun/gym_torcs
:copyright: (c) 2017 by Bastian Niebel
"""
import os
import random
import collections as col
import numpy as np
import snakeoil3_gym as snakeoil3
from gym import spaces
def obs_to_state(obs):
return np.hstack(
(obs.angle, obs.track, obs.trackPos, obs.speedX, obs.speedY,
obs.speedZ, obs.wheelSpinVel / 100.0, obs.rpm))
class TorcsDockerEnv(object):
'''A torcs docker environment
based on gym_torcs, here we only consider vision with throttle as
input
'''
def __init__(
self, docker_client, name="torcs", port=3101, vncport=None,
torcsdocker_id='bn2302/torcs', track_name='', training=False):
self.terminal_judge_start = 100
self.termination_limit_progress = 5
self.default_speed = 50
self.initial_reset = True
self.name = name
self.docker_client = docker_client
self.port = port
self.vncport = vncport
self.torcsdocker_id = torcsdocker_id
self.container = self._start_docker()
self.track_name = track_name
self.training = training
self.container.exec_run("start_torcs.sh", detach=True)
self.action_space = spaces.Box(low=-1.0, high=1.0, shape=(2,))
high = np.array([1., np.inf, np.inf, np.inf, 1., np.inf, 1., np.inf,
255])
low = np.array([0., -np.inf, -np.inf, -np.inf,
0., -np.inf, 0., -np.inf, 0])
self.observation_space = spaces.Box(low=low, high=high)
def _set_track(self):
if self.track_name is '':
if self.training:
t_name = random.choice(
['g-track-1', 'g-track-2', 'ruudskogen', 'forza',
'ole-road-1', 'street-1'])
else:
t_name = random.choice(
['g-track-3', 'e-track-6', 'alpine-2'])
else:
t_name = self.track_name
self.container.exec_run(
'set_track.py -t {}'.format(t_name), detach=True)
def _start_docker(self):
os.system(
'nvidia-docker run' +
' --rm' +
' -it' +
' --volume="/tmp/.X11-unix/X0:/tmp/.X11-unix/X0:rw"' +
' --volume="/usr/lib/x86_64-linux-gnu/libXv.so.1:/usr/lib/x86_64-linux-gnu/libXv.so.1:rw"' +
' -p {:d}:3101/udp'.format(self.port) +
' --name={}'.format(self.name) +
' -d {}'.format(self.torcsdocker_id))
return self.docker_client.containers.get(self.name)
def reset(self, relaunch=False):
self.time_step = 0
if not self.initial_reset:
self.client.R.d['meta'] = True
self.client.respond_to_server()
if relaunch is True:
self._set_track()
self.container.exec_run("kill_torcs.sh", detach=True)
self.container.exec_run("start_torcs.sh", detach=True)
self.client = snakeoil3.Client(p=self.port)
self.client.MAX_STEPS = np.inf
self.client.get_servers_input()
obs = self.client.S.d
self.observation = self._make_observaton(obs)
self.last_u = None
self.initial_reset = False
return self.get_obs()
def end(self):
self.container.stop()
def step(self, u):
# Apply Action
this_action = self.agent_to_torcs(u)
action_torcs = self.client.R.d
action_torcs['steer'] = this_action['steer']
action_torcs['accel'] = this_action['accel']
action_torcs['brake'] = this_action['brake']
action_torcs['gear'] = 1
if self.client.S.d['speedX'] > 50:
action_torcs['gear'] = 2
if self.client.S.d['speedX'] > 80:
action_torcs['gear'] = 3
if self.client.S.d['speedX'] > 110:
action_torcs['gear'] = 4
if self.client.S.d['speedX'] > 140:
action_torcs['gear'] = 5
if self.client.S.d['speedX'] > 170:
action_torcs['gear'] = 6
# Save the privious full-obs from torcs for the reward calculation
damage_pre = self.client.S.d["damage"]
# One-Step Dynamics Update
# Apply the Agent's action into torcs
self.client.respond_to_server()
self.client.get_servers_input()
# Get the current full-observation from torcs
obs = self.client.S.d
self.observation = self._make_observaton(obs)
# Reward setting Here
# direction-dependent positive reward
progress = (
np.array(obs['speedX']) *
(np.cos(obs['angle']) - np.sin(obs['angle'])))
reward = progress
# collision detection
if obs['damage'] - damage_pre > 0:
reward = -1
# Episode is terminated if the agent runs backward
if np.cos(obs['angle']) < 0:
self.client.R.d['meta'] = True
if self.client.R.d['meta'] is True:
self.client.respond_to_server()
self.time_step += 1
return self.get_obs(), reward, self.client.R.d['meta'], {}
def get_obs(self):
return self.observation
def agent_to_torcs(self, u):
accel = 0
brake = 0
if u[1] >= 0:
accel = u[1]
else:
brake = u[1]
torcs_action = {'steer': u[0], 'accel': accel, 'brake': brake}
return torcs_action
def _make_observaton(self, raw_obs):
names = ['focus',
'speedX', 'speedY', 'speedZ',
'angle',
'damage',
'opponents',
'rpm',
'track',
'trackPos',
'wheelSpinVel',
'img']
Observation = col.namedtuple('Observation', names)
image_rgb = self._obs_vision_to_image_rgb(raw_obs['img'])
return Observation(
focus=np.array(raw_obs['focus'], dtype=np.float32) / 200.,
speedX=np.array(raw_obs['speedX'], dtype=np.float32)
/ self.default_speed,
speedY=np.array(raw_obs['speedY'], dtype=np.float32)
/ self.default_speed,
speedZ=np.array(raw_obs['speedZ'], dtype=np.float32)
/ self.default_speed,
angle=np.array(raw_obs['angle'], dtype=np.float32) / 3.1416,
damage=np.array(raw_obs['damage'], dtype=np.float32),
opponents=np.array(raw_obs['opponents'], dtype=np.float32) / 200.,
rpm=np.array(raw_obs['rpm'], dtype=np.float32),
track=np.array(raw_obs['track'], dtype=np.float32) / 200.,
trackPos=np.array(raw_obs['trackPos'], dtype=np.float32) / 1.,
wheelSpinVel=np.array(raw_obs['wheelSpinVel'], dtype=np.float32),
img=image_rgb)
def _obs_vision_to_image_rgb(self, obs_image_vec):
image_vec = obs_image_vec
r = image_vec[0:len(image_vec):3]
g = image_vec[1:len(image_vec):3]
b = image_vec[2:len(image_vec):3]
sz = (64, 64)
r = np.array(r).reshape(sz)
g = np.array(g).reshape(sz)
b = np.array(b).reshape(sz)
return np.array([r, g, b], dtype=np.uint8)
if __name__ == '__main__':
import docker
docker_client = docker.from_env()
# Generate a Torcs environment
env = TorcsDockerEnv(docker_client)
env.reset(True)
env.end()