def reset(self): # Reset simulation with the random seed randomly selected the pool. if self.demonstration == False: self.sumoBinary = "sumo" seed = self.seed()[1] traci.start([ self.sumoBinary, '-c', self.projectFile + 'ramp.sumo.cfg', '--start', '--seed', str(seed), '--quit-on-end' ], label='training') self.scenario = traci.getConnection('training') else: self.sumoBinary = "sumo-gui" seed = self.seed()[1] traci.start([ self.sumoBinary, '-c', self.projectFile + 'ramp.sumo.cfg', '--start', '--seed', str(seed), '--quit-on-end' ], label='demonstration') self.scenario = traci.getConnection('evaluation') self.warm_up_simulation() self.run_step = 0 return self.update_observation()
def _configure_environment(self): if self.GUI: sumoBinary = set_sumo_home.sumoBinaryGUI else: sumoBinary = set_sumo_home.sumoBinary cfgfile = module_path + "/assets/tl.sumocfg" if self.Generate: ODrandomizer.randomize() cfgfile = module_path + "/assets/tl_auto.sumocfg" self.argslist = [ sumoBinary, "-c", cfgfile, "--collision.action", "remove", "--step-length", str(self.SUMOSTEP), "-S", "--time-to-teleport", "-1", "--collision.mingap-factor", "0", "--collision.check-junctions", "true", "--no-step-log", "--device.rerouting.with-taz", "true" ] if self.Play: #self.argslist.append("--game") self.argslist.append("--window-size") self.argslist.append("1000,1000") # if self.GUI: # self.arglist.append("--gui-settings-file") # self.arglist.append(module_path+"/assets/viewsettings.xml") traci.start(self.argslist, label=self.runcode) self.conn = traci.getConnection(self.runcode) time.sleep(5) # Wait for server to startup
def _start_simulation(self): sumo_cmd = [self._sumo_binary, '-n', self._net, '-r', self._route, '--max-depart-delay', str(self.max_depart_delay), '--waiting-time-memory', '10000', '--time-to-teleport', str(self.time_to_teleport)] if self.begin_time > 0: sumo_cmd.append('-b {}'.format(self.begin_time)) if self.sumo_seed == 'random': sumo_cmd.append('--random') else: sumo_cmd.extend(['--seed', str(self.sumo_seed)]) if not self.sumo_warnings: sumo_cmd.append('--no-warnings') if self.use_gui: sumo_cmd.extend(['--start', '--quit-on-end']) if self.virtual_display is not None: sumo_cmd.extend(['--window-size', f'{self.virtual_display[0]},{self.virtual_display[1]}']) from pyvirtualdisplay.smartdisplay import SmartDisplay print("Creating a virtual display.") self.disp = SmartDisplay(size=self.virtual_display) self.disp.start() print("Virtual display started.") if LIBSUMO: traci.start(sumo_cmd) self.sumo = traci else: traci.start(sumo_cmd, label=self.label) self.sumo = traci.getConnection(self.label) if self.use_gui: self.sumo.gui.setSchema(traci.gui.DEFAULT_VIEW, "real world")
def start_simulation(self, parent_dir=None, output_type='--tripinfo-output', eval_label='tripinfo.xml'): """Opens a connection to sumo/traci [with or without GUI] and updates obs atribute (the current state of the environment). """ tools.generate_routefile(route_file_dir=self.route, demand=self.demand, network=self.network) sumo_cmd = [ self.sumo_binary, '-n', self.net, '-r', self.route, '--time-to-teleport', '-1', '--device.emissions.probability', '1.0' ] if parent_dir: sumo_cmd.append('--tripinfo-output') sumo_cmd.append(os.path.join(parent_dir, eval_label)) traci.start(sumo_cmd, label=self.connection_label) # print('Started connection for worker #', self.connection_label) self.connection = traci.getConnection(self.connection_label) self.state.update_state(connection=self.connection) self.warm_up_net(WARM_UP_NET) self.state.update_state(connection=self.connection)
def __init__(self, label, sumocfg, state_size, max_steps, agent, gui=False): self.sumocfg = sumocfg self.state_size = state_size self.agent = agent self.max_steps = max_steps # stats self.cumulative_reward = 0 self.cumulative_waiting_time = 0 self.throughput = 0 self.cumulative_intersection_queue = 0 # tls state self.yellow_phase = False self.green_phase = False self.yellow_phase_step_count = 0 self.green_phase_step_count = 0 # action self.action = None self.previous_action = None self.label = label self.junction_id = 'TL' # Control SUMO mode (with or without GUI) if gui: sumo_binary = checkBinary('sumo-gui') else: sumo_binary = checkBinary('sumo') # Start SUMO with TraCI and some flags traci.start([sumo_binary, "-c", self.sumocfg, "--no-step-log", "true"], label=self.label) self.connection = traci.getConnection(self.label) # The following code retrieves all vehicle speeds and waiting times within range (1000m) of a junction (the vehicle ids are retrieved implicitly). (The values retrieved are always the ones from the last time step, it is not possible to retrieve older values.) # add tc.VAR_ACCUMULATED_WAITING_TIME, tc.VAR_POSITION if more than one tl self.connection.junction.subscribeContext( self.junction_id, tc.CMD_GET_VEHICLE_VARIABLE, 1000, [ tc.VAR_SPEED, tc.VAR_LANEPOSITION, tc.VAR_LANE_ID, tc.VAR_WAITING_TIME ]) # VAR_LANEPOSITION = 86 # VAR_LANE_ID = 81 # VAR_WAITING_TIME = 122 # VAR_ARRIVED_VEHICLES_NUMBER = 121 # VAR_SPEED = 64 self.states = self._get_state(self.junction_id)
def reset(self, label=None, eval_seed=None): # Reset simulation with the random seed randomly selected the pool. if self.evaluation: self.sumoBinary = "sumo" seed = eval_seed traci.start([self.sumoBinary, '-c', self.projectFile + 'ramp.sumo.cfg', '--start',\ '--seed', str(seed), '--netstate-dump', self.projectFile + \ 'Output/Traj/traj{}.xml'.format(label), \ '--emergencydecel.warning-threshold', '1.2', '--quit-on-end'], label='evaluation') self.scenario = traci.getConnection('evaluation') else: self.sumoBinary = "sumo" seeds = self.seed()[1] traci.start([self.sumoBinary, '-c', self.projectFile + 'ramp.sumo.cfg','--start','--seed',\ str(seeds), '--emergencydecel.warning-threshold', '1.2', '--quit-on-end'], label='training') self.scenario = traci.getConnection('training') self.warm_up_simulation() obs = self.get_state() self.run_step = 0 return obs
def reset(self): r"""Reset the environment state and returns an initial observation. Returns: observation (object): The initial observation for the new episode after reset. """ traci.start(self._sumo_cmd) # Reinitialize the collaborator since the connection changed self.collaborator = Collaborator(traci.getConnection(), self.trafficlight_skeletons, self.additional) return self.collaborator.compute_observations()
def _open_connection(self): self._generate_routefile() sumoB = checkBinary('sumo' if not self.vis else 'sumo-gui') # Need to edit .sumocfg to have the right route file self._generate_configfile() traci.start([ sumoB, "-c", "data/{}_{}.sumocfg".format(self.env_name, self.agent_ID) ], label=self.conn_label) self.connection = traci.getConnection(self.conn_label)
def run_fixed(self, parent_dir, eval_label): """ Evaluate network with previously set fixed policy in .net file. """ if self.network == "simple": fixed = os.path.join( os.path.split(self.net)[0], "simple_cross_no_RL.net.xml") if self.network == "complex": fixed = os.path.join( os.path.split(self.net)[0], "complex_cross_no_RL.net.xml") sumo_cmd = [ self.sumo_binary, '-n', fixed, '-r', self.route, '--time-to-teleport', '-1', '--device.emissions.probability', '1.0' ] if parent_dir: sumo_cmd.append('--tripinfo-output') sumo_cmd.append(os.path.join(parent_dir, eval_label)) label = str(self.connection_label) + "_fixed" traci.start(sumo_cmd, label=label) fixed_con = traci.getConnection(label) t = 0 done = False reward = 0 while not done: wt = self.compute_waiting_time() t += self.time_step fixed_con.simulationStep(float(t)) for lane in self.input_lanes: self.state.compute_time_in_lane(fixed_con, lane) wt_next = self.compute_waiting_time() reward += self.compute_reward(wt, wt_next) done = fixed_con.simulation.getMinExpectedNumber() == 0 fixed_con.close() return reward, t / self.time_step, np.mean( tools.get_vehicle_delay(parent_dir, eval_label)) #Reward, ep_length, av_delay
def reset(self): '''reset env''' self.connect = traci.getConnection(self.label) self.connect.load(["-c", self.path]) '''init curr_veh_id''' while True: self.connect.simulationStep() if len(self.connect.vehicle.getIDList()) > 0: self.curr_veh_id = self.connect.vehicle.getIDList()[0] break '''init 环境动态的活动变量''' self.step_index = 0 self.done = False self.info = {'sum_cost': 0} self._init_veh() # 初始化 veh变量 self._update_most_closely_peds() return self.update_obs()
def initiate(intersection_id, looper=True, simu_connection=123, roads_in=[], roads_out=[]): # Stop any other previous STOPPER[str(intersection_id)] = True options = get_options() # check binary if options.nogui: sumoBinary = checkBinary('sumo') else: sumoBinary = checkBinary('sumo-gui') # traci starts sumo as a subprocess and then this script connects and runs threadId = str(simu_connection) path = os.getcwd() + "\main_app\simulation\generic" sumocfg = os.getcwd( ) + "\main_app\media\config\intersection\simulation\inter_" + str( intersection_id) + ".sumocfg" tripinfo = os.getcwd( ) + "\main_app\media\config\intersection\\tripinfo\inter_" + str( intersection_id) + "_" + str(threadId) + ".xml" print(sumocfg) print(tripinfo) #try: traci.start( [sumoBinary, "-c", sumocfg, "--tripinfo-output", tripinfo, "-S", "-Q"], label=str(threadId)) traci_connection = traci.getConnection(str(threadId)) print( "Before: run ....................................................................................." ) run(traci_connection, intersection_id, loop=looper, roads_in=roads_in, roads_out=roads_out) #except: print( "********************************* >> Something went wrong << ******************************* " )
def _init_sim(self, gui): """ initialize simulation in SUMO, accounting for different OS @params gui : boolean, whether to run the simulation with gui @returns : simulation connection labelled by user """ if sys.platform == "win32": if self.GUI: sumoBinary = checkBinary('sumo-gui') else: sumoBinary = checkBinary('sumo') else: if self.GUI: sumoBinary = "sumo-gui" else: sumoBinary = "sumo" traci.start([sumoBinary, "-c", "ring.sumocfg", "--time-to-teleport", "-1"], label="sim") return traci.getConnection("sim")
def _init_sim(self, gui): """ initialize simulation Args: gui : boolean, whether to run the simulation with gui """ if sys.platform == "win32": if self.gui: sumoBinary = checkBinary('sumo-gui') else: sumoBinary = checkBinary('sumo') else: if self.gui: sumoBinary = "sumo-gui" else: sumoBinary = "sumo" traci.start([sumoBinary, "-c", "seattle.sumocfg", "--time-to-teleport", "-1"], label="sim") return traci.getConnection("sim")
def __init__(self): self.simulation = traci.getConnection("simulation") trafficlight_ids = self.simulation.trafficlight.getIDList() self.controlled_lanes = [] for trafficlight_id in trafficlight_ids: self.controlled_lanes = self.controlled_lanes + list( set( self.simulation.trafficlight.getControlledLanes( trafficlight_id))) self.vehicles_on_lanes_dict = { lane: 0 for lane in self.controlled_lanes } self.lanes_lights_dict = {} self.change_light_request_dict = {} self.simulation_step_ = 0 self.waiting_cars_in_time = [] with open('simulation.csv', 'w') as f: f.write('STEP;' + ';'.join(self.vehicles_on_lanes_dict.keys()) + '\n')
def start_simulation(self, parent_dir=None): """Opens a connection to sumo/traci [with or without GUI] and updates obs atribute (the current state of the environment). """ tools.generate_routefile(route_file_dir=self.route, demand=self.demand) sumo_cmd = [ self.sumo_binary, '-n', self.net, '-r', self.route, '--time-to-teleport', '-1' ] if parent_dir: sumo_cmd.append('--tripinfo-output') sumo_cmd.append(parent_dir + '/tripinfo.xml') traci.start(sumo_cmd, label=self.connection_label) # print('Started connection for worker #', self.connection_label) self.connection = traci.getConnection(self.connection_label) self.state.update_state(connection=self.connection) self.warm_up_net(WARM_UP_NET) self.state.update_state(connection=self.connection)
def __init__(self, sumo_cmd, vehicle_generator_config, max_steps=None, env_name=None): self.max_steps = max_steps self.sumo_cmd = sumo_cmd self.env_name = env_name with semaphore: self.unique_id = str(uuid.uuid4()) print(self.sumo_cmd) traci.start(self.sumo_cmd, label=self.unique_id) if 'sumo' in self.sumo_cmd: self.sumo_cmd.remove('sumo') else: self.sumo_cmd.remove('sumo-gui') self.connection = traci.getConnection(self.unique_id) self.vehicle_generator = VehiclesGenerator.from_config_dict( self.connection, vehicle_generator_config) self.num_steps = 0 self.was_step = False
def __init__(self, net_file, config_file, additional_file, use_gui=True): self.trafficlight_skeletons = {} self.trafficlight_ids = [] self.action_space = None self.observation_space = None self.collaborator = None net = sumolib.net.readNet(net_file) # Preprocess the network definition and produce internal representation of each trafficlight for trafficlight in net.getTrafficLights(): id_ = trafficlight.getID() self.trafficlight_skeletons[id_] = netextractor.extract_tl_skeleton(net, trafficlight) self.trafficlight_ids.append(id_) # Preprocess the file with information about induction loops self.additional = process_additional_file(additional_file) # Define the command to run SUMO simulation if use_gui: sumo_binary = sumolib.checkBinary('sumo-gui') else: sumo_binary = sumolib.checkBinary('sumo') self._sumo_cmd = [sumo_binary, '--no-warnings', '--no-step-log', '--time-to-teleport', '-1', '--start', '--quit-on-end', '-c', config_file] # Start simulation to get insight about the environment traci.start(self._sumo_cmd) tmp = Collaborator(traci.getConnection(), self.trafficlight_skeletons, self.additional) self.observation_space = spaces.Dict({ 'obs': spaces.Box(low=-0.5, high=1.5, shape=tmp.observation_space_shape, dtype=np.float32), 'action_mask': spaces.Box(low=0, high=1, shape=tmp.action_space_shape, dtype=np.int32) }) self.action_space = spaces.Discrete(tmp.available_actions) traci.close()
# this is the main entry point of this script if __name__ == "__main__": options = get_options() # this script has been called from the command line. It will start sumo as a # server, then connect and run if options.nogui: sumoBinary = checkBinary('sumo') else: sumoBinary = checkBinary('sumo-gui') # first, generate the route file for this simulation # this is the normal way of using traci. sumo is started as a # subprocess and then the python script connects and runs traci.start([ sumoBinary, "-c", "data/anl427.sumocfg", "--tripinfo-output", "tripinfo.xml" ], label="a") #traci.start([sumoBinary, "-c", "data/anl427.sumocfg", # "--tripinfo-output", "tripinfo.xml"], label="b") conn1 = traci.getConnection("a") #conn2 = traci.getConnection("b") import time input("Logs can be attached now. Press Enter to continue") run(conn1) #run(conn2)
def eval_genomes(genomes, config, episode): """" SOMETHING SMETHING""" x = 0 # state_empty=np.zeros([feature_number,1]) for genome_id, genome in genomes: x += 1 SUMO.step = 0 SUMO.Colliion = False SUMO.RouteEnd = False v_episode = [] a_episode = [] distance_episode = [] created = multiprocessing.Process() sim = current._identity[1] simulation = traci.getConnection('sim' + str(sim - 1)) SUMO.init_vars_episode() """Anmerkung: Hier werden einige Variationen des Verkehrsszenarios für meine Trainingsepisoden definiert, wenn 'training = True' gesetzt ist. Im Fall 'training = False' oder 'evaluation = True' (Evaluierungsepisoden unter gleichen Randbedingungen) wird immer eine Episode mit gleichen Randbedingungen (z.B. Geschwindigkeitsprofil vorausfahrendes Fahrzeug) gesetzt""" if trafic.evaluation: simulation.vehicle.add(trafic.vehicle_ego.ID, trafic.vehicle_ego.RouteID, departSpeed='0', typeID='ego_vehicle') # Ego vehicle simulation.trafficlight.setPhase( 'junction1', 0 ) # set traffic light phase to 0 for evaluation (same conditions) else: traci.vehicle.add(trafic.vehicle_ego.ID, trafic.vehicle_ego.RouteID, departSpeed=np.array2string( trafic.vehicle_ego.depart_speed[episode - 1]), typeID='ego_vehicle') # Ego vehicle if trafic.vehicle2_exist: simulation.vehicle.add( trafic.vehicle_2.ID, trafic.vehicle_2.RouteID, typeID='traffic_vehicle') # preceding vehicle 1 if trafic.vehicle3: simulation.vehicle.add( trafic.vehicle_3.ID, trafic.vehicle_3.RouteID, typeID='traffic_vehicle') # preceding vehicle 2 if trafic.training and not trafic.evaluation: simulation.vehicle.moveTo(trafic.vehicle_3.ID, 'gneE01_0', trafic.episoden_variante) else: simulation.vehicle.moveTo(trafic.vehicle_3.ID, 'gneE01_0', 0.) simulation.simulationStep() # to spawn vehicles # if controller != 'SUMO': simulation.vehicle.setSpeedMode( trafic.vehicle_ego.ID, 16 ) # only emergency stopping at red traffic lights --> episode ends if trafic.vehicle2_exist: simulation.vehicle.setSpeedMode(trafic.vehicle_2.ID, 17) if trafic.vehicle3: simulation.vehicle.setSpeedMode(trafic.vehicle_3.ID, 17) SUMO.currentvehiclelist = simulation.vehicle.getIDList() # SUMO subscriptions simulation.vehicle.subscribeLeader(trafic.vehicle_ego.ID, 10000) simulation.vehicle.subscribe(trafic.vehicle_ego.ID, [ traci.constants.VAR_SPEED, traci.constants.VAR_BEST_LANES, traci.constants.VAR_FUELCONSUMPTION, traci.constants.VAR_NEXT_TLS, traci.constants.VAR_ALLOWED_SPEED, traci.constants.VAR_LANE_ID ]) dynamics_ego = Longitudinal_dynamics(tau=0.5) net = neat.nn.FeedForwardNetwork.create(genome, config) """"Run episode =======================================================================""" while simulation.simulation.getMinExpectedNumber( ) > 0: # timestep loop """Get state for first iteration ===================================================================""" if SUMO.step == 0: calculate_features_firststep() # if controller == 'DDPG_v' or controller == 'DDPG' or controller == 'DQN': # nn_controller.state = get_state() """Controller ====================================================================================== Hier wird die Stellgröße des Reglers (z.B. Sollbeschleunigung) in Abhängigkeit der Reglereingänge (zusammengefasst in 'features' für den ACC Controller bzw. 'nn_controller.state' für die Neuronalen Regler""" # if controller == 'ACC': # SUMO.a_set[SUMO.step] = acc_controller.calculate_a(features) # Calculate set acceleration with ACC controller # # Save ACC mode to SUMO class # SUMO.mode_ego[SUMO.step] = acc_controller.mode # ACC control modes (speed control, headway control, etc) # elif controller == 'DQN': # # Calculate Q-Values for all actions # nn_controller.action_value = nn_controller.model.predict(nn_controller.state) # NN inputs: distance, delta_v # # Choose Action according to epsilon-greedy policy # nn_controller.index[SUMO.step] = nn_controller.choose_action(nn_controller.action_value, episode) # # Calculate the set acceleration for the timestep # nn_controller.a_set = nn_controller.actions[nn_controller.index[SUMO.step]] # SUMO.a_set[SUMO.step] = nn_controller.a_set # elif controller == 'DDPG': # if training and not evaluation: # if exploration_policy == 'ACC': # SUMO.a_set[SUMO.step] = explo_policy.calculate_a(features) # SUMO.a_set[SUMO.step] = nn_controller.add_noise(SUMO.a_set[SUMO.step]) # else: # SUMO.a_set[SUMO.step] = nn_controller.choose_action(copy(nn_controller.state), network='main', noise=True) # else: # SUMO.a_set[SUMO.step] = nn_controller.choose_action(copy(nn_controller.state), network='main', noise=False) # elif controller == 'hybrid_a': # acc_controller.a_set = acc_controller.calculate_a( # features) # Calculate set acceleration with ACC controller # # Save ACC mode to SUMO class # SUMO.mode_ego[SUMO.step] = acc_controller.mode # ACC control modes (speed control, headway control, etc) # if SUMO.step == 0: # nn_controller.state[0, 3] = acc_controller.a_set # if training and not evaluation: # nn_controller.a_set = nn_controller.choose_action(copy(nn_controller.state), network='main', noise=True) # else: # nn_controller.a_set = nn_controller.choose_action(copy(nn_controller.state), network='main', noise=False) # SUMO.a_set[SUMO.step] = acc_controller.a_set + nn_controller.k_hybrid_a * nn_controller.a_set # SUMO.a_hybrid_a[SUMO.step] = nn_controller.a_set # elif controller == 'DDPG_v': # if training and not evaluation: # SUMO.v_set[SUMO.step], SUMO.v_set_nonoise[SUMO.step] = nn_controller.choose_action(copy(nn_controller.state), network='main', features=features, noise=True) # else: # SUMO.v_set[SUMO.step] = nn_controller.choose_action(copy(nn_controller.state), network='main', noise=False) # SUMO.a_set[SUMO.step] = acc_controller.calc_a_P(features, SUMO.v_set[SUMO.step]) state = get_state(trafic.state_empty) # print(state) # net = neat.nn.FeedForwardNetwork.create(genome, config) SUMO.a_set[SUMO.step] = net.activate( state) #[0,0], state[0,1], state[0,2], state[0,3]) SUMO.a_set[SUMO.step] = 20 * SUMO.a_set[ SUMO.step] - 10 #[0,0], state[0,1], state[0,2], state[0,3]) # print(SUMO.a_set[SUMO.step], SUMO.step) """Longitudinal Dynamics =========================================================================== Hier werden Längsdynamische Größen des Egofahrzeugs berechnet. beispielsweise die Realbeschleunigung aus den Stellgrößen des Reglers oder der Kraftstoffverbrauch für ein definiertes Fahrzeug (aktuell konventionelles Verbrennerfahrzeug mit Mehrganggetriebe). Am Ende wird als Schnittstelle zu SUMO eine neue Geschwindigkeit für das Egofahrzeug für den nächsten Zeitschritt gesetzt""" dynamics_ego = Longitudinal_dynamics(tau=0.5) # if controller == 'SUMO': # dynamics_ego.a_real = traci.vehicle.getAcceleration(vehicle_ego.ID) # dynamics_ego.wheel_demand(SUMO.v_ego[SUMO.step], vehicle_ego, # SUMO.step) # Calculate the torque and speed wheel demand of this timestep # dynamics_ego.operating_strategy(SUMO.sim['timestep'], vehicle_ego, s0=2, kp=10, step=SUMO.step) # SUMO.a_real[SUMO.step] = dynamics_ego.a_real # else: dynamics_ego.low_lev_controller( SUMO.a_set[SUMO.step], SUMO.sim['timestep'] ) # Calculate a_real with a_set and a PT1 Transfer function dynamics_ego.wheel_demand( SUMO.v_ego[SUMO.step], trafic.vehicle_ego, SUMO.step ) # Calculate the torque and speed wheel demand of this timestep dynamics_ego.operating_strategy(SUMO.sim['timestep'], trafic.vehicle_ego, s0=2, kp=10, step=SUMO.step) dynamics_ego.v_real_next = SUMO.v_ego[ SUMO.step] + dynamics_ego.a_real * SUMO.sim['timestep'] dynamics_ego.v_real_next = np.clip(dynamics_ego.v_real_next, 0., None) SUMO.a_real[SUMO.step] = dynamics_ego.a_real simulation.vehicle.setSpeed( trafic.vehicle_ego.ID, dynamics_ego.v_real_next ) # Set velocity of ego car for next time step """Control traffic ================================================================""" if trafic.vehicle2_exist and trafic.vehicle_2.ID in SUMO.currentvehiclelist: if trafic.vehicle_2.end == False: if simulation.vehicle.getLaneID( trafic.vehicle_2.ID ) == 'junction_out_0' and traci.vehicle.getLanePosition( trafic.vehicle_2.ID) > 90: simulation.vehicle.remove(trafic.vehicle_2.ID) trafic.vehicle_2.end = True else: #traci.vehicle.setSpeed(vehicle_2.ID, SUMO.v_profile[SUMO.step]) # set velocity of preceding car 1 pass if trafic.vehicle3 and trafic.vehicle_3.ID in SUMO.currentvehiclelist: traci.vehicle.setSpeed(trafic.vehicle_3.ID, SUMO.v_profile[ SUMO.step]) # set velocity of preceding car 2 """SUMO simulation step ============================================================================""" simulation.simulationStep() SUMO.currentvehiclelist = traci.vehicle.getIDList() """Check if any of the endstate conditions is true (e.g. collision) ===================================""" if trafic.vehicle_ego.ID not in SUMO.currentvehiclelist: SUMO.RouteEnd = True SUMO.endstate = True print([x, SUMO.step], ' Route finished!') elif simulation.simulation.getCollidingVehiclesNumber() > 0: SUMO.endstate = True SUMO.Collision = True print([x, SUMO.step], ' Collision!') elif trafic.vehicle_ego.ID in simulation.simulation.getEmergencyStoppingVehiclesIDList( ): # Check for ego vehicle passing red traffic light SUMO.Collision = True SUMO.endstate = True print([x, SUMO.step], ' Red lights passed!') # Set a maximum time step limit for 1 episode elif SUMO.step > 5000: SUMO.RouteEnd = True SUMO.endstate = True print([x, SUMO.step], ' Maximum time reached!') """get new state ===================================================================================""" calculate_features() v_episode.append(features.v_ego) a_episode.append(dynamics_ego.a_real) distance_episode.append(features.distance) # if controller == 'DDPG_v' or controller == 'DDPG' or controller == 'DQN': # nn_controller.new_state = get_state() """Prepare next timestep ===========================================================================""" if SUMO.Collision or SUMO.RouteEnd: # end episode when endstate conditions are true break dynamics_ego.a_previous = copy( dynamics_ego.a_real ) # copy --> to create an independent copy of the variable, not a reference to it # if controller == 'DQN' or controller == 'hybrid_a' or controller == 'DDPG' or controller == 'DDPG_v': # nn_controller.state = copy(nn_controller.new_state) if sample_generation: sample_generator.state = copy(sample_generator.new_state) trafic.vehicle_ego.ID_prec_previous = copy( trafic.vehicle_ego.ID_prec) # print(SUMO.step) SUMO.step += 1 # print(v_episode) """Calculate Reward ================================================================================""" fitness = len(v_episode) * np.sqrt(np.sum( np.square(v_episode))) - np.sum( 0.5 * np.sign(features.v_allowed - np.asarray(v_episode)) + 1) - np.mean(np.square(a_episode)) - 1 / sum( np.square( np.asarray(v_episode) * 3.6 - np.asarray(distance_episode))) - 100000 * int( SUMO.Collision) genome.fitness = fitness[0].item() print(fitness) # cum_reward[episode] += reward """End of episode - prepare next episode ====================================================================== Reset position of vehicles by removing and (later) adding them again, call running plots and export data """ if trafic.vehicle_ego.ID in SUMO.currentvehiclelist: simulation.vehicle.remove(trafic.vehicle_ego.ID) if trafic.vehicle2_exist and trafic.vehicle_2.ID in SUMO.currentvehiclelist: simulation.vehicle.remove(trafic.vehicle_2.ID) if trafic.vehicle3 and trafic.vehicle_3.ID in SUMO.currentvehiclelist: simulation.vehicle.remove(trafic.vehicle_3.ID) simulation.simulationStep() dynamics_ego.reset_variables()
def _configure_environment(self): if self.GUI: sumoBinary = set_sumo_home.sumoBinaryGUI else: sumoBinary = set_sumo_home.sumoBinary if self.predefined: self.cfgfile = "/assets/corniche_predefined.sumocfg" else: self.cfgfile = "/assets/corniche.sumocfg" self.argslist = [ sumoBinary, "-c", module_path + self.cfgfile, "--collision.action", "teleport", "--step-length", str(self.SUMOSTEP), "-S", "--time-to-teleport", "-1", "--scale", str(self.scale), #scale volume "--collision.mingap-factor", "0", "--collision.check-junctions", "true", "--no-step-log", "--vehroute-output", "vehicles.out.xml" ] if self.GUI: self.argslist += ["--gui-settings-file", "viewsettings.xml"] traci.start(self.argslist, label=self.runcode) self.conn = traci.getConnection(self.runcode) time.sleep(5) # Wait for server to startup #get list of all edges self.EDGES = self.conn.edge.getIDList() #create dictionary for incoming and outgoing edges self.edgesDict = {} #get network file name cfg = ET.parse(module_path + self.cfgfile) config = cfg.getroot() name = config.find(".//input/net-file") name = name.get("value") #pressures observation tree = ET.parse(module_path + "/assets/" + name) root = tree.getroot() #make a dataframe for lane counts allConnectedEdges = set() for x in root.findall('.//connection'): if x.attrib["from"][0] != ":": allConnectedEdges.add(x.attrib["from"]) if x.attrib["to"][0] != ":": allConnectedEdges.add(x.attrib["to"]) allConnectedEdges = sorted(allConnectedEdges) #allConnectedEdgesIndex = {x:i for i,x in enumerate(allConnectedEdges)} #create dictionary of edge predecessor internal lanes in junctions self.junctionPreds = set() self.junctionPair = {} for x in root.findall('.//connection[@via]'): if x.attrib["to"][0] != ":" and x.attrib["via"][ 0] == ":" and x.attrib["via"][1:2] != "tl": pred = "_".join(x.attrib["via"].split("_")[:-1]) edge = x.attrib["to"] self.junctionPreds.add(pred) self.junctionPair[edge] = pred self.junctionPreds = sorted(self.junctionPreds) allEdges = self.getAllNonInternalEdges(module_path + "/assets/" + name) self.allConnectedEdges = allConnectedEdges self.allEdges = allEdges self._subscribeALL() if not self.predefined: for tlsID in self.phases.getTls(): self.edgesDict[tlsID] = {} #programs = root.findall(".//tlLogic[@id='%d']" % tlsID) #phases = set([int(program.get("programID").split(" ")[-1]) for program in programs]) #phases = sorted(phases) pressures = [] for phase in self.phases.getTlsPhases(tlsID): self.edgesDict[tlsID][phase] = { "incoming": set(), "outgoing": set() } state = root.findall( ".//tlLogic[@programID='TL_%d from 0 to %d']/phase" % (tlsID, phase))[-1] #state = state.find(".//phase[2]") state = state.get("state") leng = len(state) for i in range(0, leng): if state[i] == 'G': data = root.find( ".//connection[@tl='%d'][@linkIndex='%d']" % (tlsID, i)) connectionFROM = data.get('from') connectionTO = data.get('to') self.edgesDict[tlsID][phase]["incoming"].add( connectionFROM) self.edgesDict[tlsID][phase]["outgoing"].add( connectionTO)
poly_path = city_path + "/" + city + ".poly.xml" off_parking_path = scenario_path + "/parking/off_parking.add.xml" drop_off_parking_path = scenario_path + "/parking/drop_off_parking.add.xml" conns = [] for i in np.arange(2): sim_id = "sim" + str(i) traci.start([ "sumo", "-c", cfg_path, "-r", rou_path + "," + trip_path, "--additional-files", poly_path + "," + off_parking_path + "," + drop_off_parking_path ], label=sim_id) # traci.start(["sumo", "-c", "../cities/san_francisco/Scenario_Set_2/san_francisco.sumo.cfg", # "-r", "../cities/san_francisco/san_francisco.rou.xml,../cities/san_francisco/Scenario_Set_2/trip_0.01_with_0.5_drop-off_0.8_drop-off_only.xml"], label="sim2") conns.append(traci.getConnection(sim_id)) # conn2 = traci.getConnection("sim2") step = 0 while step < 86400: # traci.simulationStep() for conn in conns: conn.simulationStep() # run 1 step for sim1 # conn2.simulationStep() # run 1 step for sim2 step += 1 traci.close() # -r ../san_francisco.rou.xml,trips/trip_"$sample_rate"_with_"$drop_off_percentage"_drop-off_"$drop_off_only_percentage"_drop-off_only.xml \ # --additional-files ../san_francisco.poly.xml,parking/off_parking.add.xml,parking/drop_off_parking.add.xml,edge_dump_config/edgedata_"$drop_off_percentage"_drop-off_"$drop_off_only_percentage"_drop-off_only.add.xml,rerouter/reroute_parking_"$drop_off_only_percentage"_drop-off_only.xml \ # --stop-output results/stops_"$sample_rate"_with_"$drop_off_percentage"_drop-off_"$drop_off_only_percentage"_drop-off_only.xml \
def start_sumo(self): traci.start(self.sumo_cmd) return traci.getConnection()
import subprocess import sys SUMO_HOME = os.environ.get( 'SUMO_HOME', os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', '..', '..')) sys.path.append(os.path.join(SUMO_HOME, 'tools')) import traci import sumolib # noqa sumoBinary = sumolib.checkBinary('sumo') conns = [] sumoProcess = [] for i in range(2): traci.start([sumoBinary, "-c", "sumo.sumocfg"], label=str(i)) conns.append(traci.getConnection(str(i))) for c in conns: for step in range(3): print("step", step) c.simulationStep() for c in conns: print("routes", c.route.getIDList()) print("route count", c.route.getIDCount()) routeID = "horizontal" print("examining", routeID) print("edges", c.route.getEdges(routeID)) c.route.subscribe(routeID) for c in conns: print(c.route.getSubscriptionResults(routeID)) for step in range(3, 6): print("step", step)
def calculate_features(sim, SUMO): try: simulation = traci.getConnection(sim) traci.switch(sim) sub_ego = simulation.vehicle.getSubscriptionResults( trafic.vehicle_ego.ID) if SUMO.Collision: # collision happened! features.distance = 0 # set the distance of the cars after a collision to 0 features.v_prec = SUMO.v_prec[ SUMO. step] # set the velocity of the preceding car after a collision to the value of the previous timestep features.v_ego = SUMO.v_ego[ SUMO. step] # set the velocity of the ego car after a collision to the value of the previous timestep elif SUMO.RouteEnd: features.distance = SUMO.distance[ SUMO. step] # set the distance of the cars after preceding vehicle ends route to previous timestep features.v_prec = SUMO.v_prec[ SUMO. step] # set the velocity of the preceding car after preceding vehicle ends route to the value of the previous timestep features.v_ego = SUMO.v_ego[ SUMO. step] # set the velocity of the ego car after preceding vehicle ends route to the value of the previous timestep else: ## TLS Distance if traci.constants.VAR_NEXT_TLS in sub_ego and len( sub_ego[traci.constants.VAR_NEXT_TLS]) > 0: features.distance_TLS = sub_ego[ traci.constants.VAR_NEXT_TLS][0][2] features.TLS_state = sub_ego[ traci.constants.VAR_NEXT_TLS][0][3] else: features.distance_TLS = 1000 # TODO: Handling when no TLS ahead features.TLS_state = None ## v_ego features.v_ego = sub_ego[traci.constants.VAR_SPEED] # print(dynamics_ego.fuel_cons_per_100km) ## fuel_consumption trafic.vehicle_ego.fuel_cons[SUMO.step + 1] = sub_ego[ traci.constants.VAR_FUELCONSUMPTION] # in ml/s # trafic.vehicle_ego.fuel_cons_ECMS[SUMO.step + 1] = dynamics_ego.fuel_cons_per_100km # trafic.vehicle_ego.fuel_cons_ECMS_per_s[SUMO.step + 1] = dynamics_ego.fuel_cons_per_s ## distance, v_prec try: if traci.constants.VAR_LEADER in sub_ego: trafic.vehicle_ego.ID_prec, features.distance = sub_ego[ traci.constants.VAR_LEADER] SUMO.distance[SUMO.step + 1] = features.distance features.distance = np.clip(features.distance, None, 250.) features.v_prec = simulation.vehicle.getSpeed( trafic.vehicle_ego.ID_prec) SUMO.v_prec[SUMO.step + 1] = features.v_prec else: raise TypeError except TypeError: features.distance = 250 SUMO.distance[SUMO.step + 1] = features.distance SUMO.v_prec[SUMO.step + 1] = features.v_ego trafic.vehicle_ego.ID_prec = 'none' if features.distance == 250: features.v_prec = copy( features.v_ego ) # when no preceding car detected OR distance > 250 (clipped), set a 'virtual velocity' = v_ego ## correct distance, v_prec with virtual TLS vehicle if trafic.TLS_virt_vehicle: if features.TLS_state == 'y' or features.TLS_state == 'r': if features.distance_TLS < features.distance: features.distance = copy(features.distance_TLS) features.v_prec = 0 ## headway if features.v_ego < 0.1: features.headway = 10000. else: features.headway = features.distance / features.v_ego ## v_allowed if traci.constants.VAR_LANE_ID in sub_ego: features.v_allowed = simulation.lane.getMaxSpeed( sub_ego[traci.constants.VAR_LANE_ID]) else: features.v_allowed = 33.33 # tempo limit set to 120 km/h when no signal received, unlikely to happen ## plotting variables SUMO.headway[SUMO.step + 1] = features.headway SUMO.v_ego[SUMO.step + 1] = features.v_ego except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) # fitness=timestep raise Exception('My error! Calc features', message)
def eval_genomes(genome_id, genome, config, episode, trafic): """" SOMETHING SMETHING""" error = False error_return = 10000 timestep = 0 try: [simname, SUMO, error] = SimInitializer(trafic, episode) except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) if error == True: return error_return # fitness=timestep # raise Exception('Error SimInitializer', message) if error == True: return error_return try: x = 0 # state_empty=np.zeros([feature_number,1]) # for genome_id, genome in genomes: x += 1 SUMO.step = 0 SUMO.Colliion = False SUMO.RouteEnd = False v_episode = [] a_episode = [] distance_episode = [] timestep = 1 process_param = multiprocessing.Process() sim = simname #process_param.name timestep = [sim, simname] simulation = traci.getConnection(simname) timestep = 1.5 SUMO.init_vars_episode() """Anmerkung: Hier werden einige Variationen des Verkehrsszenarios für meine Trainingsepisoden definiert, wenn 'training = True' gesetzt ist. Im Fall 'training = False' oder 'evaluation = True' (Evaluierungsepisoden unter gleichen Randbedingungen) wird immer eine Episode mit gleichen Randbedingungen (z.B. Geschwindigkeitsprofil vorausfahrendes Fahrzeug) gesetzt""" if trafic.evaluation: simulation.vehicle.add(trafic.vehicle_ego.ID, trafic.vehicle_ego.RouteID, departSpeed='0', typeID='ego_vehicle') # Ego vehicle simulation.trafficlight.setPhase( 'junction1', 0 ) # set traffic light phase to 0 for evaluation (same conditions) else: simulation.vehicle.add( trafic.vehicle_ego.ID, trafic.vehicle_ego.RouteID, departSpeed=np.array2string( trafic.vehicle_ego.depart_speed[episode - 1]), typeID='ego_vehicle') # Ego vehicle if trafic.vehicle2_exist: simulation.vehicle.add( trafic.vehicle_2.ID, trafic.vehicle_2.RouteID, typeID='traffic_vehicle') # preceding vehicle 1 timestep = 1.7 if trafic.vehicle3: simulation.vehicle.add( trafic.vehicle_3.ID, trafic.vehicle_3.RouteID, typeID='traffic_vehicle') # preceding vehicle 2 if trafic.training and not trafic.evaluation: simulation.vehicle.moveTo(trafic.vehicle_3.ID, 'gneE01_0', trafic.episoden_variante) else: simulation.vehicle.moveTo(trafic.vehicle_3.ID, 'gneE01_0', 0.) timestep = 2 simulation.simulationStep() # to spawn vehicles # if controller != 'SUMO': simulation.vehicle.setSpeedMode( trafic.vehicle_ego.ID, 16 ) # only emergency stopping at red traffic lights --> episode ends if trafic.vehicle2_exist: simulation.vehicle.setSpeedMode(trafic.vehicle_2.ID, 17) if trafic.vehicle3: simulation.vehicle.setSpeedMode(trafic.vehicle_3.ID, 17) SUMO.currentvehiclelist = simulation.vehicle.getIDList() # SUMO subscriptions simulation.vehicle.subscribeLeader(trafic.vehicle_ego.ID, 10000) simulation.vehicle.subscribe(trafic.vehicle_ego.ID, [ traci.constants.VAR_SPEED, traci.constants.VAR_BEST_LANES, traci.constants.VAR_FUELCONSUMPTION, traci.constants.VAR_NEXT_TLS, traci.constants.VAR_ALLOWED_SPEED, traci.constants.VAR_LANE_ID ]) timestep = 3 dynamics_ego = Longitudinal_dynamics(tau=0.5) timestep = 3.1 net = neat.nn.FeedForwardNetwork.create(genome[1], config) """"Run episode =======================================================================""" while simulation.simulation.getMinExpectedNumber( ) > 0: # timestep loop """Get state for first iteration ===================================================================""" if SUMO.step == 0: calculate_features_firststep(sim, SUMO) # if controller == 'DDPG_v' or controller == 'DDPG' or controller == 'DQN': # nn_controller.state = get_state() timestep = 3.3 """Controller ====================================================================================== Hier wird die Stellgröße des Reglers (z.B. Sollbeschleunigung) in Abhängigkeit der Reglereingänge (zusammengefasst in 'features' für den ACC Controller bzw. 'nn_controller.state' für die Neuronalen Regler""" state = get_state(trafic.state_empty) timestep = 4 # print(state) # net = neat.nn.FeedForwardNetwork.create(genome, config) SUMO.a_set[SUMO.step] = net.activate( state) #[0,0], state[0,1], state[0,2], state[0,3]) SUMO.a_set[SUMO.step] = 10 * SUMO.a_set[ SUMO.step] #-10 #[0,0], state[0,1], state[0,2], state[0,3]) # print(SUMO.a_set[SUMO.step], SUMO.step) timestep = 5 """Longitudinal Dynamics =========================================================================== Hier werden Längsdynamische Größen des Egofahrzeugs berechnet. beispielsweise die Realbeschleunigung aus den Stellgrößen des Reglers oder der Kraftstoffverbrauch für ein definiertes Fahrzeug (aktuell konventionelles Verbrennerfahrzeug mit Mehrganggetriebe). Am Ende wird als Schnittstelle zu SUMO eine neue Geschwindigkeit für das Egofahrzeug für den nächsten Zeitschritt gesetzt""" dynamics_ego = Longitudinal_dynamics(tau=0.5) dynamics_ego.low_lev_controller( SUMO.a_set[SUMO.step], SUMO.sim['timestep'] ) # Calculate a_real with a_set and a PT1 Transfer function dynamics_ego.wheel_demand( SUMO.v_ego[SUMO.step], trafic.vehicle_ego, SUMO.step ) # Calculate the torque and speed wheel demand of this timestep dynamics_ego.operating_strategy(SUMO.sim['timestep'], trafic.vehicle_ego, s0=2, kp=10, step=SUMO.step) dynamics_ego.v_real_next = SUMO.v_ego[ SUMO.step] + dynamics_ego.a_real * SUMO.sim['timestep'] dynamics_ego.v_real_next = np.clip(dynamics_ego.v_real_next, 0., None) SUMO.a_real[SUMO.step] = dynamics_ego.a_real simulation.vehicle.setSpeed( trafic.vehicle_ego.ID, dynamics_ego.v_real_next ) # Set velocity of ego car for next time step timestep = 6 """Control traffic ================================================================""" if trafic.vehicle2_exist and trafic.vehicle_2.ID in SUMO.currentvehiclelist: if trafic.vehicle_2.end == False: if simulation.vehicle.getLaneID( trafic.vehicle_2.ID ) == 'junction_out_0' and simulation.vehicle.getLanePosition( trafic.vehicle_2.ID) > 90: simulation.vehicle.remove(trafic.vehicle_2.ID) trafic.vehicle_2.end = True else: #traci.vehicle.setSpeed(vehicle_2.ID, SUMO.v_profile[SUMO.step]) # set velocity of preceding car 1 pass if trafic.vehicle3 and trafic.vehicle_3.ID in SUMO.currentvehiclelist: simulation.vehicle.setSpeed( trafic.vehicle_3.ID, SUMO.v_profile[ SUMO.step]) # set velocity of preceding car 2 """SUMO simulation step ============================================================================""" simulation.simulationStep() SUMO.currentvehiclelist = traci.vehicle.getIDList() timestep = 7 """Check if any of the endstate conditions is true (e.g. collision) ===================================""" if trafic.vehicle_ego.ID not in SUMO.currentvehiclelist: SUMO.RouteEnd = True SUMO.endstate = True # print([x, SUMO.step],' Route finished!') elif simulation.simulation.getCollidingVehiclesNumber() > 0: SUMO.endstate = True SUMO.Collision = True # print([x, SUMO.step],' Collision!') elif trafic.vehicle_ego.ID in simulation.simulation.getEmergencyStoppingVehiclesIDList( ): # Check for ego vehicle passing red traffic light SUMO.Collision = True SUMO.endstate = True # print([x, SUMO.step],' Red lights passed!') # Set a maximum time step limit for 1 episode elif SUMO.step > 5000: SUMO.RouteEnd = True SUMO.endstate = True # print([x, SUMO.step],' Maximum time reached!') timestep = 7.5 """get new state ===================================================================================""" calculate_features(sim, SUMO) v_episode.append(features.v_ego) a_episode.append(dynamics_ego.a_real) distance_episode.append(features.distance) # """Prepare next timestep ===========================================================================""" if SUMO.Collision or SUMO.RouteEnd: # end episode when endstate conditions are true timestep = 9 break dynamics_ego.a_previous = copy( dynamics_ego.a_real ) # copy --> to create an independent copy of the variable, not a reference to it trafic.vehicle_ego.ID_prec_previous = copy( trafic.vehicle_ego.ID_prec) SUMO.step += 1 timestep = 8 """Calculate Reward ================================================================================""" # fitness = np.sqrt(len(v_episode))*np.sqrt(np.sum(np.square(np.asarray(v_episode)/250)))+np.sum(0.5*np.sign(features.v_allowed/250-np.asarray(v_episode)/250)+1) -0.0001*np.sum(np.square(a_episode))-1/sum(np.square(np.asarray(v_episode)/250*3.6-np.asarray(distance_episode)/250))-100000*int(SUMO.Collision) +100000*int(SUMO.RouteEnd) verbrauch = (10 * (np.mean(trafic.vehicle_ego.fuel_cons) / 0.05) - 1 ) #*50#*100 travelled_distance = sum([c * 0.2 for c in v_episode]) travelled_distance_divisor = 1 / 1800 if travelled_distance == 0 else travelled_distance abstand = np.sum([ 1 for ii in range(len(v_episode)) if distance_episode[ii] < 2 * v_episode[ii] * 3.6 and distance_episode[ii] > v_episode[ii] * 3.6 ]) crash = int(SUMO.Collision) #route_ende=int(SUMO.RouteEnd and not SUMO.maxtime) speed = np.mean(v_episode) / features.v_allowed if np.mean( v_episode) / features.v_allowed < 1 else 0 over_speed_limit = sum([ 1 for jj in range(len(v_episode)) if v_episode[jj] > features.v_allowed ]) evil_acc = sum([ 1 for kk in range(len(a_episode)) if abs(a_episode[kk].item()) > 4 ]) travelled_distance_divisor = evil_acc + 1 if travelled_distance == 0 else travelled_distance fitness = travelled_distance / 1800 + abstand / SUMO.step - crash + speed - 10 * over_speed_limit / SUMO.step - evil_acc / travelled_distance_divisor - 0.5 * verbrauch genome[1].fitness = fitness[0].item() # print(fitness) output = fitness[0].item() # cum_reward[episode] += reward """End of episode - prepare next episode ====================================================================== Reset position of vehicles by removing and (later) adding them again, call running plots and export data """ traci.close() except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) output = 10000 traci.close() # raise Exception('My error eval genome!', timestep, message) return output
def calculate_features_firststep(sim, SUMO): try: simulation = traci.getConnection(sim) traci.switch(sim) # state = np.zeros([1, feature_number]) sub_ego = simulation.vehicle.getSubscriptionResults( trafic.vehicle_ego.ID) # print(sub_ego[traci.constants.VAR_NEXT_TLS]) ## TLS Distance if traci.constants.VAR_NEXT_TLS in sub_ego and len( sub_ego[traci.constants.VAR_NEXT_TLS]) > 0: features.distance_TLS = sub_ego[traci.constants.VAR_NEXT_TLS][0][2] features.TLS_state = sub_ego[traci.constants.VAR_NEXT_TLS][0][3] else: features.distance_TLS = 1000 # TODO: Handling when no TLS ahead features.TLS_state = None errorat = 1 ## v_ego if traci.constants.VAR_SPEED in sub_ego: SUMO.v_ego[SUMO.step] = sub_ego[traci.constants.VAR_SPEED] else: SUMO.v_ego[SUMO.step] = 0. features.v_ego = SUMO.v_ego[SUMO.step] ## Fuel Consumption if traci.constants.VAR_FUELCONSUMPTION in sub_ego: trafic.vehicle_ego.fuel_cons[SUMO.step] = sub_ego[ traci.constants.VAR_FUELCONSUMPTION] else: trafic.vehicle_ego.fuel_cons[SUMO.step] = 0. errorat = 2 ## distance, v_prec try: if traci.constants.VAR_LEADER in sub_ego: trafic.vehicle_ego.ID_prec, features.distance = sub_ego[ traci.constants.VAR_LEADER] SUMO.distance[SUMO.step] = features.distance features.distance = np.clip(features.distance, None, 250.) features.v_prec = simulation.vehicle.getSpeed( trafic.vehicle_ego.ID_prec) SUMO.v_prec[SUMO.step] = features.v_prec if features.distance == 250: features.v_prec = features.v_ego else: raise TypeError except TypeError: trafic.vehicle_ego.ID_prec = 'none' features.distance = 250 SUMO.distance[SUMO.step] = features.distance SUMO.v_prec[SUMO.step] = features.v_ego if features.distance == 250: features.v_prec = copy(features.v_ego) errorat = 3 ## v_allowed if traci.constants.VAR_LANE_ID in sub_ego: features.v_allowed = simulation.lane.getMaxSpeed( sub_ego[traci.constants.VAR_LANE_ID]) else: features.v_allowed = 33.33 # tempo limit set to 120 km/h when no signal received, unlikely to happen ## correct distance, v_prec with virtual TLS vehicle if trafic.TLS_virt_vehicle: if features.TLS_state == 'y' or features.TLS_state == 'r': if features.distance_TLS < features.distance: features.distance = copy(features.distance_TLS) features.v_prec = 0 ## headway if features.v_ego < 0.1: features.headway = 10000. else: features.headway = features.distance / features.v_ego SUMO.headway[SUMO.step] = features.headway except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) # fitness=timestep raise Exception('My error! firststep', errorat, message)
def __init__( self, net_file: str, route_file: str, out_csv_name: Optional[str] = None, use_gui: bool = False, virtual_display: Optional[Tuple[int,int]] = None, begin_time: int = 0, num_seconds: int = 20000, max_depart_delay: int = 100000, time_to_teleport: int = -1, delta_time: int = 5, yellow_time: int = 2, min_green: int = 5, max_green: int = 50, single_agent: bool = False, sumo_seed: Union[str,int] = 'random', fixed_ts: bool = False, sumo_warnings: bool = True, ): self._net = net_file self._route = route_file self.use_gui = use_gui if self.use_gui: self._sumo_binary = sumolib.checkBinary('sumo-gui') else: self._sumo_binary = sumolib.checkBinary('sumo') self.virtual_display = virtual_display assert delta_time > yellow_time, "Time between actions must be at least greater than yellow time." self.begin_time = begin_time self.sim_max_time = num_seconds self.delta_time = delta_time # seconds on sumo at each step self.max_depart_delay = max_depart_delay # Max wait time to insert a vehicle self.time_to_teleport = time_to_teleport self.min_green = min_green self.max_green = max_green self.yellow_time = yellow_time self.single_agent = single_agent self.sumo_seed = sumo_seed self.fixed_ts = fixed_ts self.sumo_warnings = sumo_warnings self.label = str(SumoEnvironment.CONNECTION_LABEL) SumoEnvironment.CONNECTION_LABEL += 1 self.sumo = None if LIBSUMO: traci.start([sumolib.checkBinary('sumo'), '-n', self._net]) # Start only to retrieve traffic light information conn = traci else: traci.start([sumolib.checkBinary('sumo'), '-n', self._net], label='init_connection'+self.label) conn = traci.getConnection('init_connection'+self.label) self.ts_ids = list(conn.trafficlight.getIDList()) self.traffic_signals = {ts: TrafficSignal(self, ts, self.delta_time, self.yellow_time, self.min_green, self.max_green, self.begin_time, conn) for ts in self.ts_ids} conn.close() self.vehicles = dict() self.reward_range = (-float('inf'), float('inf')) self.metadata = {} self.spec = EnvSpec('SUMORL-v0') self.run = 0 self.metrics = [] self.out_csv_name = out_csv_name self.observations = {ts: None for ts in self.ts_ids} self.rewards = {ts: None for ts in self.ts_ids}
import traci import traci.constants as tc sumoBinary = "C:/Program Files (x86)/DLR/Sumo/bin/sumo-gui" sumoCmd = [sumoBinary, "-c", "kreuzung.sumocfg"] traci.start(sumoCmd, label = "sim1") conn1 = traci.getConnection("sim1") veh = "veh0" i = 0 """---------------------Simulation of crossing without traffic light----------------------------------------""" """-----------------------------Add Route dynamicaly--------------------------------------------------------""" traci.route.add("route0", ['lane1_1','-lane3_1','lane10_1']) traci.route.add("route1", ['lane4_1','-lane3_1','lane10_1']) traci.route.add("route2", ['lane2_1','-lane3_1','lane10_1']) traci.route.add("route3", ['-lane10_1','lane3_1','-lane1_1']) traci.route.add("route4", ['lane9_1','lane3_1','-lane1_1']) traci.route.add("route5", ['lane4_1','-lane2_1']) traci.route.add("route6", ['lane9_1','lane10_1']) """-----------------------------Add Vehicles dynamicaly--------------------------------------------------------"""
def SimInitializer(trafic, episode): error = False try: timestep = 1 trafic.number_episodes = 100000 feature_number = 4 trafic.training = True sample_generation = False trafic.TLS_virt_vehicle = True # Red and Yellow traffic lights are considered as preceding vehicles with v=0 trafic.TLS_ID = ['0', '1', '2', '3', '4'] trafic.evaluation = False trafic.vehicle2_exist = False # currently no meaningful trajectory / route - keep on False trafic.vehicle3_exist = True trafic.vehicle3_vprofile = 'sinus' # 'sinus', 'emergstop' liveplot = False # memory leak problem on windows when turned on trafic.Route_Ego = ['startedge', 'gneE01', 'gneE02', 'stopedge'] trafic.ego_depart_speed = np.ones( (trafic.number_episodes, )) * 0. # specific depart speed for ego vehicle when not training trafic.Route_Traffic1 = ['gneE01', 'junction_out'] # for vehicle2 trafic.Route_Traffic2 = ['gneE01', 'gneE02', 'stopedge'] # for vehicle3 trafic.state_empty = np.zeros([feature_number, 1]) timestep = 2 np.random.seed(42 + episode) SUMO2 = SUMO(trafic.Route_Ego, trafic.Route_Traffic1, trafic.Route_Traffic2, timestep=0.2) ## create velocity profile of preceding vehicle ## """Hier werden bei 'training = True' unterschiedliche Geschwindigkeitsprofile für das vorausfahrende Fahrzeug definiert. Für 'training = False' wird ein festes sinusförmiges Profil mit Mittelwert 30 km/h und Amplitude +- 25 km/h definiert.""" if trafic.vehicle3_exist: if trafic.vehicle3_vprofile == 'sinus': if trafic.training: # create random sinusodial velocity profiles for training SUMO2.prec_train_amplitude = np.random.rand( trafic.number_episodes) * 20 / 3.6 SUMO2.prec_train_mean = np.random.rand( trafic.number_episodes) * 20 / 3.6 + 10 / 3.6 else: SUMO2.prec_train_amplitude = 25 / 3.6 # a=25/3.6 SUMO2.prec_train_mean = 30 / 3.6 # c=30/3.6 SUMO2.create_v_profile_prec(a=SUMO.prec_train_amplitude, c=SUMO.prec_train_mean) elif vehicle3_vprofile == 'emergstop': SUMO2.create_v_profile_emerg_stop() else: raise NameError('No valid velocity profile selected') trafic.vehicle_ego = Vehicle(ego=True, ID='ego', RouteID='RouteID_ego', Route=trafic.Route_Ego, powertrain_concept='ICEV') trafic.dynamics_ego = Longitudinal_dynamics(tau=0.5) if trafic.vehicle2_exist: trafic.vehicle_2 = Vehicle(ego=False, ID='traffic.0', RouteID='traci_route_traffic.0', Route=trafic.Route_Traffic1) if trafic.vehicle3_exist: trafic.vehicle_3 = Vehicle(ego=False, ID='traffic.1', RouteID='traci_route_traffic.1', Route=trafic.Route_Traffic2) acc_controller = {} timestep = 3 # if trafic.training and liveplot: # fig_running, ax_running_1, ax_running_2, ax_running_3, ax_running_4 = plot_running_init(training) process_param = multiprocessing.Process() # print(process_param.name) traci.start(['sumo', '-c', 'SUMO_config.sumocfg', '--no-warnings'], label=str(process_param.name)) #, label=sim) simulation = traci.getConnection(process_param.name) simulation.route.add(trafic.vehicle_ego.RouteID, trafic.vehicle_ego.Route) if trafic.vehicle2_exist: simulation.route.add(trafic.vehicle_2.RouteID, trafic.vehicle_2.Route) if trafic.vehicle3_exist: simulation.route.add(trafic.vehicle_3.RouteID, trafic.vehicle_3.Route) simulation.vehicletype.setSpeedFactor(typeID='traffic_vehicle', factor=5.0) length_episode = np.zeros((trafic.number_episodes, 1)) restart_step = 0 # counter for calculating the reset timing when the simulation time gets close to 24 days evaluation = False if trafic.training: trafic.vehicle_ego.depart_speed = np.random.randint( 0, 30, size=trafic.number_episodes) else: trafic.vehicle_ego.depart_speed = ego_depart_speed simulation.trafficlight.setProgram(tlsID='junction1', programID=np.random.choice( trafic.TLS_ID)) timestep = 4 + episode if not trafic.training: simulation.trafficlight.setPhase('junction1', 0) if trafic.training and not trafic.evaluation and trafic.vehicle3_exist: trafic.vehicle3 = np.random.choice([True, False], p=[0.95, 0.05]) simulation.lane.setMaxSpeed( 'gneE01_0', np.random.choice([8.33, 13.89, 19.44, 25.])) simulation.lane.setMaxSpeed( 'gneE02_0', np.random.choice([8.33, 13.89, 19.44, 25.])) simulation.lane.setMaxSpeed( 'startedge_0', np.random.choice([8.33, 13.89, 19.44, 25.])) SUMO2.create_v_profile_prec(a=SUMO2.prec_train_amplitude[episode - 1], c=SUMO2.prec_train_mean[episode - 1]) else: trafic.vehicle3 = vehicle3_exist simulation.lane.setMaxSpeed('startedge_0', 13.89) # 13.89 simulation.lane.setMaxSpeed('gneE01_0', 19.44) # 19.44 simulation.lane.setMaxSpeed('gneE02_0', 13.89) # 13.89 simulation.lane.setMaxSpeed('stopedge_0', 8.33) # 8.33 trafic.episoden_variante = np.random.rand() * 240. return process_param.name, SUMO2, error except Exception as ex: template = "An exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) error = True traci.close()
"""--------------------------------------------------------------------------------------------------""" """--------------------------Simulation of collision ----------------------------------------------- """ """--------------------------------------------------------------------------------------------------""" import traci import traci.constants as tc sumoBinary = "C:/Program Files (x86)/DLR/Sumo/bin/sumo-gui" sumoCmd = [sumoBinary, "-c", "autobahn.sumocfg"] traci.start(sumoCmd, label = "sim3") conn1 = traci.getConnection("sim3") """ -----------------------------------------Simulation Rear Collision-----------------------------------------------------------""" #Information des Ego vehicle veh = "veh0" # Variable to begin step. i = 0 """------------------------------------Add Route dynamicaly-----------------------------------------------------------------------""" traci.route.add("route0", ['Lane1','Lane2','Lane4','Lane5']) """------------------------------------Add Vehicle dynamicaly---------------------------------------------------------------------""" traci.vehicle.add(veh, "route0", depart=0, pos=0, speed=10, lane=0, typeID='Car')
def evaluate(self, genotype, saveState, index): genotype = genotype.tolist() sumoBinary = "C:/Program Files (x86)/Eclipse/Sumo/bin/sumo" if saveState and os.path.isfile("save"): sumoCmd = [ sumoBinary, "--net-file", "test.net.xml", "--route-files", "test.rou.xml", "--load-state", "save", "--save-state.times", str(self.intervalSize), "--save-state.files", "save", "--begin", "0", "--waiting-time-memory", "99999", "--end", str(self.intervalSize) ] elif saveState and not os.path.isfile("save"): sumoCmd = [ sumoBinary, "--net-file", "test.net.xml", "--route-files", "test.rou.xml", "--save-state.times", str(self.intervalSize), "--save-state.files", "save", "--begin", "0", "--waiting-time-memory", "99999", "--end", str(self.intervalSize) ] elif not saveState and os.path.isfile("save"): sumoCmd = [ sumoBinary, "--net-file", "test.net.xml", "--route-files", "test.rou.xml", "--load-state", "save", "--begin", "0", "--waiting-time-memory", "99999", "--end", str(self.intervalSize) ] elif not saveState and not os.path.isfile("save"): sumoCmd = [ sumoBinary, "--net-file", "test.net.xml", "--route-files", "test.rou.xml", "--begin", "0", "--waiting-time-memory", "99999", "--end", str(self.intervalSize) ] traci.start(sumoCmd, label="sim_" + str(index)) connection = traci.getConnection("sim_" + str(index)) step = 0 accumulatedWaitingTimes = dict() timings = dict() tls = connection.trafficlight.getIDList() for light in tls: phases = connection.trafficlight.getCompleteRedYellowGreenDefinition( light)[0].phases if len(phases) >= 4: timings[light] = [] for i in range(len(phases)): maxDuration = max([phase.duration for phase in phases]) flag = True if phases[i].duration > 0.9 * maxDuration and flag: timings[light].append( (self.phaseSum + (genotype[0])) // 2) flag = False elif phases[i].duration > 0.9 * maxDuration and not flag: timings[light].append( math.ceil((self.phaseSum - (genotype[0])) / 2)) else: timings[light].append(phases[i].duration) del genotype[0] for light in timings: connection.trafficlight.setPhaseDuration( light, timings[light][connection.trafficlight.getPhase(light)]) while step < self.intervalSize + 1: tls = connection.trafficlight.getIDList() for light in timings: intendedDuration = timings[light] currentPhase = connection.trafficlight.getPhase(light) remainingTime = connection.trafficlight.getNextSwitch( light) - connection.simulation.getTime() if remainingTime == 0: connection.trafficlight.setPhase( light, (int(currentPhase) + 1) % len( connection.trafficlight. getCompleteRedYellowGreenDefinition( light)[0].phases)) connection.trafficlight.setPhaseDuration( light, intendedDuration[ connection.trafficlight.getPhase(light)]) connection.simulationStep() cars = connection.vehicle.getIDList() for car in cars: accumulatedWaitingTimes[int( car)] = connection.vehicle.getAccumulatedWaitingTime(car) step += 1 traci.close() del genotype return (sum(accumulatedWaitingTimes.values()), ), index