Esempio n. 1
0
    def test_controlled_id_params(self):
        """
        Ensure 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 SimCarFollowingController
        # vehicle, then its minGap is equal to 0
        vehicles = VehicleParams()
        vehicles.add("typeA",
                     acceleration_controller=(IDMController, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="no_collide", ),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode="no_lat_collide", ))
        self.assertEqual(vehicles.types[0]["type_params"]["minGap"], 0)

        # check that, if the vehicle is a SimCarFollowingController vehicle,
        # then its minGap, accel, and decel are set to default
        vehicles = VehicleParams()
        vehicles.add("typeA",
                     acceleration_controller=(SimCarFollowingController, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="no_collide", ),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode="no_lat_collide", ))
        default_mingap = SumoCarFollowingParams().controller_params["minGap"]
        self.assertEqual(vehicles.types[0]["type_params"]["minGap"],
                         default_mingap)
Esempio n. 2
0
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)
Esempio n. 3
0
    def create_vehicles(self):
        vehicles = VehicleParams()

        # Vehicle parameters https://sumo.dlr.de/docs/Definition_of_Vehicles,_Vehicle_Types,_and_Routes.html
        # Emission classes https://sumo.dlr.de/docs/Models/Emissions/HBEFA3-based.html
        ego_additional_params = {
            "vClass": "passenger",
            "emissionClass": "HBEFA3/PC_G_EU6",
            "guiShape": "passenger/sedan",
        }
        truck_additional_params = {
            "vClass": "trailer",
            "emissionClass": "HBEFA3/HDV_D_EU6",
            "guiShape": "truck/semitrailer",
        }

        # RL vehicles
        vehicles.add(self.vehicle_types[0],
                     acceleration_controller=(RLController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(
                         max_speed=self.vehicle_speeds[0], accel=3.5),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=self.lane_change_modes[0]),
                     num_vehicles=3,
                     additional_parameters=ego_additional_params)
        # Flow vehicles
        vehicles.add(self.vehicle_types[1],
                     acceleration_controller=(IDMController, {
                         "v0":
                         random.uniform(0.7, 1.1) * self.vehicle_speeds[1],
                         "a":
                         3.5
                     }),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=self.lane_change_modes[1]),
                     num_vehicles=0)
        # Flow trucks
        vehicles.add(self.vehicle_types[2],
                     acceleration_controller=(IDMController, {
                         "v0":
                         random.uniform(0.7, 1) * self.vehicle_speeds[2],
                         "a":
                         1.5
                     }),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=self.lane_change_modes[2]),
                     num_vehicles=0,
                     additional_parameters=truck_additional_params)

        return vehicles
Esempio n. 4
0
    def test_speed_lane_change_modes(self):
        """
        Check to make sure vehicle class correctly specifies lane change and
        speed modes
        """
        vehicles = VehicleParams()
        vehicles.add(
            "typeA",
            acceleration_controller=(IDMController, {}),
            car_following_params=SumoCarFollowingParams(
                speed_mode='obey_safe_speed',
            ),
            lane_change_params=SumoLaneChangeParams(
                lane_change_mode="no_lat_collide",
            )
        )

        self.assertEqual(vehicles.type_parameters["typeA"][
                             "car_following_params"].speed_mode, 1)
        self.assertEqual(vehicles.type_parameters["typeA"][
                             "lane_change_params"].lane_change_mode, 512)

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

        self.assertEqual(vehicles.type_parameters["typeB"][
                             "car_following_params"].speed_mode, 0)
        self.assertEqual(vehicles.type_parameters["typeB"][
                             "lane_change_params"].lane_change_mode, 1621)

        vehicles.add(
            "typeC",
            acceleration_controller=(IDMController, {}),
            car_following_params=SumoCarFollowingParams(
                speed_mode=31,
            ),
            lane_change_params=SumoLaneChangeParams(
                lane_change_mode=277
            )
        )

        self.assertEqual(vehicles.type_parameters["typeC"][
                             "car_following_params"].speed_mode, 31)
        self.assertEqual(vehicles.type_parameters["typeC"][
                             "lane_change_params"].lane_change_mode, 277)
Esempio n. 5
0
def eval_veh_params(orig_params):
    """
    Evaluate vehicle parameters.

    This is needed since params like IDMController can't be
    serialized and output to JSON. Thus, the JSON file stores those as
    their names, with string 'IDMController' instead of the object
    ``<flow.controllers.car_following_models.IDMController``. ``util.py``
    imports required car-following models, lane-change controllers,
    routers, and SUMO parameters. This function evaluates those names
    and returns a dict with the actual objects (evaluated) instead of
    their names.

    Parameters
    ----------
    orig_params : dict
        Original vehicle parameters, read in from ``flow_params.json``

    Returns
    -------
    dict
        Evaluated vehicle parameters, string names of objects
        replaced with actual objects
    """
    new_params = orig_params.copy()

    if 'acceleration_controller' in new_params:
        new_controller = (eval(orig_params['acceleration_controller'][0]),
                          orig_params['acceleration_controller'][1])
        new_params['acceleration_controller'] = new_controller
    if 'lane_change_controller' in new_params:
        new_lc_controller = (eval(orig_params['lane_change_controller'][0]),
                             orig_params['lane_change_controller'][1])
        new_params['lane_change_controller'] = new_lc_controller
    if 'routing_controller' in new_params:
        new_route_controller = (eval(orig_params['routing_controller'][0]),
                                orig_params['routing_controller'][1])
        new_params['routing_controller'] = new_route_controller
    if 'sumo_car_following_params' in new_params:
        cf_params = SumoCarFollowingParams()
        cf_params.controller_params = (
            orig_params['sumo_car_following_params']['controller_params'])
        new_params['sumo_car_following_params'] = cf_params

    if 'sumo_lc_params' in new_params:
        lc_params = SumoLaneChangeParams()
        lc_params.controller_params = \
            orig_params['sumo_lc_params']['controller_params']
        new_params['sumo_lc_params'] = lc_params

    return new_params
    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(additional_params=ADDITIONAL_ENV_PARAMS)

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

        # create the environment and network classes for a ring road
        self.env, _, _ = ring_road_exp_setup(net_params=net_params,
                                             env_params=env_params,
                                             vehicles=vehicles)
Esempio n. 7
0
    def _get_lc_params(vtypes):
        """Return the lane change sumo params from vtypes."""
        ret = {}
        for typ in vtypes:
            ret[typ] = SumoLaneChangeParams(lane_change_mode=1621)

        return ret
Esempio n. 8
0
    def test_deprecated(self):
        """Ensures that deprecated forms of the attribute still return proper
        values to the correct attributes"""
        # start a SumoLaneChangeParams with some attributes
        lc_params = SumoLaneChangeParams(model="SL2015",
                                         lcStrategic=1.0,
                                         lcCooperative=1.0,
                                         lcSpeedGain=1.0,
                                         lcKeepRight=1.0,
                                         lcLookaheadLeft=2.0,
                                         lcSpeedGainRight=1.0,
                                         lcSublane=1.0,
                                         lcPushy=0,
                                         lcPushyGap=0.6,
                                         lcAssertive=1)

        # ensure that the attributes match their correct element in the
        # "controller_params" dict
        self.assertAlmostEqual(
            float(lc_params.controller_params["lcStrategic"]), 1)
        self.assertAlmostEqual(
            float(lc_params.controller_params["lcCooperative"]), 1)
        self.assertAlmostEqual(
            float(lc_params.controller_params["lcSpeedGain"]), 1)
        self.assertAlmostEqual(
            float(lc_params.controller_params["lcKeepRight"]), 1)
        self.assertAlmostEqual(float(lc_params.controller_params["lcSublane"]),
                               1)
        self.assertAlmostEqual(float(lc_params.controller_params["lcPushy"]),
                               0)
        self.assertAlmostEqual(
            float(lc_params.controller_params["lcPushyGap"]), 0.6)
        self.assertAlmostEqual(
            float(lc_params.controller_params["lcAssertive"]), 1)
Esempio n. 9
0
    def create_vehicles(self):
        vehicles = VehicleParams()

        # RL vehicle
        vehicles.add(self.vehicle_types[0],
                     acceleration_controller=(RLController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(
                         max_speed=self.vehicle_speeds[0], accel=3.5),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=self.lane_change_modes[0]),
                     num_vehicles=1,
                     additional_parameters=self.
                     additional_vehicle_params["rl_additional_params"])

        # Flow vehicles
        vehicles.add(self.vehicle_types[1],
                     acceleration_controller=(IDMController, {
                         "v0":
                         random.uniform(0.7, 1) * self.vehicle_speeds[1],
                         "a":
                         3.5
                     }),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=self.lane_change_modes[1]),
                     num_vehicles=0,
                     additional_parameters=self.
                     additional_vehicle_params["flow_additional_params"])
        # Flow trucks
        vehicles.add(self.vehicle_types[2],
                     acceleration_controller=(IDMController, {
                         "v0":
                         random.uniform(0.7, 1) * self.vehicle_speeds[2],
                         "a":
                         1.5
                     }),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=self.lane_change_modes[2]),
                     num_vehicles=0,
                     additional_parameters=self.
                     additional_vehicle_params["truck_additional_params"])

        return vehicles
Esempio n. 10
0
def minicity_example(render=None,
                     save_render=None,
                     sight_radius=None,
                     pxpm=None,
                     show_radius=None):
    """
    Perform a simulation of vehicles on modified minicity of University of
    Delaware.

    Parameters
    ----------
    render: bool, optional
        specifies whether to use the gui during execution

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on the minicity scenario.
    """
    sim_params = SumoParams(sim_step=0.25)

    # update sim_params values if provided as inputs
    sim_params.render = render or sim_params.render
    sim_params.save_render = save_render or sim_params.save_render
    sim_params.sight_radius = sight_radius or sim_params.sight_radius
    sim_params.pxpm = pxpm or sim_params.pxpm
    sim_params.show_radius = show_radius or sim_params.show_radius

    vehicles = VehicleParams()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 routing_controller=(MinicityRouter, {}),
                 car_following_params=SumoCarFollowingParams(speed_mode=1, ),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode="no_lat_collide", ),
                 initial_speed=0,
                 num_vehicles=90)
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 routing_controller=(MinicityRouter, {}),
                 car_following_params=SumoCarFollowingParams(
                     speed_mode="obey_safe_speed", ),
                 initial_speed=0,
                 num_vehicles=10)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    net_params = NetParams(no_internal_links=False)

    initial_config = InitialConfig(spacing="random", min_gap=5)
    scenario = MiniCityScenario(name="minicity",
                                vehicles=vehicles,
                                initial_config=initial_config,
                                net_params=net_params)

    env = AccelEnv(env_params, sim_params, scenario)

    return Experiment(env)
Esempio n. 11
0
    def runTest(self):
        """Tests basic usage of the SumoLaneChangeParams object. Ensures that
        the controller_params attribute contains different elements depending
        on whether LC2103 or SL2015 is being used as the model."""
        # test for LC2013
        lc_params_1 = SumoLaneChangeParams(model="LC2013")
        attributes_1 = list(lc_params_1.controller_params.keys())
        # TODO: modify with all elements once the fix is added to sumo
        expected_attributes_1 = ["laneChangeModel", "lcStrategic",
                                 "lcCooperative", "lcSpeedGain", "lcKeepRight"]
        self.assertCountEqual(attributes_1, expected_attributes_1)

        # test for SL2015
        lc_params_2 = SumoLaneChangeParams(model="SL2015")
        attributes_2 = list(lc_params_2.controller_params.keys())
        expected_attributes_2 = \
            ["laneChangeModel", "lcStrategic", "lcCooperative", "lcSpeedGain",
             "lcKeepRight", "lcLookaheadLeft", "lcSpeedGainRight", "lcSublane",
             "lcPushy", "lcPushyGap", "lcAssertive", "lcImpatience",
             "lcTimeToImpatience", "lcAccelLat"]
        self.assertCountEqual(attributes_2, expected_attributes_2)
Esempio n. 12
0
def bottleneck1_baseline(num_runs, render=True):
    """Run script for the bottleneck1 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render: str, optional
            specifies whether to use the gui during execution

    Returns
    -------
        flow.core.experiment.Experiment
            class needed to run simulations
    """
    sim_params = flow_params['sim']
    env_params = flow_params['env']
    net_params = flow_params['net']

    # we want no autonomous vehicles in the simulation
    vehicles = VehicleParams()
    vehicles.add(veh_id='human',
                 car_following_params=SumoCarFollowingParams(speed_mode=9, ),
                 routing_controller=(ContinuousRouter, {}),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode=1621, ),
                 num_vehicles=1 * SCALING)

    # only include human vehicles in inflows
    flow_rate = 2300 * SCALING
    inflow = InFlows()
    inflow.add(veh_type='human',
               edge='1',
               vehs_per_hour=flow_rate,
               departLane='random',
               departSpeed=10)
    net_params.inflows = inflow

    # modify the rendering to match what is requested
    sim_params.render = render

    # set the evaluation flag to True
    env_params.evaluate = True

    flow_params['env'].horizon = env_params.horizon
    exp = Experiment(flow_params)

    results = exp.run(num_runs)

    return np.mean(results['returns']), np.std(results['returns'])
Esempio n. 13
0
    def test_wrong_model(self):
        """Tests that a wrongly specified model defaults the sumo lane change
        model to LC2013."""
        # input a wrong lane change model
        lc_params = SumoLaneChangeParams(model="foo")

        # ensure that the model is set to "LC2013"
        self.assertEqual(lc_params.controller_params["laneChangeModel"],
                         "LC2013")

        # ensure that the correct parameters are currently present
        attributes = list(lc_params.controller_params.keys())
        expected_attributes = ["laneChangeModel", "lcStrategic",
                               "lcCooperative", "lcSpeedGain", "lcKeepRight"]
        self.assertCountEqual(attributes, expected_attributes)
Esempio n. 14
0
def run_task(*_):
    sumo_params = SumoParams(sim_step=0.2, sumo_binary="sumo-gui")

    # note that the vehicles are added sequentially by the generator,
    # so place the merging vehicles after the vehicles in the ring
    vehicles = Vehicles()
    # Inner ring vehicles
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=6,
                 sumo_car_following_params=SumoCarFollowingParams(minGap=0.0,
                                                                  tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())

    # A single learning agent in the inner ring
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 speed_mode="no_collide",
                 num_vehicles=1,
                 sumo_car_following_params=SumoCarFollowingParams(minGap=0.01,
                                                                  tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())

    # Outer ring vehicles
    vehicles.add(veh_id="merge-human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=10,
                 sumo_car_following_params=SumoCarFollowingParams(minGap=0.0,
                                                                  tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())

    env_params = EnvParams(horizon=HORIZON,
                           additional_params={
                               "max_accel": 3,
                               "max_decel": 3,
                               "target_velocity": 10,
                               "n_preceding": 2,
                               "n_following": 2,
                               "n_merging_in": 2,
                           })

    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=exp_tag,
        generator_class=TwoLoopOneMergingGenerator,
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config)

    env_name = "TwoLoopsMergePOEnv"
    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=(100, 50, 25))

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=64 * 3 * horizon,
        max_path_length=horizon,
        # whole_paths=True,
        n_itr=1000,
        discount=0.999,
        # step_size=0.01,
    )
    algo.train()
Esempio n. 15
0
# number of rollouts per training iteration
N_ROLLOUTS = N_CPUS * 4

SCALING = 1
NUM_LANES = 4 * SCALING  # number of lanes in the widest highway
DISABLE_TB = True
DISABLE_RAMP_METER = True
AV_FRAC = 0.10

vehicles = VehicleParams()
vehicles.add(veh_id="human",
             lane_change_controller=(SimLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode="all_checks", ),
             lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
             num_vehicles=1 * SCALING)
vehicles.add(veh_id="followerstopper",
             acceleration_controller=(RLController, {}),
             lane_change_controller=(SimLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             car_following_params=SumoCarFollowingParams(speed_mode=9, ),
             lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
             num_vehicles=1 * SCALING)

controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True),
                       ("4", 2, True), ("5", 1, False)]
num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)]
additional_env_params = {
    "target_velocity": 40,
    "disable_tb": True,
Esempio n. 16
0
    'grid_array': grid_array,
    'horizontal_lanes': 2,
    'vertical_lanes': 2,
    'traffic_lights': True  ######### !
}

vehicles = VehicleParams()
vehicles.add(veh_id='idm',
             acceleration_controller=(SimCarFollowingController, {}),
             car_following_params=SumoCarFollowingParams(
                 min_gap=2.5,
                 max_speed=V_ENTER,
                 speed_mode="all_checks",
                 tau=1.1),
             lane_change_params=SumoLaneChangeParams(
                 lane_change_mode="strategic",
                 model="LC2013",
             ),
             lane_change_controller=(StaticLaneChanger, {}),
             routing_controller=(MinicityRouter, {}),
             num_vehicles=tot_cars)

# vehicles.add(
#     veh_id='idm',
#     acceleration_controller=(SimCarFollowingController, {}),
#     car_following_params=SumoCarFollowingParams(
#         minGap=2.5,
#         max_speed=V_ENTER,
#         speed_mode="all_checks",
#     ),
#     routing_controller=(GridRouter, {}),
#     num_vehicles=tot_cars)
Esempio n. 17
0
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), TEMPLATE),
          "wb+") as f:
    f.write(data_to_write)

vehicles = VehicleParams()
vehicles.add(veh_id="human",
             acceleration_controller=(SimCarFollowingController, {}),
             routing_controller=(BayBridgeRouter, {}),
             car_following_params=SumoCarFollowingParams(
                 speedDev=0.2,
                 speed_mode="all_checks",
             ),
             lane_change_params=SumoLaneChangeParams(
                 model="LC2013",
                 lcCooperative=0.2,
                 lcSpeedGain=15,
                 lane_change_mode="no_lc_safe",
             ),
             num_vehicles=50)

inflow = InFlows()
inflow.add(veh_type="human",
           edge="393649534",
           probability=0.2,
           departLane="random",
           departSpeed=10)
inflow.add(veh_type="human",
           edge="4757680",
           probability=0.2,
           departLane="random",
           departSpeed=10)
Esempio n. 18
0
    def add(self,
            veh_id,
            acceleration_controller=(SumoCarFollowingController, {}),
            lane_change_controller=(SumoLaneChangeController, {}),
            routing_controller=None,
            initial_speed=0,
            num_vehicles=1,
            speed_mode='right_of_way',
            lane_change_mode="no_lat_collide",
            sumo_car_following_params=None,
            sumo_lc_params=None):
        """Add a sequence of vehicles to the list of vehicles in the network.

        Parameters
        ----------
        veh_id : str
            base vehicle ID for the vehicles (will be appended by a number)
        acceleration_controller : tup, optional
            1st element: flow-specified acceleration controller
            2nd element: controller parameters (may be set to None to maintain
            default parameters)
        lane_change_controller : tup, optional
            1st element: flow-specified lane-changer controller
            2nd element: controller parameters (may be set to None to maintain
            default parameters)
        routing_controller : tup, optional
            1st element: flow-specified routing controller
            2nd element: controller parameters (may be set to None to maintain
            default parameters)
        initial_speed : float, optional
            initial speed of the vehicles being added (in m/s)
        num_vehicles : int, optional
            number of vehicles of this type to be added to the network
        speed_mode : str or int, optional
            may be one of the following:

             * "right_of_way" (default): respect safe speed, right of way and
               brake hard at red lights if needed. DOES NOT respect
               max accel and decel which enables emergency stopping.
               Necessary to prevent custom models from crashing
             * "no_collide": Human and RL cars are preventing from reaching
               speeds that may cause crashes (also serves as a failsafe).
             * "aggressive": Human and RL cars are not limited by sumo with
               regard to their accelerations, and can crash longitudinally
             * "all_checks": all sumo safety checks are activated
             * int values may be used to define custom speed mode for the given
               vehicles, specified at:
               http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29

        lane_change_mode : str or int, optional
            may be one of the following:

            * "no_lat_collide" (default): Human cars will not make lane
              changes, RL cars can lane change into any space, no matter how
              likely it is to crash
            * "strategic": Human cars make lane changes in accordance with SUMO
              to provide speed boosts
            * "aggressive": RL cars are not limited by sumo with regard to
              their lane-change actions, and can crash longitudinally
            * int values may be used to define custom lane change modes for the
              given vehicles, specified at:
              http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#lane_change_mode_.280xb6.29

        sumo_car_following_params : flow.core.params.SumoCarFollowingParams
            Params object specifying attributes for Sumo car following model.
        sumo_lc_params : flow.core.params.SumoLaneChangeParams
            Params object specifying attributes for Sumo lane changing model.
        """
        if sumo_car_following_params is None:
            sumo_car_following_params = SumoCarFollowingParams()

        if sumo_lc_params is None:
            sumo_lc_params = SumoLaneChangeParams()

        type_params = {}
        type_params.update(sumo_car_following_params.controller_params)
        type_params.update(sumo_lc_params.controller_params)

        # If a vehicle is not sumo or RL, let the minGap be zero so that it
        # does not tamper with the dynamics of the controller
        if acceleration_controller[0] != SumoCarFollowingController \
                and acceleration_controller[0] != RLController:
            type_params["minGap"] = 0.0

        # adjust the speed mode value
        if isinstance(speed_mode, str) and speed_mode in SPEED_MODES:
            speed_mode = SPEED_MODES[speed_mode]
        elif not (isinstance(speed_mode, int)
                  or isinstance(speed_mode, float)):
            logging.error("Setting speed mode of {0} to "
                          "default.".format(veh_id))
            speed_mode = SPEED_MODES["no_collide"]

        # adjust the lane change mode value
        if isinstance(lane_change_mode, str) and lane_change_mode in LC_MODES:
            lane_change_mode = LC_MODES[lane_change_mode]
        elif not (isinstance(lane_change_mode, int)
                  or isinstance(lane_change_mode, float)):
            logging.error("Setting lane change mode of {0} to "
                          "default.".format(veh_id))
            lane_change_mode = LC_MODES["no_lat_collide"]

        # this dict will be used when trying to introduce new vehicles into
        # the network via a flow
        self.type_parameters[veh_id] = \
            {"acceleration_controller": acceleration_controller,
             "lane_change_controller": lane_change_controller,
             "routing_controller": routing_controller,
             "initial_speed": initial_speed,
             "speed_mode": speed_mode,
             "lane_change_mode": lane_change_mode,
             "sumo_car_following_params": sumo_car_following_params,
             "sumo_lc_params": sumo_lc_params}

        self.initial.append({
            "veh_id": veh_id,
            "acceleration_controller": acceleration_controller,
            "lane_change_controller": lane_change_controller,
            "routing_controller": routing_controller,
            "initial_speed": initial_speed,
            "num_vehicles": num_vehicles,
            "speed_mode": speed_mode,
            "lane_change_mode": lane_change_mode,
            "sumo_car_following_params": sumo_car_following_params,
            "sumo_lc_params": sumo_lc_params
        })

        # this is used to return the actual headways from the vehicles class
        self.minGap[veh_id] = type_params["minGap"]

        for i in range(num_vehicles):
            v_id = veh_id + '_%d' % i

            # add the vehicle to the list of vehicle ids
            self.__ids.append(v_id)

            self.__vehicles[v_id] = dict()

            # specify the type
            self.__vehicles[v_id]["type"] = veh_id

            # specify the acceleration controller class
            self.__vehicles[v_id]["acc_controller"] = \
                acceleration_controller[0](
                    v_id,
                    sumo_cf_params=sumo_car_following_params,
                    **acceleration_controller[1])

            # specify the lane-changing controller class
            self.__vehicles[v_id]["lane_changer"] = \
                lane_change_controller[0](veh_id=v_id,
                                          **lane_change_controller[1])

            # specify the routing controller class
            if routing_controller is not None:
                self.__vehicles[v_id]["router"] = \
                    routing_controller[0](veh_id=v_id,
                                          router_params=routing_controller[1])
            else:
                self.__vehicles[v_id]["router"] = None

            # specify the speed of vehicles at the start of a rollout
            self.__vehicles[v_id]["initial_speed"] = initial_speed

            # check if the vehicle is human-driven or autonomous
            if acceleration_controller[0] == RLController:
                self.__rl_ids.append(v_id)
            else:
                self.__human_ids.append(v_id)

                # check if the vehicle's lane-changing / acceleration actions
                # are controlled by sumo or not.
                if acceleration_controller[0] != SumoCarFollowingController:
                    self.__controlled_ids.append(v_id)
                if lane_change_controller[0] != SumoLaneChangeController:
                    self.__controlled_lc_ids.append(v_id)

            # specify the speed and lane change mode for the vehicle
            self.__vehicles[v_id]["speed_mode"] = speed_mode
            self.__vehicles[v_id]["lane_change_mode"] = lane_change_mode

        # update the variables for the number of vehicles in the network
        self.num_vehicles = len(self.__ids)
        self.num_rl_vehicles = len(self.__rl_ids)

        # increase the number of unique types of vehicles in the network, and
        # add the type to the list of types
        self.num_types += 1
        self.types.append({"veh_id": veh_id, "type_params": type_params})
Esempio n. 19
0
        else:
            return None
            # next_route=[vehicles.get_edge(veh_id)]
        print(veh_id,", ",veh_type,": ",veh_edge)
        print(next_route,",,,",veh_route)
        return next_route

# class LanechangeController(BaseLaneChangeController):
#     def

vehicles = VehicleParams()
vehicles.add(
    veh_id="human_left",
    acceleration_controller=(IDMController, {}),
    lane_change_params=SumoLaneChangeParams(
        model="SL2015",
        lc_sublane=2.0,
    ),
    routing_controller=(LanechangeRouter,{}),
    lane_change_controller=(SimLaneChangeController,{}),
    num_vehicles=0)
vehicles.add(
    veh_id="human_center",
    acceleration_controller=(IDMController, {}),
    lane_change_params=SumoLaneChangeParams(
        model="SL2015",
        lc_sublane=2.0,
    ),
    lane_change_controller=(SimLaneChangeController,{}),
    routing_controller=(LanechangeRouter,{}),
    num_vehicles=0)    
vehicles.add(
Esempio n. 20
0
})


# CREATE VEHICLE TYPES AND INFLOWS

vehicles = VehicleParams()
inflows = InFlows()

# human vehicles
vehicles.add(
    veh_id="idm",
    car_following_params=SumoCarFollowingParams(
        speed_mode="obey_safe_speed",  # for safer behavior at the merges
        tau=1.5  # larger distance between cars
    ),
    lane_change_params=SumoLaneChangeParams(lane_change_mode=1621))

# autonomous vehicles
vehicles.add(
    veh_id='rl',
    acceleration_controller=(RLController, {}))

# add human vehicles on the highway
inflows.add(
    veh_type="idm",
    edge="highway_0",
    vehs_per_hour=HIGHWAY_INFLOW_RATE,
    depart_lane="free",
    depart_speed="max",
    name="idm_highway_inflow")
Esempio n. 21
0
def get_flow_params(fixed_density,
                    stopping_penalty,
                    acceleration_penalty,
                    evaluate=False,
                    multiagent=False,
                    imitation=False):
    """Return the flow-specific parameters of the ring road network.

    This scenario consists of 50 (if density is fixed) or 50-75 vehicles (5 of
    which are automated) are placed on a sing-lane circular track of length
    1500m. In the absence of the automated vehicle, the human-driven vehicles
    exhibit stop-and-go instabilities brought about by the string-unstable
    characteristic of human car-following dynamics. Within this setting, the
    RL vehicles are tasked with dissipating the formation and propagation of
    stop-and-go waves via an objective function that rewards maximizing
    system-level speeds.

    Parameters
    ----------
    fixed_density : bool
        specifies whether the number of human-driven vehicles updates in
        between resets
    stopping_penalty : bool
        whether to include a stopping penalty
    acceleration_penalty : bool
        whether to include a regularizing penalty for accelerations by the AVs
    evaluate : bool
        whether to compute the evaluation reward
    multiagent : bool
        whether the automated vehicles are via a single-agent policy or a
        shared multi-agent policy with the actions of individual vehicles
        assigned by a separate policy call
    imitation : bool
        whether to use the imitation environment

    Returns
    -------
    dict
        flow-related parameters, consisting of the following keys:

        * exp_tag: name of the experiment
        * env_name: environment class of the flow environment the experiment
          is running on. (note: must be in an importable module.)
        * network: network class the experiment uses.
        * simulator: simulator that is used by the experiment (e.g. aimsun)
        * sim: simulation-related parameters (see flow.core.params.SimParams)
        * env: environment related parameters (see flow.core.params.EnvParams)
        * net: network-related parameters (see flow.core.params.NetParams and
          the network's documentation or ADDITIONAL_NET_PARAMS component)
        * veh: vehicles to be placed in the network at the start of a rollout
          (see flow.core.params.VehicleParams)
        * initial (optional): parameters affecting the positioning of vehicles
          upon initialization/reset (see flow.core.params.InitialConfig)
        * tls (optional): traffic lights to be introduced to specific nodes
          (see flow.core.params.TrafficLightParams)
    """
    vehicles = VehicleParams()
    for i in range(NUM_AUTOMATED):
        vehicles.add(
            veh_id="human_{}".format(i),
            acceleration_controller=(IDMController, {
                "a": 1.3,
                "b": 2.0,
                "noise": 0.3 if INCLUDE_NOISE else 0.0
            }),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(min_gap=0.5, ),
            lane_change_params=SumoLaneChangeParams(lane_change_mode=1621, ),
            num_vehicles=NUM_VEHICLES[0] - NUM_AUTOMATED if i == 0 else 0)
        vehicles.add(
            veh_id="rl_{}".format(i),
            acceleration_controller=(RLController, {}),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(min_gap=0.5, ),
            lane_change_params=SumoLaneChangeParams(
                lane_change_mode=0,  # no lane changes by automated vehicles
            ),
            num_vehicles=NUM_AUTOMATED if i == 0 else 0)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["length"] = RING_LENGTH
    additional_net_params["lanes"] = NUM_LANES

    if multiagent:
        if imitation:
            env_name = None  # FIXME
        else:
            env_name = AVClosedMultiAgentEnv
    else:
        if imitation:
            env_name = AVClosedEnv
        else:
            env_name = AVClosedImitationEnv

    return dict(
        # name of the experiment
        exp_tag='multilane-ring',

        # name of the flow environment the experiment is running on
        env_name=env_name,

        # name of the network class the experiment is running on
        network=RingNetwork,

        # simulator that is used by the experiment
        simulator="traci",

        # sumo-related parameters (see flow.core.params.SumoParams)
        sim=SumoParams(
            use_ballistic=True,
            render=False,
            sim_step=0.5,
        ),

        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            horizon=1800,
            warmup_steps=50,
            sims_per_step=2,
            evaluate=evaluate,
            additional_params={
                "max_accel": 1,
                "max_decel": 1,
                "target_velocity": 30,  # FIXME
                "stopping_penalty": stopping_penalty,
                "acceleration_penalty": acceleration_penalty,
                "num_vehicles": None if fixed_density else NUM_VEHICLES,
                "even_distribution": False,
                "sort_vehicles": True,
                "expert_model": (IDMController, {
                    "a": 1.3,
                    "b": 2.0,
                }),
            },
        ),

        # network-related parameters (see flow.core.params.NetParams and the
        # network's documentation or ADDITIONAL_NET_PARAMS component)
        net=NetParams(additional_params=additional_net_params, ),

        # vehicles to be placed in the network at the start of a rollout (see
        # flow.core.params.VehicleParams)
        veh=vehicles,

        # parameters specifying the positioning of vehicles upon init/reset
        # (see flow.core.params.InitialConfig)
        initial=InitialConfig(
            spacing="random",
            min_gap=0.5,
            shuffle=True,
        ),
    )
Esempio n. 22
0
NUM_MERGE_HUMANS = 9
NUM_MERGE_RL = 1

# note that the vehicles are added sequentially by the scenario,
# so place the merging vehicles after the vehicles in the ring
vehicles = VehicleParams()
# Inner ring vehicles
vehicles.add(veh_id='human',
             acceleration_controller=(IDMController, {
                 'noise': 0.2
             }),
             lane_change_controller=(SimLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             num_vehicles=6,
             car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5),
             lane_change_params=SumoLaneChangeParams())
# A single learning agent in the inner ring
vehicles.add(veh_id='rl',
             acceleration_controller=(RLController, {}),
             lane_change_controller=(SimLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             num_vehicles=1,
             car_following_params=SumoCarFollowingParams(
                 minGap=0.01,
                 tau=0.5,
                 speed_mode="obey_safe_speed",
             ),
             lane_change_params=SumoLaneChangeParams())
# Outer ring vehicles
vehicles.add(veh_id='merge-human',
             acceleration_controller=(IDMController, {
def bottleneck1_baseline(num_runs, render=True):
    """Run script for the bottleneck1 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render: str, optional
            specifies whether to use the gui during execution

    Returns
    -------
        flow.core.experiment.Experiment
            class needed to run simulations
    """
    exp_tag = flow_params['exp_tag']
    sim_params = flow_params['sim']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())
    traffic_lights = flow_params.get('tls', TrafficLightParams())

    # we want no autonomous vehicles in the simulation
    vehicles = VehicleParams()
    vehicles.add(veh_id='human',
                 car_following_params=SumoCarFollowingParams(speed_mode=9, ),
                 routing_controller=(ContinuousRouter, {}),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode=1621, ),
                 num_vehicles=1 * SCALING)

    # only include human vehicles in inflows
    flow_rate = 1900 * SCALING
    inflow = InFlows()
    inflow.add(veh_type='human',
               edge='1',
               vehs_per_hour=flow_rate,
               departLane='random',
               departSpeed=10)
    net_params.inflows = inflow

    # modify the rendering to match what is requested
    sim_params.render = render

    # set the evaluation flag to True
    env_params.evaluate = True

    # import the scenario class
    module = __import__('flow.scenarios', fromlist=[flow_params['scenario']])
    scenario_class = getattr(module, flow_params['scenario'])

    # create the scenario object
    scenario = scenario_class(name=exp_tag,
                              vehicles=vehicles,
                              net_params=net_params,
                              initial_config=initial_config,
                              traffic_lights=traffic_lights)

    # import the environment class
    module = __import__('flow.envs', fromlist=[flow_params['env_name']])
    env_class = getattr(module, flow_params['env_name'])

    # create the environment object
    env = env_class(env_params, sim_params, scenario)

    exp = Experiment(env)

    results = exp.run(num_runs, env_params.horizon)

    return np.mean(results['returns']), np.std(results['returns'])
Esempio n. 24
0
def bay_bridge_toll_example(render=None, use_traffic_lights=False):
    """Perform a simulation of the toll portion of the Bay Bridge.

    This consists of the toll booth and sections of the road leading up to it.

    Parameters
    ----------
    render : bool, optional
        specifies whether to use the gui during execution
    use_traffic_lights: bool, optional
        whether to activate the traffic lights in the scenario

    Note
    ----
    Unlike the bay_bridge_example, inflows are always activated here.
    """
    sim_params = SumoParams(sim_step=0.4, overtake_right=True)

    if render is not None:
        sim_params.render = render

    car_following_params = SumoCarFollowingParams(
        speedDev=0.2,
        speed_mode="all_checks",
    )
    lane_change_params = SumoLaneChangeParams(
        model="LC2013",
        lcCooperative=0.2,
        lcSpeedGain=15,
        lane_change_mode="no_lat_collide",
    )

    vehicles = VehicleParams()

    vehicles.add(veh_id="human",
                 acceleration_controller=(SimCarFollowingController, {}),
                 routing_controller=(BayBridgeRouter, {}),
                 car_following_params=car_following_params,
                 lane_change_params=lane_change_params,
                 num_vehicles=50)

    additional_env_params = {}
    env_params = EnvParams(additional_params=additional_env_params)

    inflow = InFlows()

    inflow.add(veh_type="human",
               edge="393649534",
               probability=0.2,
               departLane="random",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="4757680",
               probability=0.2,
               departLane="random",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="32661316",
               probability=0.2,
               departLane="random",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="90077193#0",
               vehs_per_hour=2000,
               departLane="random",
               departSpeed=10)

    net_params = NetParams(inflows=inflow,
                           no_internal_links=False,
                           template=TEMPLATE)

    # download the template from AWS
    if use_traffic_lights:
        my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \
                 "bay_bridge_TL_all_green.net.xml"
    else:
        my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \
                 "bay_bridge_junction_fix.net.xml"
    my_file = urllib.request.urlopen(my_url)
    data_to_write = my_file.read()

    with open(
            os.path.join(os.path.dirname(os.path.abspath(__file__)), TEMPLATE),
            "wb+") as f:
        f.write(data_to_write)

    initial_config = InitialConfig(
        spacing="uniform",  # "random",
        min_gap=15,
        edges_distribution=EDGES_DISTRIBUTION.copy())

    scenario = BayBridgeTollScenario(name="bay_bridge_toll",
                                     vehicles=vehicles,
                                     net_params=net_params,
                                     initial_config=initial_config)

    env = BayBridgeEnv(env_params, sim_params, scenario)

    return Experiment(env)
Esempio n. 25
0
    def reset(self):
        """Reset the environment with a new inflow rate.

        The diverse set of inflows are used to generate a policy that is more
        robust with respect to the inflow rate. The inflow rate is update by
        creating a new scenario similar to the previous one, but with a new
        Inflow object with a rate within the additional environment parameter
        "inflow_range", which is a list consisting of the smallest and largest
        allowable inflow rates.

        **WARNING**: The inflows assume there are vehicles of type
        "followerstopper" and "human" within the VehicleParams object.
        """
        add_params = self.env_params.additional_params
        if add_params.get("reset_inflow"):
            inflow_range = add_params.get("inflow_range")
            flow_rate = np.random.uniform(min(inflow_range),
                                          max(inflow_range)) * self.scaling

            # We try this for 100 trials in case unexpected errors during
            # instantiation.
            for _ in range(100):
                try:
                    # introduce new inflows within the pre-defined inflow range
                    inflow = InFlows()
                    inflow.add(
                        veh_type="followerstopper",  # FIXME: make generic
                        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)

                    # all other network parameters should match the previous
                    # environment (we only want to change the inflow)
                    additional_net_params = {
                        "scaling":
                        self.scaling,
                        "speed_limit":
                        self.net_params.additional_params['speed_limit']
                    }
                    net_params = NetParams(
                        inflows=inflow,
                        no_internal_links=False,
                        additional_params=additional_net_params)

                    vehicles = VehicleParams()
                    vehicles.add(
                        veh_id="human",  # FIXME: make generic
                        car_following_params=SumoCarFollowingParams(
                            speed_mode=9, ),
                        lane_change_controller=(SimLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        lane_change_params=SumoLaneChangeParams(
                            lane_change_mode=0,  # 1621,#0b100000101,
                        ),
                        num_vehicles=1 * self.scaling)
                    vehicles.add(
                        veh_id="followerstopper",
                        acceleration_controller=(RLController, {}),
                        lane_change_controller=(SimLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        car_following_params=SumoCarFollowingParams(
                            speed_mode=9, ),
                        lane_change_params=SumoLaneChangeParams(
                            lane_change_mode=0, ),
                        num_vehicles=1 * self.scaling)

                    # recreate the scenario object
                    self.scenario = self.scenario.__class__(
                        name=self.scenario.orig_name,
                        vehicles=vehicles,
                        net_params=net_params,
                        initial_config=self.initial_config,
                        traffic_lights=self.scenario.traffic_lights)
                    observation = super().reset()

                    # reset the timer to zero
                    self.time_counter = 0

                    return observation

                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
Esempio n. 26
0
def para_produce_rl(HORIZON=3500, NUM_AUTOMATED=4):

    # time horizon of a single rollout
    HORIZON = HORIZON
    # number of rollouts per training iteration
    N_ROLLOUTS = 20
    # number of parallel workers
    N_CPUS = 4
    # number of automated vehicles. Must be less than or equal to 22.
    NUM_AUTOMATED = NUM_AUTOMATED

    # We evenly distribute the automated vehicles in the network.
    num_human = 40 - NUM_AUTOMATED
    humans_remaining = num_human

    vehicles = VehicleParams()
    for i in range(NUM_AUTOMATED):
        # Add one automated vehicle.
        vehicles.add(veh_id="rl_{}".format(i),
                     acceleration_controller=(RLController, {}),
                     routing_controller=(MinicityRouter, {}),
                     initial_speed=0,
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="obey_safe_speed", ),
                     num_vehicles=1)

        # Add a fraction of the remaining human vehicles.
        vehicles_to_add = round(humans_remaining / (NUM_AUTOMATED - i))
        humans_remaining -= vehicles_to_add
        vehicles.add(veh_id="human_{}".format(i),
                     acceleration_controller=(IDMController, {
                         "noise": 0.2
                     }),
                     routing_controller=(MinicityRouter, {}),
                     car_following_params=SumoCarFollowingParams(speed_mode=1),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode="no_lat_collide"),
                     initial_speed=0,
                     num_vehicles=vehicles_to_add)

        flow_params = dict(
            # name of the experiment
            exp_tag="minicity",

            # name of the flow environment the experiment is running on
            env_name=AccelEnv,

            # name of the network class the experiment is running on
            network=MiniCityNetwork,

            # simulator that is used by the experiment
            simulator='traci',

            # sumo-related parameters (see flow.core.params.SumoParams)
            sim=SumoParams(sim_step=0.25,
                           render=False,
                           save_render=True,
                           sight_radius=15,
                           pxpm=3,
                           show_radius=True,
                           restart_instance=True),

            # environment related parameters (see flow.core.params.EnvParams)
            env=EnvParams(
                horizon=HORIZON,
                additional_params=ADDITIONAL_ENV_PARAMS,
            ),
            # network-related parameters (see flow.core.params.NetParams and the
            # network's documentation or ADDITIONAL_NET_PARAMS component)
            # net=NetParams(
            #     additional_params={
            #         "length": 260,
            #         "lanes": 1,
            #         "speed_limit": 30,
            #         "resolution": 40,
            #     }, ),
            net=NetParams(),

            # vehicles to be placed in the network at the start of a rollout (see
            # flow.core.params.VehicleParams)
            veh=vehicles,

            # parameters specifying the positioning of vehicles upon initialization/
            # reset (see flow.core.params.InitialConfig)
            initial=InitialConfig(
                spacing="uniform",
                min_gap=20,
            ),
        )

    flow_params['env'].horizon = HORIZON
    return flow_params
        # Define speed mode that will minimize collisions: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29
        speed_mode=9,  #"all_checks", #"all_checks", #no_collide",
        #decel=7.5,  # avoid collisions at emergency stops
        # desired time-gap from leader
        #tau=2, #7,
        #min_gap=2.5,
        #speed_factor=1,
        #speed_dev=0.1
    ),
    lane_change_params=SumoLaneChangeParams(
        model="SL2015",
        # Define a lane changing mode that will allow lane changes
        # See: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#lane_change_mode_.280xb6.29
        # and: ~/local/flow_2019_07/flow/core/params.py, see LC_MODES = {"aggressive": 0 /*bug, 0 is no lane-changes*/, "no_lat_collide": 512, "strategic": 1621}, where "strategic" is the default behavior
        lane_change_mode=
        1621,  #0b011000000001, # (like default 1621 mode, but no lane changes other than strategic to follow route, # 512, #(collision avoidance and safety gap enforcement) # "strategic", 
        #lc_speed_gain=1000000,
        lc_pushy=0,  #0.5, #1,
        lc_assertive=5,  #20,
        # the following two replace default values which are not read well by xml parser
        lc_impatience=1e-8,
        lc_time_to_impatience=1e12),
    num_vehicles=0)

vehicles.add(
    veh_id="rl",
    acceleration_controller=(RLController, {}),
    lane_change_controller=(SimLaneChangeController, {}),
    #routing_controller=(ContinuousRouter, {}),
    car_following_params=SumoCarFollowingParams(
        # Define speed mode that will minimize collisions: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29
Esempio n. 28
0
def get_flow_params(config):
    """Return Flow experiment parameters, given an experiment result folder.

    Parameters
    ----------
    config : dict
        stored RLlib configuration dict

    Returns
    -------
    dict
        Dict of flow parameters, like net_params, env_params, vehicle
        characteristics, etc
    """
    # collect all data from the json file
    flow_params = json.loads(config['env_config']['flow_params'])

    # reinitialize the vehicles class from stored data
    veh = Vehicles()
    for veh_params in flow_params["veh"]:
        module = __import__(
            "flow.controllers",
            fromlist=[veh_params['acceleration_controller'][0]])
        acc_class = getattr(module, veh_params['acceleration_controller'][0])
        lc_class = getattr(module, veh_params['lane_change_controller'][0])

        acc_controller = (acc_class, veh_params['acceleration_controller'][1])
        lc_controller = (lc_class, veh_params['lane_change_controller'][1])

        rt_controller = None
        if veh_params['routing_controller'] is not None:
            rt_class = getattr(module, veh_params['routing_controller'][0])
            rt_controller = (rt_class, veh_params['routing_controller'][1])

        sumo_cf_params = SumoCarFollowingParams()
        sumo_cf_params.__dict__ = veh_params["sumo_car_following_params"]

        sumo_lc_params = SumoLaneChangeParams()
        sumo_lc_params.__dict__ = veh_params["sumo_lc_params"]

        del veh_params["sumo_car_following_params"], \
            veh_params["sumo_lc_params"], \
            veh_params["acceleration_controller"], \
            veh_params["lane_change_controller"], \
            veh_params["routing_controller"]

        veh.add(acceleration_controller=acc_controller,
                lane_change_controller=lc_controller,
                routing_controller=rt_controller,
                sumo_car_following_params=sumo_cf_params,
                sumo_lc_params=sumo_lc_params,
                **veh_params)

    # convert all parameters from dict to their object form
    sumo = SumoParams()
    sumo.__dict__ = flow_params["sumo"].copy()

    net = NetParams()
    net.__dict__ = flow_params["net"].copy()
    net.inflows = InFlows()
    if flow_params["net"]["inflows"]:
        net.inflows.__dict__ = flow_params["net"]["inflows"].copy()

    env = EnvParams()
    env.__dict__ = flow_params["env"].copy()

    initial = InitialConfig()
    if "initial" in flow_params:
        initial.__dict__ = flow_params["initial"].copy()

    tls = TrafficLightParams()
    if "tls" in flow_params:
        tls.__dict__ = flow_params["tls"].copy()

    flow_params["sumo"] = sumo
    flow_params["env"] = env
    flow_params["initial"] = initial
    flow_params["net"] = net
    flow_params["veh"] = veh
    flow_params["tls"] = tls

    return flow_params
Esempio n. 29
0
    def reset(self):
        add_params = self.env_params.additional_params
        if add_params.get("reset_inflow"):
            inflow_range = add_params.get("inflow_range")
            flow_rate = np.random.uniform(
                min(inflow_range), max(inflow_range)) * 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,
                        "speed_limit": self.scenario.net_params.
                        additional_params['speed_limit']
                    }
                    net_params = NetParams(
                        inflows=inflow,
                        no_internal_links=False,
                        additional_params=additional_net_params)

                    vehicles = VehicleParams()
                    vehicles.add(
                        veh_id="human",
                        car_following_params=SumoCarFollowingParams(
                            speed_mode=9,
                        ),
                        lane_change_controller=(SimLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        lane_change_params=SumoLaneChangeParams(
                            lane_change_mode=0,  # 1621,#0b100000101,
                        ),
                        num_vehicles=1 * self.scaling)
                    vehicles.add(
                        veh_id="followerstopper",
                        acceleration_controller=(RLController, {}),
                        lane_change_controller=(SimLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        car_following_params=SumoCarFollowingParams(
                            speed_mode=9,
                        ),
                        lane_change_params=SumoLaneChangeParams(
                            lane_change_mode=0,
                        ),
                        num_vehicles=1 * self.scaling)

                    self.scenario = self.scenario.__class__(
                        name=self.scenario.orig_name,
                        vehicles=vehicles,
                        net_params=net_params,
                        initial_config=self.scenario.initial_config,
                        traffic_lights=self.scenario.traffic_lights)
                    observation = super().reset()

                    # reset the timer to zero
                    self.time_counter = 0

                    return observation

                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
Esempio n. 30
0
# probability of exiting at the next off-ramp
additional_net_params["next_off_ramp_proba"] = 0.25

# RL vehicles constitute 5% of the total number of vehicles
vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(IDMController, {
        "noise": 0.2
    }),
    # lane_change_controller=(StaticLaneChanger, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode="obey_safe_speed", ),
    lane_change_params=SumoLaneChangeParams(lane_change_mode=1621,
                                            model="SL2015",
                                            lc_impatience="0.1",
                                            lc_time_to_impatience="1.0"),
    num_vehicles=5)
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             lane_change_controller=(StaticLaneChanger, {}),
             routing_controller=(HighwayRouter, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode="obey_safe_speed", ),
             lane_change_params=SumoLaneChangeParams(
                 lane_change_mode=256,
                 model="SL2015",
                 lc_impatience="0.1",
                 lc_time_to_impatience="1.0"),
             num_vehicles=0)