-
Notifications
You must be signed in to change notification settings - Fork 2
/
torcs_env.py
178 lines (160 loc) · 5.34 KB
/
torcs_env.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
"""
author: zj
file: torcs_env.py
time: 17-8-1
"""
import time
import copy
import scipy
from . import Tool
from .constant import *
import math
import os
import numpy as np
import subprocess
from itertools import count
from easydict import EasyDict as ed
from glob import glob
import random
# from keras.models import load_model
SPEED_X = -8
SPEED_Y = 1
SPEED_Z = 2
STEER = 3
BRAKE = 4
ACCEL = 5
# GEAR = 6
# CLUTCH = 7
TRACK_ANGLE = 0
TRACK_POS = 20
RPM = -1
RADIUS = 9
def clip(lo, x, hi):
"""
:param lo:
:param x:
:param hi:
:return: x -> [lo , hi]
"""
return lo if x <= lo else hi if x >= hi else x
class TorcsEnv:
terminal_judge_start = 100 # If after 100 timestep still no progress, terminated
termination_limit_progress = 5 #
def __init__(self,torcs_path = '/home/bst2017/zj/software/bin',grab_img = True,memory_key = None):
assert memory_key, 'you must specific shared memory key'
#torcs_path = '/home/bst2017/zj/software/bin'
rel = '/home/bst2017/zj/software/torcs-1.3.7/data'
self.track_category_name = track_category_name
self.all_track_name = all_track_names
os.system('ipcrm -M {}'.format(memory_key))
command = '{}/torcs --key {}'.format(torcs_path, memory_key)
self.torcs_proc = subprocess.Popen([command], shell=True, preexec_fn=os.setsid)
time.sleep(1)
self.tool = Tool.torcs_tool(grab_shot=grab_img ,key=memory_key)
time.sleep(0.5)
os.system('sh /home/bst2017/zj/projects/back_up_ddpg/autostart.sh')
time.sleep(0.5)
self.time_step = 0
self.begin_protection = 0
self.norm_factory = self.norm()
def norm(self):
norm = ed()
norm.focus = 200.
norm.speedX = 300.0
norm.speedY = 300.0
norm.speedZ = 300.0
norm.angle = 3.1416
norm.damage = None
norm.opponents = 200.
norm.rpm = 10000.
norm.track = 200.
norm.trackPos = None
norm.wheelSpinVel = 100.
norm.radius = None
norm.toleft = None
norm.toright = None
return norm
def auto_shift(self):
if 0 <= self.tool.speed < 40:
self.tool.gear = 1
elif 40 < self.tool.speed <= 80:
self.tool.gear = 2
elif 80 < self.tool.speed <= 120:
self.tool.gear = 3
elif 120 < self.tool.speed <= 160:
self.tool.gear = 4
elif 160 < self.tool.speed <= 200:
self.tool.gear = 5
elif 200 < self.tool.speed:
self.tool.gear = 6
def make_obs(self,obs):
for k,v in self.norm_factory.items():
setattr(obs,k,obs[k] / v if v else obs[k])
# INFO
# uncomment to scale angle to (0,1)
#obs.angle = obs.angle / 2. + 0.5
return obs
def change_track(self,index = None,category = None):
if index:
track = self.all_track_name[index % len(self.all_track_name)]
else:
track = random.choice(self.all_track_name)
track_name = all_log_track_names[self.all_track_name.index(track)]
self.tool.changeTrack(track.encode('utf-8'))
self.tool.changeTrackOk('1'.encode('utf-8'))
return track_name
#self.tool.changeTrack('tracks/dirt/dirt-1/dirt-1.xml'.encode('utf-8'))
#def change_track_ok(self):
# self.tool.changeTrackOk('1'.encode('utf-8'))
def make_obs_origin(self):
obs = ed()
t = self.tool.get29Data
for k,v in self.norm_factory.items():
setattr(obs,k,t[k])
return obs
def restart(self):
#self.change_track()
self.tool.changeTrackOk('0'.encode('utf-8'))
self.tool.restart()
self.time_step = 0
return self.make_obs_origin()
def reset(self):
origin = self.restart()
return self.make_obs(origin)
def step(self,actions):
self.tool.steer, self.tool.accel ,self.tool.brake = actions
self.auto_shift()
is_stuck = self.tool.is_stuck
is_finish = self.tool.is_finish
self.time_step += 1
obs = self.make_obs_origin()
obs_pre = copy.deepcopy(obs)
done = False
track = obs['track']
trackPos = obs['trackPos']
sp = obs['speedX']
damage = obs['damage']
rpm = obs['rpm']
progress = sp * np.cos(obs['angle']) - np.abs(sp * np.sin(obs['angle'])) - sp * np.abs(obs['trackPos'])
reward = progress
# collision detection
if obs['damage'] - obs_pre['damage'] > 0:
reward = -1
if (abs(track.any()) > 1 or abs(trackPos) > 1): # Episode is terminated if the car is out of track
reward = -200
done = True
if self.terminal_judge_start < self.time_step: # Episode terminates if the progress of agent is small
if progress < self.termination_limit_progress:
print("No progress")
done = True
if np.cos(obs['angle']) < 0: # Episode is terminated if the agent runs backward
done = True
#done = (is_stuck or is_finish) and self.begin_protection > 10
self.begin_protection += 1
if is_finish:
done = True
#if self.begin_protection < 100:
# done = False
return self.make_obs(obs), reward, done, {}
if __name__ == '__main__':
env = TorcsEnv(None,memory_key=1234)