blocking_flag_ego = True
blocking_flag_neighbors = True
step = 1
while len(traci.vehicle.getIDList()) <= 0:
traci.simulationStep()
LC_MODE = 513 # collision avoidance, safety-gap enforcement AND strategic changes ONLY if not conflicting with TraCI (to get on the correct route)
while len(traci.vehicle.getIDList()) > 0:
veh_list = traci.vehicle.getIDList()
for vh in veh_list:
traci.vehicle.setLaneChangeMode(vh, LC_MODE)
relevant_vehicles = []
speed = traci.vehicle.getSpeed(vh)
right_followers = [x[0] for x in traci.vehicle.getRightFollowers(vehID=vh, blockingOnly=blocking_flag_ego)]
right_leaders = [x[0] for x in traci.vehicle.getRightLeaders(vehID=vh, blockingOnly=blocking_flag_ego)]
left_followers = [x[0] for x in traci.vehicle.getLeftFollowers(vehID=vh, blockingOnly=blocking_flag_ego)]
left_leaders = [x[0] for x in traci.vehicle.getLeftLeaders(vehID=vh, blockingOnly=blocking_flag_ego)]
relevant_vehicles.extend(right_followers)
relevant_vehicles.extend(right_leaders)
relevant_vehicles.extend(left_followers)
relevant_vehicles.extend(left_leaders)
print('Blocking vehicles around ego vehicle {} : {}'.format(vh, relevant_vehicles))
for rel_veh in relevant_vehicles:
blocking_vehicles = []
right_followers_blocking = [x[0] for x in traci.vehicle.getRightFollowers(vehID=vh, blockingOnly=blocking_flag_neighbors) if x[0] not in [rel_veh]]
right_leaders_blocking = [x[0] for x in traci.vehicle.getRightLeaders(vehID=vh, blockingOnly=blocking_flag_neighbors) if x[0] not in [rel_veh]]
left_followers_blocking = [x[0] for x in traci.vehicle.getLeftFollowers(vehID=vh, blockingOnly=blocking_flag_neighbors) if x[0] not in [rel_veh]]
left_leaders_blocking = [x[0] for x in traci.vehicle.getLeftLeaders(vehID=vh, blockingOnly=blocking_flag_neighbors) if x[0] not in [rel_veh]]
blocking_vehicles.extend(right_followers_blocking)
blocking_vehicles.extend(right_leaders_blocking)
blocking_vehicles.extend(left_followers_blocking)
blocking_vehicles.extend(left_leaders_blocking)
print('Blocking vehicles for ego-blocking vehicle {} : {}'.format(rel_veh, blocking_vehicles))
if vh in blocking_vehicles: # IF ego is one of the vehicles blocking it
print('Ego vehicle {} will cooperate'.format(vh))
input()
traci.simulationStep()
print('### STEP {} ###'.format(step))
step += 1
traci.close()