Beispiel #1
0
                required_speed=2,
            ),
            Via(
                "edge-west-EW",
                lane_offset=70,
                lane_index=1,
                required_speed=2,
            ),
        ),
    ),
]

scenario = Scenario(
    traffic={
        "basic":
        Traffic(flows=[
            Flow(
                route=RandomRoute(),
                rate=3600,
                actors={TrafficActor(name="car"): 1.0},
            )
        ])
    },
    ego_missions=ego_missions,
)

gen_scenario(
    scenario=scenario,
    output_dir=Path(__file__).parent,
)
Beispiel #2
0
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}",
    output_dir=scenario_path,
Beispiel #3
0
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)
Beispiel #4
0
            ("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},
Beispiel #5
0
)

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
Beispiel #6
0
    UTurn,
)

ego_missions = [
    Mission(
        route=Route(begin=("edge-west-WE", 1, 30),
                    end=("edge-west-EW", 0, 100)),
        task=UTurn(initial_speed=20),
    ),
]

scenario = Scenario(
    traffic={
        "basic":
        Traffic(flows=[
            Flow(
                route=Route(begin=("edge-west-EW", 0, 20),
                            end=("edge-west-EW", 1, "max")),
                rate=400,
                actors={TrafficActor(name="car"): 1.0},
            )
        ])
    },
    ego_missions=ego_missions,
)

gen_scenario(
    scenario=scenario,
    output_dir=Path(__file__).parent,
)
Beispiel #7
0
def generate_social_vehicles(
    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
patient_car = TrafficActor(
    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,
    seed=seed,
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,
)
Beispiel #10
0
]

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={
                impatient_car: 0.5,
                patient_car: 0.5
            },
        ) for r in routes
    ])

    gen_traffic(scenario, traffic, name=name)