def run(): global current_hour """execute the TraCI control loop""" traci.init(PORT) src = [ Source("D2",100,1), Source("D2",100,2), Source("D2",100,3), Source("L16",100,51), Source("L14",50,25), Source("V4",0,30) ] dest = [ Destination("V4",150), Destination("V4",100), Destination("D8",5), Destination("V4",150), Destination("D1",10), Destination("D1",20) ] stops = [ ChargingStation(0,"D6",50,2,10,[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]) , ChargingStation(1,"V2",50,2,8,[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5]) ] types = ["CarA", "CarA", "CarD", "CarB", "CarC", "CarC"] #number of elechtric vehicle insterted veh_count = 0 vehicles = [] temp_vehs = [] for i in range(6): vehicles.append(Electric_Vehicle(veh_count,src[i],dest[i],types[i],1000,temp_vehs)) veh_count+=1 center = Center(stops,vehicles) for s in stops: center.seller_report(s) temp_toDel = False while vehicles != []: traci.simulationStep() if traci.simulation.getCurrentTime()/1000 % 1000 == 0 and traci.simulation.getCurrentTime()>0: current_hour += 1 print '[HOUR]', current_hour deleteTempVehicles(temp_vehs) for v in vehicles: if (v.updateState(dest) == 1 and not v.reported): center.buyer_report(v,stops,current_hour,temp_vehs) center.assign(v,stops) v.assinged = v.stopToCharge() v.reported = True if (v.toDel): vehicles.remove(v) if v.reported and not v.assinged: v.assinged = v.stopToCharge() traci.close() sys.stdout.flush()
def run(): """execute the TraCI control loop""" traci.init(PORT) step = 0 num_cars = -1 parkable_lanes = [] parked_cars = [] for lane_id in traci.lane.getIDList(): if PARKING_EDGE_TWO_LABEL in lane_id: parkable_lanes.append(lane_id) while traci.simulation.getMinExpectedNumber() > 0: num_cars += 1 traci.simulationStep() traci.vehicle.add("parking_{}".format(num_cars), "right") if len(parkable_lanes) >0: parking_target = "" parking_target = random.choice(parkable_lanes) print parking_target traci.vehicle.changeTarget("parking_{}".format(num_cars), "1i_parking_lane_2_2")#parking_target[:-2]) parkable_lanes.remove(parking_target) parked_cars.append("parking_{}".format(num_cars)) step += 1 traci.close() sys.stdout.flush()
def main(): sumoCmd = [sumoBinary, "-c", sumoConfig] traci.start(sumoCmd) # 开始仿真 simulationSteps = 0 Phaseflag = 0 while simulationSteps < 1000: # 仿真1000个步长 traci.simulationStep() # 执行一步仿真 # 获取仿真环境中当前仿真步的实时信息 print('红绿灯路口西侧排队车辆数目:', traci.lanearea.getJamLengthVehicle('W')) print('红绿灯路口西侧排队长度:', traci.lanearea.getJamLengthMeters('W')) print('红绿灯路口东侧排队车辆数目:', traci.lanearea.getJamLengthVehicle('E')) print('红绿灯路口东侧排队长度:', traci.lanearea.getJamLengthMeters('E')) print('红绿灯路口北侧排队车辆数目:', traci.lanearea.getJamLengthVehicle('N')) print('红绿灯路口北侧排队长度:', traci.lanearea.getJamLengthMeters('N')) print('红绿灯路口南侧排队车辆数目:', traci.lanearea.getJamLengthVehicle('S')) print('红绿灯路口南侧排队长度:', traci.lanearea.getJamLengthMeters('S')) # 红绿灯控制程序 # 所控红绿灯的相位顺序为:东西直行南北禁行(30秒),南北直行东西禁行(30秒) if simulationSteps % 30 == 0: if Phaseflag % 2 == 0: traci.trafficlight.setPhase('3', 0) # 改变红绿灯的相位为东西直行南北禁行 else: traci.trafficlight.setPhase('3', 1) # 改变红绿灯的相位为南北直行东西禁行 Phaseflag += 1 simulationSteps += 1 traci.close() # 结束仿真
def run(edges, connections, speedChanges, optimalization, nogui): '''execute the TraCI control loop''' traci.init(PORT) if not nogui: traci.gui.trackVehicle('View #0', '0') #print 'route: {0}'.format(traci.vehicle.getRoute('0')) destination = traci.vehicle.getRoute('0')[-1] edgesPoll = list(edges) time = traci.simulation.getCurrentTime() while traci.simulation.getMinExpectedNumber() > 0: if optimalization: change_edge_speed(edgesPoll, edges, speedChanges) edge = traci.vehicle.getRoadID('0') if edge in edges and traci.vehicle.getLanePosition('0') >= 0.9 * traci.lane.getLength(traci.vehicle.getLaneID('0')): shortestPath = graph.dijkstra(edge, destination, edges, get_costs(edges), connections) #print 'dijkstra: {0}'.format(shortestPath) traci.vehicle.setRoute('0', shortestPath) else: if len(speedChanges) > 0: edge, speed = speedChanges.pop() traci.edge.setMaxSpeed(edge, speed) else: change_edge_speed(edgesPoll, edges, []) traci.simulationStep() time = traci.simulation.getCurrentTime() - time traci.close() sys.stdout.flush() return time
def run(): traci.init(int(PORT)) step = 0 while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() step += 1 traci.close()
def run_ere(scenario_name, closed_roads, s_time, duration): """ This is to enable the enroute event scenario using TraCI :param scenario_name: the name of the scenario :param closed_roads: the list of closed road id :param s_time: the starting time stamp for the road closure in seconds :param duration: the road closure duration in seconds """ sumo_net = net.readNet(load_map(scenario_name)) pre_sp_lim = [] for i in closed_roads: pre_sp_lim.append(sumo_net.getEdge(i).getSpeed()) s_time += traci.simulation.getCurrentTime()/1000 e_time = s_time + duration while traci.simulation.getMinExpectedNumber() > 0: cur_step = traci.simulation.getCurrentTime()/1000 if cur_step == s_time: for i in closed_roads: traci.edge.setMaxSpeed(i, 0.1) if cur_step == e_time: for seq, rid in enumerate(closed_roads): traci.edge.setMaxSpeed(rid, pre_sp_lim[seq]) traci.simulationStep() traci.close() sys.stdout.flush()
def traciSimulation(port): ''' Execute the TraCI control loop on the SUMO simulation. :param port: The port used for communicating with your sumo instance :return: The end second time of the simulation ''' try: # Init the TraCI server traci.init(port) # TraCI runs the simulation step by step step = 0 while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() step += 1 except traci.TraCIException as ex: logger.fatal("Exception in simulation step %d: %s" % (step, ex.message)) return -1 except traci.FatalTraCIError as ex: logger.fatal("Fatal error in simulation step %d: %s" % (step, ex.message)) return -1 else: return step + 1 finally: # Close the TraCI server traci.close()
def run(): """execute the TraCI control loop""" traci.init(PORT) programPointer = len(PROGRAM) - 1 step = 0 while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() programPointer = min(programPointer + 1, len(PROGRAM) - 1) numPriorityVehicles = traci.inductionloop.getLastStepVehicleNumber("0") if numPriorityVehicles > 0: if programPointer == len(PROGRAM) - 1: # we are in the WEGREEN phase. start the priority phase # sequence programPointer = 0 elif PROGRAM[programPointer] != WEYELLOW: # horizontal traffic is already stopped. restart priority phase # sequence at green programPointer = 3 else: # we are in the WEYELLOW phase. continue sequence pass traci.trafficlights.setRedYellowGreenState( "0", PROGRAM[programPointer]) step += 1 traci.close() sys.stdout.flush()
def run(): """execute the TraCI control loop""" traci.init(PORT) # track the duration for which the green phase of the vehicles has been # active greenTimeSoFar = 0 # whether the pedestrian button has been pressed activeRequest = False # main loop. do something every simulation step until no more vehicles are # loaded or running while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() # decide wether there is a waiting pedestrian and switch if the green # phase for the vehicles exceeds its minimum duration if not activeRequest: activeRequest = checkWaitingPersons() if traci.trafficlights.getPhase(TLSID) == VEHICLE_GREEN_PHASE: greenTimeSoFar += 1 if greenTimeSoFar > MIN_GREEN_TIME: # check whether someone has pushed the button if activeRequest: # switch to the next phase traci.trafficlights.setPhase( TLSID, VEHICLE_GREEN_PHASE + 1) # reset state activeRequest = False greenTimeSoFar = 0 sys.stdout.flush() traci.close()
def beginEvaluate(self): """ Given the parameters during initialization, we run the simulator to get the fitness using port num to identify a connection """ traci.init(self.portNum, 10, "localhost", str(self.portNum)) #traverse all the traffic lights for i in xrange(len(self.trafficLightIdList)): #traverse all the traffic lights tlsLogicList = traci.trafficlights.getCompleteRedYellowGreenDefinition(self.trafficLightIdList[i]) #One traffic light has only one phase list now tlsLogicList = tlsLogicList[0] #each traffic light has several phases phaseList = [] #traverse all the phase for j in xrange(len(tlsLogicList._phases)): # print self.individual.genes[i].times[j] phaseList.append(traci.trafficlights.Phase(self.individual.genes[i].times[j], self.individual.genes[i].times[j], self.individual.genes[i].times[j], tlsLogicList._phases[j]._phaseDef)) tlsLogicList._phases = phaseList traci.trafficlights.setCompleteRedYellowGreenDefinition(self.trafficLightIdList[i], tlsLogicList) totalNumPassed = 0 for _ in xrange(600): traci.simulationStep() totalNumPassed = totalNumPassed + traci.simulation.getArrivedNumber() traci.close() self.fitness = totalNumPassed return totalNumPassed
def runSimulationStep(mtraci): """ Runs one SUMO simulation step """ mtraci.acquire() traci.simulationStep() mtraci.release()
def main(args): sumoBinary = sumolib.checkBinary('sumo') sumo_call = [sumoBinary, "-c", "data/hello.sumocfg", "--remote-port", str(PORT_TRACI), "--netstate-dump", "rawdump.xml", "--no-step-log"] sumoProcess = subprocess.Popen( sumo_call, stdout=sys.stdout, stderr=sys.stderr) traci.init(PORT_TRACI) for step in range(161): traci.simulationStep() if step == 120: print(traci.vehicle.getDistance('Stapler_00')) traci.vehicle.setRoute('Stapler_00', ('ed1', 'ed5')) print(traci.vehicle.getRoute('Stapler_00')) assert(traci.vehicle.getRoute('Stapler_00') == ['ed0', 'ed1', 'ed5']) print(traci.vehicle.getDistance('Stapler_00')) if step == 122: assert(traci.vehicle.getRoute('Stapler_00') == ['ed0', 'ed1', 'ed5']) print(traci.vehicle.getDistance('Stapler_00')) traci.vehicle.setRouteID('Stapler_00', "short") print(traci.vehicle.getRoute('Stapler_00')) print(traci.vehicle.getDistance('Stapler_00')) traci.close() sumoProcess.wait()
def run(): create_simulation_scenario() if mode == "train": for i in range(200): if random.uniform(0, 1) > 0.5: accident_cars.add("veh" + str(i)) client_socket = socket.socket() client_socket.connect(('127.0.0.1', 9999)) traci.init(PORT) step = 0 client_socket.send(scenario + "," + mode + "," + time + "\n") message = client_socket.recv(1024).splitlines()[0] print message cars_in_perimeter = set() while traci.simulation.getMinExpectedNumber() > 0: manage_car_set(cars_in_perimeter) send_data_to_rsu(client_socket, cars_in_perimeter) traci.simulationStep() step += 1 if mode == "run": sleep(0.2) traci.close() client_socket.close()
def run(run_time): ## execute the TraCI control loop traci.init(PORT) programPointer = 0 # initiates at start # len(PROGRAM) - 1 # initiates at end step = 0 flow_count = 0 first_car = True prev_veh_id = ' ' pointer_offset = 0 car_speeds = [] while traci.simulation.getMinExpectedNumber() > 0 and step <= run_time*(1/step_length): traci.simulationStep() # advance a simulation step programPointer = int(math.floor(step/(int(1/step_length))))%len(PROGRAM) - pointer_offset sensor_data = traci.inductionloop.getVehicleData("sensor") if len(sensor_data) != 0: flow_increment,prev_veh_id = flowCount([sensor_data],["sensor"],prev_veh_id) car_speeds.append(traci.vehicle.getSpeed(sensor_data[0][0])) flow_count += flow_increment if first_car: #if its the first car, record the time that it comes in first_time = sensor_data[0][2] print first_time first_car = False if step < 600: #24960, let queue accumulate traci.trafficlights.setRedYellowGreenState("0", ALLRED) else: traci.trafficlights.setRedYellowGreenState("0",PROGRAM[programPointer]) step += 1 #print str(step) print "\n \n" print "-------------------------------------------------------- \n" print "Total number of cars that have passed: " + str(flow_count) tau = np.diff(leaving_times) print "Total throughput extrapolated to 1hr: " + str(flow_count*(3600/(run_time-first_time))) print "Average car speed: " + str(np.mean(car_speeds)) print "Max Theoretical throughput: " + str(3600/min(min(tau))) print "Min Theoretical throughput: " + str(3600/max(max(tau))) # print tau # print "Mean tau: " + str(np.mean(tau)) + "\n" # print "Var tau: " + str(np.var(tau)) + "\n" # print "Standard Dev tau: " + str(np.std(tau)) +"\n" traci.close() sys.stdout.flush() return [np.mean(tau),np.var(tau),np.std(tau)]
def _createVehicle(self,vehID, laneID, pos=0): routeID = _edge(laneID) laneIndex = int( laneID[laneID.rfind('_')+1:] ) traci.vehicle.add(vehID,routeID,pos=pos,lane=laneIndex) traci._sendIntCmd(traci.constants.CMD_SET_VEHICLE_VARIABLE, traci.constants.VAR_SPEEDSETMODE, vehID, 0) traci.vehicle.setLaneChangeMode(vehID, 0) traci.simulationStep()
def run(run_time): ## execute the TraCI control loop traci.init(PORT) programPointer = 0 # initiates at start # len(PROGRAM) - 1 # initiates at end step = 0 flow_count = 0 first_car = True prev_veh_id = ' ' leaving_times = [] car_speeds = [] while traci.simulation.getMinExpectedNumber() > 0 and step <= run_time*(1/step_length): traci.simulationStep() # advance a simulation step programPointer = (step*int(1/step_length))%len(PROGRAM) sensor_data = traci.inductionloop.getVehicleData("sensor") if len(sensor_data) != 0: if first_car: #if its the first car, record the time that it comes in first_time = sensor_data[0][2] print first_time first_car = False veh_id = sensor_data[0][0] if veh_id != prev_veh_id: #if the vehicle coming in has a different id than the previous vehicle, count it towards total flow flow_count += 1 car_speeds.append(traci.inductionloop.getLastStepMeanSpeed("sensor")) if sensor_data[0][3] != -1: #if the vehicle is leaving the sensor, record the time it left leaving_times.append(sensor_data[0][2]) prev_veh_id = veh_id traci.trafficlights.setRedYellowGreenState("13", PROGRAM[programPointer]) step += 1 #print str(step) print "\n \n" print "-------------------------------------------------------- \n" print "Total number of cars that have passed: " + str(flow_count) tau = np.diff(leaving_times) print "Total throughput extrapolated to 1hr: " + str(flow_count*(3600/(run_time-first_time))) print "Max Theoretical throughput: " + str(3600/tau_effective(max(car_speeds))) print "Min Theoretical throughput: " + str(3600/tau_effective(min(car_speeds))) print tau print "Mean tau: " + str(np.mean(tau)) + "\n" print "Var tau: " + str(np.var(tau)) + "\n" print "Standard Dev tau: " + str(np.std(tau)) +"\n" traci.close() sys.stdout.flush() return [np.mean(tau),np.var(tau),np.std(tau)]
def step(self): self.performActions() traci.simulationStep() if self.gui: traci.gui.screenshot("View #0", "../../images/" + str(self.current_step) + ".png") self.current_step+=1 self.makeObservations() self.computeRewards() self.storeLastActions()
def doStep(): state.step += 1 if setting.verbose: print("step",state.step) traci.simulationStep() # adding new vehicles, if any departed = traci.simulation.getSubscriptionResults()[tc.VAR_DEPARTED_VEHICLES_IDS] for v in departed: traci.vehicle.subscribe(v,dataFromTraciState) vehicleSetParams[v] = getSetParams(v) if v=='ego': thisv = egov else: thisv = alterv controllers[v] = Controllers.Inter1Constant(v, thisv) #getController(v, vehicleSetParams[v]) if controllers[v] is not None: if setting.verbose: print("allowing forward and lane crashes for",v) traci._sendIntCmd(tc.CMD_SET_VEHICLE_VARIABLE, tc.VAR_SPEEDSETMODE, v, controllers[v].speedMode) traci.vehicle.setLaneChangeMode(v, controllers[v].laneChangeMode) # gather vehicle states for this step vStates = {} for vehID, subs in traci.vehicle.getSubscriptionResults().iteritems(): tempParams = getFromSubscription(vehID, subs, dataFromTraciState) vStates[vehID] = VState( tempParams + vehicleSetParams[vehID] ) # for vehID, vState in vStates.iteritems(): sensor = sensorToUse(vState, realign=True) # for otherID, otherState in vStates.iteritems(): if vehID != otherID: # # check for collisions if collisionCheck.check(vState, otherState): #print "collision! pos",vState.x,vState.y,"step",state.step if state.collisionOccurred <= 0: state.collisionOccurred = state.step*.1 break # # update sensor sensor.addObstacle(otherState) # if setting.verbose: for vstat in sensor.getObstacles(): print(vehID, "detects", vstat.x, vstat.y,"speed",vstat.speed) # store this vehicle movement output.add([[state.step*.1, vehID, vState.x, vState.y, vState.angle, vState.speed]]) # use data to decide speed if controllers[vehID] is not None: controllers[vehID].updateSpeed(vState.speed) commandedSpeed = controllers[vehID].nextStep(sensor.getObstacles()) traci.vehicle.setSpeed(vehID, commandedSpeed) if setting.verbose: print("setting speed", commandedSpeed)
def run(): """execute the TraCI control loop""" step = 0 while traci.simulation.getMinExpectedNumber() > 0: step += 1 traci.simulationStep() traci.close() sys.stdout.flush()
def runSingle(traciEndTime): step = 0 sumoProcess = subprocess.Popen("%s -c used.sumocfg %s" % (sumoBinary, addOption), shell=True, stdout=sys.stdout) traci.init(PORT) while not step>traciEndTime: traci.simulationStep() step += 1 print "Print ended at step %s" % (traci.simulation.getCurrentTime() / DELTA_T) traci.close() sys.stdout.flush()
def run(): """execute the TraCI control loop""" traci.init(PORT) step = 0 while traci.simulation.getMinExpectedNumber() > 0 and step < 100: traci.simulationStep() step += 1 if step == 4: traci.trafficlights.setProgram("center", "0") traci.close() sys.stdout.flush()
def step(self): # print "step", self.simtime,self.simtime_end,self.simtime >= self.simtime_end traci.simulationStep() self.process_step() self.get_logger().w(100.0*self.simtime/self.duration, key='progress') self.simtime += self.time_step if self.simtime >= self.simtime_end: print ' end of simulation reached, start closing traci', self.simtime traci.close() print ' traci closed' self.get_attrsman().status.set('success') return True
def __inner_run__(self, output_file): if self._options.gui: sumoBinary = checkBinary('sumo-gui') self.__sumoProcess = subprocess.Popen( [sumoBinary,"-W", "-n", self.conf.network_file, "-r", self.conf.route_file, "--tripinfo-output", output_file, "--remote-port", str(self.conf.port), "--gui-settings-file", self.conf.gui_setting_file, "--step-length", "1", "-v", "true", "--time-to-teleport", "-1"], stdout = sys.stdout, stderr=sys.stderr) time.sleep(20) else: sumoBinary = checkBinary('sumo') self.__sumoProcess = subprocess.Popen( [sumoBinary, "-W", "-n", self.conf.network_file, "-r", self.conf.route_file, "--tripinfo-output", output_file, "--remote-port", str(self.conf.port), "--step-length", "1", "-v", "true", "--time-to-teleport", "-1"], stdout = sys.stdout, stderr=sys.stderr) time.sleep(20) traci.init(self.conf.port) self.initIteration() while True: traci.simulationStep() if traci.simulation.getMinExpectedNumber() <= 0: break self.stepProcess() if traci.simulation.getCurrentTime() % self.conf.short_term_sec == 0: self.can_change_lane_list = [] self.want_chage_vehicle_list = [] self.endIteration() traci.close() if self._options.gui: os.system('pkill sumo-gui') sys.stdout.flush()
def sim(): step = 0 while step < 86400: traci.simulationStep() xy=[] vehicles = traci.vehicle.getIDList() for v in vehicles: x=112.96771+(traci.vehicle.getPosition(v)[0]+1509.33)/98838.1145 y=28.193671+(traci.vehicle.getPosition(v)[1]-105.97)/106287.597 xy.append([x,y]) return xy time.sleep(10) step += 10 traci.close()
def runSingle(traciEndTime, sumoEndTime=None): step = 0 opt = addOption if sumoEndTime is not None: opt += (" --end %s" % sumoEndTime) sumoProcess = subprocess.Popen( "%s -c sumo.sumocfg %s" % (sumoBinary, opt), shell=True, stdout=sys.stdout) traci.init(PORT) while not step > traciEndTime: traci.simulationStep() step += 1 print("Print ended at step %s" % (traci.simulation.getCurrentTime() / DELTA_T)) traci.close() sys.stdout.flush()
def main(args): init_traci() step = -1 while True: step += 1 traci.simulationStep() if step == 140: traci.vehicle.resume('Stapler_00') if step == 160: traci.close() break
def check(vehID, steps=1): for i in range(steps): if i > 0: traci.simulationStep() try: print("%s vehicle %s on lane=%s pos=%s speed=%s" % ( traci.simulation.getCurrentTime() / 1000.0, vehID, traci.vehicle.getLaneID(vehID), traci.vehicle.getLanePosition(vehID), traci.vehicle.getSpeed(vehID))) except traci.TraCIException: pass if steps > 1: print()
def run(): """execute the TraCI control loop""" traci.init(PORT) programPointer = len(PROGRAM)-1 step = 0 while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() programPointer = min(programPointer+1, len(PROGRAM)-1) no = traci.inductionloop.getLastStepVehicleNumber("0") if no > 0: programPointer = (0 if programPointer == len(PROGRAM)-1 else 3) traci.trafficlights.setRedYellowGreenState("0", PROGRAM[programPointer]) step += 1 traci.close() sys.stdout.flush()
def run(max_step=-1): """execute the TraCI control loop""" traci.init(PORT) step = 0 # we start with phase 2 where EW has green #traci.trafficlights.setPhase("0", 0) #pdb.set_trace() while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() step += 1 if step > max_step and max_step > 0: break traci.close() sys.stdout.flush()
def doStep(): step.step += 1 if setting.verbose: print "step", step.step traci.simulationStep() # adding new vehicles, if any departed = traci.simulation.getSubscriptionResults()[tc.VAR_DEPARTED_VEHICLES_IDS] for v in departed: traci.vehicle.subscribe(v,dataFromTraciState) vehicleSetParams[v] = getSetParams(v) controllers[v] = getController(v, vehicleSetParams[v]) if controllers[v] is not None: #True if setting.verbose: print "allowing forward and lane crashes for",v traci._sendIntCmd(tc.CMD_SET_VEHICLE_VARIABLE, tc.VAR_SPEEDSETMODE, v, 22) traci.vehicle.setLaneChangeMode(v, 85) #traci.vehicle.changeLane(v, 0, 10000) # gather vehicle states for this step vStates = {} for vehID, subs in traci.vehicle.getSubscriptionResults().iteritems(): tempParams = getFromSubscription(vehID, subs, dataFromTraciState) vStates[vehID] = VState( tempParams + vehicleSetParams[vehID] ) # for vehID, vState in vStates.iteritems(): sensor = sensorToUse(vState) # for otherID, otherState in vStates.iteritems(): if vehID != otherID: # # check for collisions if collisionCheck.check(vState, otherState): print "collision! pos",vState.x,vState.y,"step",step.step setting.collisionOccurred = True break # # update sensor sensor.addObstacle(otherState) # if setting.verbose: for vstat in sensor.getObstacles(): print vehID, "detects", vstat.x, vstat.y,"speed",vstat.speed # use data to decide speed if controllers[vehID] is not None: controllers[vehID].updateSpeed(vState.speed) commandedSpeed = controllers[vehID].nextStep(sensor.getObstacles()) traci.vehicle.setSpeed(vehID, commandedSpeed) if setting.verbose: print "setting speed", commandedSpeed
try: print("%s vehicle %s on lane=%s pos=%s speed=%s" % ( traci.simulation.getCurrentTime() / 1000.0, vehID, traci.vehicle.getLaneID(vehID), traci.vehicle.getLanePosition(vehID), traci.vehicle.getSpeed(vehID))) except traci.TraCIException: pass if steps > 1: print() vehID = "v0" traci.init(PORT) traci.simulationStep() check(vehID) try: print("%s setStop for %s" % (traci.simulation.getCurrentTime() / 1000.0, vehID)) traci.vehicle.setStop(vehID, "beg", pos=1.0, laneIndex=0, duration=5000) except traci.TraCIException: pass check(vehID, 10) traci.simulationStep(21 * 1000) vehID = "v1" check(vehID) try: print("%s setStop for %s" % (traci.simulation.getCurrentTime() / 1000.0, vehID))
def run(): print("RUN") # All simulation step = 0 # Emissions NOx_total = 0 NOx_control_zone = 0 NOx_per_vehicle = [] veh_total_number = 0 total_kilometers = 0 # In all simulation vehicles_in_simulation = [] # Vehicles in each step km_per_vehicle = [] # all km per vehicle # Window # Each window 50 steps # Emissions in each window NOx_total_window = 0 NOx_control_zone_window = 0 # Variables in each window veh_total_number_window = 0 vehicles_in_window = set() # All windows windows = [] ## Case 1 # Threshold: threshold = 14 # NOx_total in main_case_window = 28 => 28/2 = 14 control_area_edges = [ "gneE19_0", "-gneE19_0", "gneE21_0", "-gneE21_0", "gneE16_0", "-gneE16_0", "gneE17_0", "-gneE17_0", "gneE18_0", "-gneE18_0", "gneE22_0", "-gneE22_0", "gneE20_0", "-gneE20_0", "gneE15_0", "-gneE15_0", "gneE24_0", "-gneE24_0", "gneE25_0", "-gneE25_0", "gneE14_0", "-gneE14_0", "gneE23_0", "-gneE23_0" ] restrictionMode = False NOx_control_zone_restriction_mode = 0 # Vehicles to control area vehs_load = traci.simulation.getLoadedIDList() for veh_load in vehs_load: if (veh_load != "simulation.findRoute"): traci.vehicle.setParameter(veh_load, "has.rerouting.device", "true") vClass_last = traci.vehicle.getVehicleClass(veh_load) edges_last = traci.vehicle.getRoute(veh_load) string_edge = edges_last[len(edges_last) - 1] + "_0" if (string_edge in control_area_edges): # Destination in control area traci.vehicle.setType(vehID=veh_load, typeID="authority") if (vClass_last == "evehicle"): traci.vehicle.setEmissionClass(veh_load, "zero") while traci.simulation.getMinExpectedNumber( ) > 0: # While there are cars (and waiting cars) # LAST STEP # Vehicles to control area vehs_load = traci.simulation.getLoadedIDList() for veh_load in vehs_load: if (veh_load != "simulation.findRoute"): traci.vehicle.setParameter(veh_load, "has.rerouting.device", "true") vClass_last = traci.vehicle.getVehicleClass(veh_load) edges_last = traci.vehicle.getRoute(veh_load) string_edge = edges_last[len(edges_last) - 1] + "_0" if (string_edge in control_area_edges): # Destination in control area traci.vehicle.setType(vehID=veh_load, typeID="authority") if (vClass_last == "evehicle"): traci.vehicle.setEmissionClass(veh_load, "zero") # NEW STEP traci.simulationStep() # Advance one time step: one second step += 1 # Window if step != 0 and ( (step % 50) == 0) and vehicles_in_window != []: # Each window 50 steps for w in range(len(windows)): # Discount NOx of the last window if windows[w][0] == step - 50: print(NOx_control_zone_restriction_mode, windows[w][4]) NOx_control_zone_restriction_mode -= windows[w][4] if (NOx_control_zone_restriction_mode < 0): NOx_control_zone_restriction_mode = 0 print(NOx_control_zone_restriction_mode) #print(step) # Add variables for the last 50 steps windows.append([ step, vehicles_in_window, veh_total_number_window, NOx_total_window, NOx_control_zone_window ]) #windows.append([step, vehicles_in_window, veh_total_number_window, NOx_total_window, NOx_control_zone_restriction_mode]) print(windows) # Reboot all NOx_total_window = 0 NOx_control_zone_window = 0 veh_total_number_window = 0 vehicles_in_window = set() # MANAGE VEHICLES - All simulation id_vehicles_departed = traci.simulation.getDepartedIDList( ) # Vehicles in simulation id_list_vehicles_departed = list(id_vehicles_departed) if (id_list_vehicles_departed): # if the list is not empty # All simulation: vehicles_in_simulation.extend( id_list_vehicles_departed ) # Add vehicles to the simulation list km_per_vehicle.extend( id_list_vehicles_departed) # Add vehicles to the km list NOx_per_vehicle.extend( id_list_vehicles_departed) # Add vehicles to the NOx list # Initialize NOx values for each vehicle: for lVeh in range(len(NOx_per_vehicle)): vehi = NOx_per_vehicle[lVeh] # Select vehicle #print(len(NOx_per_vehicle[lVeh])) if (len(NOx_per_vehicle[lVeh]) != 2): # If not initialized NOx_per_vehicle[lVeh] = [vehi, 0] # Initialized #print(NOx_per_vehicle) # Per window: for veh in vehicles_in_simulation: vehicles_in_window.add( veh) # Add the vehicles in the window list veh_total_number_window = len( vehicles_in_window ) # Add vehicles to the vehicle window counter # Taking into account the vehicles that reach their destination: id_vehicles_arrived = traci.simulation.getArrivedIDList() for veh in id_vehicles_arrived: if veh in vehicles_in_simulation: # If the vehicle has arrived then remove it from the simulation list vehicles_in_simulation.remove(veh) if veh in vehicles_in_window: vehicles_in_window.remove(veh) veh_total_number += 1 # Update Vehicle Total Number in all simulation for veh in vehicles_in_simulation: # For each vehicle # Emissions: # All simulation vehNOxEmission = traci.vehicle.getNOxEmission( veh) # Return the NOx value per vehicle in each step NOx_total += vehNOxEmission for iNVeh in range(len(NOx_per_vehicle)): if veh in NOx_per_vehicle[iNVeh]: NOx_per_vehicle[iNVeh][1] = float( NOx_per_vehicle[iNVeh][1] ) + vehNOxEmission # Update the NOx value per vehicle # Per Window NOx_total_window += vehNOxEmission # Control Area: pos = traci.vehicle.getPosition(vehID=veh) # (x,y) #print(pos, vehNOxEmission, veh) if (pos[1] <= 296 and pos[1] >= 3) and ( pos[0] >= 3 and pos[0] <= 296 ): # x=> 0, y=>1. If the vehicle is in the control area # All simulation: NOx_control_zone += vehNOxEmission #print("SI ", CO2_zona_control) # Per window: NOx_control_zone_window += vehNOxEmission # Control are: NOx_control_zone_restriction_mode += vehNOxEmission # Route lenght per vehicle rouIndex = traci.vehicle.getRouteIndex(veh) edges = traci.vehicle.getRoute(veh) if rouIndex == (len(edges) - 1): # Only if is the last edge #print(edges, veh, rouIndex, edges[0],'-',edges[rouIndex]) for iVeh in range(len(km_per_vehicle)): #if veh in km_per_vehicle[iVeh]: if km_per_vehicle[ iVeh] == veh: # km_per_vehicle position selected for this vehicle stage = traci.simulation.findRoute( edges[0], edges[rouIndex]) rouLength = stage.length # Route Length km_per_vehicle[iVeh] = [veh, rouLength] #print(km_per_vehicle) #print(veh, NOx_control_zone, NOx_total, pos) # Control area - Threshold: string_current_edge = edges[rouIndex] + "_0" if restrictionMode == True and traci.vehicle.getVehicleClass( veh) != "authority": if (string_current_edge in control_area_edges ): # current edge in control area vClass_last2 = traci.vehicle.getVehicleClass(veh) traci.vehicle.setType(vehID=veh, typeID="authority") if (vClass_last2 != "passenger"): traci.vehicle.setEmissionClass(veh, "zero") if restrictionMode == True: inList = False for edg in edges: edgStrng = edg + "_0" if edgStrng in control_area_edges: inList = True if inList: traci.vehicle.rerouteTraveltime(veh, True) print(veh, traci.vehicle.getTypeID(veh)) #if auxSandra==0 and step != 0 and ((step % 50) == 0): if vehicles_in_simulation != [] and restrictionMode == False and NOx_control_zone_restriction_mode > threshold: print("CONTROL ZONE ON", NOx_control_zone_restriction_mode) #auxSandra = 1 restrictionMode = True print("B") for aEd in control_area_edges: traci.lane.setDisallowed( laneID=aEd, disallowedClasses=["passenger", "evehicle"]) print("disa ", traci.lane.getDisallowed(laneID=aEd)) traci.lane.setAllowed(laneID=aEd, allowedClasses=["authority"]) print("alo ", traci.lane.getAllowed(laneID=aEd)) """ traci.lane.setDisallowed(laneID="gneE22_0", disallowedClasses=["passenger", "evehicle"]) #traci.lane.setDisallowed(laneID="-gneE22_0", disallowedClasses=["passenger", "evehicle"])""" if (restrictionMode == True and NOx_control_zone_restriction_mode <= threshold): print("CONTROL ZONE OFF") restrictionMode = False for aEd in control_area_edges: traci.lane.setAllowed( laneID=aEd, allowedClasses=["authority", "passenger", "evehicle"]) for veh in vehicles_in_simulation: traci.vehicle.rerouteTraveltime(veh, True) print(step, "NOx_control_zone: ", NOx_control_zone, ". NOx_control_zone_restriction_mode: ", NOx_control_zone_restriction_mode, ". NOx_total: ", NOx_total) minutes = round(step / 60, 3) for i in range(len(km_per_vehicle)): total_kilometers += km_per_vehicle[i][1] # Results: print( "[step,vehicles_in_window, veh_total_number_window, NOx_total_window, NOx_control_zone] :" ) print(windows) print("NOx_per_vehicle: ", NOx_per_vehicle) print("NOx_total: ", NOx_total, ". NOx_control_zone", NOx_control_zone, ". NOx_control_zone_restriction_mode: ", NOx_control_zone_restriction_mode) print(veh_total_number, " vehicles - ", total_kilometers, " kilometers") print("In ", step, "seconds (", minutes, " minutes)") # TraCI traci.close() sys.stdout.flush()
def run(): """execute the TraCI control loop""" step = 0 count = 0 tempo = 0 """ Number of executions""" while step < 1000: traci.simulationStep() #Se passou um veiculo. Ou seja um Contador if traci.inductionloop.getLastStepVehicleNumber("e1Detector_Faixa_7_0_0") > 0: count += 1 print ('Numero de Veiculos', count) x = traci.trafficlight.getNextSwitch("Inter") # print (x) phase = traci.trafficlight.getPhaseDuration("Inter") print ('Duracao do Semaforo',phase) #Logica do semaforo #logic = traci.trafficlight.getCompleteRedYellowGreenDefinition('Inter') #print ('Logica do semaforo',logic) """[Logic(programID='0', type=0, currentPhaseIndex=2, phases=[Phase(duration=42.0, state='GGGGrrrrGGGGgrrrr', minDur=42.0, maxDur=42.0, next=-1), Phase(duration=3.0, state='yyyyrrrryyyyyrrrr', minDur=3.0, maxDur=3.0, next=-1), Phase(duration=42.0, state='rrrrGGGGrrrrrGGGG', minDur=42.0, maxDur=42.0, next=-1), Phase(duration=3.0, state='rrrryyyyrrrrryyyy', minDur=3.0, maxDur=3.0, next=-1)], subParameter={})]""" #Variavel Norte/Sul que soma o número de veículos nesse fluxo a cada timestep nor_sul=traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_7_0_0") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_7_1_1") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_7_2_2") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_3_0_0") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_3_1_1") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_3_2_2") #Variavel Leste/Oeste que soma o número de veículos nesse fluxo a cada timestep les_oes=traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_5_0_0") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_5_1_1") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_5_2_2") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_1_0_0") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_1_1_1") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_1_2_2") print ('Veiculos na via Norte/Sul: ',nor_sul) print ('Veiculos na via Leste/Oeste: ',les_oes) z = traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_5_0_0") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_5_1_1") + traci.lanearea.getLastStepVehicleNumber("e2Detector_Faixa_5_2_2") y = traci.lanearea.getJamLengthVehicle("e2Detector_Faixa_5_0_0") + traci.lanearea.getJamLengthVehicle("e2Detector_Faixa_5_1_1") + traci.lanearea.getJamLengthVehicle("e2Detector_Faixa_5_2_2") print ('Veiculos na Area', z) print ('Veiculos Parados', y) #teste logica simples de mudança de fase (0 - nor sul, 1 - amarelo nor sul, 2 - les oes, 3 - amarelo les oes) - necessárias condições para mudança de fase ainda. #if nor_sul>les_oes: # traci.trafficlight.setPhase('Inter',0) #if les_oes>nor_sul: # traci.trafficlight.setPhase('Inter',2) Dur_fase = (traci.trafficlight.getNextSwitch("Inter") - traci.simulation.getTime()) if Dur_fase == 0: print('Duracao da fase ainda: ', (traci.trafficlight.getNextSwitch("Inter") - traci.simulation.getTime())) print('Tempo Parado Faixa 5: ', (traci.lane.getWaitingTime('Faixa_5_0')+traci.lane.getWaitingTime('Faixa_5_1')+traci.lane.getWaitingTime('Faixa_5_2'))) tempo += traci.lane.getWaitingTime('Faixa_5_0')+traci.lane.getWaitingTime('Faixa_5_1')+traci.lane.getWaitingTime('Faixa_5_2')+ traci.lane.getWaitingTime('Faixa_1_0')+traci.lane.getWaitingTime('Faixa_1_1')+traci.lane.getWaitingTime('Faixa_1_2') + traci.lane.getWaitingTime('Faixa_3_0')+traci.lane.getWaitingTime('Faixa_3_1')+traci.lane.getWaitingTime('Faixa_3_2')+traci.lane.getWaitingTime('Faixa_7_0')+traci.lane.getWaitingTime('Faixa_7_1')+traci.lane.getWaitingTime('Faixa_7_2') step += 1 print('Espera (s): ', tempo) #traci.junction.subscribeContext("Inter", tc.CMD_GET_VEHICLE_VARIABLE, 42, [tc.VAR_SPEED, tc.VAR_WAITING_TIME]) #print('Results: ', traci.junction.getContextSubscriptionResults("Inter")) #scResults = traci.junction.getContextSubscriptionResults("Inter") #halting = 0 #if scResults: # relSpeeds = [d[tc.VAR_SPEED] / d[tc.VAR_ALLOWED_SPEED] for d in scResults.values()] # # compute values corresponding to summary-output # running = len(relSpeeds) # halting = len([1 for d in scResults.values() if d[tc.VAR_SPEED] < 0.1]) # meanSpeedRelative = sum(relSpeeds) / running # timeLoss = (1 - meanSpeedRelative) * running * stepLength # print(traci.simulation.getTime(), timeLoss, halting) traci.close() sys.stdout.flush()
data_writer="MyPublisher::CameraControllWriter", output_buffer_size=10) print("DDS connection has established") while True: print("starting sumo gui") sumoBinary = "sumo-gui" sumoCmd = [sumoBinary, "-c", "osm.sumocfg"] #,"--scale",'0.9' traci.start(sumoCmd) print("sumo gui has been started") print("starting the simulation") # warming up the simulation (because the simulation start without car) traci.simulationStep(1500) print("warm up has been finished") for i in range(6000): #wait 20 ms sleep(1 / sim_speed) #execute a simulation step traci.simulationStep() if traci.lanearea.getLastStepVehicleNumber("Camera1") > 0: try: dds_streamer_output_buffer.put_nowait({ "destination": str(0), "validdata": True }) except:
def run(self, i_run): """ Runs the simulation on both SUMO and Python layers Args: i_run (int): run number """ # if there is a run configuration loaded use it to populate # parkingspaces in environment otherwise initialize new if not self._config.getRunCfg(str(i_run)): if self._sim_config.get("verbose"): print( "* no run cfg found. Initializing random parking spaces.") self._environment.initParkingSpaces(i_run) elif self._config.isRunCfgOk(i_run): self._environment.loadParkingSpaces(i_run) else: return # if --routefile flag is provided, use the file for routing, otherwise # generate (and overwrite if exists) route file (reroute.rou.xml) for # this simulation run using the given number of parking search vehicles route_file = os.path.join(self._sim_config.get("resourcedir"), self._sim_config.get("routefile")) if not (os.path.isfile(route_file) and self._sim_config.get("forceroutefile")): generatePsvDemand(self._sim_config.get("vehicles"), self._sim_config.get("resourcedir"), self._sim_config.get("routefile")) # start sumo as a subprocess otherwise it wont work (because reasons) l_sumoProcess = open_sumo(self._sim_config) # execute the TraCI control loop traci.init(self._sim_config.get("sumoport")) # internal clock variable, start with 0 step = 0 # create empty list for parking search vehicles l_parkingSearchVehicles = [] # compute phase 2 routing information (individual and cooperative) l_individualRoutes, l_cooperativeRoutes = self.computePhase2Routings() self.initPOI() self.updatePOIColors() # do simulation as long as vehicles are present in the network while traci.simulation.getMinExpectedNumber() > 0: # tell SUMO to do a simulation step traci.simulationStep() self.updatePOIColors() # increase local time counter step += 1 # every 1000 steps: ensure local time still corresponds to SUMO if step != (traci.simulation.getCurrentTime() / 1000): print("TIMESTEP ERROR", step, "getCurrentTime", traci.simulation.getCurrentTime()) # if a new vehicle has departed in SUMO, create the corresponding # Python representation and remove the vehicles that have # disappeared in SUMO dep_list = traci.simulation.getDepartedIDList() # TODO: arr list is always empty? Possible bug i.e. we dont set # vehicles to arrived or something arr_list = traci.simulation.getArrivedIDList() l_departedVehicles = (x for x in dep_list if x not in arr_list) # TODO: from one debugging session I got the order of # parkinSearchVehicles = [veh0, veh1, veh3, veh2, veh4] # probably arr_list is not given in order... l_parkingSearchVehicles.extend( ParkingSearchVehicle( vehID, self._environment, self._config, i_run, step, self._environment._net.getEdge(l_individualRoutes[vehID] [-1]).getToNode().getID(), l_cooperativeRoutes[vehID], l_individualRoutes[vehID]) for vehID in l_departedVehicles) # update status of all vehicles for psv in (v for v in l_parkingSearchVehicles if v.is_parked() is False): psv.update(step) env_edges = self._environment._roadNetwork["edges"] for edge in env_edges: oppositeEdgeID = env_edges[edge]["oppositeEdgeID"] visitCount = (psv.traversed_route.count(str(edge)) + psv.traversed_route.count(oppositeEdgeID)) plannedCount = (psv.active_route.count(str(edge)) + psv.active_route.count(oppositeEdgeID)) env_edges[edge]["visitCount"][psv.name] = visitCount env_edges[edge]["plannedCount"][psv.name] = plannedCount # if last edge, choose next possible edges to continue if psv.last_edge(): lastSegment = psv.current_route[-1] succEdges = self._environment._net.getEdge( lastSegment).getToNode().getOutgoing() # calculate costs for every edge except opposite direction # of current edge succEdgeCost = {} for edge in succEdges: # consider all successor edges, BUT if no opposite edge # exists, don't try to exclude it. if lastSegment in self._environment._oppositeEdgeID: if len(succEdges) == 1: succEdgeCost[str( edge.getID())] = self.edgeCost(psv, edge) elif not str(edge.getID( )) == self._environment._oppositeEdgeID[ lastSegment]: succEdgeCost[str( edge.getID())] = self.edgeCost(psv, edge) # TODO: there is missing else here? else: succEdgeCost[str(edge.getID())] = self.edgeCost( psv, edge) # calculate minima of succEdgeCost minValue = min(succEdgeCost.values()) minKeys = [ key for key in succEdgeCost if succEdgeCost[key] == minValue ] # choose randomly if costs are equal p3_prob = self._vehicle_config["phase3randomprob"] if random.random() < p3_prob: next_link = random.choice(list(succEdgeCost.keys())) else: next_link = random.choice(minKeys) psv.append_route(next_link) # break the while-loop if all SUMO vehicles have parked if remaining_vehicles(l_parkingSearchVehicles) == 0: if self._sim_config.get("verbose"): print("SUCCESSFULLY PARKED:", parked_vehicles(l_parkingSearchVehicles), "OUT OF", self._sim_config.get("vehicles")) break sumo_close(l_sumoProcess) total_parked = parked_vehicles(l_parkingSearchVehicles) searchTimes = [veh.search_time for veh in l_parkingSearchVehicles] searchDistances = [ veh.search_distance for veh in l_parkingSearchVehicles ] walkingTimes = [veh.walk_time for veh in l_parkingSearchVehicles] walkingDistances = [ veh.walk_distance for veh in l_parkingSearchVehicles ] searchPhases = [veh.search_phase for veh in l_parkingSearchVehicles] # TODO: this should probably just return vehicles and leave processing # to other instances return (total_parked, searchTimes, walkingTimes, searchDistances, walkingDistances, searchPhases)
def reset(self): self.startSUMO() self.addEgoVehicle() # Add the ego car to the scene traci.simulationStep()
def start_sumo(sumo_cmd_str): traci.start(sumo_cmd_str) for i in range(20): traci.simulationStep()
def run(): traci.vehicle.add("add1", "right", "typeA", departLane="1", departPos="250") step = 0 period = 20 junction_x = traci.junction.getPosition("gneJ1")[0] offset = 5 schedule_A = [] schedule_B = [] """execute the TraCI control loop""" # we start with phase 2 where EW has green # traci.trafficlight.setPhase("0", 2) while traci.simulation.getMinExpectedNumber( ) > 0: # The number of vehicles which are in the net plus the ones still waiting to start. if traci.simulation.getLoadedNumber() > 0: for vehID in traci.simulation.getLoadedIDList(): # print(vehID) traci.vehicle.setLaneChangeMode(vehID, 0b000000000000) # for vehID in traci.vehicle.getIDList(): # print(vehID + str(traci.vehicle.getPosition(vehID))) traci.simulationStep() step += 1 currentTime = traci.simulation.getTime() print(currentTime) # if len(schedule_A) > 0: # vehID = schedule_A[0][0] # # print('x', traci.vehicle.getPosition(vehID)[0]) # if traci.vehicle.getPosition(vehID)[0] >= (junction_x) and schedule_A[0][1] <= currentTime: # print(vehID, traci.vehicle.getPosition(vehID)[0], traci.vehicle.getStops(vehID)) # try: # traci.vehicle.resume(vehID) # except traci.exceptions.TraCIException: # pass # del schedule_A[0] # if len(schedule_B) > 0: # vehID = schedule_B[0][0] # # print('x', traci.vehicle.getPosition(vehID)[0]) # if traci.vehicle.getPosition(vehID)[0] >= (junction_x) and schedule_B[0][1] <= currentTime: # print(vehID, traci.vehicle.getPosition(vehID)[0], traci.vehicle.getStops(vehID)) # try: # traci.vehicle.resume(vehID) # except traci.exceptions.TraCIException: # pass # del schedule_B[0] # print(traci.vehicle.getSpeed(schedule_A[0][0])) # break if len(schedule_A) > 0 and schedule_A[0][2] == False: d = schedule_A[0][1] - traci.simulation.getTime() print('stop', schedule_A[0][0]) try: traci.vehicle.setStop(schedule_A[0][0], "E1", pos=junction_x, laneIndex=1, duration=d) schedule_A[0][2] = True except traci.exceptions.TraCIException: pass if len(schedule_B) > 0 and schedule_B[0][2] == False: d = schedule_B[0][1] - traci.simulation.getTime() print('stop', schedule_B[0][0]) try: traci.vehicle.setStop(schedule_B[0][0], "E1", pos=junction_x, laneIndex=0, duration=d) schedule_B[0][2] = True except traci.exceptions.TraCIException: pass # if len(schedule_A) > 0: for i in schedule_A: if i[1] < traci.simulation.getTime(): schedule_A.remove(i) # if len(schedule_B) > 0: for i in schedule_B: if i[1] < traci.simulation.getTime(): schedule_B.remove(i) if step == period: if traci.lanearea.getLastStepVehicleNumber( "dA") > 0 and traci.lanearea.getLastStepVehicleNumber( "dB") > 0: a, b, id_a, id_b = compute_earliest_arrival( junction_x, schedule_A, schedule_B) order_stack_A, order_stack_B = compute_entering_time(a, b) index_A = 1 index_B = 1 schedule_A = [] schedule_B = [] while len(order_stack_A) > 0: top = order_stack_A.pop() schedule_A.append([id_a[index_A], top[2], False]) print(id_a[index_A], 'enter =', top[2]) # if index_A == 1: # d = top[2]-currentTime # print('stop', id_a[index_A]) # try: # traci.vehicle.setStop(id_a[1], "E1", pos=junction_x, laneIndex=1, duration=d) # except traci.exceptions.TraCIException: # pass index_A += 1 while len(order_stack_B) > 0: top = order_stack_B.pop() schedule_B.append([id_b[index_B], top[2], False]) print(id_b[index_B], 'enter =', top[2]) # if index_B == 1: # d = top[2]-currentTime # print('stop', id_b[index_B]) # try: # traci.vehicle.setStop(id_b[1], "E1", pos=junction_x, laneIndex=0, duration=d) # except traci.exceptions.TraCIException: # pass index_B += 1 step = 0 print(traci.simulation.getTime()) traci.close() sys.stdout.flush()
def run(): net = sumolib.net.readNet('campusmap.net.xml') oldUpcomingLightID = '' """execute the TraCI control loop""" step = 0 #while traci.simulation.getMinExpectedNumber() > 0: traci.vehicle.add("bike", "bike1", typeID='typeBike') #Adds bike to simulation traci.vehicle.setSpeed("bike", 0) while True: radius = 0.1 closestEdge = 0 #lat, lon, bear, speed = get_GPS_data() #debugging if step == 1: data = (34.058250, -117.82232, 160, 5) fileObject = open("gps.pkl", "wb") pickle.dump(data, fileObject) fileObject.close() if step == 5: fileObject2 = open("gps.pkl", "r") data2 = pickle.load(fileObject2) print(data2) lat, lon, bear, speed = data2 # lat = 34.058250 # lon = -117.82232 # bear = 160 # speed= 5 #bear = (bear+360) % 360 #convert -180 -- +180 to 0 - 360 angles x, y = net.convertLonLat2XY(lon, lat) #print(x) #print(y) edges = net.getNeighboringEdges(x, y, 0.6) # numedges = len(edges) # print(numedges) # for elem in edges: # print(elem) # pick the closest edge (FIX THIS) if len(edges) > 0: distancesAndEdges = sorted([(dist, edge) for edge, dist in edges]) dist, closestEdge = distancesAndEdges[0] edge_id = closestEdge.getID() # temp = closestEdge.split(' ') # temp2 = temp[1].split('"') # print(temp2[-1]) #update bike parameters traci.vehicle.moveToXY( "bike", edge_id, 0, x, y, angle=bear, keepRoute=1 ) #(vehID, edge, lane index, x, y, angle, keepRoute 0-2) traci.vehicle.setSpeed("bike", speed) #speed update timeRemainingInPhase = 0 if step == 8: lights = traci.vehicle.getNextTLS("bike") newUpcomingLightID = str(lights[0][0]) if newUpcomingLightID != oldUpcomingLightID: oldUpcomingLightID = newUpcomingLightID upcomingLightPhase = traci.trafficlights.getPhase( newUpcomingLightID) nextLightProg = traci.trafficlights.getCompleteRedYellowGreenDefinition( newUpcomingLightID) lightProg = str(nextLightProg[0]) nextSwitch = traci.trafficlights.getNextSwitch( newUpcomingLightID) timeRemainingInPhase += nextSwitch - traci.simulation.getCurrentTime( ) p = multiprocessing.Process(target=timeWorker, args=( lightProg, upcomingLightPhase, timeRemainingInPhase, )) p.start() #print(step) if False: #step > 6: lights = traci.vehicle.getNextTLS("bike") nextLightID = str(lights[0][0]) nextSwitch = traci.trafficlights.getNextSwitch(nextLightID) #duration = traci.trafficlights.getPhaseDuration(nextLightID) #print(nextSwitch - traci.simulation.getCurrentTime()) if False: #step == 10: lat = 34.057634 lon = -117.822184 bear = 165 speed = 2 #34.057634, -117.822184 #bear = (bear+360) % 360 #convert -180 -- +180 to 0 - 360 angles x, y = net.convertLonLat2XY(lon, lat) print(x) print(y) edges = net.getNeighboringEdges(x, y, 0.6) # numedges = len(edges) # print(numedges) # for elem in edges: # print(elem) # file = open('testfile.txt','w') # file.write(edges) # file.close() # for dist,edge in edges: # print(dist) # print('---'.join(edge)) # pick the closest edge (FIX THIS) if len(edges) > 0: distancesAndEdges = sorted([(dist, edge) for edge, dist in edges]) dist, closestEdge = distancesAndEdges[0] edge_id = closestEdge.getID() # temp = closestEdge.split(' ') # temp2 = temp[1].split('"') # print(temp2[-1]) #update bike parameters traci.vehicle.moveToXY( "bike", edge_id, 0, x, y, angle=bear, keepRoute=1 ) #(vehID, edge, lane index, x, y, angle, keepRoute 0-2) traci.vehicle.setSpeed("bike", speed) #speed update #calcSpeed = get_best_speed("bike") #calc best speed lights = traci.vehicle.getNextTLS("bike") print(lights) #parse light id for light in lights[0]: print(light) #getCompleteRedYellowGreenDefinition() traci.simulationStep() step += 1 traci.close() sys.stdout.flush()
def step(): s = traci.simulation.getCurrentTime() / 1000 traci.simulationStep() return s
def episode(RB_RLAlgorithm=None, Proudhon=None, episode_num=0): ######################## # 1 inits done = False # are we done with the episode or not step = 0 # step number errors_arrays = np.zeros(7) # for error checking capability if (Proudhon is None): Proudhon = env(vehicles_list) # vehicles_list = [LH, RB] ## -- ## Proudhon.reset() traci.load(["-c", Sumocfg_DIR, "--tripinfo-output", "tripinfo.xml", "--seed", str(Sumo_random_seed)]) for vehc in vehicles_list: vehc.initialize() ## -- ## if (RB_RLAlgorithm is None): algo_params = q_learning_params # from Config.py RB_RLAlgorithm = RLAlgorithm(Proudhon, algo_params=algo_params, load_q_table=load_q_table, test_mode_on=vis_update_params['test_mode_on']) # Algorithm for RB Agent ## ###################### ######################## # 2 init measurements traci.simulationStep() # After stepping Proudhon.get_emer_start_lane() # (communication from vehicle): getters(vehicles_list) # measure all values from environment # (communication to environment): Proudhon.measure_full_state() # measure all values into our agents # (communication to algorithm/agent): new_observed_state_for_this_agent = Proudhon.observed_state[0] # Chose Action from Feasible Actions: feasible_actions_for_current_state = Proudhon.get_feasible_actions(vehicles_list[1]) chosen_action = RB_RLAlgorithm.pickAction(feasible_actions_for_current_state, new_observed_state_for_this_agent) RB_RLAlgorithm.applyAction(chosen_action, vehicles_list[1]) # Request Action on Agent episode_reward = 0 episode_reward_list = [] ######################## # 3: MAIN LOOP if (episode_num % vis_update_params['every_n_episodes'] == 0): print(f'E:{episode_num: <{6}}|S:{0: <{4}} | ' f'epsilon: {RB_RLAlgorithm.epsilon: <{31}} | ' f'state: {[str(x)[:5] + " " * max(0, 5 - len(str(x))) for x in Proudhon.observed_state[0]]} |') while traci.simulation.getMinExpectedNumber() > 0: # 3.1: Store last states amb_last_velocity = Proudhon.emer.spd last_observed_state = Proudhon.observed_state last_observed_state_for_this_agent = last_observed_state[0] # ----------------------------------------------------------------# # 3.1.1: Store last picked action last_picked_action = chosen_action # check_related_fcn # ----------------------------------------------------------------- # # 3.2: M O V E O N E S I M U L A T I O N S T E P # ----------------------------------------------------------------- # traci.simulationStep() # actual action applying step += 1 # TODO: Turn this into are_we_ok function if (vehicles_list[0].getL() != Proudhon.emer_start_lane): raise ValueError( f"Ambulance Changed lane from {Proudhon.emer_start_lane} to {vehicles_list[0].getL()} on step {step}. " f"\nAmbulance Should not change lane. Quitting.") # ----------------------------------------------------------------- # # 3.3: measurements and if we are done check getters(vehicles_list) Proudhon.measure_full_state() new_observed_state_for_this_agent = Proudhon.observed_state[0] done = Proudhon.are_we_done(full_state=Proudhon.full_state, step_number=step) if (step % vis_update_params['every_n_iters'] == 0 and episode_num % vis_update_params[ 'every_n_episodes'] == 0): # print step info print(f'E:{episode_num: <{6}}|S:{step: <{4}} | ' f'reward : {str(Proudhon.reward)[:min(5, len(str(Proudhon.reward)))]: <{5}}, ' f'lastAction: {chosen_action : <{12}} | ' f'cumReward: ' + str(episode_reward)[:6] + ' ' * max(0, 6 - len(str(episode_reward))) + f' | state: {[str(x)[:5] + " " * max(0, 5 - len(str(x))) for x in Proudhon.observed_state[0]]}, ' f'actionMethod: {RB_RLAlgorithm.action_chosing_method : <{14}}') # 3.3.1: checking if we are ok if (enable_checks): errors_arrays += are_we_ok(1, True, Proudhon, step, last_picked_action, last_observed_state_for_this_agent, new_observed_state_for_this_agent) # 3.4: reward last step's chosen action reward = Proudhon.calc_reward(amb_last_velocity, done, step) episode_reward += reward # for history episode_reward_list.append(reward) # for history # 3.6: Feasibility check for current_state (for next step) feasible_actions_for_current_state = Proudhon.get_feasible_actions(vehicles_list[1]) # 3.5: update q table using backward reward logic RB_RLAlgorithm.update_q_table(chosen_action, reward, new_observed_state_for_this_agent, last_observed_state_for_this_agent, feasible_actions_for_current_state) if (done): # DO NOT REMOVE THIS (IT BREAKS IF WE ARE DONE) if (episode_num % vis_update_params['every_n_episodes'] == 0): if ( done == 1): # TODO: Remove episode_end_reason outsisde the print check -- we might need it elsewehere episode_end_reason = "max steps" elif (done == 2): episode_end_reason = "ambulance goal" elif (done == 3): # TODO: #TOFIX: What should be the state here? episode_end_reason = "agent goal" else: raise ValueError(f"Episode: {episode_num} done is True ={done} but reason not known !") print(f'E:{episode_num: <{6}}|S:{step: <{4}} | ' f'reward : {str(Proudhon.reward)[:min(5, len(str(Proudhon.reward)))]: <{5}}, ' f'lastAction: {chosen_action : <{12}} | ' f'cumReward: ' + str(episode_reward)[:6] + ' ' * max(0, 6 - len(str(episode_reward))) + f' | state: {[str(x)[:5] + " " * max(0, 5 - len(str(x))) for x in Proudhon.observed_state[0]]}, ' f'actionMethod: {RB_RLAlgorithm.action_chosing_method : <{14}}') if(enable_checks): print( "non_requested_lane_change , acc_errors , dec_errors , _no_acc_errors , non_requested_speed_change ,change_left_errors , change_right_errors") print(f"Error_happened_during_simulation {errors_arrays}") break # 3.7: Actually Choose Action from feasible ones (for next step) chosen_action = RB_RLAlgorithm.pickAction(feasible_actions_for_current_state, new_observed_state_for_this_agent) # 3.8: Request environment to apply new action (Request action on Agent for next step) # Action is still not applied here, but on #3.2 RB_RLAlgorithm.applyAction(chosen_action, vehicles_list[1]) # Episode end sys.stdout.flush() # 4: Update Epsilon after episode is done old_epsilon = RB_RLAlgorithm.epsilon RB_RLAlgorithm.epsilon = RB_RLAlgorithm.min_epsilon + (RB_RLAlgorithm.max_epsilon - RB_RLAlgorithm.min_epsilon) * \ np.exp( -RB_RLAlgorithm.decay_rate * episode_num) # DONE: Change epsilon to update every episode not every iteration if (episode_num % vis_update_params['every_n_episodes'] == 0): print(f'\n\nE:{episode_num: <{6}}| END:{step: <{4}} | ' f'finalCumReward: ' + str(episode_reward)[:6] + ' ' * max(0, 6 - len(str(episode_reward))) + " | " f'reason: {episode_end_reason: <{15}} | ' f'old_eps: {old_epsilon: <{10}}, ' f'new_eps: {RB_RLAlgorithm.epsilon: <{10}}') print('-' * 157) print('=' * 157) print('\n') if (vis_update_params['print_reward_every_episode'] and episode_num % vis_update_params['every_n_episodes'] != 0): print(f'E:{episode_num: <{6}}| END:{step: <{4}} | ' f'finalCumReward: ' + str(episode_reward)[:6] + ' ' * max(0, 6 - len(str(episode_reward))) + " | ") return RB_RLAlgorithm, Proudhon, episode_reward, episode_reward_list, errors_arrays
"""判断是否开始某个相位""" current_phase_id = traci.trafficlight.getPhase('J1') current_phase_duration = traci.trafficlight.getPhaseDuration('J1') next_switch = traci.trafficlight.getNextSwitch('J1') sim_time = traci.simulation.getTime() if next_switch - sim_time == current_phase_duration - 1: # 开始直行某个相位 if current_phase_id == 0: print('开始:东西直行相位') phase_0_extended = 0 # 恢复标志,即允许修改 elif current_phase_id == 2: print('开始:南北直行相位') else: pass """执行仿真""" traci.simulationStep() step += 1 """判断是否完成某个相位""" current_phase_id = traci.trafficlight.getPhase('J1') current_phase_duration = traci.trafficlight.getPhaseDuration('J1') next_switch = traci.trafficlight.getNextSwitch('J1') sim_time = traci.simulation.getTime() if next_switch == sim_time: # 当前相位直行结束 if current_phase_id == 0: print('结束:东西直行相位') if phase_0_extended == 0: traci.trafficlight.setPhaseDuration('J1', 3) # 加了3秒 phase_0_extended = 1 # 修改相位时长标识,修改标志后执行完该相位前,不再修改相位长度 elif current_phase_id == 2: print('结束:南北直行相位')
def execute(self, action): run = Assembler(self.carID) self.output = run.getTensor() #print(run.getTensor()) if self.output is None: term = True return self.getObservation(), term, 0 else: self.co2.append(traci.vehicle.getFuelConsumption(self.carID)) rew = Reward('ego', run.getTraffic(), self.output, run.getPrioritizedTraffic()) coll, term = rew.collision_test() if term is True: self.setCollisionCounter() cost = coll return self.getObservation(), term, cost traci.vehicle.setSpeedMode('ego', 0) num_vec = traci.vehicle.getIDList() disruption = [] for i in range(len(num_vec)): if num_vec[i] != 'ego': if traci.vehicle.getAcceleration(num_vec[i]) < 0: disruption.append(traci.vehicle.getAcceleration( num_vec[i])) traci.vehicle.setLaneChangeMode(num_vec[i], 512) self.disruptionIntensity.append(sum(disruption)) ### #if i % 2 == 0: #traci.vehicle.setSpeedMode(num_vec[i], 23) carID = 'ego' act = Action(carID) if action == 0: act.decelerate() #print('dec') elif action == 1: act.accelerate() #print('acc') #elif action == 2: # print('eme') # act.emergency() else: act.remain() #print('rem') #print(traci.vehicle.getSpeed(self.carID)) #gap, term = rew.emergency_gap_test() #wary = rew.wary_before_intersection() #win = rew.target() #brake = rew.emergency_brake() cost = rew.optimum_speed_deviation() #+ gap #+ brake # #+ wary traci.simulationStep() #print(self.output) self.terminationTime.append(traci.simulation.getCurrentTime()) #print(traci.simulation.getCurrentTime()) return self.getObservation(), term, cost
def simulate_step(self): traci.simulationStep()
def run(isTesting): """execute the TraCI control loop""" step = 0 YELLOW_PHASE = 4 RED_PHASE = 2 site4235_detector_delay = [] site4235_LOOP_COUNT = 11 for i in range(0, site4235_LOOP_COUNT + 1): # initiliaze array of 0s site4235_detector_delay.append(0) lanesForPhases_s4235 = { # key = phase, values = lanes with green lights 0: [1, 2, 3, 9, 10, 11], #A 1: [1, 2, 3, 4], #B 2: [6, 7] #C } detector_delay_s4219 = [] site4219_LOOP_COUNT = 21 ignored_phases_s4219 = [ 20, 21, 22 ] # could automate this I guess? if not in dict TODO for i in range(0, site4219_LOOP_COUNT + 1): # initiliaze array of 0s detector_delay_s4219.append(0) lanesForGroupsForPhases_s4219 = { # key = phase, values = lanes with green lights 0: [0, 1], #A 1: [1, 6, 7], #B 2: [0, 2], #C 3: [3, 8], #D 4: [3, 4, 7], #E 5: [4, 5, 7], #F 6: [6, 2, 7] #G } lanesForGroups_s4219 = { # which lanes are grouped together, this is useful for finding when a lane group is no longer active 0: [ 11, 12 ], # such that, a right turn lane can be replaced by oncoming traffic 1: [1, 2], 2: [13, 14], 3: [15, 16, 17], 4: [7, 8], 5: [9, 10], 6: [3, 4], 7: [5, 6], 8: [18, 19] } conflictingGroups_s4219 = { # TODO automate this from lanesForGroupsForPhases_s4219 (necessary?) 0: [3, 4, 5, 6, 7, 8], 1: [2, 3, 4, 5, 8], 2: [1, 3, 4, 5, 8], 3: [0, 1, 2, 3, 5, 6], 4: [0, 1, 2, 6, 8], 5: [0, 1, 2, 3, 6, 8], 6: [0, 3, 4, 5, 8], 7: [1, 8], 8: [1, 2, 4, 5, 6, 7] } earliestDeadline_s4219 = 0 earliestDeadlineGroup_s4219 = 0 secondDeadlineGroup_s4219 = 0 waitTime_s4219 = 0 activeTraffic_s4219 = [] site4235_next_phase = 0 for i in range(0, len(lanesForGroups_s4219)): # initiliaze array of 99s activeTraffic_s4219.append(99) detector_delay_s4220 = [] site4220_LOOP_COUNT = 15 ignored_phases_s4220 = [ 3, 8, 11 ] # could automate this I guess? if not in dict TODO for i in range(0, site4220_LOOP_COUNT + 1): # initiliaze array of -1s detector_delay_s4220.append(0) lanesForGroupsForPhases_s4220 = { # key = phase, values = lanes with green lights 0: [1, 5], #A 1: [5, 6, 0], #B 2: [1, 2], #C 3: [3, 4], #D 4: [2, 6] #E } lanesForGroups_s4220 = { # which lanes are grouped together, this is useful for finding when a lane group is no longer active 0: [1, 2], 1: [4, 5, 6], 2: [7], 3: [9], 4: [10], 5: [12, 13, 14], 6: [15] } conflictingGroups_s4220 = { # TODO automate this from lanesForGroupsForPhases_s4220 (necessary?) 0: [1, 4], 1: [0, 6, 4, 3], 2: [5, 4, 3], 3: [0, 1, 2, 5, 6], 4: [0, 1, 2, 5, 6], 5: [2, 3, 4], 6: [2, 3, 4, 5] } earliestDeadline_s4220 = 0 earliestDeadlineGroup_s4220 = 0 waitTime_s4220 = 0 activeTraffic_s4220 = [] for i in range(0, len(lanesForGroups_s4220)): # initiliaze array of 99s activeTraffic_s4220.append(99) next_phase_s4220 = 0 detector_delay_s4221 = [] site4221_LOOP_COUNT = 18 ignored_phases_s4221 = [ 6, 10, 14 ] # could automate this I guess? if not in dict TODO for i in range(0, site4221_LOOP_COUNT + 1): # initiliaze array of -1s detector_delay_s4221.append(0) lanesForGroupsForPhases_s4221 = { # key = phase, values = lanes with green lights 0: [3, 7], #A 1: [3, 4], #B 2: [7, 8, 0], #C 3: [1, 2, 0], #D 4: [5, 6], #E 5: [4, 8, 0] #f } lanesForGroups_s4221 = { # which lanes are grouped together, this is useful for finding when a lane group is no longer active 0: [1, 2], 1: [3], 2: [4, 5], 3: [7, 8], 4: [9], 5: [11, 12], 6: [13], 7: [15, 16], 8: [17, 18] } conflictingGroups_s4221 = { # TODO automate this from lanesForGroupsForPhases_s4221 (necessary?) 0: [3, 6], 1: [3, 4, 6, 7, 8], 2: [3, 4, 5, 7, 8], 3: [0, 1, 2, 5, 6, 8], 4: [1, 2, 5, 6, 7], 5: [2, 3, 4, 7, 8], 6: [0, 1, 2, 3, 4, 7, 8], 7: [1, 2, 4, 5, 6], 8: [1, 2, 3, 5, 6] } earliestDeadline_s4221 = 0 earliestDeadlineGroup_s4221 = 0 waitTime_s4221 = 0 activeTraffic_s4221 = [] for i in range(0, len(lanesForGroups_s4221)): # initiliaze array of 99s activeTraffic_s4221.append(99) next_phase_s4221 = 0 MINIMUM_TRAFFIC_ACTIVITY = 3 traci.trafficlight.setPhase("5861321343", 0) #RL stuff e = 0.0 rng = str(datetime.datetime.now().time())[6:8] random.seed(rng) # make this a function #db_file = r"C:\Users\Elliot\Documents\GitHub\P4Project\_TiRakauDrive\RLv4.2\QTable.db" db_file = os.getcwd() + "\QTable.db" #print(db_file) conn = None try: conn = sqlite3.connect(db_file) except Error as e: print(e) cur = conn.cursor() db_file2 = os.getcwd() + "\\UCB.db" #print(db_file) conn2 = None try: conn2 = sqlite3.connect(db_file2) except Error as e: print(e) cur2 = conn2.cursor() totalEps = open("learningUpdates.csv", "r") t = 1 for ep in totalEps: t += 1 totalEps.close() saveStateActions = open("stateActions.csv", "w") takeMove = True transitionCounter_s4219 = 0 currentActivePhase = traci.trafficlight.getPhase( "cluster_25977365_314059191_314060044_314061754_314061758_314062509_314062525" ) transition_s2419 = chr(65 + currentActivePhase) + "-" + chr( 65 + currentActivePhase) + "-1" # eg. A-A-1 move = currentActivePhase while step <= 600: traci.simulationStep() # add in other traffic lights #static edf algo # ----------------------------------------------------------------------SITE 4221-------------------------------------------------------------------------------------------------- phase_s4221 = traci.trafficlight.getPhase( "cluster_25953432_313863435_313863521_314053282" ) # phase indexing starts at 0 if (phase_s4221 in lanesForGroupsForPhases_s4221.keys() ): # if not a transition phase waitTime_s4221 += 1 # current phase wait time transitionCounter_s4221 = 0 for i in range(0, len(lanesForGroups_s4221)): if i in lanesForGroupsForPhases_s4221[phase_s4221]: groupActivity = [] for lane in lanesForGroups_s4221[i]: groupActivity.append( int( traci.inductionloop.getTimeSinceDetection( "site4221_" + str(lane)))) detector_delay_s4221[lane] = 0 activeTraffic_s4221[i] = min(groupActivity) else: for lane in lanesForGroups_s4221[i]: if int( traci.inductionloop.getTimeSinceDetection( "site4221_" + str(lane)) ) == 0: # if getLastStepVehicleNumber>0, detector_delay_s4221[lane] = detector_delay_s4221[ lane] + 1 # increment loopDetectorDelay earliestDeadline_s4221 = detector_delay_s4221.index( max(detector_delay_s4221)) switchPhase_s4221 = True for group in lanesForGroups_s4221: # find which group the EDF lane belongs to if earliestDeadline_s4221 in lanesForGroups_s4221[group]: earliestDeadlineGroup_s4221 = group for group in lanesForGroupsForPhases_s4221[ phase_s4221]: # do not switch phase if any conflicting lane has traffic, all inactive groups automatically are 99 if group != 0: if activeTraffic_s4221[group] < MINIMUM_TRAFFIC_ACTIVITY: switchPhase_s4221 = False #if earliestDeadlineGroup_s4221 in lanesForGroupsForPhases_s4221[phase_s4221]: # switchPhase_s4221 = False if earliestDeadline_s4221 == 0: switchPhase_s4221 = False if waitTime_s4221 == 180: switchPhase_s4221 = True if switchPhase_s4221 == True: transitionCounter = 0 # build new phase secondDeadline = -1 secondaryDeadlineGroup_s4221 = earliestDeadlineGroup_s4221 for i in range( 0, len(lanesForGroupsForPhases_s4221) ): # find the secondary group to create new phase if earliestDeadlineGroup_s4221 in lanesForGroupsForPhases_s4221[ i]: for group in lanesForGroupsForPhases_s4221[i]: if group != earliestDeadlineGroup_s4221: for lane in lanesForGroups_s4221[group]: if detector_delay_s4221[ lane] >= secondDeadline: secondDeadline = detector_delay_s4221[ lane] secondaryDeadlineGroup_s4221 = group for i in range(0, len(lanesForGroupsForPhases_s4221) ): # find the [hase containing both groups if earliestDeadlineGroup_s4221 in lanesForGroupsForPhases_s4221[ i]: if secondaryDeadlineGroup_s4221 in lanesForGroupsForPhases_s4221[ i]: next_phase_s4221 = i prev_phase_s4221 = phase_s4221 transition = chr(65 + phase_s4221) + "-" + chr( 65 + next_phase_s4221) + "-1" # eg. A-C-1 if phase_s4221 != next_phase_s4221: logics = traci.trafficlight.getAllProgramLogics( "cluster_25953432_313863435_313863521_314053282") names = str(logics[0].getPhases()).split("name='") for i in range(1, len(names)): if str(names[i].split("'")[0]) == str(transition): transitionID = i - 1 traci.trafficlight.setPhase( "cluster_25953432_313863435_313863521_314053282", transitionID) #change to transition for group in lanesForGroups_s4221: # find which group the EDF lane belongs to if next_phase_s4221 in lanesForGroups_s4221[group]: next_group_s4221 = group leftTurn_phase = traci.trafficlight.getPhase("313863797") if ( next_phase_s4221 == 0 or next_phase_s4221 == 1 or next_phase_s4221 == 4 ) and leftTurn_phase == 0: # should it be green for 4? # if traffic is turning left and should not in next phase, switch to red light traci.trafficlight.setPhase("313863797", 1) elif ( next_phase_s4221 == 2 or next_phase_s4221 == 3 or next_phase_s4221 == 5 ) and leftTurn_phase == 2: # if traffic can turn left, switch to green light traci.trafficlight.setPhase("313863797", 3) else: # if active phase is transition phase switchPhase_s4221 = False waitTime_s4221 = 0 for group in lanesForGroupsForPhases_s4221[ prev_phase_s4221]: # reset the traffic activity level of each active phase group activeTraffic_s4221[group] = 99 if transitionCounter_s4221 < YELLOW_PHASE: # while lights are still yellow switchPhase_s4221 = False #for lane in lanesForGroups_s4221[group]: # detector_delay_s4221[lane] = 0 elif transitionCounter_s4221 == YELLOW_PHASE + RED_PHASE - 1: traci.trafficlight.setPhase( "cluster_25953432_313863435_313863521_314053282", next_phase_s4221) #change to next phase transitionCounter_s4221 = transitionCounter_s4221 + 1 # ----------------------------------------------------------------------SITE 4220-------------------------------------------------------------------------------------------------- phase_s4220 = traci.trafficlight.getPhase( "gneJ41") # phase indexing starts at 0 if (phase_s4220 in lanesForGroupsForPhases_s4220.keys() ): # if not a transition phase waitTime_s4220 += 1 # current phase wait time transitionCounter_s4220 = 0 for i in range(0, len(lanesForGroups_s4220)): if i in lanesForGroupsForPhases_s4220[phase_s4220]: groupActivity = [] for lane in lanesForGroups_s4220[i]: groupActivity.append( int( traci.inductionloop.getTimeSinceDetection( "site4220_" + str(lane)))) detector_delay_s4220[lane] = 0 activeTraffic_s4220[i] = min(groupActivity) else: for lane in lanesForGroups_s4220[i]: if int( traci.inductionloop.getTimeSinceDetection( "site4220_" + str(lane)) ) == 0: # if getLastStepVehicleNumber>0, detector_delay_s4220[lane] = detector_delay_s4220[ lane] + 1 # increment loopDetectorDelay earliestDeadline_s4220 = detector_delay_s4220.index( max(detector_delay_s4220)) switchPhase_s4220 = True for group in lanesForGroups_s4220: # find which group the EDF lane belongs to if earliestDeadline_s4220 in lanesForGroups_s4220[group]: earliestDeadlineGroup_s4220 = group for group in lanesForGroupsForPhases_s4220[ phase_s4220]: # do not switch phase if any conflicting lane has traffic, all inactive groups automatically are 99 if activeTraffic_s4220[group] < MINIMUM_TRAFFIC_ACTIVITY: switchPhase_s4220 = False #if earliestDeadlineGroup_s4220 in lanesForGroupsForPhases_s4220[phase_s4220]: # switchPhase_s4220 = False if earliestDeadline_s4220 == 0: switchPhase_s4220 = False if waitTime_s4220 == 180: switchPhase_s4220 = True if switchPhase_s4220 == True: transitionCounter = 0 # build new phase secondDeadline = -1 secondaryDeadlineGroup_s4220 = earliestDeadlineGroup_s4220 for i in range( 0, len(lanesForGroupsForPhases_s4220) ): # find the secondary group to create new phase if earliestDeadlineGroup_s4220 in lanesForGroupsForPhases_s4220[ i]: for group in lanesForGroupsForPhases_s4220[i]: if group != earliestDeadlineGroup_s4220: for lane in lanesForGroups_s4220[group]: if detector_delay_s4220[ lane] >= secondDeadline: secondDeadline = detector_delay_s4220[ lane] secondaryDeadlineGroup_s4220 = group for i in range(0, len(lanesForGroupsForPhases_s4220) ): # find the [hase containing both groups if earliestDeadlineGroup_s4220 in lanesForGroupsForPhases_s4220[ i]: if secondaryDeadlineGroup_s4220 in lanesForGroupsForPhases_s4220[ i]: next_phase_s4220 = i prev_phase_s4220 = phase_s4220 transition_s4220 = chr(65 + phase_s4220) + "-" + chr( 65 + next_phase_s4220) + "-1" # eg. A-C-1 if phase_s4220 != next_phase_s4220: logics = traci.trafficlight.getAllProgramLogics("gneJ41") names = str(logics[0].getPhases()).split("name='") for i in range(1, len(names)): if str(names[i].split("'")[0]) == str( transition_s4220): transitionID = i - 1 traci.trafficlight.setPhase( "gneJ41", transitionID) #change to transition for group in lanesForGroups_s4220: # find which group the EDF lane belongs to if next_phase_s4220 in lanesForGroups_s4220[group]: next_group_s4220 = group else: # if active phase is transition phase switchPhase_s4220 = False waitTime_s4220 = 0 for group in lanesForGroupsForPhases_s4220[ prev_phase_s4220]: # reset the traffic activity level of each active phase group activeTraffic_s4220[group] = 99 if transitionCounter_s4220 < YELLOW_PHASE: # while lights are still yellow switchPhase_s4220 = False #for lane in lanesForGroups_s4220[group]: # detector_delay_s4220[lane] = 0 elif transitionCounter_s4220 == YELLOW_PHASE + RED_PHASE - 1: traci.trafficlight.setPhase( "gneJ41", next_phase_s4220) #change to next phase transitionCounter_s4220 = transitionCounter_s4220 + 1 # ----------------------------------------------------------------------SITE 4219-------------------------------------------------------------------------------------------------- #https://sumo.dlr.de/docs/Simulation/Output/Summary.html for each step reward # e1 output can be used if timesteps can be changed to 1 per step # Files: rl-runner, stateActionValues.xml, actions.xml, summary.xml, episodeStates.xml, updateTable.xml #States: # Loop detectors groups high/low (2^8) * # or time since last activation? 4s = 4^8 # max expected demand group (9 options) * # (and 2nd max? (8 options)) # Current phase (7 options) * # -sum of exit lane detector_delays? would indicate the load on adjacent junctions # state space size = 4,194,000 or 16,384 # can hard-code minimum activity time to reduce state size (but kind of goes against RL) #Actions: # Do nothing, transition to new phase (must stay in new phase for >=1s, transitoining to phase B will affect the reward for state B) #Rewards: # -vehicles halting # or -meanSpeedRelative, # not -sum(detector_delay_s4219) as this won't give total time spent waiting? # during that state (divide by total steps between actions?) #Algorithm: # Expected return of next move = Sum(nextMaxGroup&&nextPhase)[loopDetectorsOff->loopDetectorsOn] # e-greedy policy for training: e = .05 = %chance of taking random move. 1-e = .95 = %chance of taking move with max expected reward currentPhase_s4219 = traci.trafficlight.getPhase( "cluster_25977365_314059191_314060044_314061754_314061758_314062509_314062525" ) groupActivity_s4219 = [] for i in range(0, len(lanesForGroups_s4219)): if i in lanesForGroupsForPhases_s4219[ move]: # if group in current phase for lane in lanesForGroups_s4219[ i]: # reset delay for each lane detector_delay_s4219[lane] = 0 else: for lane in lanesForGroups_s4219[i]: if int( traci.inductionloop.getTimeSinceDetection( "site4219_" + str(lane)) ) == 0: # if vehicle waiting on any lane in group detector_delay_s4219[lane] += 1 # increment delay time for group in lanesForGroupsForPhases_s4219[move]: for lane in lanesForGroups_s4219[ group]: #if getTimeSinceDetection > 0? groupActivity_s4219.append( int( traci.inductionloop.getTimeSinceDetection("site4219_" + str(lane)))) carsFlowing = min(groupActivity_s4219) carsFlowing = min(carsFlowing, 9) earliestDeadline_s4219 = detector_delay_s4219.index( max(detector_delay_s4219)) detector_delay_s4219_sorted = sorted(detector_delay_s4219) secondDeadline_s4219 = detector_delay_s4219.index( detector_delay_s4219_sorted[-2]) for group in lanesForGroups_s4219: if earliestDeadline_s4219 in lanesForGroups_s4219[group]: earliestDeadlineGroup_s4219 = group if secondDeadline_s4219 in lanesForGroups_s4219[group]: secondDeadlineGroup_s4219 = group currentState = str(next_phase_s4220) + str( secondDeadlineGroup_s4219) + str(carsFlowing) + str( currentActivePhase) + str(earliestDeadlineGroup_s4219) if takeMove: # when takeMove is true, the move is made on the same timestep transitionCounter_s4219 = 0 stateInDec = (int(currentState[0]) * 9 * 10 * 9 * 7) + ( int(currentState[1]) * 10 * 9 * 7) + (int(currentState[2]) * 9 * 7) + (int(currentState[3]) * 9) + int( currentState[4], 9) + 1 #convert the state to its row no equivalent cur.execute("SELECT * FROM States WHERE rowid = " + str(stateInDec)) nextStateActions = cur.fetchone() cur2.execute("SELECT * FROM States WHERE rowid = " + str(stateInDec)) timesVisited = np.array(cur2.fetchone()) ntArray = np.sqrt((2 * math.log(t)) / timesVisited) #print("qvals = " + str(nextStateActions)) #print("UCB = " + str(ntArray)) #print(t) nextStateActions += ntArray move = np.argmax(nextStateActions) transition_s2419 = chr(65 + currentPhase_s4219) + "-" + chr( 65 + move) + "-1" # eg. A-C-1 if move != currentPhase_s4219: logics = traci.trafficlight.getAllProgramLogics( "cluster_25977365_314059191_314060044_314061754_314061758_314062509_314062525" ) names = str(logics[0].getPhases()).split("name='") for i in range(1, len(names)): if str(names[i].split("'")[0]) == str(transition_s2419): transitionID_s4219 = i - 1 traci.trafficlight.setPhase( "cluster_25977365_314059191_314060044_314061754_314061758_314062509_314062525", transitionID_s4219) #change to transition takeMove = False # do not take moves while transitioning phase leftTurn_phase = traci.trafficlight.getPhase("5861321343") if ( move == 0 or move == 2 or move == 3 ) and leftTurn_phase == 0: # if traffic is turning left and should not in next phase, switch to red light traci.trafficlight.setPhase("5861321343", 1) elif ( move == 1 or move == 4 or move == 5 or move == 6 ) and leftTurn_phase == 2: # if traffic can turn left, switch to green light traci.trafficlight.setPhase("5861321343", 3) #otherwise, do nothing. Reset duration of phase? #saveStateActions.write(transition_s2419 +","+ currentLoopsState +","+ str(currentActivePhase) +"\n")#TODO: add max group else: #need to account for left turn traffic light transitionCounter_s4219 += 1 if transitionCounter_s4219 == YELLOW_PHASE + RED_PHASE: currentActivePhase = move takeMove = True traci.trafficlight.setPhase( "cluster_25977365_314059191_314060044_314061754_314061758_314062509_314062525", move) #change to next phase currentState = str(next_phase_s4220) + str( secondDeadlineGroup_s4219) + str(carsFlowing) + str( currentActivePhase) + str(earliestDeadlineGroup_s4219) #in order: current phase, index of longest waiting group, loop states[8 bits], activity in current phase[1 bit], action #print(currentState +","+ str(move)) saveStateActions.write(currentState + "," + str(move) + "\n") #TODO: add max group # ----------------------------------------------------------------------SITE 4235-------------------------------------------------------------------------------------------------- site4235_phase = traci.trafficlight.getPhase( "cluster_1707799581_314056954_5931861577" ) # phase indexing starts at 0 for i in range(1, site4235_LOOP_COUNT): # for each loop detector if (i != 5) and (i != 8): # ignore lanes 5 and 8 if int( traci.inductionloop.getLastStepVehicleNumber( "site4235_" + str(i))) > 0 or site4235_detector_delay[ i] > 0: # if getLastStepVehicleNumber>0, site4235_detector_delay[i] = site4235_detector_delay[ i] + 1 # increment loopDetectorDelay if (site4235_phase in lanesForPhases_s4235.keys()): # if not a transition phase activeTraffic = 99 for i in lanesForPhases_s4235[site4235_phase]: site4235_detector_delay[ i] = 0 # reset loopDetectorDelay for loops in current phase if i != 1 and i != 2 and i != 3: # ignore non conflicting traffic if int( traci.inductionloop.getTimeSinceDetection( "site4235_" + str(i))) < activeTraffic: activeTraffic = int( traci.inductionloop.getTimeSinceDetection( "site4235_" + str(i))) if activeTraffic > MINIMUM_TRAFFIC_ACTIVITY: # if no traffic through active lanes, switch to transition phase for max(loopDetectorDelay) activeTraffic = 0 transitionCounter = 0 site4235_prev_phase = site4235_phase earliestDeadline = site4235_detector_delay.index( max(site4235_detector_delay)) # build next phase if earliestDeadline == 9 or earliestDeadline == 10 or earliestDeadline == 11: #this can be changed with lanesForGroups_s4235 (TODO) site4235_next_phase = 0 #A if site4235_phase == 1: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 7) #change to B-A elif site4235_phase == 2: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 11) #change to C-A elif earliestDeadline == 4: # loop detector no. 4 site4235_next_phase = 1 #B if site4235_phase == 0: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 3) #change to A-B elif site4235_phase == 2: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 13) #change to C-B elif earliestDeadline == 6 or earliestDeadline == 7: site4235_next_phase = 2 #C if site4235_phase == 0: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 5) #change to A-C elif site4235_phase == 1: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 9) #change to B-C elif earliestDeadline == 1 or earliestDeadline == 2 or earliestDeadline == 3: if site4235_detector_delay[4] > max( site4235_detector_delay[9:11] ): # compare conflicting lanes site4235_next_phase = 1 #B if site4235_phase == 0: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 3) #change to A-B elif site4235_phase == 2: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 13) #change to C-B else: site4235_next_phase = 0 #A if site4235_phase == 1: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 7) #change to B-A elif site4235_phase == 2: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", 11) #change to C-A else: # if active phase is transition phase if transitionCounter < YELLOW_PHASE: # while lights are still yellow for i in lanesForPhases_s4235[site4235_prev_phase]: site4235_detector_delay[i] = 0 elif transitionCounter == YELLOW_PHASE + RED_PHASE - 1: traci.trafficlight.setPhase( "cluster_1707799581_314056954_5931861577", site4235_next_phase) #change to next phase transitionCounter = transitionCounter + 1 #add for case where lights change with no traffic? step += 1 traci.close() sys.stdout.flush()
def run(): """execute the TraCI control loop""" traci.init(PORT) while traci.simulation.getMinExpectedNumber() > 0: #A=np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0]]) #d={} column_names = ['1', '2', '3', '4'] row_names = ['_2', '_1', '_0'] matrix = np.reshape((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (3, 4)) df = pd.DataFrame(matrix, columns=column_names, index=row_names) c_names = ['2', '1', '0'] r_names = ['RS1', 'RS2', 'RS3', 'RS4'] matrix1 = np.reshape( (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (4, 3)) df1 = pd.DataFrame(matrix1, columns=c_names, index=r_names) traci.simulationStep() l = traci.lane.getIDList() RS1 = l[34:37] RS2 = l[37:40] RS3 = l[40:43] RS4 = l[43:46] RS = [RS1, RS2, RS3, RS4] for segment in RS: for lane in segment: veh_list = traci.lane.getLastStepVehicleIDs(lane) #print veh_list , lane le = rig = stra = 0 for vehi in veh_list: rou = traci.vehicle.getRoute(vehi) if rou in left: le = le + 1 elif rou in right: rig = rig + 1 elif rou in straight: stra = stra + 1 print(le, rig, stra, lane) if '_2' in lane: if stra == 0: df['3']['_2'] = 1 df['2']['_1'] = 1 df['2']['_0'] = 1 elif rig == 0: if stra != 0: if le == 0: df['2']['_2'] = 1 df['2']['_1'] = 1 df['2']['_0'] = 1 elif stra != 0: if le != 0: df['4']['_2'] = 1 df['4']['_1'] = 1 df['4']['_0'] = 1 elif '_0' in lane: if stra == 0: df['1']['_0'] = 1 print("******The Drift attribute Matrix is as follows******") print(df) #.plot() for segment in range(0, 4): for lane in RS[segment]: #finding the number of vehiles n = traci.lane.getLastStepVehicleNumber(lane) lane_ = lane[4:5] # Declare speed, acceleration, distnace and passing rate variables speed, accel, distance, ptime, prate = 0.0, 0.0, 0.0, 0.0, 0.0 #getting the vehicle ids veh_list = traci.lane.getLastStepVehicleIDs(lane) #get last and second last vehicle ids #print("+++++++++++++++++") #print("+++++++++++++++++") # get the distance of the last vehicle from the intersection if not veh_list: print( "*********** veh_list is empty to show distance from junction" ) else: #print("+++++++++++++++++") distance = traci.vehicle.getDistance(veh_list[-1]) print("Distance from junction is " + str(distance)) #print ("+++++++++++++++") # get speed of last vehicle if not veh_list: print("*********** veh_list is empty to show last speed ") else: #print(" ?????????????????????????? ") speed = traci.vehicle.getSpeed(veh_list[-1]) print("Speed of last vehicle is " + str(speed)) #print(" ?????????????????????????? ") '''for veh in veh_list: var_speed=traci.vehicle.getSpeed(veh) print(var_speed)''' #get acceleration of second last vehicle if not veh_list: print( "*********** veh_list is empty to show second last acceleration " ) else: # print("###########################") accel = traci.vehicle.getAccel(veh_list[-1]) print("Acceleration of second last vehicle is " + str(accel)) #print("###########################") #print acc #calculate the flow rate using the formula if not veh_list: print( "*********** veh_list is empty to show passing rate ") else: ptime = (-speed + math.sqrt( (speed * speed) + 2 * accel * distance)) / accel print("Passing time is " + str(ptime)) ##########################################enter flow time matrix here #col = ['2', '1', '0'] #row = ['RS1','RS2','RS3','RS4'] #matrix2 = np.reshape((0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0), (4, 3)) #df2 = pd.DataFrame(matrix2, columns=col, index=row) if ptime != 0: prate = n / ptime print("Flow rate is " + str(prate)) a = (str(lane_)) #print a b = ('RS' + str(segment + 1)) #print b df1[a][b] = prate #df2[a][b]=ptime print("******The Flow rate Matrix is as follows******") print(df1) #df1.plot() X = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] # 3x4 matrix Y = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] # result is 3x4 APR = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] # iterate through rows of X for i in range(len(X)): # iterate through columns of Y for j in range(len(Y[0])): # iterate through rows of Y for k in range(len(Y)): APR[i][j] += X[i][k] * Y[k][j] #for r in APR: # print(r) print("******The aggregated flow rate is as follows******") for r in APR: print(APR) print("******The 16 Condition Matrices Printed below******") data1 = [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]] TM1 = pd.DataFrame(data1, dtype=float) print(TM1) data2 = [[0, 0, 1, 0], [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0]] TM2 = pd.DataFrame(data2, dtype=float) print(TM2) data3 = [[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] TM3 = pd.DataFrame(data3, dtype=float) print(TM3) data4 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]] TM4 = pd.DataFrame(data4, dtype=float) print(TM4) data5 = [[0, 0, 0, 1], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]] TM5 = pd.DataFrame(data5, dtype=float) print(TM5) data6 = [[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [1, 0, 0, 0]] TM6 = pd.DataFrame(data6, dtype=float) print(TM6) data7 = [[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]] TM7 = pd.DataFrame(data7, dtype=float) print(TM7) data8 = [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]] TM8 = pd.DataFrame(data8, dtype=float) print(TM8) data9 = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0]] TM9 = pd.DataFrame(data9, dtype=float) print(TM9) data10 = [[0, 1, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]] TM10 = pd.DataFrame(data10, dtype=float) print(TM10) data11 = [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [1, 0, 0, 0]] TM11 = pd.DataFrame(data11, dtype=float) print(TM11) data12 = [[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]] TM12 = pd.DataFrame(data12, dtype=float) print(TM12) data13 = [[1, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]] TM13 = pd.DataFrame(data13, dtype=float) print(TM13) data14 = [[0, 0, 0, 0], [1, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0]] TM14 = pd.DataFrame(data14, dtype=float) print(TM14) data15 = [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 1, 0], [0, 0, 0, 0]] TM15 = pd.DataFrame(data15, dtype=float) print(TM15) data16 = [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0]] TM16 = pd.DataFrame(data16, dtype=float) print(TM16) #enter code here list_T = [ TM1, TM2, TM3, TM4, TM5, TM6, TM7, TM8, TM9, TM10, TM11, TM12, TM13, TM14, TM15, TM16 ] WAPR = [] STOTAL = [] for frame in list_T: #WAPR.append(np.matmul(APR,frame)) WAPR.append(APR * frame) #print WAPR for frame in WAPR: STOTAL.append(frame.sum().sum()) print( "******The following list contains the sum of flow rates for each ont f the 16 condition matrices when multiplied with aggregated flow rates******" ) print(STOTAL) #plt.rcdefaults() #objects = ('TM1', 'TM2', 'TM3', 'TM4' , 'TM5' , 'TM6' , 'TM7' , 'TM8' , 'TM9' , 'TM10' , 'TM11' , 'TM12' , 'TM13' , 'TM14' , 'TM15' , 'TM16') #y_pos = np.arange(len(objects)) #performance = STOTAL #plt.bar(y_pos, performance, align='center', alpha=0.5) #plt.xticks(y_pos, objects) #plt.ylabel('Total Passing Rate') #plt.title('Transition Matrix Selection') #plt.show() max_value = max(STOTAL) print(max_value) matrix_selection = STOTAL.index(max_value) #print matrix_selection print("#####The selected Condition matrix#####") print(list_T[matrix_selection]) no_of_veh = traci.vehicle.getIDCount() print(no_of_veh) tgreen = no_of_veh / max_value if tgreen < 15: tgreen = 15 elif tgreen > 150: tgreen = 150 print("The Tgreen value after applying limits is as given below") print(tgreen) #t=traci.simulation.getCurrentTime() #list_of_max.append(max_value) #print list_of_max #print max(list_of_max) #print help(traci.trafficlights) # print(traci.trafficlights.getIDList()) #traci.trafficlights.setRedYellowGreenState('0','rRGGGG') #traci.trafficlights.setPhaseDuration('0',tgreen) #print traci.trafficlights.getControlledLanes('0') #print "plzzz work" #print traci.trafficlights.getCompleteRedYellowGreenDefinition('0') #print "plzzz work" #print traci.trafficlights.getControlledLinks('0') #print "plzzz work" #print traci.trafficlights.getNextSwitch('0') #print "plzzz work" #print traci.trafficlights.getPhase('0') #print "plzzz work" #print traci.trafficlights.getPhaseDuration('0') #print "plzzz work" #print traci.trafficlights.getProgram('0') #print "plzzz work" #print traci.trafficlights.getRedYellowGreenState('0') #print "plzzz work" #traci.trafficlights.setLinkState('0','E10_0', 'E02_0', ':0_0_0','rRgGyYoOu') #postioning of the dictionary think about it sys.stdout.flush() traci.close()
def main(): POIEdges = { 'Sathorn_Thai_1':['L197#1','L197#2'], 'Sathorn_Thai_2': ['L30', 'L58#1','L58#2'], 'Charoenkrung_1': ['L30032'], 'Charoenkrung_2': ['L60', 'L73', 'L10149#1','L10149#2'], 'Charoenkrung_3': ['L67'], 'Silom_1': ['L138'], 'Silom_2': ['L133.25'], 'Silom_3': ['L49'], 'Mehasak':['L64'], 'Surasak': ['L10130', 'L10189'], 'Charoen_Rat': ['L40'] } percentage = ['1%', '5%', '10%', '15%', '20%', '25%', '30%', '35%', '40%', '45%', '50%','100%'] frequency = [1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60] randomlist = [] dirpath = os.getcwd() path = dirpath + 'RetrieveOnly100%DATAFROMSUMO_RANDOMSEED(One time)-DATASET-WithoutReplicatedVID/random.csv' myfile1 = open(path,'w', newline='') writer1 = csv.writer(myfile1) heading = ["Output File","random seed"] writer1.writerow(heading) myfile1.close() for outputFile in range(0,100): random_number = random.randint(50, 23423) #################### this code block is to keep random seed number permanently#################### random_df = pd.read_csv(path) # print(random_df.columns) randomlist = random_df['random seed'].values.tolist() if len(randomlist) >=0: while random_number in randomlist: random_number = random.randint(50, 23423) #randomlist.append(r) myfile = open(path, 'a', newline='') writer = csv.writer(myfile) with myfile: writer.writerow([(outputFile+1),random_number]) myfile.close() ################################################################################################## print(randomlist) print('Random Seed Number : ', random_number) os.mkdir(dirpath + '/'+ str(outputFile+1)) createFile(POIEdges, percentage, frequency, str(outputFile+1)) # sumoBinary = sumolib.checkBinary('sumo-gui') sumoBinary = sumolib.checkBinary('sumo') sumoCmd = [sumoBinary, "--no-internal-links", 'true', "--ignore-junction-blocker", '1', '--start', 'true', '--quit-on-end', 'true', # "--random",'true', "-c", "sathorn_w.sumo.cfg", # '-a',"sathon_wide_tls_20160418_edited.add(upperSurasak)_withoutLaneclose.xml", '-a', "sathon_wide_tls_20160418_edited.add.xml", '--time-to-teleport', "-1", '--seed', str(random_number), '--no-warnings','true' ] # sumoCmd = [sumoBinary, "-c", "sathorn_w.sumo.cfg",'-a',"sathon_wide_tls_20160418_edited.add.xml",'--time-to-teleport',"-1"] # sumoCmd = [sumoBinary, "-c", "sathorn_w.sumo.cfg"] traci.start(sumoCmd) step = 21600 import time start_time = time.time() while step <= 32400: traci.simulationStep() for freq in frequency: if step % freq == 0: for pcent in percentage: percent = parseFloat(pcent) for road, links in POIEdges.items(): temp = [] vList = [] for link in links: IDs = list(traci.edge.getLastStepVehicleIDs(link)) vList.extend(IDs) # print(vList) temp.append(len(list(traci.edge.getLastStepVehicleIDs(link)))) ###### This code segment has issues about replicated vehicle IDs########## # random_v = [] # g = (r(0, len(vList) - 1) for _ in range(int(len(vList) * (percent)))) # for i in g: # random_v.append(vList[i]) # print(percent,random_v) ########################################################################## random_v = random.sample(vList, int(len(vList) * (percent))) # print(percent,random_v) totalSpeed = 0.0 for v in random_v: totalSpeed += float(traci.vehicle.getSpeed(v)) if (len(random_v)) > 0: meanSpeed = float(totalSpeed / int(len(random_v))) else: meanSpeed = -1.00 format_time = datetime.strptime(getTime(float(step)), '%H:%M:%S') time = format_time.time() if meanSpeed * 3.6 <= 5 and meanSpeed >= 0: low_meanSpeed = 1 else: low_meanSpeed = 0 persistent_low_meanSpeed = 0 myfile = open( dirpath + '/' + str( outputFile + 1) + '/' + road + '_' + str( freq) + '_' + pcent + '.csv', 'a', newline='') writer = csv.writer(myfile) with myfile: writer.writerow( [time, step, *temp, int(len(random_v)), meanSpeed, low_meanSpeed, persistent_low_meanSpeed]) myfile.close() step += 1 traci.close() import time print("--- %s seconds ---" % (time.time() - start_time))
def workerThread(self): try: traci.start([ checkBinary("sumo-gui"), "-c", self.sumocfg, "--lateral-resolution", "0.32", "--collision.action", "warn", "--step-length", str(TS) ]) # steal focus for keyboard input after sumo-gui has loaded # self.master.focus_force() # not working on all platforms # make sure ego vehicle is loaded traci.simulationStep() speed = traci.vehicle.getSpeed(self.egoID) angle = traci.vehicle.getAngle(self.egoID) traci.vehicle.setSpeedMode(self.egoID, 0) steerAngle = 0 x, y = traci.vehicle.getPosition(self.egoID) traci.gui.trackVehicle(traci.gui.DEFAULT_VIEW, self.egoID) while traci.simulation.getMinExpectedNumber() > 0: try: if eventQueue.qsize() == 0: if steerAngle > 0: steerAngle = max(0, steerAngle - 15 * TS) else: steerAngle = min(0, steerAngle + 15 * TS) # print("revert steerAngle=%.2f" % steerAngle) while eventQueue.qsize(): try: msg = eventQueue.get(0) if msg == 'up': speed += TS * traci.vehicle.getAccel( self.egoID) if msg == 'down': speed -= TS * traci.vehicle.getDecel( self.egoID) if msg == 'left': steerAngle -= TS * 5 if msg == 'right': steerAngle += TS * 5 except Queue.Empty: pass # move vehicle speed = max( -5, min(speed, traci.vehicle.getMaxSpeed(self.egoID))) steerAngle = min(15, max(-15, steerAngle)) angle += steerAngle angle = angle % 360 rad = -angle / 180 * math.pi + 0.5 * math.pi x2 = x + math.cos(rad) * TS * speed y2 = y + math.sin(rad) * TS * speed traci.vehicle.setSpeed(self.egoID, speed) traci.vehicle.moveToXY(self.egoID, "dummy", -1, x2, y2, angle, keepRoute=2) x3, y3 = traci.vehicle.getPosition(self.egoID) x, y = x2, y2 traci.simulationStep() if VERBOSE: print( "old=%.2f,%.2f new=%.2f,%.2f found=%.2f,%.2f speed=%.2f steer=%.2f angle=%s rad/pi=%.2f cos=%.2f sin=%.2f" % (x, y, x2, y2, x3, y3, speed, steerAngle, angle, rad / math.pi, math.cos(rad), math.sin(rad))) except traci.TraCIException: pass traci.close() except traci.FatalTraCIError: pass self.running = False
def run(): global ExtensionGiven global LastExtension global Extensions global RedShortenings global TotalExtraGreen global RedShortTrafficLight #Set simulation variables to 0 step = 0 x = 0 RedShort = 0 #This is to check if the current red light has already been shortened Redshortened = { "333": { 3: 0, 6: 0 }, "334": { 3: 0, 6: 0 }, "336": { 0: 0, }, "338": { 3: 0, 6: 0 }, "405": { 0: 0, 3: 0, 9: 0 }, "409": { 0: 0, 3: 0, 9: 0 }, "410": { 3: 0, 6: 0 }, "420": { 3: 0 } } #Gives the minimum phase time. The first key is the intersection number. The second key #in the nested dictionary is the phase number that can be shortened and the value tells us #what the minimum time for that phase is. #The extension key, tells which phase can be extended and by how much it can be extended. The phase #that can be extended is always in the first position(index 0) in the list and the extension time is #in the second position (index 1) MinimumPhaseTime = { "333": { 3: 6, 6: 6, "extension": [0, 30] }, "334": { 6: 15, 3: 7, "extension": [0, 30] }, "336": { 0: 0, "extension": [3, 20] }, "338": { 3: 0, 6: 0, "extension": [0, 30] }, "405": { 0: 15, 3: 6, 9: 8, "extension": [6, 30] }, "409": { 0: 12, 3: 8, 9: 10, "extension": [6, 25] }, "420": { 3: 12, "extension": [0, 30] } } #To check if the sixth phase has been shortened vehicleIDsBefore = { } #This stores all the before the intersection detector values from the current step in the simulation vehicleIDsAfter = { } #This stores all the after the intersection detector values from the current step in the simulation BusPriorityRequested = { } #To check if there is buses in the intersection currently ExtraGreen = { "334": [ 0, 2, 14, 6 ] #[(0= no extra green, 1 = extra green ongoing; 2 extra green allowed to end, 3=extra green shortened),phase when it should continue normaly , "409": [0, 8, 10, 0] # minimum time of extra green, phase that continues] } ExtraGreenRequest = { "334": [5, 0] #[phase where extra green is applied, green phase] , "409": [11, 6] } All_intersections = [ "333", "334", "336", "338", "405", "409", "410", "420" ] #Create the variables for x in All_intersections: BusPriorityRequested[x] = [] ExtensionGiven[x] = 0 #Intersection 334 while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() step += 1 #Retrieve loop data. Sometimes there are detectors for longer distances that needs to be added here. #60m: Remove: 334E_B3; add: 420W_B2,420E_B2, 334_B3 #90m: Remove: Add 420E_B2; Remove: 334E_B3 #120m: Remove: 334E_B3,420E_B2, 334E_B3 #In 150m: Remove: 409W_B2, 420E_B2,420W_B2; add: 334E_B3 #180m: 409W_B2, 420E_B2,420W_B2; add: 334E_B3 vehicleIDsBefore["333"] = traci.inductionloop.getLastStepVehicleIDs( "333E_B1") + traci.inductionloop.getLastStepVehicleIDs( "333E_B2") + traci.inductionloop.getLastStepVehicleIDs( "333E_B3") + traci.inductionloop.getLastStepVehicleIDs( "333W_B1") + traci.inductionloop.getLastStepVehicleIDs( "333W_B2") vehicleIDsAfter["333"] = traci.inductionloop.getLastStepVehicleIDs( "333E_A1") + traci.inductionloop.getLastStepVehicleIDs( "333E_A2") + traci.inductionloop.getLastStepVehicleIDs( "333W_A1") + traci.inductionloop.getLastStepVehicleIDs( "333W_A2") vehicleIDsBefore["334"] = traci.inductionloop.getLastStepVehicleIDs( "334E_B1") + traci.inductionloop.getLastStepVehicleIDs( "334E_B2") + traci.inductionloop.getLastStepVehicleIDs( "334E_B3") + traci.inductionloop.getLastStepVehicleIDs( "334W_B1") + traci.inductionloop.getLastStepVehicleIDs( "334W_B2") vehicleIDsAfter["334"] = traci.inductionloop.getLastStepVehicleIDs( "334E_A1") + traci.inductionloop.getLastStepVehicleIDs( "334E_A2") + traci.inductionloop.getLastStepVehicleIDs( "334W_A1") + traci.inductionloop.getLastStepVehicleIDs( "334W_A2") vehicleIDsBefore["336"] = traci.inductionloop.getLastStepVehicleIDs( "336E_B1") + traci.inductionloop.getLastStepVehicleIDs("336W_B1") vehicleIDsAfter["336"] = traci.inductionloop.getLastStepVehicleIDs( "336E_A1") + traci.inductionloop.getLastStepVehicleIDs("336W_A1") vehicleIDsBefore["338"] = traci.inductionloop.getLastStepVehicleIDs( "338E_B1") + traci.inductionloop.getLastStepVehicleIDs("338W_B1") vehicleIDsAfter["338"] = traci.inductionloop.getLastStepVehicleIDs( "338E_A1") + traci.inductionloop.getLastStepVehicleIDs("338W_A1") vehicleIDsBefore["405"] = traci.inductionloop.getLastStepVehicleIDs( "405E_B1") + traci.inductionloop.getLastStepVehicleIDs( "405W_B1") + traci.inductionloop.getLastStepVehicleIDs( "405E_B2") vehicleIDsAfter["405"] = traci.inductionloop.getLastStepVehicleIDs( "405E_A1") + traci.inductionloop.getLastStepVehicleIDs("405W_A1") vehicleIDsBefore["409"] = traci.inductionloop.getLastStepVehicleIDs( "409E_B1") + traci.inductionloop.getLastStepVehicleIDs( "409W_B1" ) # + traci.inductionloop.getLastStepVehicleIDs("409W_B2") vehicleIDsAfter["409"] = traci.inductionloop.getLastStepVehicleIDs( "409E_A1") + traci.inductionloop.getLastStepVehicleIDs( "409E_A2") + traci.inductionloop.getLastStepVehicleIDs( "409W_A1") + traci.inductionloop.getLastStepVehicleIDs( "409W_A2") vehicleIDsBefore["410"] = traci.inductionloop.getLastStepVehicleIDs( "410E_B1") + traci.inductionloop.getLastStepVehicleIDs("410W_B1") vehicleIDsAfter["410"] = traci.inductionloop.getLastStepVehicleIDs( "410E_A1") + traci.inductionloop.getLastStepVehicleIDs("410W_A1") vehicleIDsBefore["420"] = traci.inductionloop.getLastStepVehicleIDs( "420E_B1" ) + traci.inductionloop.getLastStepVehicleIDs( "420W_B1" ) #+ traci.inductionloop.getLastStepVehicleIDs("420W_B2") + traci.inductionloop.getLastStepVehicleIDs("420E_B2") vehicleIDsAfter["420"] = traci.inductionloop.getLastStepVehicleIDs( "420E_A1") + traci.inductionloop.getLastStepVehicleIDs("420W_A1") #Check if any buses drove past a detector for intersection in vehicleIDsBefore: if "bus" in str(vehicleIDsBefore[intersection]): for vehicle in vehicleIDsBefore[intersection]: if 'bus' in vehicle: if vehicle not in BusPriorityRequested[intersection]: BusPriorityRequested[intersection].append(vehicle) #Extra green for intersection in ExtraGreenRequest: if ExtraGreen[intersection][0] != 0: continue elif len(BusPriorityRequested[intersection]) != 0: if ExtraGreenRequest[intersection][0] == get_phase( intersection ): #Check if the current phase is the phase before the extra green can be implemented if int( traci.trafficlight.getNextSwitch(intersection) - traci.simulation.getTime() ) == 0: #Switches to green phase traci.trafficlight.setPhase( intersection, ExtraGreenRequest[intersection][1]) ExtraGreen[intersection][0] = 1 TotalExtraGreen += 1 #First Checks if there are any busses in the intersections #Checks if the current phase can be shorten and shortens it #The index(get_Phase) of the traffic phases can be seen in netedit or directly in the net-file. for intersection in MinimumPhaseTime: if len(BusPriorityRequested[intersection] ) != 0: #Checks if there is any buses in the intersection for phase in MinimumPhaseTime[intersection]: if phase == get_phase( intersection ) and Redshortened[intersection][ phase] == 0 and intersection != "410": #Checks if the current phase can be shortened and if it has already been shortened RedShorter(intersection, MinimumPhaseTime[intersection][phase]) Redshortened[intersection][phase] = 1 elif phase == "extension" and ExtensionGiven[ intersection] != 1 and intersection != "410": #Check if current intersection is allowed to have extension, check if there is an extension on going and check if it is an extra green. if MinimumPhaseTime[intersection][phase][ 0] == get_phase(intersection): TrafficLightExtension( intersection, MinimumPhaseTime[intersection][phase][1]) #Intersection 410, special case because the extension depends on what direction the bus is going #Did not do for 338 because extending phase 3 would go over the maximum time for turning direction if len(BusPriorityRequested["410"]) != 0: phase = get_phase("410") #Only for busses going east if "bus_W" not in str( BusPriorityRequested["410"]): #Only for busses going east #Shorten current phase if phase == 6 and Redshortened["410"][6] == 0: RedShorter("410", 6) Redshortened["410"][6] = 1 #Extension elif phase == 0 and ExtensionGiven["410"] == 0: TrafficLightExtension("410", 30) elif phase == 3 and ExtensionGiven["410"] == 0: TrafficLightExtension("410", 30) else: #Shorten current phase, even if there is an extension for phase 3, while a bus is incoming from west, #it's not a problem because phase 3 will then be shortened. Happens when bus from both directions if phase == 3 and Redshortened["410"][3] == 0: RedShorter("410", 20) Redshortened["410"][3] = 1 elif phase == 6 and Redshortened["410"][6] == 0: RedShorter("410", 6) Redshortened["410"][6] = 1 #Extension elif phase == 0 and ExtensionGiven["410"] == 0: TrafficLightExtension("410", 30) for intersection in vehicleIDsAfter: if "bus" in str(vehicleIDsAfter[intersection]): for x in vehicleIDsAfter[intersection]: try: BusPriorityRequested[intersection].remove( x ) #Removes bus from priority list, must be try because the bus sometimes triggers the detectors twice except: pass if BusPriorityRequested[ intersection] == []: #Checks that no other buses are in the same intersection at the moment. if intersection in ExtensionGiven: if ExtensionGiven[intersection] == 1: ExtensionGiven[intersection] = 0 traci.trafficlight.setPhaseDuration( intersection, 0) for TrafficPhase in Redshortened[intersection]: if Redshortened[intersection][TrafficPhase] == 1: Redshortened[intersection][TrafficPhase] = 0 RedShortenings += 1 RedShort = 1 if RedShort == 1: RedShortTrafficLight += 1 RedShort = 0 if intersection in ExtraGreen: if ExtraGreen[intersection][0] == 1: ExtraGreen[intersection][0] = 2 #This ends the extra green and keeps track of phases for intersection in ExtraGreen: #Shorten the phase if possible if ExtraGreen[intersection][0] == 2: RedShorter(intersection, ExtraGreen[intersection][2]) ExtraGreen[intersection][0] = 3 #Continue the cycle as it was before the extra green if ExtraGreen[intersection][0] == 3: if get_phase(intersection) == ExtraGreen[intersection][1]: if int( traci.trafficlight.getNextSwitch(intersection) - traci.simulation.getTime()) == 0: traci.trafficlight.setPhase( intersection, ExtraGreen[intersection][3]) ExtraGreen[intersection][0] = 0 traci.close() sys.stdout.flush() print("Extra Greens: ", TotalExtraGreen, "\nRedShortenings: ", RedShortenings, "\nRedShortInTrafficLight: ", RedShortTrafficLight, "\nExtensions: ", Extensions)
def main(): xmldoc = md.parse('../maps/kt2.net.xml') find_junction = xmldoc.getElementsByTagName('junction') find_edge = xmldoc.getElementsByTagName('edge') edge_num = len(find_edge) check = bool count = 0 junc_list, from_node, to_node, edge_list = insertIntoList( find_junction, edge_num, find_edge) rank = len(junc_list) traci.start(sumoCmd) step = 0 end = "e40" weight_list = [] '''while step < 1: for i in range(len(edge_list)): weight = traci.edge.getTraveltime(edge_list[i]) weight_list.append(weight) step += 1 traci.simulationStep()''' while traci.simulation.getMinExpectedNumber() > 0: if step == 70: for i in range(len(edge_list)): weight = traci.edge.getTraveltime(edge_list[i]) weight_list.append(weight) cost_matrix = createMatrix(junc_list, from_node, to_node, weight_list, check) aco = ACO(5, 100, 0.6, 0.4, 0.3, 10) graph = Graph(cost_matrix, rank) path, cost = aco.solve(graph) route = createRoute(path, junc_list) edge = selectEdges(edge_num, find_edge, route, count) traci.route.add("optRoute", edge) traci.vehicle.add('veh0', "optRoute", typeID='DEFAULT_VEHTYPE') traci.vehicle.setColor('veh0', (255, 0, 0)) # traci.vehicle.setRoute('veh0', edge) while traci.simulation.getMinExpectedNumber() > 0: if "veh0" in traci.simulation.getArrivedIDList(): break traci.simulationStep() break step += 1 traci.simulationStep() traci.close() display(cost, route, edge)
def run(env): rou_curr0 = "0to" + str(random.choice([1, 5])) rou_curr1 = "24to" + str(random.choice([19, 23])) rou_curr = [rou_curr0, rou_curr1] env.reset(rou_curr0, rou_curr1) sumo_step = 1.0 cr = [0.0, 0.0] rl_step = 1.0 idle = [np.zeros((25, 1)), np.zeros((25, 1))] global_idl = np.zeros((25, 1)) global_v_idl = [[] for _ in range(25)] v_idle = [[[] for _ in range(25)], [[] for _ in range(25)]] edge = [0, 0] prev_node = env.state curr_node = [0.0, 0.0] temp_n = [0., 0.] temp_p = [0, 24] ga = [] gav = [] ss = [] while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() idle[0] += 1 idle[1] += 1 global_idl += 1 edge[0] = traci.vehicle.getRoadID('veh0') edge[1] = traci.vehicle.getRoadID('veh1') #print('veh edge data: ',edge) for i, ed in enumerate(edge): if ed and (ed[0] != ':'): curr_node[i] = ed.split('to') curr_node[i] = int(curr_node[i][1]) elif ed[0] == ':': curr_node[i] = ed[1:].split('_') curr_node[i] = int(curr_node[i][0]) env.state = curr_node.copy() #print('p_node:',prev_node, 'c_node:',curr_node, 'temp_p: ', temp_p, 'temp_n: ', temp_n) # Action decision on new edge for i in range(2): if prev_node[i] != curr_node[i]: temp_p[i] = prev_node[i] print(':::::::::::::to next node for', i, '::::::::::::::::') print('Veh angle: ', traci.vehicle.getAngle('veh' + str(i))) rou_step = [] glo_reward = env.reward_out(global_idl, prev_node[i], i)[0] prev_reward = env.reward_out(idle[i], prev_node[i], i)[0] print('reward on prev step: ', prev_reward) v_idle[i][int(prev_node[i])].append(prev_reward.copy()) global_v_idl[int(prev_node[i])].append(glo_reward.copy()) avg_v_idl, max_v_idl, sd_v_idl, glo_v_idl, glo_max_v_idl, glo_sd_v_idl, glo_idl, glo_max_idl = eval_met( global_idl, global_v_idl, sumo_step, 25) print('global avg node visit idleness: ', glo_v_idl, '\nglobal max node visit idleness: ', glo_max_v_idl) print('global avg instant idleness: ', glo_idl, '\nglobal max instant idleness: ', glo_max_idl) #print(np.array(v_idle).reshape(5,5)) gav.append(glo_v_idl) ga.append(glo_idl) ss.append(sumo_step) cr[i] += prev_reward #acr=cr/sumo_step #print('acr: ', acr) idle[i][int(prev_node[i])] = 0 global_idl[int(prev_node[i])] = 0 print('agent_', i, 'idleness:\n', idle[i].reshape(5, 5)) print('global idleness:\n', global_idl.reshape(5, 5)) # fa=[[True, True, True, True], [True, True, True, True]] # bool_f, j=forb_action(temp_p, curr_node, temp_n) # if j==0 or j==1: # fa[j]= bool_f # print(fa) action = CR_patrol(idle[i], curr_node[i], env) next_state, reward, action = env.step(action, idle[i], i) temp_n[i] = next_state print('action: ', action, 'next_state: ', next_state, 'reward: ', reward) #print('curr_node after step: ',curr_node, env.state) rou_new = str(curr_node[i]) + 'to' + str(next_state) rou_step.append(rou_curr[i]) rou_step.append(rou_new) print('next_route: ', rou_step) traci.vehicle.setRoute(vehID='veh' + str(i), edgeList=rou_step) rou_curr[i] = rou_new prev_node = curr_node.copy() #print('curr route: ',rou_curr) sumo_step += 1 if sumo_step == 20000: break plt.plot(ss, ga, "-r", linewidth=0.6, label="Global Average Idleness") plt.plot(ss, gav, "-b", linewidth=4, label="Global Average Node Visit Idleness") plt.legend(loc="lower right") up = np.ceil(max(ga) / 10) * 10 plt.yticks(np.linspace(0, up, (up / 10) + 1, endpoint=True)) plt.xlabel('Unit Time') plt.ylabel('Idleness') plt.title('Performance') traci.close() plt.show() sys.stdout.flush()
def simulation(self): lastNumDict = {} #记录上一帧各个检测器的状态 passedNumDict = {} #记录各个检测器累积的车辆数 for k,v in config.junction_detectors.items(): for det in v: passedNumDict[det] = 0 print("passedNumDict = ", passedNumDict) while True: # for vec_id in traci.vehicle.getIDList(): # # #订阅车辆信息:坐标,角度,车辆类型,颜色,速度 # traci.vehicle.subscribe(str(vec_id), # (tc.VAR_POSITION, tc.VAR_ANGLE, tc.VAR_TYPE, tc.VAR_COLOR, tc.VAR_SPEED)) traci.simulationStep() #开始切帧 # loopdata = traci.inductionloop.getInductionLoopStat() # lanedata = traci.lane.getLaneStat() # cross_data = [loopdata, lanedata] # print(cross_data) # result = traci.inductionloop.getLastStepVehicleNumber(self.inductionloopid) # print("getLastStepVehicleNumber = ", result) self.frame+=1 #步长+1 if not self.inductionloopid == '##': temp = traci.inductionloop.getContextSubscriptionResults(self.inductionloopid) #print(temp) if self.frame > 1: for k, v in temp.items(): currStatus = v[16] preStatus = lastNumDict[k][16] if currStatus - preStatus == 1: passedNumDict[k] += 1 if self.frame % int(20 / config.steplength) == 0: ##时长达到1分钟,将数据推出去 passedNumDictDup = copy.deepcopy(passedNumDict) A_queuedata.flow_queue.put(passedNumDictDup) ##将1分钟流量数据,推送到flow队列中,用别的线程进行处理 for k, v in config.junction_detectors.items(): for det in v: passedNumDict[det] = 0 # if not self.lane == '##': # temp1 = traci.lane.getContextSubscriptionResults(self.lane) # print(temp1) #print("更新后的passedNumDict1 = ", passedNumDict) lastNumDict = temp A_queuedata.det_queue.put(temp) #print(currStatus, preStatus) #print(k) #print(passedNumDict) #print("当来车时:", temp) #print(temp) ##此处打印检测器是否检测到车辆的信息 # for vec_id in traci.vehicle.getIDList(): # dic = traci.vehicle.getSubscriptionResults(vec_id) # print(dic) ##这里打印的是之前车辆订阅的信息:坐标,角度,车辆类型,颜色,速度 # if dic: # #print(dic[66]) # x,y = dic[66] # lon, lat = traci.simulation.convertGeo(x,y) #print(lon,lat) q_size = A_queuedata.ryg_queue.qsize() while q_size > 0: print(" 5.4、当前ryg_queue.qsize:", q_size) tlsID, newryg = A_queuedata.ryg_queue.get() traci.trafficlight.setRedYellowGreenState(tlsID, newryg) traci.trafficlight.setPhaseDuration(tlsID,999) print(" 5.5、向sumo仿真系统中设置灯色信息及其持续时间:", (tlsID, newryg)) q_size -= 1 logitTime = self.frame * config.steplength realTime = time.perf_counter() - self.start diff = logitTime - realTime if(diff > 0): time.sleep(diff)
def step(self, action): # define action: # action | meaning # 0 | go straight # 1 | break down # 2 | change left # 3 | change right # 4 | do nothing traci.vehicle.setSpeedMode(self.AgentId, 0) traci.vehicle.setLaneChangeMode(self.AgentId, 0) position = traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_POSITION] if (abs(position[0]) + abs(position[1])) < 999: self.maxLaneNumber = 2 else: self.maxLaneNumber = 1 self.end = 0 reward = 0 DistanceTravelled = 0 if action == 0: maxSpeed = 16 time = (maxSpeed - (traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_SPEED])) / self.AgentAccRate traci.vehicle.slowDown(self.AgentId, maxSpeed, 100 * time) elif action == 1: time = ((traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_SPEED]) - 0) / self.AgentDecRate traci.vehicle.slowDown(self.AgentId, 0, 100 * time) elif action == 2: laneindex = traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_LANE_INDEX] if laneindex < self.maxLaneNumber: traci.vehicle.changeLane(self.AgentId, laneindex + 1, 100) traci.vehicle.setSpeed( self.AgentId, traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_SPEED]) elif action == 3: laneindex = traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_LANE_INDEX] if laneindex > self.minLaneNumber: traci.vehicle.changeLane(self.AgentId, laneindex - 1, 100) traci.vehicle.setSpeed( self.AgentId, traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_SPEED]) elif action == 4: traci.vehicle.setSpeed( self.AgentId, traci.vehicle.getSubscriptionResults( self.AgentId)[tc.VAR_SPEED]) traci.simulationStep() self.VehicleIds = traci.vehicle.getIDList() if self.AgentId in self.VehicleIds: for vehId in self.VehicleIds: traci.vehicle.subscribe(vehId, (tc.VAR_SPEED, tc.VAR_POSITION, tc.VAR_LANE_INDEX, tc.VAR_DISTANCE)) traci.vehicle.subscribeLeader(self.AgentId, 50) Vehicle_Params = traci.vehicle.getSubscriptionResults(self.AgentId) self.AutocarSpeed = Vehicle_Params[tc.VAR_SPEED] posAutox = Vehicle_Params[tc.VAR_POSITION] if math.sqrt((self.end_x - posAutox[0])**2 + (self.end_y - posAutox[1])**2) < 30: self.end = 100 self.state, breaklight, breakstop, wronglane = self.perception() reward = self.cal_reward(self.end, breaklight, breakstop, wronglane) else: #self.state = self.perception() self.end = 1 reward = self.cal_reward(is_collision=self.end, breaklight=0, breakstop=0, wronglane=0) DistanceTravelled = 0 return self.state, reward, self.end, DistanceTravelled
def makeMove(state, action): traci.trafficlight.setPhase("0", action) # agent.simulateFrames(SIM_FRAMES) traci.simulationStep() traci.simulationStep() traci.simulationStep() traci.simulationStep() traci.simulationStep() traci.simulationStep() traci.simulationStep() traci.simulationStep() traci.simulationStep() newState = getState() return newState
def runSimulation(): '''runSimulation(CarsInSimulation) -> none''' while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() traci.close()
def _sumo_step(self): traci.simulationStep() self.get_departed_veh() self.get_travel_time()
def run(self): traci.start( self.sumoCmd ) # Start SUMO. Comment out if running Driver as standalone module. # Run set-up script and acquire list of user defined rules and traffic light agents in simulation userDefinedRules = self.setUpTuple[0] trafficLights = self.setUpTuple[1] rule = None nextRule = None # Assign each traffic light an individual from their agent pool for this simulation run, and a starting rule for tl in trafficLights: tl.assignIndividual() rule = self.applicableUserDefinedRule( tl, userDefinedRules) # Check user-defined rules # If no user-defined rules can be applied, get a rule from Agent Pool if rule == False: validRules = self.getValidRules(tl, tl.getAssignedIndividual()) rule = tl.getNextRule(validRules[0], validRules[1], traci.simulation.getTime() ) # Get a rule from assigned Individual # if no valid rule applicable, apply the Do Nothing rule. if rule == -1: # print("No valid rule. Do Nothing action applied.") tl.doNothing() # Update traffic light's Do Nothing counter tl.getAssignedIndividual().updateFitnessPenalty( False, 0) # Update fitness penalty for individual else: # If rule conditions are satisfied, apply its action. Otherwise, do nothing. if not rule.hasDoNothingAction(): traci.trafficlight.setPhase(tl.getName(), rule.getAction()) else: self.applyUserDefinedRuleAction( tl, traci.trafficlight.getPhaseName(tl.getName()), rule) tl.setCurrentRule(rule) # Set current rule in traffic light # Simulation loop step = 0 # Variables for rule rewards carsWaitingBefore = {} carsWaitingAfter = {} while traci.simulation.getMinExpectedNumber( ) > 0 and traci.simulation.getTime() < self.maxSimulationTime: traci.simulationStep( ) # Advance SUMO simulation one step (1 second) # Traffic Light agents reevaluate their state every 5 seconds if step % 5 == 0: # For every traffic light in simulation, select and evaluate new rule from its agent pool for tl in trafficLights: carsWaitingBefore = tl.getCarsWaiting() carsWaitingAfter = self.carsWaiting(tl) nextRule = self.applicableUserDefinedRule( tl, userDefinedRules ) # Check if a user-defined rule can be applied # If no user-defined rules can be applied, get a rule from Agent Pool if nextRule == False: validRules = self.getValidRules( tl, tl.getAssignedIndividual()) nextRule = tl.getNextRule( validRules[0], validRules[1], traci.simulation.getTime( )) # Get a rule from assigned Individual # if no valid rule applicable, apply the Do Nothing rule. if nextRule == -1: tl.doNothing( ) # Update traffic light's Do Nothing counter tl.getAssignedIndividual().updateFitnessPenalty( False, False) # Update fitness penalty for individual # If next rule is not a user-defined rule, update the weight of the last applied rule else: oldRule = tl.getCurrentRule() # If applied rule isn't user-defined, update its weight if oldRule not in userDefinedRules: if oldRule != -1: ruleWeightBefore = oldRule.getWeight( ) # Used to calculate fitness penalty to individual oldRule.updateWeight( ReinforcementLearner.updatedWeight( oldRule, nextRule, self.getThroughputRatio( self.getThroughput( tl, carsWaitingBefore, carsWaitingAfter), len(carsWaitingBefore)), self.getWaitTimeReducedRatio( self.getThroughputWaitingTime( tl, carsWaitingBefore, carsWaitingAfter), self.getTotalWaitingTime( carsWaitingBefore)), len(carsWaitingAfter) - len(carsWaitingBefore))) tl.getAssignedIndividual( ).updateFitnessPenalty( True, oldRule.getWeight() > ruleWeightBefore) print("Old weight was", ruleWeightBefore, "and new weight is", oldRule.getWeight()) # Apply the next rule; if action is -1 then action is do nothing if not nextRule.hasDoNothingAction(): # print('Next rule action is', nextRule.getAction()) traci.trafficlight.setPhase( tl.getName(), nextRule.getAction()) if nextRule.getType() == 0: print( "Applying TL action from RS! Action is", nextRule.getAction(), "\n\n") else: print( "Applying TL action from RSint! Action is", nextRule.getAction(), "\n\n") else: self.applyUserDefinedRuleAction( tl, traci.trafficlight.getPhaseName(tl.getName()), nextRule) # # print("Applying action of", nextRule.getConditions()) tl.setCurrentRule( nextRule ) # Update the currently applied rule in the traffic light tl.updateCarsWaiting( carsWaitingAfter ) # Set the number of cars waiting count within the TL itself step += 1 # Increment step in line with simulator # Update the fitnesses of the individuals involved in the simulation based on their fitnesses simRunTime = traci.simulation.getTime() print("***SIMULATION TIME:", simRunTime, "\n\n") for tl in trafficLights: # # print(tl.getName(), "has these communicated intentions:", tl.getCommunicatedIntentions()) i = tl.getAssignedIndividual() i.updateLastRunTime(simRunTime) print("Individual", i, "has a last runtime of", i.getLastRunTime()) i.updateFitness( EvolutionaryLearner.rFit(i, simRunTime, i.getAggregateVehicleWaitTime())) traci.close() # End simulation return self.setUpTuple[ 2] # Returns all the agent pools to the main module
def learnDiscretization(daysToTrain): # """ SIMULATION """ dynamic = 0 day = 0 totalDays = daysToTrain # learning rates and discount factors gamma = 0.95 # epsilons = [1, 0.99, 0.97, 0.93, 0.91, 0.89, 0.80, 0.75, 0.70, 0.6, 0.5, 0.45, 0.3, 0.20, 0.18, 0.17, 0.15, 0.10, 0.08, 0.05, 0.04, 0.02, 0.01, 0.01] # print 'len(epsilons) = ', len(epsilons) for day in range(totalDays): # range(len(epsilons)+1): #len(alphas) # generate the random route schedule for the day arrivalRateGen.writeRoutes(day + 1) sumoProcess = subprocess.Popen(['sumo-gui.exe', "-c", "palm.sumocfg", \ "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr) # sumoProcess = subprocess.Popen(['sumo.exe', "-c", "palm.sumocfg", "--fcd-output", \ # "out.fcd.xml", "--tripinfo-output", "out.trip.xml", "--summary", "out.summary.xml", "--queue-output", "out.queue.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr) traci.init(PORT) dfObjVals = pd.DataFrame() dfQueueTracker = pd.DataFrame() dfWaitingTracker = pd.DataFrame() action = 0 # number of seconds over minimum that we decided to take in 5 buckets (0,1,2,4) lastAction = 0 hod = 0 currSod = 0 epsilon = 1 # TODO - change epsilon dynamically? currPhaseID = 0 secsThisPhase = 0 while currSod < secondsInDay: if currPhaseID == int(traci.trafficlights.getPhase( SL)) and currSod != 0: # if phase HAS NOT changed secsThisPhase += 1 # increase the seconds in the currentPhase else: # IF THE PHASE HAS CHANGED secsThisPhase = 0 currPhaseID = int(traci.trafficlights.getPhase(SL)) # STORE INFORMATION TO DETERMINE IF ITS TIME TO MAKE A DECISION # ARRAY TO MAP STATE: # (2) Hour of day (24) # (1) Light phase for decision (4) getPhase # (2) Num stopped cars X 4 getLastStepHaltingNumber # (3) Num vehicles in lane getLastStepVehicleNumber # (4) Cum waiting time x 4 getWaitingTime # (5) Last step mean speed X 4 getLastStepMeanSpeed if currPhaseID % 2 == 0 and secsThisPhase == 0: # only collecting data when we come to the end of a yellow phase #============ HOD if hod != currSod / secondsInHour: hod = int(currSod / secondsInHour) print 'observation day = ', day print 'hod = ', hod # print 'len(stateData[h][1]) = ', len(stateData[hod][1]) # print 'int(sum(np.std(stateData[h][a], axis = 0))) = ', int(sum(np.std(stateData[hod][1], axis = 0))) # print 'len(stateData[h][3]) = ', len(stateData[hod][3]) # print 'int(sum(np.std(stateData[h][a], axis = 0))) = ', int(sum(np.std(stateData[hod][3], axis = 0))) # print 'len(stateData[h][5]) = ', len(stateData[hod][5]) # print 'int(sum(np.std(stateData[h][a], axis = 0))) = ', int(sum(np.std(stateData[hod][5], axis = 0))) # print 'len(stateData[h][7]) = ', len(stateData[hod][7]) # print 'int(sum(np.std(stateData[h][a], axis = 0))) = ', int(sum(np.std(stateData[hod][7], axis = 0))) #============ currPhaseID #================= count halted vehicles (4 elements) for lane in listLanes: laneQueueTracker[ lane] = traci.lane.getLastStepHaltingNumber(str(lane)) # laneQueueTracker[lane] = traci.lane.getLastStepVehicleNumber(str(lane)) for edge in queueTracker.keys(): queueTracker[edge] = laneQueueTracker[ str(edge) + '_' + str(0)] + laneQueueTracker[str(edge) + '_' + str(1)] # inherently, we assume balancing here # TODO - later chage this to only track # df = pd.DataFrame([[currSod, queueTracker['8949170'], queueTracker['-164126513'], queueTracker['52016249'], queueTracker['-164126511']]]) # dfQueueTracker = dfQueueTracker.append(df, ignore_index = True) # ================ count vehicles in lane for lane in listLanes: laneNumVehiclesTracker[ lane] = traci.lane.getLastStepVehicleNumber(str(lane)) for edge in numVehiclesTracker.keys(): numVehiclesTracker[edge] = laneNumVehiclesTracker[ str(edge) + '_' + str(0)] + laneNumVehiclesTracker[str(edge) + '_' + str(1)] # ================ cum waiting time in minutes for lane in listLanes: laneWaitingTracker[lane] = traci.lane.getWaitingTime( str(lane)) / 60 for edge in waitingTracker.keys(): waitingTracker[edge] = laneWaitingTracker[ str(edge) + '_' + str(0)] + laneWaitingTracker[str(edge) + '_' + str(1)] # df = pd.DataFrame([[currSod, waitingTracker['8949170'], waitingTracker['-164126513'], waitingTracker['52016249'], waitingTracker['-164126511']]]) # dfWaitingTracker = dfWaitingTracker.append(df, ignore_index = True) # ================ mean speed # for lane in listLanes: # laneMeanSpeedTracker[lane] = traci.lane.getLastStepMeanSpeed(str(lane)) # for edge in meanSpeedTracker.keys(): # meanSpeedTracker[edge] = (laneMeanSpeedTracker[str(edge) + '_' + str(0)] + laneMeanSpeedTracker[str(edge) + '_' + str(1)])/2 # ============== CREATE A NEW ENTRY FOR OUR STATE TRACKER stateDataEntry = [] for edge in listEdges: stateDataEntry.append(queueTracker[edge]) for edge in listEdges: stateDataEntry.append(numVehiclesTracker[edge]) for edge in listEdges: stateDataEntry.append(waitingTracker[edge]) # for edge in listEdges: # stateDataEntry.append(meanSpeedTracker[edge]) if len(stateData[hod][currPhaseID]) == 0: stateData[hod][currPhaseID] = np.array(stateDataEntry) else: stateData[hod][currPhaseID] = np.vstack( [stateData[hod][currPhaseID], stateDataEntry]) # TRACK OBJECTIVE FUNCTION currObjValue = computeObjValue(queueTracker, waitingTracker) df = pd.DataFrame([[ currSod, currObjValue ]]) # todo - fix so plot shows the second of the day dfObjVals = dfObjVals.append(df, ignore_index=True) # print 'currPhaseID = ', currPhaseID # print 'secsThisPhase = ', secsThisPhase # print 'currSod = ', currSod # print 'hod = ', hod # print 'queueTracker = ', queueTracker # print 'waitingTracker = ', waitingTracker # # we can make a decision # if currPhaseID%2 == 0 and secsThisPhase%secsPerInterval == 0 and secsThisPhase >=4: # and currSod > 20000: # # print 'currPhaseID = ', currPhaseID # # print 'secsThisPhase = ', secsThisPhase # # arrayObjVals = np.append(arrayObjVals, currObjValue) # # arrayQueueSizes = np.append(arrayQueueSizes, queueTracker.values()) # # if day > 0: # # dynamic = 1 # if dynamic: # # CONTROL ACTION # phaseNum = traci.trafficlights.getPhase(SL) # currState = assignStateNum(phaseNum, queueTracker, waitingTracker, queueBuckets, waitingBuckets) # # reward = objective value; we want it to be as close to zero as possible (will always be negative) # reward = currObjValue - lastObjValue # lastObjValue = currObjValue # updateQValues(int(lastState), int(lastAction), int(currState), reward, alpha) # alpha controls whether we explore or exploit # updateQProbs(int(lastState), int(lastAction), epsilon) # # pick action # unigen = random.random() # if QProbs[currState,0] < unigen or secsThisPhase == 0: # action = 0 # stay in this phase # else: # action = 1 # change phases; transition to the next yellow phase immediately # # TODO - totally change the actions to be the number of seconds (# of 4-second time chunks we are choosing for the cycle) # traci.trafficlights.setPhase(SL, (int(currPhaseID) + 1)%8) # #TODO - tell the light how long to run for; not just whether or not to change # # print 'int(traci.trafficlights.getPhase(SL)) = ', int(traci.trafficlights.getPhase(SL)) # df = pd.DataFrame([[currSod, secsThisPhase, currPhaseID, currState, lastState, action, currObjValue, lastObjValue, reward]]) # todo - fix so plot shows the second of the day # dfActions = dfActions.append(df, ignore_index=True) # lastState = currState # lastAction = action currSod += 1 # print traci.vehicle.getIDList() traci.simulationStep() # print stateData # totalSize = 0 # for i in actionPhases: # totalSize += len(stateData[0][i]) # print 'totalSize = ', totalSize # for i in actionPhases: # print 'np.std(stateData[0][i], axis = 0) = ', np.std(stateData[0][i], axis = 0) # print 'sumvariability = ', sum(np.std(stateData[0][i], axis = 0)) # print 'np.std(stateData[0][i], axis = 0) = ', np.std(stateData[0][i], axis = 0) # print 'sumstd = ', sum(np.std(stateData[0][i], axis = 0)) # totalSize += len(stateData[0][i]) traci.close( ) # TODO - fix; need to figure out how to plot multiple with different x-axes print 'dfObjVals = ', dfObjVals dfMean = dfObjVals.mean(axis=0) meanObjVal = dfMean[1] dfMedian = dfObjVals.median(axis=0) medianObjVal = dfMedian[1] # vMedian = dfMedian[1] dfMin = dfObjVals.min(axis=0) minObjVal = dfMin[1] listMeanObjVals.append(meanObjVal) listMedianObjVals.append(medianObjVal) listMinObjVals.append(minObjVal) for h in range(hoursInDay): #TODo - change to hoursInDay for a in actionPhases: numClustersTracker[h][a] = int(sum(np.std(stateData[h][a], axis=0))) # print 'h = ', h print 'a = ', a print 'numClustersTracker[h][a] = ', numClustersTracker[h][a] dictClusterObjects[h][a] = KMeans( n_clusters=numClustersTracker[h][a]) dictClusterObjects[h][a].fit(stateData[h][a]) # result = dictClusterObjects[0][a].predict(stateData[0][a]) # print 'result = ', result # print 'max(result) = ', max(result) # print 'hod = ', hod # print 'dictClusterObjects = ', dictClusterObjects print 'numClustersTracker = ', numClustersTracker totalClusters = 0 for h in range(hoursInDay): #TODO - change to hoursInDay for a in actionPhases: totalClusters += numClustersTracker[h][a] print 'totalClusters = ', totalClusters stateCounter = 0 for h in range(hoursInDay): #TODO - change to hoursInDay mapDiscreteStates[h] = {} for a in actionPhases: mapDiscreteStates[h][a] = {} for c in range(numClustersTracker[h][a]): mapDiscreteStates[h][a][c] = stateCounter stateCounter += 1 print 'stateCounter = ', stateCounter
def runQLearning(): stateVect = [[]] policyPi = [] for i in range(0, 80, 1): policyPi.insert(i, random.randint(0, 5)) QSA = np.random.rand(81, 6) # Returns = [[0]*6 for x in range(81)] currTLS = [] Actions = [20, 25, 30, 20, 25, 30] # Actions = [30, 60, 120, 30, 60, 120] totTime = [] avgTime = [] numVehs = [] avgNumVehs = [] EPSILON = 0.20 ALPHA = 0.05 GAMMA = 0.9 step = 0 stateNorth = 0 stateSouth = 0 stateEast = 0 stateWest = 0 currAction = 0 reward = 0 totWaittimeSoFar = 0 curWaittime = 0 totVehsSoFar = 0 currVehsHere = 0 while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() stateNorth = traci.lane.getLastStepOccupancy('nc_0') stateSouth = traci.lane.getLastStepOccupancy('sc_0') stateEast = traci.lane.getLastStepOccupancy('ec_0') stateWest = traci.lane.getLastStepOccupancy('wc_0') tlsPhase = traci.trafficlight.getPhase("center") stateVect.insert(step, [ getState(stateNorth), getState(stateEast), getState(stateSouth), getState(stateWest), getPhase(tlsPhase), 0, 0, 0 ]) index = getStateIndex(stateVect[step]) reward = getStateReturn(stateVect[step]) currAction = policyPi[index] stateVect[step][5] = index stateVect[step][6] = currAction stateVect[step][7] = reward if currAction > 2: phaseToChange = 2 else: phaseToChange = 0 currTLS = traci.trafficlight.getCompleteRedYellowGreenDefinition( "center") currTLS[0].phases[phaseToChange].duration = Actions[currAction] currTLS[0].phases[phaseToChange].minDur = Actions[currAction] currTLS[0].phases[phaseToChange].maxDur = Actions[currAction] traci.trafficlight.setCompleteRedYellowGreenDefinition( "center", currTLS[0]) if step > 0 and step % 850 == 0: episodeStates = [[]] index = 0 A = 0 R = 0 for t in range(0, 849, 1): episodeStates.insert(t, stateVect[step - 849 + t]) for t in range(0, 848, 1): if random.uniform(0, 1) < EPSILON: policyPi[index] = random.randint(0, 5) else: policyPi[index] = np.argmax(QSA[index]) S = episodeStates[t] R = S[7] A = S[6] index = S[5] Sprime = episodeStates[t + 1] # print(Sprime) indexPrime = Sprime[5] QSA[index][A] = QSA[index][A] + ALPHA * ( R + (GAMMA * max(QSA[indexPrime])) - QSA[index][A]) step += 1 curWaittime = traci.edge.getWaitingTime( 'nc') + traci.edge.getWaitingTime( 'ec') + traci.edge.getWaitingTime( 'sc') + traci.edge.getWaitingTime('wc') totWaittimeSoFar = totWaittimeSoFar + curWaittime totTime.append(totWaittimeSoFar) avgTime.append(totWaittimeSoFar / step) currVehsHere = traci.simulation.getArrivedNumber() totVehsSoFar = totVehsSoFar + currVehsHere numVehs.append(totVehsSoFar) avgNumVehs.append(totVehsSoFar / (step)) traci.close() sys.stdout.flush() return step, totTime, avgTime, numVehs, avgNumVehs
def dqnlearn(num_timesteps, num_simulations, *args, **kwargs): yellowTime = 3 # I have Warning: Vehicle '3.527' performs emergency stop at the end of lane for 3 redTime = 0 itBetweenDecision = int( (yellowTime + redTime + 3) / stepLength ) # each itteration is stepLength s, I need at least 3s of green time lightList = traci.trafficlight.getIDList() #obsLen = 2*4*3*2 # max of in and out lanes, 4 directions, 3 lanes per edges, 2 information per lane alg = [] lanes = [] lanesEdge = [] OutLanes = [] OutLanesEdge = [] rewardsLength = 0 for i, tl in enumerate(lightList): phases = traci.trafficlight.getCompleteRedYellowGreenDefinition( tl)[0].getPhases() num_phases = len( phases ) // 2 # The tl have 4 or 6 phases in their base program, so so it can be 2 or 3 ph = [] for phase in phases[::2]: # skip yellow ph.append(phase._phaseDef) print('phase', ph) outlane = set() #outlanedic = {} for link in traci.trafficlight.getControlledLinks(tl): #print('link', link[0][0]) # incoming lane #print('link', link[0][1]) # outgoing lane outlane.add(link[0][1]) # outgoing lane #print('link', link[0][2]) # junction lane #print() #outlanedic[link[0][0]] = link[0][1] # I want to ensure it's the straight path, not the left or right turning one though, so I will use another way OutLanes.append(outlane) # get number of edges and number of lanes per edges # they are sorted by their order in the phase # To be coherent: obs must be sorted in the same order as hidden and # output. It works for obs since lanes are sorted in the same order as # phase. Now, we need to ensure it works also for hidden state (since it is sent) lanes.append(traci.trafficlight.getControlledLanes( tl)) # also needed to get neighbors #print('lanes', lanes[-1]) lanesEdge.append([]) edgeList = [None] oldlane = None OutLanesEdge.append([]) for lane in lanes[-1]: # 1st dim: tl, 2nd dim: one for each lane if lane == oldlane: continue oldlane = lane if traci.lane.getEdgeID(lane) == edgeList[-1]: lanesEdge[-1][-1].append(lane) #OutLanesEdge[-1].append(outlanedic[lane]) #print(lane, outlanedic[lane]) else: lanesEdge[-1].append([lane]) #OutLanesEdge[-1].append([outlanedic[lane]]) #print('new edge',lane, outlanedic[lane]) edgeList.append(traci.lane.getEdgeID(lane)) for iedge, edge in enumerate( edgeList[1:] ): # get the 2 lanes that are before the junction where the road goes from 2 to 3 lanes (will need to be change for different road network) for ilane in range(2): lanesEdge[-1][iedge].append( edge[:-1] + '_' + str(ilane)) # I wrote that for this network. Not general edgeDic = {} for lane in outlane: # it's a set so no repeat even though they aren't in the same order edge = traci.lane.getEdgeID(lane) if edge in edgeDic: OutLanesEdge[-1][edgeDic[edge]].append(lane) #print('old edge',lane, edgeDic[edge], edge) else: OutLanesEdge[-1].append([lane]) edgeDic[edge] = len(OutLanesEdge[-1]) - 1 #print('new edge',lane, edgeDic[edge], edge) print(lanesEdge[-1]) print(OutLanesEdge[-1]) #return None # stop here to look at the lanes obs, rw = getobs( tl, num_phases, lanesEdge[-1] ) #, OutLanesEdge[-1]) # first dim of lanesEdge is tl, 2nd dim is edge (direction), value is lane (3rd dim) alg.append( dqn.QLearner(num_phases=num_phases, observation_shape=len(obs), scope=str(i), phases=ph, timeBetweenDecision=itBetweenDecision * stepLength, *args, **kwargs)) alg[i].last_obs = obs * itBetweenDecision # because we will divide by that afterwards # neighTL = [] # I would like to know for sure which one is left and which one is right. Maybe use lanesEdge # # Comparing lanes in common doesn't work, because only incoming lanes are in the list # for i, tl in enumerate(lightList): # neigh = np.array([-1,-1]) # only 2 neighbors in this map # for j, tl2 in enumerate(lightList): # if i != j: # # if i == 0 and j == 4: # # print(lanes[i]) # # print(OutLanes[j]) # # print(lanes[i][:len(lanes[i])//2]) # # print(lanes[i][len(lanes[i])//2:]) # # print(set(lanes[i][:len(lanes[i])//2]).intersection(OutLanes[j])) # # print(set(lanes[i][len(lanes[i])//2:]).intersection(OutLanes[j])) # # print() # if len(set(lanes[i][:len(lanes[i])//2]).intersection(OutLanes[j]))>0: # neigh[0] = j # elif len(set(lanes[i][len(lanes[i])//2:]).intersection(OutLanes[j]))>0: # neigh[1] = j # #print('neighbor tl', i, neigh, tl) # neighTL.append(neigh) # 1st dim of neighTL is tl, value is the neighbor tl (2nd dim) # # 0,4,5,1,3,2 # 0 didn't get 4 # neighTL[0][1] = 4 # correct manually. Due to edge expanding from 3 lanes to 4 lanes # # I will need a better way on another map saver = tf.train.Saver() # create saver #saveFrequency = 10000 saveFile = r"/model/A.ckpt" if False: # put True here if I want to restore saver.restore(kwargs['session'], saveFile) print("Model restored.") for i in range(len(alg)): alg[i].model_initialized = True # I believe it is initialized if it is restored episode_rewards = [] action = np.empty_like( alg, int) # Need to save all actions and then use them to learn each q reward = np.empty((len(alg), 2), float) # now I have 2 rewards # hid = np.empty((len(alg),kwargs['num_hidden']),float) # Hs = np.empty((len(alg),2*kwargs['num_hidden']),float) # 2 because each tl has 2 neighbors #it = 0 for isim in range(num_simulations): for it in range(num_timesteps): for q in alg: q.t += 1 # upgrade timestep here #print(it, traci.simulation.getTime()) #if (it+1) % saveFrequency == 0: # save model # Don't save for now # saver.save(kwargs['session'], saveFile) # print("Model saved.") if traci.simulation.getMinExpectedNumber() <= 0: print('Error: No more cars') break if it % itBetweenDecision == 0: if it > 0: #not first time episode_rewards.append(reward / itBetweenDecision) done = it > num_timesteps - itBetweenDecision # done if it's the last action for i, tl in enumerate(lightList): if it > 0: #not first time #info = None #print('reward',reward[i], np.sum(reward[i])) alg[i].replay_buffer.store_effect( idx, action[i], np.sum(reward[i]), done) #alg[i].replay_buffer.store_effect(idx, action[i], reward[i], done, Hs[i]) alg[i].update_model() lastObs = alg[i].last_obs / itBetweenDecision #idx = alg[i].replay_buffer.store_frame(lastObs, 2*kwargs['num_hidden']) idx = alg[i].replay_buffer.store_frame(lastObs) #lastObs = alg[i].replay_buffer.encode_recent_observation() # No need to encode, I'm not using context if not alg[i].model_initialized: alg[i].model_initialized = True initialize_interdependent_variables( alg[i].session, tf.global_variables(), { alg[i].obs_t_ph: lastObs[np.newaxis, :], }) #hid[i] = alg[i].session.run(alg[i].hid, feed_dict={alg[i].obs_t_ph:lastObs[np.newaxis,:]}) alg[0].log_progress(episode_rewards, it) # need to log only once. if done: print('done') continue # no need to finish if it's done. it will be less than itBetweenDecision iterations # Choose action (with epsilon greedy) for i, tl in enumerate(lightList): # h = [] # for ntl in neighTL[i]: # if ntl != -1: # h.append(hid[ntl,:]) # else: # h.append(np.zeros((kwargs['num_hidden'],))) # h = np.concatenate(h,axis=0) # Hs[i] = h if random.random() > alg[i].exploration.value(alg[i].t): #q = alg[i].session.run(alg[i].q, feed_dict={alg[i].obs_t_ph:lastObs[np.newaxis,:],alg[i].tl_ph:h[np.newaxis,:]}) q = alg[i].session.run(alg[i].q, feed_dict={ alg[i].obs_t_ph: lastObs[np.newaxis, :] }) action[i] = np.argmax(q, axis=1) #print('chosen action', q, action[i]) else: action[i] = alg[i].randomAction( ) #random.randrange(alg[i].num_phases) # random integer #print('random action', action[i], alg[i].num_phases) alg[i].updatePhase(action[i]) current_phase = traci.trafficlight.getRedYellowGreenState( tl) newphase = alg[i].phases[action[i]] #print(i, current_phase, newphase) if newphase != current_phase: # not same phase traci.trafficlight.setPhaseDuration( tl, yellowTime ) # trying to force switch by putting duration to 0. Actually, 0 made it swtich to red, so let's try yellowtime #program = traci.trafficlight.Logic(programID, type, currentPhaseIndex, phases=None, subParameter=None) yellowphase = list(current_phase) #redphase = list(current_phase) for ichar, char in enumerate(current_phase): if newphase[ichar] == 'r' and char != 'r': yellowphase[ichar] = 'y' #redphase[ichar] = 'r' yellowphase = ''.join(yellowphase) #redphase = ''.join(redphase) # Says duration are in miliseconds, but that was in 2012 they might have changed it phases = [ traci.trafficlight.Phase(phaseDef=yellowphase, duration=yellowTime, duration1=yellowTime, duration2=yellowTime), #traci.trafficlight.Phase(phaseDef=redphase,duration=redTime,duration1=redTime,duration2=redTime), traci.trafficlight.Phase(phaseDef=newphase, duration=10000, duration1=10000, duration2=10000) ] program = traci.trafficlight.Logic(phases=phases, subID='0', type=0, subParameter=0, currentPhaseIndex=0) traci.trafficlight.setCompleteRedYellowGreenDefinition( tl, program) #print(traci.trafficlight.getRedYellowGreenState(tl)) traci.simulationStep() for i, tl in enumerate(lightList): #print(' ', i, traci.trafficlight.getRedYellowGreenState(tl), traci.trafficlight.getNextSwitch(tl), traci.trafficlight.getPhaseDuration(tl)) obs, rw = getobs(tl, alg[i].num_phases, lanesEdge[i]) #,OutLanesEdge[i]) #print('obs', obs) #print('rw', rw) if it % itBetweenDecision == 0: alg[i].last_obs = obs reward[i] = rw else: alg[i].last_obs += obs reward[i] += rw traci.close() saver.save(kwargs['session'], saveFile) print("Model saved.") print('End of one simulation', 'iteration this time', len(episode_rewards) - rewardsLength, 'mean reward', np.mean(episode_rewards[rewardsLength:], axis=0)) rewardsLength = len(episode_rewards) traci.start([ r"D:\InstalledProgram\SUMO\bin\sumo.exe", "-c", "conf2.sumocfg", "--tripinfo-output", "tripinfo" + str(isim) + ".xml", "--no-step-log", "--time-to-teleport", "-1" ])