Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run():
"""execute the TraCI control loop"""
step = 0
# we start with phase 2 where EW has green
traci.trafficlight.setPhase("0", 2)
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
if traci.trafficlight.getPhase("0") == 2:
# we are not already switching
if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
# there is a vehicle from the north, switch
traci.trafficlight.setPhase("0", 3)
else:
# otherwise try to keep green for EW
traci.trafficlight.setPhase("0", 2)
step += 1
traci.close()
sys.stdout.flush()
#print(States_.get_tails())
state = np.zeros((5,1))
# state[i,0] = readScreen2.getUpperQlength()
# state[i,1] = readScreen2.getLowerQlength()
# state[i,2] = readScreen2.getRightQlength()
# state[i,3] = readScreen2.getLeftQlength()
# phase = traci.trafficlight.getPhase("0")
# state[i,4] = phase
state[0, 0] = readScreen2.getUpperQlength()
state[1,0] = readScreen2.getLowerQlength()
state[2,0] = readScreen2.getRightQlength()
state[3,0] = readScreen2.getLeftQlength()
phase = traci.trafficlight.getPhase("0")
state[4,0] = phase
#print (state)
return state
def getUpperLeftPhaseState(transition_time):
num_lanes = 4
num_phases = 4
phase = traci.trafficlight.getPhase("01")
phaseState = np.zeros((transition_time, num_lanes, num_phases))
for i in range(transition_time):
for j in range(num_lanes):
phaseState[i][j][phase] = 1
return phaseState
def getPhaseState(transition_time):
phase_left = traci.trafficlight.getPhase("0")#left and right do
phase_right = traci.trafficlight.getPhase("10")
#calculate floor number
phase = get_floor_number(phase_left,phase_right)
phaseState = np.zeros((transition_time,num_lanes,num_phases))
for i in range(transition_time):
for j in range(num_lanes):
phaseState[i][j][phase] = 1
return phaseState
def getState(): # made the order changes
state = [readscreen3.getLowerQlength() / 80,
readscreen3.getRightQlength() / 80,
readscreen3.getUpperQlength() / 80,
readscreen3.getLeftQlength() / 80,
traci.trafficlight.getPhase("0")]
# print (state)
return state
def getRightPhaseState(transition_time):
num_lanes = 4
num_phases = 4
phase = traci.trafficlight.getPhase("10")
phaseState = np.zeros((transition_time, num_lanes, num_phases))
for i in range(transition_time):
for j in range(num_lanes):
phaseState[i][j][phase] = 1
return phaseState
def run(self):
import traci
"""execute the TraCI control loop"""
step = 0
phase = 0
states = [0,1,2,3,4,5,6,7]
traci.trafficlight.setPhase("0", 0)
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
action = random.choice(states)
traci.trafficlight.setPhase("0", action)
phase = traci.trafficlight.getPhase("0")
#print(phase)
# timeout = 0.2
# print("Time right now - ",step)
# rlist, wlist, xlist = select([sys.stdin],[],[],timeout)
# if rlist:
# print("Key pressed - ")
# print(rlist)
# traci.vehicle.addFull(vehID='left_'+str(step),routeID='r0',typeID='car',depart='triggered',departLane='random',departPos='random')
# termios.tcflush(sys.stdin,termios.TCIFLUSH)
'''
key = fstdscr.getch()
stdscr.addch(20,25,key)
stdscr.refresh()
if key == curses.KEY_RIGHT:
def makeMoves(leftAction, rightAction, transition_time):
if leftAction == 1:
traci.trafficlight.setPhase("0", (int(traci.trafficlight.getPhase("0")) + 1) % 4)
if rightAction == 1:
traci.trafficlight.setPhase("10", (int(traci.trafficlight.getPhase("10")) + 1) % 4)
return getStates(transition_time)