-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrafficLight.py
91 lines (71 loc) · 2.4 KB
/
trafficLight.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
import traci
class TrafficLight:
def __init__(self, inLanes, outLane, inEdge, outEdge, environment):
# 巴拉巴拉
self.id = tsl_id
self.env = environment # 当前所处环境
self.min_green = min_green
self.max_green = max_green
self.green_phase = 0 # 绿灯相位
self.duration = 0 # 记录当前相位持续时间
self.phase = 0 # 记录当前相位
self.t = 0 # 记录当前时间
self.is_yellow = False
self.inLanes = []
self.outLanes = []
self.inEdges = []
self.outEdges = []
self.observation_space = []
self.action_space = [i for i in range(action_dim)]
def getPhase(self):
self.phase = traci.trafficlight.getPhase(self.id)
return self.phase
def update(self):
pass
def getState(self):
def sim_step(self):
'''
sumo执行 更新t
:return:
'''
traci.simulationStep() # 执行一步
self.t += 1
def changeAction(self, new_action):
while new_action == self.phase:
new_action = self.agent.select_action(self.state)
return new_action
def step(self, new_action, nsteps):
'''
:param new_action: 设置的相位
:param step: n需要执行的步数
:return:
'''
self.phase = new_action
traci.trafficlight.setPhase(self.id, self.phase)
for _ in nsteps:
self.sim_step()
def takeAction(self, new_action):
if self.phase == new_action:
# 不需要切换
# 判断是否超过最大时间了
if self.duration + self.green_phase > self.max_green:
# 重新随机选一个
new_action = self.changeAction(new_action)
# 先执行一个过渡黄灯
nsteps = 3
self.step(self.phase + 1, nsteps)
nsteps = 30
self.step(new_action*2, nsteps)
self.duration = nsteps
else:
# 直接执行
nsteps = 30
self.step(new_action*2, nsteps)
self.duration += nsteps
else:
# 需要切换
nsteps = 3
self.step(self.phase + 1, nsteps)
nsteps = 30
self.step(new_action*2, nsteps)
self.duration = nsteps