Exemple #1
0
    def setUp(self):
        # create a 2-lane ring road network
        additional_net_params = {
            "length": 230,
            "lanes": 3,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

        # turn on starting position shuffle
        env_params = EnvParams(starting_position_shuffle=True,
                               additional_params=ADDITIONAL_ENV_PARAMS)

        # place 5 vehicles in the network (we need at least more than 1)
        vehicles = Vehicles()
        vehicles.add(veh_id="test",
                     acceleration_controller=(IDMController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     sumo_car_following_params=SumoCarFollowingParams(
                         accel=1000, decel=1000),
                     num_vehicles=5)

        # create the environment and scenario classes for a ring road
        self.env, scenario = ring_road_exp_setup(net_params=net_params,
                                                 env_params=env_params,
                                                 vehicles=vehicles)
Exemple #2
0
    def runSpeedLaneChangeModes(self):
        """
        Checks to make sure vehicle class correctly specifies lane change and
        speed modes
        """
        vehicles = Vehicles()
        vehicles.add("typeA",
                     acceleration_controller=(IDMController, {}),
                     speed_mode='no_collide',
                     lane_change_mode="no_lat_collide")

        self.assertEqual(vehicles.get_speed_mode("typeA_0"), 1)
        self.assertEqual(vehicles.get_lane_change_mode("typeA_0"), 256)

        vehicles.add("typeB",
                     acceleration_controller=(IDMController, {}),
                     speed_mode='aggressive',
                     lane_change_mode="strategic")

        self.assertEqual(vehicles.get_speed_mode("typeB_0"), 0)
        self.assertEqual(vehicles.get_lane_change_mode("typeB_0"), 853)

        vehicles.add("typeC",
                     acceleration_controller=(IDMController, {}),
                     speed_mode=31,
                     lane_change_mode=277)
        self.assertEqual(vehicles.get_speed_mode("typeC_0"), 31)
        self.assertEqual(vehicles.get_lane_change_mode("typeC_0"), 277)
Exemple #3
0
    def test_add_vehicles_human(self):
        """
        Ensures that added human vehicles are placed in the current vehicle
        IDs, and that the number of vehicles is correct.
        """
        # generate a vehicles class
        vehicles = Vehicles()

        # vehicles whose acceleration and LC are controlled by sumo
        vehicles.add("test_1", num_vehicles=1)

        # vehicles whose acceleration are controlled by sumo
        vehicles.add("test_2",
                     num_vehicles=2,
                     lane_change_controller=(StaticLaneChanger, {}))

        # vehicles whose LC are controlled by sumo
        vehicles.add("test_3",
                     num_vehicles=4,
                     acceleration_controller=(IDMController, {}))

        self.assertEqual(vehicles.num_vehicles, 7)
        self.assertEqual(len(vehicles.get_ids()), 7)
        self.assertEqual(len(vehicles.get_rl_ids()), 0)
        self.assertEqual(len(vehicles.get_human_ids()), 7)
        self.assertEqual(len(vehicles.get_controlled_ids()), 4)
        self.assertEqual(len(vehicles.get_controlled_lc_ids()), 2)
    def setUp_gen_start_pos(self, initial_config=InitialConfig()):
        # ensures that the random starting position method is being used
        initial_config.spacing = "random"

        # create a multi-lane ring road network
        additional_net_params = {
            "length": 230,
            "lanes": 4,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

        # place 5 vehicles in the network (we need at least more than 1)
        vehicles = Vehicles()
        vehicles.add(
            veh_id="test",
            acceleration_controller=(IDMController, {}),
            routing_controller=(ContinuousRouter, {}),
            num_vehicles=5)

        # create the environment and scenario classes for a ring road
        self.env, scenario = ring_road_exp_setup(
            net_params=net_params,
            initial_config=initial_config,
            vehicles=vehicles)
    def setUp_gen_start_pos(self, initial_config=InitialConfig()):
        """
        Replace with any scenario you would like to test gen_even_start_pos on.
        In ordering for all the tests to be meaningful, the scenario must
        contain MORE THAN TWO LANES.
        """
        # create a multi-lane ring road network
        additional_net_params = {
            "length": 230,
            "lanes": 4,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

        # place 5 vehicles in the network (we need at least more than 1)
        vehicles = Vehicles()
        vehicles.add(
            veh_id="test",
            acceleration_controller=(IDMController, {}),
            routing_controller=(ContinuousRouter, {}),
            num_vehicles=15)

        # create the environment and scenario classes for a ring road
        self.env, scenario = ring_road_exp_setup(
            net_params=net_params,
            initial_config=initial_config,
            vehicles=vehicles)
Exemple #6
0
def start():
    """Start a environment object with ray."""
    sumo_params = SumoParams(sim_step=0.1, render=False)

    vehicles = Vehicles()
    vehicles.add(
        veh_id="idm",
        acceleration_controller=(IDMController, {}),
        routing_controller=(ContinuousRouter, {}),
        num_vehicles=22)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(additional_params=additional_net_params)

    initial_config = InitialConfig(bunching=20)

    scenario = LoopScenario(
        name="sugiyama",
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)
    env._close()
Exemple #7
0
def start():
    sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo")

    sumo_params.sumo_binary = 'sumo'

    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=22)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(additional_params=additional_net_params)

    initial_config = InitialConfig(bunching=20)

    scenario = LoopScenario(name="sugiyama",
                            generator_class=CircleGenerator,
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)
    env.start_sumo()
Exemple #8
0
def figure_eight_example(sumo_binary=None):
    sumo_params = SumoParams(sumo_binary="sumo-gui")

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(StaticLaneChanger, {}),
                 routing_controller=(ContinuousRouter, {}),
                 speed_mode="no_collide",
                 initial_speed=0,
                 num_vehicles=14)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(no_internal_links=False,
                           additional_params=additional_net_params)

    scenario = Figure8Scenario(name="figure8",
                               generator_class=Figure8Generator,
                               vehicles=vehicles,
                               net_params=net_params)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Exemple #9
0
def run_task(*_):
    sumo_params = SumoParams(sim_step=0.1,
                             sumo_binary="sumo")

    vehicles = Vehicles()
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 speed_mode="no_collide",
                 num_vehicles=1)
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {"noise": 0.2}),
                 routing_controller=(ContinuousRouter, {}),
                 speed_mode="no_collide",
                 num_vehicles=13)

    additional_env_params = {"target_velocity": 20,
                             "max_accel": 3, "max_decel": 3}
    env_params = EnvParams(horizon=HORIZON,
                           additional_params=additional_env_params)

    additional_net_params = {"radius_ring": 30, "lanes": 1, "speed_limit": 30,
                             "resolution": 40}
    net_params = NetParams(no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="uniform")

    print("XXX name", exp_tag)
    scenario = Figure8Scenario(exp_tag, Figure8Generator, vehicles, net_params,
                               initial_config=initial_config)

    env_name = "AccelEnv"
    pass_params = (env_name, sumo_params, vehicles, env_params, net_params,
                   initial_config, scenario)

    env = GymEnv(env_name, record_video=False, register_params=pass_params)
    horizon = env.horizon
    env = normalize(env)

    policy = GaussianMLPPolicy(
        env_spec=env.spec,
        hidden_sizes=(16, 16)
    )

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=15000,
        max_path_length=horizon,
        n_itr=500,
        # whole_paths=True,
        discount=0.999,
        # step_size=v["step_size"],
    )
    algo.train(),
def two_loops_one_merging_exp_setup(vehicles=None):
    sumo_params = SumoParams(sim_step=0.1, render=False)

    if vehicles is None:
        vehicles = Vehicles()
        vehicles.add(
            veh_id="rl",
            acceleration_controller=(RLController, {}),
            lane_change_controller=(StaticLaneChanger, {}),
            sumo_car_following_params=SumoCarFollowingParams(
                speed_mode="no_collide",
            ),
            num_vehicles=1)
        vehicles.add(
            veh_id="idm",
            acceleration_controller=(IDMController, {}),
            lane_change_controller=(StaticLaneChanger, {}),
            sumo_car_following_params=SumoCarFollowingParams(
                speed_mode="no_collide",
            ),
            num_vehicles=5)
        vehicles.add(
            veh_id="merge-idm",
            acceleration_controller=(IDMController, {}),
            lane_change_controller=(StaticLaneChanger, {}),
            sumo_car_following_params=SumoCarFollowingParams(
                speed_mode="no_collide",
            ),
            num_vehicles=5)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = {
        "ring_radius": 50,
        "lane_length": 75,
        "inner_lanes": 3,
        "outer_lanes": 2,
        "speed_limit": 30,
        "resolution": 40
    }

    net_params = NetParams(
        no_internal_links=False, additional_params=additional_net_params)

    initial_config = InitialConfig(
        spacing="custom",
        lanes_distribution=1,
        additional_params={"merge_bunching": 0})

    scenario = TwoLoopsOneMergingScenario(
        "loop-merges",
        vehicles,
        net_params,
        initial_config=initial_config)

    env = TwoLoopsMergePOEnv(env_params, sumo_params, scenario)

    return env, scenario
Exemple #11
0
def grid_example(sumo_binary=None):
    inner_length = 300
    long_length = 500
    short_length = 300
    n = 2
    m = 3
    num_cars_left = 20
    num_cars_right = 20
    num_cars_top = 20
    num_cars_bot = 20
    tot_cars = (num_cars_left + num_cars_right) * m \
        + (num_cars_top + num_cars_bot) * n

    grid_array = {
        "short_length": short_length,
        "inner_length": inner_length,
        "long_length": long_length,
        "row_num": n,
        "col_num": m,
        "cars_left": num_cars_left,
        "cars_right": num_cars_right,
        "cars_top": num_cars_top,
        "cars_bot": num_cars_bot
    }

    sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo-gui")

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 routing_controller=(GridRouter, {}),
                 num_vehicles=tot_cars)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = {
        "grid_array": grid_array,
        "speed_limit": 35,
        "horizontal_lanes": 1,
        "vertical_lanes": 1,
        "traffic_lights": True
    }
    net_params = NetParams(no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig()

    scenario = SimpleGridScenario(name="grid-intersection",
                                  generator_class=SimpleGridGenerator,
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Exemple #12
0
    def test_collide_inflows(self):
        """Tests collisions in the presence of inflows."""
        # create the environment and scenario classes for a ring road
        sumo_params = SumoParams(sim_step=1, render=False)
        total_vehicles = 12
        vehicles = Vehicles()
        vehicles.add(
            veh_id="idm",
            acceleration_controller=(SumoCarFollowingController, {}),
            routing_controller=(GridRouter, {}),
            sumo_car_following_params=SumoCarFollowingParams(
                tau=0.1, carFollowModel="Krauss", minGap=2.5),
            num_vehicles=total_vehicles,
            speed_mode=0b00000)
        grid_array = {
            "short_length": 100,
            "inner_length": 100,
            "long_length": 100,
            "row_num": 1,
            "col_num": 1,
            "cars_left": 3,
            "cars_right": 3,
            "cars_top": 3,
            "cars_bot": 3
        }

        additional_net_params = {
            "speed_limit": 35,
            "grid_array": grid_array,
            "horizontal_lanes": 1,
            "vertical_lanes": 1
        }

        inflows = InFlows()
        inflows.add(veh_type="idm", edge="bot0_0", vehs_per_hour=1000)
        inflows.add(veh_type="idm", edge="top0_1", vehs_per_hour=1000)

        net_params = NetParams(
            no_internal_links=False,
            inflows=inflows,
            additional_params=additional_net_params)

        self.env, self.scenario = grid_mxn_exp_setup(
            row_num=1,
            col_num=1,
            sumo_params=sumo_params,
            vehicles=vehicles,
            net_params=net_params)

        # go through the env and set all the lights to green
        for i in range(self.env.rows * self.env.cols):
            self.env.traci_connection.trafficlight.setRedYellowGreenState(
                'center' + str(i), "gggggggggggg")

        # instantiate an experiment class
        self.exp = SumoExperiment(self.env, self.scenario)

        self.exp.run(50, 50)
Exemple #13
0
def highway_example(sumo_binary=None):
    """
    Perform a simulation of vehicles on a highway.

    Parameters
    ----------
    sumo_binary: bool, optional
        specifies whether to use sumo's gui during execution

    Returns
    -------
    exp: flow.core.SumoExperiment type
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a figure eight.
    """
    sumo_params = SumoParams(sumo_binary="sumo-gui")

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {}),
                 num_vehicles=20)
    vehicles.add(veh_id="human2",
                 acceleration_controller=(IDMController, {}),
                 num_vehicles=20)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    inflow = InFlows()
    inflow.add(veh_type="human",
               edge="highway",
               probability=0.25,
               departLane="free",
               departSpeed=20)
    inflow.add(veh_type="human2",
               edge="highway",
               probability=0.25,
               departLane="free",
               departSpeed=20)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(in_flows=inflow,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="uniform", shuffle=True)

    scenario = HighwayScenario(name="highway",
                               generator_class=HighwayGenerator,
                               vehicles=vehicles,
                               net_params=net_params,
                               initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Exemple #14
0
    def reset(self):
        if self.env_params.additional_params.get("reset_inflow"):
            flow_rate = np.random.uniform(1000, 2000) * self.scaling
            for _ in range(100):
                try:
                    inflow = InFlows()
                    inflow.add(veh_type="followerstopper",
                               edge="1",
                               vehs_per_hour=flow_rate * .1,
                               departLane="random",
                               departSpeed=10)
                    inflow.add(veh_type="human",
                               edge="1",
                               vehs_per_hour=flow_rate * .9,
                               departLane="random",
                               departSpeed=10)

                    additional_net_params = {"scaling": self.scaling}
                    net_params = NetParams(
                        in_flows=inflow,
                        no_internal_links=False,
                        additional_params=additional_net_params)

                    vehicles = Vehicles()
                    vehicles.add(
                        veh_id="human",
                        speed_mode=9,
                        lane_change_controller=(SumoLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        lane_change_mode=0,  # 1621,#0b100000101,
                        num_vehicles=1 * self.scaling)
                    vehicles.add(
                        veh_id="followerstopper",
                        acceleration_controller=(RLController, {}),
                        lane_change_controller=(SumoLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        speed_mode=9,
                        lane_change_mode=0,
                        num_vehicles=1 * self.scaling)
                    self.vehicles = vehicles
                    self.scenario = self.scenario.__class__(
                        name=self.scenario.name,
                        generator_class=self.scenario.generator_class,
                        vehicles=vehicles,
                        net_params=net_params,
                        initial_config=self.scenario.initial_config,
                        traffic_lights=self.scenario.traffic_lights)

                except Exception as e:
                    print('error on reset ', e)
                    # perform the generic reset function
        observation = super().reset()

        # reset the timer to zero
        self.time_counter = 0

        return observation
def two_loops_merge_straight_example(sumo_binary=None):
    sumo_params = SumoParams(sim_step=0.1, emission_path="./data/",
                             sumo_binary="sumo-gui")

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    # note that the vehicles are added sequentially by the generator,
    # so place the merging vehicles after the vehicles in the ring
    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=7,
                 sumo_car_following_params=SumoCarFollowingParams(
                     minGap=0.0, tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())
    vehicles.add(veh_id="merge-idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=10,
                 sumo_car_following_params=SumoCarFollowingParams(
                     minGap=0.01, tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["ring_radius"] = 50
    additional_net_params["inner_lanes"] = 1
    additional_net_params["outer_lanes"] = 1
    additional_net_params["lane_length"] = 75
    net_params = NetParams(
        no_internal_links=False,
        additional_params=additional_net_params
    )

    initial_config = InitialConfig(
        x0=50,
        spacing="uniform",
        additional_params={"merge_bunching": 0}
    )

    scenario = TwoLoopsOneMergingScenario(
        name="two-loop-one-merging",
        generator_class=TwoLoopOneMergingGenerator,
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config
    )

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Exemple #16
0
def get_params(render=False):
    """Create flow-specific parameters for stabilizing the ring experiments.

    Parameters
    ----------
    render : bool, optional
        specifies whether the visualizer is active

    Returns
    -------
    flow.core.params.SumoParams
        sumo-specific parameters
    flow.core.params.EnvParams
        environment-speciifc parameters
    flow.scenarios.Scenario
        a flow-compatible scenario object
    """
    sumo_params = SumoParams(sim_step=0.4,
                             sumo_binary="sumo-gui" if render else "sumo",
                             seed=0)

    vehicles = Vehicles()
    vehicles.add(
        veh_id="rl",
        acceleration_controller=(RLController, {}),
        routing_controller=(ContinuousRouter, {}),
        # speed_mode="aggressive",
        num_vehicles=1)
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0
                 }),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=21)

    env_params = EnvParams(horizon=HORIZON, warmup_steps=int(750 / 4))

    net_params = NetParams(additional_params={
        "length": 260,
        "lanes": 1,
        "speed_limit": 30,
        "resolution": 40
    })

    initial_config = InitialConfig(
        spacing="uniform",
        bunching=50,
    )

    scenario = LoopScenario(name="stabilizing_the_ring",
                            generator_class=CircleGenerator,
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config)

    return sumo_params, env_params, scenario
 def setUp(self):
     vehicles = Vehicles()
     vehicles.add("test")
     net_params = NetParams(additional_params=LOOP_PARAMS)
     env_params = EnvParams()
     sumo_params = SumoParams()
     scenario = LoopScenario("test_loop",
                             vehicles=vehicles,
                             net_params=net_params)
     self.env = TestEnv(env_params, sumo_params, scenario)
Exemple #18
0
    def setUp(self):
        vehicles = Vehicles()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {}),
                     routing_controller=(GridRouter, {}),
                     sumo_car_following_params=SumoCarFollowingParams(
                         min_gap=2.5, tau=1.1),
                     num_vehicles=16)

        self.env, scenario = grid_mxn_exp_setup(row_num=1, col_num=3,
                                                vehicles=vehicles)
Exemple #19
0
def merge_example(sumo_binary=None):
    sumo_params = SumoParams(sumo_binary="sumo-gui",
                             emission_path="./data/",
                             sim_step=0.2,
                             restart_instance=True)

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 num_vehicles=5)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS,
                           sims_per_step=5,
                           warmup_steps=0)

    inflow = InFlows()
    inflow.add(veh_type="human",
               edge="inflow_highway",
               vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE,
               departLane="free",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="inflow_merge",
               vehs_per_hour=100,
               departLane="free",
               departSpeed=7.5)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["merge_lanes"] = 1
    additional_net_params["highway_lanes"] = 1
    additional_net_params["pre_merge_length"] = 500
    net_params = NetParams(in_flows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="uniform",
                                   perturbation=5.0,
                                   lanes_distribution=float("inf"))

    scenario = MergeScenario(name="merge-baseline",
                             generator_class=MergeGenerator,
                             vehicles=vehicles,
                             net_params=net_params,
                             initial_config=initial_config)

    env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Exemple #20
0
    def setUp(self):
        # place 15 vehicles in the network (we need at least more than 1)
        vehicles = Vehicles()
        vehicles.add(veh_id="test",
                     acceleration_controller=(IDMController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     num_vehicles=15)

        initial_config = InitialConfig(x0=150)

        # create the environment and scenario classes for a ring road
        self.env, scenario = figure_eight_exp_setup(
            initial_config=initial_config, vehicles=vehicles)
    def setUp(self):
        vehicles = Vehicles()
        vehicles.add("rl", acceleration_controller=(RLController, {}))
        vehicles.add("human", acceleration_controller=(IDMController, {}))

        self.sumo_params = SumoParams()
        self.scenario = LoopScenario(
            name="test_merge",
            vehicles=vehicles,
            net_params=NetParams(additional_params=LOOP_PARAMS.copy()),
        )
        params = {"max_accel": 1, "max_decel": 1, "ring_length": [220, 270]}
        self.env_params = EnvParams(additional_params=params)
    def test_min_delay(self):
        """Test the min_delay method."""
        # try the case of an environment with no vehicles
        vehicles = Vehicles()
        env, scenario = ring_road_exp_setup(vehicles=vehicles)

        # check that the reward function return 0 in the case of no vehicles
        self.assertEqual(min_delay(env), 0)

        # try the case of multiple vehicles
        vehicles = Vehicles()
        vehicles.add("test", num_vehicles=10)
        env, scenario = ring_road_exp_setup(vehicles=vehicles)

        # check the min_delay upon reset
        self.assertAlmostEqual(min_delay(env), 0)

        # change the speed of one vehicle
        env.vehicles.test_set_speed("test_0", 10)

        # check the min_delay with the new speed
        self.assertAlmostEqual(min_delay(env), 0.0333333333333)
    def test_no_crash_LinearOVM(self):
        vehicles = Vehicles()
        vehicles.add_vehicles(veh_id="test",
                              acceleration_controller=(LinearOVM, {}),
                              routing_controller=(ContinuousRouter, {}),
                              num_vehicles=10)

        self.setUp_failsafe(vehicles=vehicles)

        # run the experiment, see if it fails
        self.exp.run(1, 200)

        self.tearDown_failsafe()
Exemple #24
0
    def test_all(self):
        vehicles = Vehicles()
        vehicles.add("human", num_vehicles=10)
        # add an RL vehicle to ensure that its color will be distinct
        vehicles.add("rl", acceleration_controller=(RLController, {}),
                     num_vehicles=1)
        _, scenario = ring_road_exp_setup(vehicles=vehicles)

        # we will use the generic environment to ensure this applies to all
        # environments
        sumo_params = SumoParams()
        env_params = EnvParams()
        env = Env(sumo_params=sumo_params,
                  env_params=env_params,
                  scenario=scenario)

        # set one vehicle as observed
        env.vehicles.set_observed("human_0")

        # update the colors of all vehicles
        env.update_vehicle_colors()
        env.traci_connection.simulationStep()

        # check that, when rendering is off, the colors don't change (this
        # avoids unnecessary API calls)
        for veh_id in env.vehicles.get_ids():
            self.assertEqual(
                env.traci_connection.vehicle.getColor(veh_id), WHITE)

        # a little hack to ensure the colors change
        env.sumo_params.render = True

        # set one vehicle as observed
        env.vehicles.set_observed("human_0")

        # update the colors of all vehicles
        env.update_vehicle_colors()
        env.traci_connection.simulationStep()

        # check the colors of all vehicles
        for veh_id in env.vehicles.get_ids():
            if veh_id == "human_0":
                self.assertEqual(
                    env.traci_connection.vehicle.getColor(veh_id), CYAN)
            elif veh_id == "rl_0":
                self.assertEqual(
                    env.traci_connection.vehicle.getColor(veh_id), RED)
            else:
                self.assertEqual(
                    env.traci_connection.vehicle.getColor(veh_id), WHITE)
Exemple #25
0
    def test_controlled_id_params(self):
        """
        Ensures that, if a vehicle is not a sumo vehicle, then minGap is set to
        zero so that all headway values are correct.
        """
        # check that, if the vehicle is not a SumoCarFollowingController
        # vehicle, then its minGap is equal to 0
        vehicles = Vehicles()
        vehicles.add("typeA",
                     acceleration_controller=(IDMController, {}),
                     speed_mode='no_collide',
                     lane_change_mode="no_lat_collide")
        self.assertEqual(vehicles.types[0][1]["minGap"], 0)

        # check that, if the vehicle is a SumoCarFollowingController vehicle,
        # then its minGap, accel, and decel are set to default
        vehicles = Vehicles()
        vehicles.add("typeA",
                     acceleration_controller=(SumoCarFollowingController, {}),
                     speed_mode='no_collide',
                     lane_change_mode="no_lat_collide")
        default_mingap = SumoCarFollowingParams().controller_params["minGap"]
        self.assertEqual(vehicles.types[0][1]["minGap"], default_mingap)
Exemple #26
0
    def setUp(self):
        # place 15 vehicles in the network (we need at least more than 1)
        vehicles = Vehicles()
        vehicles.add(veh_id="test",
                     acceleration_controller=(IDMController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     num_vehicles=50)

        initial_config = InitialConfig(spacing="random", lanes_distribution=5)

        # create the environment and scenario classes for a variable lanes per
        # edge ring road
        self.env, scenario = variable_lanes_exp_setup(
            vehicles=vehicles, initial_config=initial_config)
Exemple #27
0
    def test_no_junctions(self):
        """
        Test the above mentioned methods in the absence of junctions.
        """
        # setup a network with no junctions and several vehicles
        # also, setup with a deterministic starting position to ensure that the
        # headways/lane leaders are what is expected
        additional_net_params = {
            "length": 230,
            "lanes": 3,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

        vehicles = Vehicles()
        vehicles.add(
            veh_id="test",
            acceleration_controller=(RLController, {}),
            num_vehicles=21)

        initial_config = InitialConfig(lanes_distribution=float("inf"))

        env, scenario = ring_road_exp_setup(
            net_params=net_params,
            vehicles=vehicles,
            initial_config=initial_config)
        env.reset()

        # check the lane leaders method is outputting the right values
        actual_lane_leaders = env.vehicles.get_lane_leaders("test_0")
        expected_lane_leaders = ["test_3", "test_1", "test_2"]
        self.assertCountEqual(actual_lane_leaders, expected_lane_leaders)

        # check the lane headways is outputting the right values
        actual_lane_head = env.vehicles.get_lane_headways("test_0")
        expected_lane_head = [27.85714285714286, -5, -5]
        self.assertCountEqual(actual_lane_head, expected_lane_head)

        # check the lane followers method is outputting the right values
        actual_lane_followers = env.vehicles.get_lane_followers("test_0")
        expected_lane_followers = ["test_18", "test_19", "test_20"]
        self.assertCountEqual(actual_lane_followers, expected_lane_followers)

        # check the lane tailways is outputting the right values
        actual_lane_tail = env.vehicles.get_lane_tailways("test_0")
        expected_lane_tail = [27.85714285714286] * 3
        np.testing.assert_array_almost_equal(actual_lane_tail,
                                             expected_lane_tail)
Exemple #28
0
    def test_add_vehicles_rl(self):
        """
        Ensures that added rl vehicles are placed in the current vehicle IDs,
        and that the number of vehicles is correct.
        """
        vehicles = Vehicles()
        vehicles.add("test_rl", num_vehicles=10,
                     acceleration_controller=(RLController, {}))

        self.assertEqual(vehicles.num_vehicles, 10)
        self.assertEqual(len(vehicles.get_ids()), 10)
        self.assertEqual(len(vehicles.get_rl_ids()), 10)
        self.assertEqual(len(vehicles.get_human_ids()), 0)
        self.assertEqual(len(vehicles.get_controlled_ids()), 0)
        self.assertEqual(len(vehicles.get_controlled_lc_ids()), 0)
Exemple #29
0
    def test_no_crash_OVM(self):
        vehicles = Vehicles()
        vehicles.add(
            veh_id="test",
            acceleration_controller=(OVMController,
                                     {"fail_safe": "safe_velocity"}),
            routing_controller=(ContinuousRouter, {}),
            num_vehicles=10,
        )

        self.setUp_failsafe(vehicles=vehicles)

        # run the experiment, see if it fails
        self.exp.run(1, 200)

        self.tearDown_failsafe()
Exemple #30
0
    def setUp(self):
        # add a few vehicles to the network using the requested model
        # also make sure that the input params are what is expected
        contr_params = {"v0": 30, "b": 1.5, "delta": 4, "s0": 2, "noise": 0}

        vehicles = Vehicles()
        vehicles.add(veh_id="test",
                     acceleration_controller=(IDMController, contr_params),
                     routing_controller=(ContinuousRouter, {}),
                     sumo_car_following_params=SumoCarFollowingParams(tau=1,
                                                                      accel=1,
                                                                      decel=5),
                     num_vehicles=5)

        # create the environment and scenario classes for a ring road
        self.env, scenario = ring_road_exp_setup(vehicles=vehicles)