def traffic() -> Traffic: car1 = TrafficActor( name="car", speed=Distribution(sigma=0.2, mean=1.0), ) car2 = TrafficActor( name="car", speed=Distribution(sigma=0.2, mean=0.8), lane_changing_model=LaneChangingModel(impatience=1, cooperative=0.25), junction_model=JunctionModel(drive_after_yellow_time=1.0, impatience=0.5), ) return Traffic( flows=[ Flow( route=Route( begin=(f"edge-{r[0]}", 0, 30), end=(f"edge-{r[1]}", 0, -30) ), rate=1.0, actors={ car1: 0.5, car2: 0.5, }, ) for r in [("west-WE", "east-WE"), ("east-EW", "west-EW")] ] )
def generate_stopwatcher(stopwatcher_behavior, stopwatcher_route, start_lane_id, end_lane_id, begin_time=None): # this is a vehicle with aggressive behavior that records the number of steps to finishe a left-turn behavior = replace( get_social_vehicle_behavior(stopwatcher_behavior), name=f"stopwatcher_{stopwatcher_behavior}", ) begin_time = ( begin_time if begin_time is not None else random.randint(10, 15) ) # slight delay to wait for trafficne_id return Flow( begin=begin_time, end=begin_time + 3600, # 1 hour route=Route( begin=( f"edge-{stopwatcher_route[0]}", start_lane_id, "base", ), end=( f"edge-{stopwatcher_route[1]}", end_lane_id, "max", ), ), rate=1, actors={behavior: 1.0}, )
name="car", speed=Distribution(sigma=0.2, mean=0.5), lane_changing_model=LaneChangingModel(impatience=0, cooperative=0.5), junction_model=JunctionModel(drive_after_yellow_time=1.0, impatience=0.5), ) car_type = patient_car car_name = "patient_car" traffic = Traffic( flows=[ Flow( route=RandomRoute(), begin=0, end=1 * 60 * 60, # make sure end time is larger than the time of one episode rate=30, actors={car_type: 1}, ) for i in range(sv_num) ] ) print(f"generate flow with {sv_num} social {car_name} vehicles in {scenario_path} ") gen_traffic( scenario_path, traffic, name=f"{sv_num}_{car_name}", output_dir=scenario_path,
Flow, Route, RandomRoute, TrafficActor, Mission, ) scenario = os.path.dirname(os.path.realpath(__file__)) agent_missions = [ Mission(Route(begin=("left_in", 0, 80), end=("merged", (0, ), 40))), Mission(Route(begin=("left_in", 0, 50), end=("merged", (0, ), 40))), Mission(Route(begin=("ramp_in", 0, 80), end=("merged", (0, ), 60))), Mission(Route(begin=("ramp_in", 0, 50), end=("merged", (0, ), 60))), ] gen_missions(scenario, agent_missions, overwrite=True) gen_traffic( scenario, Traffic(flows=[ Flow( route=RandomRoute(), rate=3600, actors={TrafficActor(name="car"): 1.0}, ) ]), name="random", overwrite=True, )
("west-WE", "south-NS"), ("north-NS", "west-EW"), ("east-EW", "north-SN"), ] for name, routes in { "vertical": vertical_routes, "horizontal": horizontal_routes, "unprotected_left": turn_left_routes, "turns": turn_left_routes + turn_right_routes, "all": vertical_routes + horizontal_routes + turn_left_routes + turn_right_routes, }.items(): traffic = Traffic(flows=[ Flow( route=Route( begin=(f"edge-{r[0]}", 0, "random"), end=(f"edge-{r[1]}", 0, "random"), ), rate=1, actors={patient_car: 1.0}, ) for r in routes ]) gen_traffic(scenario, traffic, name=name, overwrite=True)
def generate_social_vehicles( ego_start_lane, route_distribution, start_end_on_different_lanes_probability, num_vehicles, route_direction, route_lanes, route_has_turn, stopwatcher_info, begin_time_init=None, deadlock_optimization=True, ): flows = [] behaviors = [] log_info = { "num_vehicles": 0, "route_distribution": None, "start_end_on_different_lanes_probability": 0.0, } stopwatcher_added = False # populate random routes based on their probability start_lane, end_lane = route_direction if stopwatcher_info and route_direction == stopwatcher_info["direction"]: if stopwatcher_info["behavior"] not in behaviors: behaviors.append(f'stopwatcher_{stopwatcher_info["behavior"]}') num_vehicles = max(0, num_vehicles - 1) # used 1 for stopwatcher stopwatcher_added = True behaviors.extend( random.choices( list(route_distribution.keys()), weights=list(route_distribution.values()), k=num_vehicles, )) random.shuffle(behaviors) # because stopwatcher is always at the beginning if begin_time_init is None: begin_time_init_func = basic_begin_time_init_func begin_time_init_params = {"probability": 0.0} else: begin_time_init_func = begin_time_init["func"] begin_time_init_params = begin_time_init["params"] begin_time_init_params = ({} if begin_time_init_params is None else begin_time_init_params) begin_times = begin_time_init_func(route_lanes[start_lane], len(behaviors), **begin_time_init_params) begin_time_idx = [0 for _ in range(route_lanes[start_lane])] for behavior_idx in behaviors: if behavior_idx not in log_info: log_info[behavior_idx] = { "count": 0, "start_end_different_lanes": 0 } if deadlock_optimization and route_has_turn: # if route has a turn, start on the left-most lane start_lane_id = route_lanes[start_lane] - 1 lane_changing_options = [ lane_id for lane_id in range(route_lanes[end_lane]) ] if (len(lane_changing_options) > 0 or random.uniform(0, 1) < start_end_on_different_lanes_probability): end_lane_id = random.choice(lane_changing_options) else: end_lane_id = start_lane_id else: start_lane_id = random.randint(0, route_lanes[start_lane] - 1) end_lane_id = random.randint(0, route_lanes[end_lane] - 1) # set begin/end time begin_time = begin_times[start_lane_id][begin_time_idx[start_lane_id]] begin_time_idx[start_lane_id] += 1 end_time = begin_time + 3600 # 1 hour if "stopwatcher" in behavior_idx: start_lane_id = route_lanes[stopwatcher_info["direction"][0]] - 1 end_lane_id = route_lanes[stopwatcher_info["direction"][1]] - 1 flows.append( generate_stopwatcher( stopwatcher_behavior=stopwatcher_info["behavior"], stopwatcher_route=stopwatcher_info["direction"], begin_time=begin_time, start_lane_id=start_lane_id, end_lane_id=end_lane_id, )) else: behavior = get_social_vehicle_behavior(behavior_idx) flows.append( Flow( begin=begin_time, end=end_time, route=Route( begin=(f"edge-{start_lane}", start_lane_id, "base"), end=(f"edge-{end_lane}", end_lane_id, "max"), ), rate=1, actors={behavior: 1.0}, )) log_info[behavior_idx]["count"] += 1 log_info["route_distribution"] = route_distribution log_info["num_vehicles"] = (num_vehicles + 1 if stopwatcher_added else num_vehicles) log_info[ "start_end_on_different_lanes_probability"] = start_end_on_different_lanes_probability log_info[behavior_idx]["start_end_different_lanes"] += ( 1 if start_lane_id != end_lane_id else 0) return flows, log_info
("west-WE", "east-WE"), ("east-EW", "west-EW"), ], "all": [ ("west-WE", "east-WE"), ("east-EW", "west-EW"), ("west-WE", "south-NS"), ("east-EW", "south-NS"), ], }.items(): traffic = Traffic(flows=[ Flow( route=Route(begin=(f"edge-{r[0]}", 0, 20), end=(f"edge-{r[1]}", 0, -20)), rate=total_rate, # Share the total rate amongst all flows actors={ impatient_car: (1.0 / len(routes)) * 0.5, patient_car: (1.0 / len(routes)) * 0.5, }, ) for r in routes ]) gen_traffic(scenario, traffic, name=name) gen_traffic( scenario, Traffic(flows=[ Flow( route=RandomRoute(), rate=3600, actors={TrafficActor(name="car"): 1.0},
car_type_patient = patient_car car_type_patient_ratio = 0.5 car_name_patient = "patient_car" car_type_impatient = impatient_car car_type_impatient_ratio = 0.5 car_name_impatient = "impatient_car" traffic = Traffic(flows=[ Flow( route=RandomRoute(), begin=0, end=1 * 60 * 60, # make sure end time is larger than the time of one episode rate=30, actors={ car_type_patient: car_type_patient_ratio, car_type_impatient: car_type_impatient_ratio }, ) for i in range(sv_num) ]) print( f"generate flow with {sv_num} social {car_name_patient, car_name_impatient} vehicles in {scenario_path} " ) gen_traffic( scenario_path, traffic, name= f"{sv_num * 2}_{car_name_patient}_{car_type_patient_ratio}_{car_name_impatient}_{car_type_impatient_ratio}",
# generate social vehicles ######################################### sv_nums = [60, 60, 60, 60] seed = 45 traffic_actor = TrafficActor(name="car", speed=Distribution(sigma=0.1, mean=0.3),) for scenario_path, sv_num in zip(scenario_paths, sv_nums): traffic = Traffic( flows=[ Flow( route=RandomRoute(), begin=0, end=1 * 60 * 60, # make sure end time is larger than the time of one episode rate=60, actors={traffic_actor: 1}, ) for i in range(sv_num) ] ) print(f"generate flow with {sv_num} social vehicles in {scenario_path.name} ") gen_traffic( scenario_path, traffic, name="all", output_dir=scenario_path,
TrafficActor, ) ego_missions = [ Mission( route=Route(begin=("edge-west-WE", 0, 10), end=("edge-west-WE", 0, "max")), task=CutIn(), ), ] scenario = Scenario( traffic={ "basic": Traffic(flows=[ Flow( route=Route(begin=("edge-west-WE", 1, 10), end=("edge-west-WE", 1, "max")), rate=400, actors={TrafficActor(name="car"): 1.0}, ) ]) }, ego_missions=ego_missions, ) gen_scenario( scenario=scenario, output_dir=Path(__file__).parent, )
Mission( route=Route(begin=("edge-south-SN", 1, 10), end=("edge-north-SN", 1, 8)), ), ] stright_traffic_actor = TrafficActor( name="car", speed=Distribution(sigma=0.2, mean=1), lane_changing_model=LaneChangingModel(impatience=0), junction_model=JunctionModel( drive_after_red_time=1.5, drive_after_yellow_time=1.0, impatience=0.5 ), ) scenario = Scenario( traffic={ "basic": Traffic( flows=[ Flow( route=RandomRoute(), rate=1, actors={stright_traffic_actor: 1.0}, ) for i in range(social_vehicle_num) ] ) }, ego_missions=ego_missions, ) gen_scenario(scenario=scenario, output_dir=Path(__file__).parent)
ego_missions = [ Mission( route=Route(begin=("edge-south-SN", 1, 10), end=("edge-west-EW", 1, 8)), # begin 45.6 ), ] left_traffic_actor = TrafficActor( name="car", speed=Distribution(sigma=0.2, mean=1), lane_changing_model=LaneChangingModel(impatience=0), # junction_model=JunctionModel( # drive_after_yellow_time=1.0, impatience=0) ) scenario = Scenario( traffic={ "basic": Traffic(flows=[ Flow( route=RandomRoute(), rate=1, actors={left_traffic_actor: 1.0}, ) for i in range(social_vehicle_num) ]) }, ego_missions=ego_missions, ) gen_scenario(scenario=scenario, output_dir=Path(__file__).parent)
) horizontal_routes = [("west-WE", "east-WE"), ("east-EW", "west-EW")] turn_left_routes = [("east-EW", "south-NS")] turn_right_routes = [("west-WE", "south-NS")] for name, routes in { "horizontal": horizontal_routes, "turns": turn_left_routes + turn_right_routes, }.items(): traffic = Traffic(flows=[ Flow( route=Route(begin=(f"edge-{r[0]}", 0, "random"), end=(f"edge-{r[1]}", 0, "max")), rate=random.randint(50, 100), actors={ cooperative_car: 0.35, car: 0.20, aggressive_car: 0.45 }, ) for r in routes ]) for seed in [0, 5]: gen_traffic( scenario, traffic, name=f"{name}-{seed}", seed=seed, ) # Social Agents
seed(seed_) scenario_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../dataset_public/3lane") traffic_actor = TrafficActor( name="car", speed=Distribution(sigma=0.2, mean=0.8), ) # add 10 social vehicles with random routes. traffic = Traffic(flows=[ # generate flows last for 10 hours Flow( route=RandomRoute(), begin=0, end=10 * 60 * 60, rate=25, actors={traffic_actor: 1}, ) for i in range(10) ]) gen_traffic( scenario_path, traffic, name="all", output_dir=scenario_path, seed=seed_, overwrite=True, )