Exemple #1
0
def grid0_baseline(num_runs, render=True):
    """Run script for the grid0 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render : bool, 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']

    # define the traffic light logic
    tl_logic = TrafficLightParams(baseline=False)

    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "GrGr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yryr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rGrG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "ryry"
    }]

    for i in range(N_ROWS * N_COLUMNS):
        tl_logic.add('center' + str(i),
                     tls_type='actuated',
                     phases=phases,
                     programID=1)

    # 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)
    total_delay = np.mean(results['returns'])

    return total_delay
    "minDur": "8",
    "maxDur": "45",
    "state": "rGrG"
}, {
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "ryry"
}]

for traffic_id in range(0, (n_rows * n_columns)):

    tl_logic.add("center" + str(traffic_id),
                 showDetectors=True,
                 programID=1,
                 phases=phases,
                 maxGap=3.0,
                 detectorGap=0.8,
                 tls_type="actuated")

additional_net_params = {
    "grid_array": grid_array,
    "speed_limit": V_TARGET,
    "horizontal_lanes": 1,
    "vertical_lanes": 1,
    'traffic_lights': True
}

if USE_INFLOWS:
    initial_config, net_params = get_flow_params(
        col_num=n_columns,
Exemple #3
0
    "duration": "5",
    "minDur": "3",
    "maxDur": "6",
    "state": "rrrryyggrrrryygg"
}, {
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "rrrrrrGGrrrrrrGG"
}, {
    "duration": "5",
    "minDur": "3",
    "maxDur": "6",
    "state": "rrrrrryyrrrrrryy"
}]
tl_logic.add("n_i3", phases=phases1, programID=1)
tl_logic.add("n_i4", phases=phases, programID=1)

additional_net_params = {"traffic_lights": True}
net_params = NetParams(additional_params=additional_net_params)
##################################################
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(name="city",
                            vehicles=VehicleParams(),
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "yryryryryryr"
}, {
    "duration": "31",
    "minDur": "8",
    "maxDur": "45",
    "state": "rGrGrGrGrGrG"
}, {
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "ryryryryryry"
}]
tl_logic.add("center0", phases=phases, programID=1, tls_type="static")
tl_logic.add("center1", phases=phases, programID=1, tls_type="static")
tl_logic.add("center2", phases=phases, programID=1, tls_type="static")
tl_logic.add("center3", phases=phases, programID=1, tls_type="static")

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

if USE_INFLOWS:
    initial_config, net_params = get_flow_params(
        col_num=n_columns,
        row_num=n_rows,
def setup_flow_params(args):
    DISABLE_TB = True
    DISABLE_RAMP_METER = True
    AV_FRAC = args.av_frac
    if args.lc_on:
        lc_mode = 1621
    else:
        lc_mode = 512

    vehicles = VehicleParams()
    if not np.isclose(AV_FRAC, 1):
        vehicles.add(veh_id="human",
                     lane_change_controller=(SimLaneChangeController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode=31, ),
                     lane_change_params=SumoLaneChangeParams(
                         lane_change_mode=lc_mode, ),
                     num_vehicles=1)
        vehicles.add(
            veh_id="av",
            acceleration_controller=(RLController, {}),
            lane_change_controller=(SimLaneChangeController, {}),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(speed_mode=31, ),
            lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
            num_vehicles=1)
    else:
        vehicles.add(
            veh_id="av",
            acceleration_controller=(RLController, {}),
            lane_change_controller=(SimLaneChangeController, {}),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(speed_mode=31, ),
            lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
            num_vehicles=1)

    # flow rate
    flow_rate = 1900 * args.scaling

    controlled_segments = [('1', 1, False), ('2', 3, True), ('3', 3, False),
                           ('4', 2, False), ('5', 1, False)]
    num_observed_segments = [('1', 1 * args.state_space_scaling),
                             ('2', 3 * args.state_space_scaling),
                             ('3', 3 * args.state_space_scaling),
                             ('4', 3 * args.state_space_scaling),
                             ('5', 1 * args.state_space_scaling)]
    additional_env_params = {
        'target_velocity': 40,
        'disable_tb': True,
        'disable_ramp_metering': True,
        'controlled_segments': controlled_segments,
        'symmetric': False,
        'observed_segments': num_observed_segments,
        'reset_inflow': True,
        'lane_change_duration': 5,
        'max_accel': 3,
        'max_decel': 3,
        'inflow_range': [args.low_inflow, args.high_inflow],
        'start_inflow': flow_rate,
        'congest_penalty': args.congest_penalty,
        "life_penalty": args.life_penalty,
        "av_frac": args.av_frac,
        "lc_mode": lc_mode,
        "congest_penalty_start": args.congest_penalty_start,
        "num_sample_seconds": args.num_sample_seconds,
        "speed_reward": args.speed_reward,
        "fair_reward": args.fair_reward,
        "exit_history_seconds": args.exit_history_seconds,
    }

    # percentage of flow coming out of each lane
    inflow = InFlows()
    if not np.isclose(args.av_frac, 1.0):
        inflow.add(veh_type='human',
                   edge='1',
                   vehs_per_hour=flow_rate * (1 - args.av_frac),
                   departLane='random',
                   departSpeed=10.0)
        inflow.add(veh_type='av',
                   edge='1',
                   vehs_per_hour=flow_rate * args.av_frac,
                   departLane='random',
                   departSpeed=10.0)
    else:
        inflow.add(veh_type='av',
                   edge='1',
                   vehs_per_hour=flow_rate,
                   departLane='random',
                   departSpeed=10.0)

    traffic_lights = TrafficLightParams()
    if not DISABLE_TB:
        traffic_lights.add(node_id='2')
    if not DISABLE_RAMP_METER:
        traffic_lights.add(node_id='3')

    additional_net_params = {'scaling': args.scaling, "speed_limit": 23.0}

    flow_params = dict(
        # name of the experiment
        exp_tag=args.exp_title,

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

        # name of the scenario class the experiment is running on
        scenario='BottleneckScenario',

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

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

        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            warmup_steps=int(args.warmup_steps /
                             (args.sims_per_step * args.sim_step)),
            sims_per_step=args.sims_per_step,
            horizon=args.horizon,
            additional_params=additional_env_params,
        ),

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

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

        # parameters specifying the positioning of vehicles upon initialization/
        # reset (see flow.core.params.InitialConfig)
        initial=InitialConfig(
            spacing='uniform',
            min_gap=5,
            lanes_distribution=float('inf'),
            edges_distribution=['2', '3', '4', '5'],
        ),

        # traffic lights to be introduced to specific nodes (see
        # flow.core.traffic_lights.TrafficLights)
        tls=traffic_lights,
    )
    return flow_params
def para_produce_rl(HORIZON=3000):
    # Create default environment parameters
    env_params = EnvParams()

    # Vehicle definition
    vehicles = VehicleParams()
    num_vehicles = 1
    vehicles.add(
        veh_id="human",
        routing_controller=(GridRouter, {}),
        lane_change_controller=(SimLaneChangeController, {}),
        car_following_params=SumoCarFollowingParams(
            min_gap=2.5,
            decel=7.5,  # avoid collisions at emergency stops
        ),
        lane_change_params=SumoLaneChangeParams(
                lane_change_mode=1621,
            ),
        num_vehicles=num_vehicles)

    # whether to allow turns at intersections
    ALLOW_TURNS = False
    
    # initialize traffic lights, used when you want define your own traffic lights
    tl_logic = TrafficLightParams(baseline=False) # To see static traffic lights in action, the `TrafficLightParams` object should be instantiated with `baseline=False`

    # when use off_ramp_grid.net.xml file, you should use a phase state example as "GGGgrrrrGGGgrrrr"
    # when use off_ramp_grid_turn.net.xml file, you should use a phase state example as "GGGggrrrrrGGGggrrrrr"
    if ALLOW_TURNS:
        phases = [{
            "duration": "31",
            "minDur": "8",
            "maxDur": "45",
            # for actuated traffic lights, you can add these optional values below
            # "maxGap": int, describes the maximum time gap between successive vehicle sthat will cause the current phase to be prolonged
            # "detectorGap": int, determines the time distance between the (automatically generated) detector and the stop line in seconds
            # "showDetectors": bool, toggles whether or not detectors are shown in sumo-gui
            "state": "GGGggrrrrrGGGggrrrrr"
        }, {
            "duration": "6",
            "minDur": "3",
            "maxDur": "6",
            "state": "yyyyyrrrrryyyyyrrrrr"
        }, {
            "duration": "31",
            "minDur": "8",
            "maxDur": "45",
            "state": "rrrrrGGGggrrrrrGGGgg"
        }, {
            "duration": "6",
            "minDur": "3",
            "maxDur": "6",
            "state": "rrrrryyyyyrrrrryyyyy"
        }]
        tl_logic.add("center0", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
        tl_logic.add("center1", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
        tl_logic.add("center2", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
        tl_logic.add("center3", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
    else:
        phases = [{
            "duration": "31",
            "minDur": "8",
            "maxDur": "45",
            # for actuated traffic lights, you can add these optional values below
            # "maxGap": int, describes the maximum time gap between successive vehicle sthat will cause the current phase to be prolonged
            # "detectorGap": int, determines the time distance between the (automatically generated) detector and the stop line in seconds
            # "showDetectors": bool, toggles whether or not detectors are shown in sumo-gui
            "state": "GGGgrrrrGGGgrrrr"
        }, {
            "duration": "6",
            "minDur": "3",
            "maxDur": "6",
            "state": "yyyyrrrryyyyrrrr"
        }, {
            "duration": "31",
            "minDur": "8",
            "maxDur": "45",
            "state": "rrrrGGGgrrrrGGGg"
        }, {
            "duration": "6",
            "minDur": "3",
            "maxDur": "6",
            "state": "rrrryyyyrrrryyyy"
        }]

        # THIS IS A BUG THAT I DON'T KNOW WHY IT HAPPENS!!!!!!
        phase0 = [{
            "duration": "31",
            "minDur": "8",
            "maxDur": "45",
            "state": "GGrrGGrrGGrrGGrr"
        }, {
            "duration": "6",
            "minDur": "3",
            "maxDur": "6",
            "state": "yyrryyrryyrryyrr"
        }, {
            "duration": "31",
            "minDur": "8",
            "maxDur": "45",
            "state": "rrGGrrGGrrGGrrGG"
        }, {
            "duration": "6",
            "minDur": "3",
            "maxDur": "6",
            "state": "rryyrryyrryyrryy"
        }]

        tl_logic.add("center0", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
        tl_logic.add("center1", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
        tl_logic.add("center2", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
        tl_logic.add("center3", phases=phases, programID=1, detectorGap=1,tls_type="actuated")
    
    flow_params = dict(
    exp_tag='offramp_multiagent_inflow_1.0_speed_20',
    env_name=MultiTrafficLightGridPOEnv,
    network=offRampGrid,
    simulator='traci',
    sim=SumoParams(
        sim_step=0.1,
        render=False,
        #emission_path='./data',
        restart_instance=True,
    ),
    env=EnvParams(
        horizon=3000, additional_params=ADDITIONAL_ENV_PARAMS.copy(),
    ),
    net=net_params,
    veh=vehicles,
    initial=initial_config,
    # used when you define your own traffic lights
    #tls=tl_logic,
    )
    #flow_params['env'].horizon = HORIZON
    return flow_params
Exemple #7
0
    def __init__(self,
                 network_id,
                 horizon=360,
                 net_params=None,
                 vehicles=None,
                 demand_type='constant',
                 demand_mode='step',
                 initial_config=None,
                 tls_type='rl'):

        """Builds a new network from inflows -- the resulting
        vehicle trips will be stochastic use it for training"""
        self.network_id = network_id

        baseline = (tls_type == 'actuated')
        self.cycle_time, self.programs = get_tls_custom(
                                network_id, baseline=baseline)

        if initial_config is None:
            initial_config = InitialConfig(
                edges_distribution=tuple(get_routes(network_id).keys())
            )

        if net_params is None:
            #TODO: check vtype
            if vehicles is None:
                vehicles = VehicleParams()
                vehicles.add(
                    veh_id="human",
                    car_following_params=SumoCarFollowingParams(
                        min_gap=2.5,
                        decel=7.5,  # Avoid collisions at emergency stops.
                    ),
                    lane_change_params=SumoLaneChangeParams(
                        lane_change_mode='strategic'
                    )
                )

            inflows = InFlows(network_id, horizon, demand_type,
                              demand_mode=demand_mode, initial_config=initial_config)

            net_params = NetParams(inflows,
                                   template=get_path(network_id, 'net'))

        # static program (required for rl)
        tls_logic = TrafficLightParams(baseline=False)
        if tls_type not in ('actuated', ):
            programs = get_logic(network_id)
            if programs:
                for prog in programs:
                    node_id = prog.pop('id')
                    prog['tls_type'] = prog.pop('type')
                    prog['programID'] = int(prog.pop('programID')) + 1
                    tls_logic.add(node_id, **prog)
        else:
            for tls_id, tls_args in self.programs.items():
                tls_logic.add(tls_id, **tls_args)

        super(Network, self).__init__(
                 network_id,
                 vehicles,
                 net_params,
                 initial_config=initial_config,
                 traffic_lights=tls_logic
        )

        self.nodes = self.specify_nodes(net_params)
        self.edges = self.specify_edges(net_params)
        self.connections = self.specify_connections(net_params)
        self.types = self.specify_types(net_params)
Exemple #8
0
def grid_example(render=None, use_inflows=False):
    """
    Perform a simulation of vehicles on a grid.

    Parameters
    ----------
    render: bool, optional
        specifies whether to use the gui during execution
    use_inflows : bool, optional
        set to True if you would like to run the experiment with inflows of
        vehicles from the edges, and False otherwise

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles and balanced traffic lights on a grid.
    """
    v_enter = 10
    inner_length = 300
    long_length = 500
    short_length = 300
    n_rows = 2
    n_columns = 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) * n_columns \
        + (num_cars_top + num_cars_bot) * n_rows

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

    sim_params = SumoParams(sim_step=0.1, render=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="human",
        routing_controller=(GridRouter, {}),
        car_following_params=SumoCarFollowingParams(
            min_gap=2.5,
            decel=7.5,  # avoid collisions at emergency stops
        ),
        num_vehicles=tot_cars)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    tl_logic = TrafficLightParams(baseline=False)
    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "GrGrGrGrGrGr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yryryryryryr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rGrGrGrGrGrG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "ryryryryryry"
    }]
    tl_logic.add("center0", phases=phases, programID=1)
    tl_logic.add("center1", phases=phases, programID=1)
    tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated")

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

    if use_inflows:
        initial_config, net_params = get_flow_params(
            col_num=n_columns,
            row_num=n_rows,
            additional_net_params=additional_net_params)
    else:
        initial_config, net_params = get_non_flow_params(
            enter_speed=v_enter, add_net_params=additional_net_params)

    scenario = SimpleGridScenario(name="grid-intersection",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    env = AccelEnv(env_params, sim_params, scenario)

    return Experiment(env)
Exemple #9
0
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "yryr"
}, {
    "duration": "31",
    "minDur": "8",
    "maxDur": "45",
    "state": "rGrG"
}, {
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "ryry"
}]
tl_logic.add("center0", phases=phases, programID=1)
# tl_logic.add("center1", phases=phases, programID=1)
# tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated")

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

if USE_INFLOWS:
    initial_config, net_params = get_flow_params(
        col_num=n_columns,
        row_num=n_rows,
        additional_net_params=additional_net_params)
Exemple #10
0
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "yryryryryryr"
}, {
    "duration": "31",
    "minDur": "8",
    "maxDur": "45",
    "state": "rGrGrGrGrGrG"
}, {
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "ryryryryryry"
}]
tl_logic.add("IT", phases=phases, programID=1)
#tl_logic.add("center1", phases=phases, programID=1)
#tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated")

flow_params = dict(
    # name of the experiment
    exp_tag='grid-intersection',
    # name of the flow environment the experiment is running on
    env_name=AccelEnv,
    # name of the network class the experiment is running on
    network=Traffic_Network,
    # simulator that is used by the experiment
    simulator='traci',
    # sumo-related parameters (see flow.core.params.SumoParams)
    sim=SumoParams(
        sim_step=0.1,
def bottleneck_example(flow_rate, horizon, restart_instance=False,
                       render=False, scaling=1, disable_ramp_meter=True, disable_tb=True,
                       lc_on=False, n_crit=8.0, q_max=None, q_min=None, feedback_coef=1, q_init=2300,
                       penetration_rate=0.4):
    """
    Perform a simulation of vehicles on a bottleneck.

    Parameters
    ----------
    flow_rate : float
        total inflow rate of vehicles into the bottleneck
    horizon : int
        time horizon
    restart_instance: bool, optional
        whether to restart the instance upon reset
    render: bool, optional
        specifies whether to use the gui during execution
    scaling: int, optional
        This sets the number of lanes so that they go from 4 * scaling -> 2 * scaling -> 1 * scaling
    disable_tb: bool, optional
        whether the toll booth should be active
    disable_ramp_meter: bool, optional
        specifies if ALINEA should be active. For more details, look at the BottleneckEnv documentation
    lc_on: bool, optional
        if true, the vehicles have LC mode 1621 which is all safe lane changes allowed. Otherwise, it is 0 for
        no lane changing.
    n_crit: float, optional
        number of vehicles in the bottleneck we feedback around. Look at BottleneckEnv for details
    q_max: float, optional
        maximum permissible ALINEA flow. Look at BottleneckEnv for details
    q_min: float, optional
        minimum permissible ALINEA flow. Look at BottleneckEnv for details
    feedback_coeff: float, optional
        gain coefficient for ALINEA. Look at BottleneckEnv for details

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a bottleneck.
    """
    if render is None:
        render = False

    sim_params = SumoParams(
        sim_step=0.5,
        render=render,
        restart_instance=restart_instance)
    vehicles = VehicleParams()

    if lc_on:
        lc_mode = 1621
    else:
        lc_mode = 0

    controlled_segments = [("1", 1, True), ("2", 8, True), ("3", 3, True),
                           ("4", 1, True), ("5", 3, True)]  # 12 controllable segments
    num_observed_segments = [('1', 1), ('2', 3), ('3', 3), ('4', 3), ('5', 1)]
    v_regions = [23,
                 23, 23, 10, 5, 5, 23, 23, 23,
                 23, 23, 23,
                 23,
                 23, 23, 23]

    # set default q_max, q_min values
    if not q_max:
        if disable_ramp_meter:
            q_max = 14401
        else:
            q_max = 3000

    if not q_min:
        if disable_ramp_meter:
            q_min = 200
        else:
            q_min = 900

    additional_env_params = {
        "target_velocity": 40,
        "max_accel": 3,
        "max_decel": 3,
        "lane_change_duration": 5,
        "add_rl_if_exit": False,
        "disable_tb": disable_tb,
        "disable_ramp_metering": disable_ramp_meter,
        "n_crit": n_crit,
        "q_max": 14401,
        "q_min": 200,
        "q_init": q_init,
        "feedback_coeff": feedback_coef,
        "controlled_segments": controlled_segments,
        "inflow_range": [flow_rate, flow_rate],
        "reset_inflow": False,
        "symmetric": True,
        "observed_segments": num_observed_segments,
        "congest_penalty": False,
        "lc_mode": lc_mode,
        'start_inflow': flow_rate,
        'life_penalty': 0.00,
        'keep_past_actions': False,
        "num_sample_seconds": 0.5,
        "speed_reward": False,
        'fair_reward': False,  # This doesn't do anything, remove
        'exit_history_seconds': 10,  # This doesn't do anything, remove

    }

    if penetration_rate != 0.0:
        vehicles.add(
            veh_id="AV",
            lane_change_controller=(SimLaneChangeController, {}),
            routing_controller=(ContinuousRouter, {}),
            # acceleration_controller=(CFMController, {"v_des": 10, "d_des": 30, "k_d": 30, "k_v": 15}),
            # acceleration_controller=(HandTunedVelocityController, {"v_regions": v_regions}),
            # acceleration_controller=(DecentralizedALINEAController, {"stop_edge": "2", "stop_pos": 310, "additional_env_params": additional_env_params}),
            acceleration_controller=(FakeDecentralizedALINEAController, {"stop_edge": "2", "stop_pos": 310, "additional_env_params": additional_env_params}),
            car_following_params=SumoCarFollowingParams(
                speed_mode=31,
            ),
            lane_change_params=SumoLaneChangeParams(
                lane_change_mode=lc_mode
            ),
            num_vehicles=1)

    vehicles.add(
        veh_id="human",
        lane_change_controller=(SimLaneChangeController, {}),
        routing_controller=(ContinuousRouter, {}),
        car_following_params=SumoCarFollowingParams(
            speed_mode=31,
        ),
        lane_change_params=SumoLaneChangeParams(
            lane_change_mode=lc_mode
        ),
        num_vehicles=1)

    env_params = EnvParams(
        horizon=horizon, additional_params=additional_env_params)

    inflow = InFlows()

    if penetration_rate != 0.0:
        av_veh_per_hour = flow_rate * (penetration_rate)
        human_veh_per_hour = flow_rate * (1 - penetration_rate)

        inflow.add(
            veh_type="AV",
            edge="1",
            vehsPerHour=av_veh_per_hour,
            departLane="random",
            departSpeed=23)
    else:
        human_veh_per_hour = flow_rate

    inflow.add(
        veh_type="human",
        edge="1",
        vehsPerHour=human_veh_per_hour,
        departLane="random",
        departSpeed=23)

    traffic_lights = TrafficLightParams()
    if not disable_tb:
        traffic_lights.add(node_id="2")
    if not disable_ramp_meter:
        traffic_lights.add(node_id="3")

    additional_net_params = {"scaling": scaling, "speed_limit": 60}
    net_params = NetParams(
        inflows=inflow,
        no_internal_links=False,
        additional_params=additional_net_params)

    initial_config = InitialConfig(
        spacing="random",
        min_gap=5,
        lanes_distribution=float("inf"),
        edges_distribution=["2", "3", "4", "5"])

    scenario = BottleneckScenario(
        name="bay_bridge_toll",
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config,
        traffic_lights=traffic_lights)

    env = DesiredVelocityEnv(env_params, sim_params, scenario)

    return BottleneckDensityExperiment(env, int(flow_rate))
Exemple #12
0
def bottleneck(render="drgb"):
    # time horizon of a single rollout
    HORIZON = 1500

    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",
        routing_controller=(ContinuousRouter, {}),
        car_following_params=SumoCarFollowingParams(speed_mode=9, ),
        lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
        num_vehicles=1 * SCALING,
    )
    vehicles.add(
        veh_id="rl",
        acceleration_controller=(RLController, {}),
        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,
        "disable_ramp_metering": True,
        "controlled_segments": controlled_segments,
        "symmetric": False,
        "observed_segments": num_observed_segments,
        "reset_inflow": False,
        "lane_change_duration": 5,
        "max_accel": 3,
        "max_decel": 3,
        "inflow_range": [1200, 2500],
    }

    # flow rate
    flow_rate = 2500 * SCALING

    # percentage of flow coming out of each lane
    inflow = InFlows()
    inflow.add(
        veh_type="human",
        edge="1",
        vehs_per_hour=flow_rate * (1 - AV_FRAC),
        depart_lane="random",
        depart_speed=10,
    )
    inflow.add(
        veh_type="rl",
        edge="1",
        vehs_per_hour=flow_rate * AV_FRAC,
        depart_lane="random",
        depart_speed=10,
    )

    traffic_lights = TrafficLightParams()
    if not DISABLE_TB:
        traffic_lights.add(node_id="2")
    if not DISABLE_RAMP_METER:
        traffic_lights.add(node_id="3")

    additional_net_params = {"scaling": SCALING, "speed_limit": 23}
    net_params = NetParams(inflows=inflow,
                           additional_params=additional_net_params)

    flow_params = dict(
        # name of the experiment
        exp_tag="bottleneck_0",
        # name of the flow environment the experiment is running on
        env_name=BottleneckDesiredVelocityEnv,
        # name of the network class the experiment is running on
        network=BottleneckNetwork,
        # simulator that is used by the experiment
        simulator="traci",
        # sumo-related parameters (see flow.core.params.SumoParams)
        sim=SumoParams(
            sim_step=0.5,
            render=render,
            save_render=True,
            print_warnings=False,
            restart_instance=True,
        ),
        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            warmup_steps=40,
            sims_per_step=1,
            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(
            inflows=inflow,
            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 initialization/
        # reset (see flow.core.params.InitialConfig)
        initial=InitialConfig(
            spacing="uniform",
            min_gap=5,
            lanes_distribution=float("inf"),
            edges_distribution=["2", "3", "4", "5"],
        ),
        # traffic lights to be introduced to specific nodes (see
        # flow.core.params.TrafficLightParams)
        tls=traffic_lights,
    )
    return flow_params
Exemple #13
0
    "duration": "31",
    "state": "GrGr"
}, {
    "duration": "6",
    "state": "yryr"
}, {
    "duration": "31",
    "state": "rGrG"
}, {
    "duration": "6",
    "state": "ryry"
}]
for nodeid in nodes:
    tl_logic.add(nodeid,
                 tls_type="static",
                 programID=1,
                 offset=None,
                 phases=phases)
# vehicles
vehicles = VehicleParams()
vehicles.add("human",
             acceleration_controller=(SimCarFollowingController, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode="right_of_way", min_gap=2.5),
             routing_controller=(GridRouter, {}),
             num_vehicles=tot_cars)
# inflow, probleem met de inflow is er nog steeds, geen routes gedefinieerd
inflow = InFlows()
outer_edges = []
outer_edges += ["left{}_{}".format(rows, i) for i in range(columns)]
outer_edges += ["right0_{}".format(i) for i in range(rows)]
Exemple #14
0
def bottleneck_example(flow_rate,
                       horizon,
                       restart_instance=False,
                       render=None):
    """
    Perform a simulation of vehicles on a bottleneck.

    Parameters
    ----------
    flow_rate : float
        total inflow rate of vehicles into the bottleneck
    horizon : int
        time horizon
    restart_instance: bool, optional
        whether to restart the instance upon reset
    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 a bottleneck.
    """
    if render is None:
        render = False

    sim_params = SumoParams(sim_step=0.5,
                            render=render,
                            overtake_right=False,
                            restart_instance=restart_instance)

    vehicles = VehicleParams()

    vehicles.add(veh_id="human",
                 lane_change_controller=(SimLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 car_following_params=SumoCarFollowingParams(speed_mode=25, ),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode=1621, ),
                 num_vehicles=1)

    additional_env_params = {
        "target_velocity": 40,
        "max_accel": 1,
        "max_decel": 1,
        "lane_change_duration": 5,
        "add_rl_if_exit": False,
        "disable_tb": DISABLE_TB,
        "disable_ramp_metering": DISABLE_RAMP_METER
    }
    env_params = EnvParams(horizon=horizon,
                           additional_params=additional_env_params)

    inflow = InFlows()
    inflow.add(veh_type="human",
               edge="1",
               vehsPerHour=flow_rate,
               departLane="random",
               departSpeed=10)

    traffic_lights = TrafficLightParams()
    if not DISABLE_TB:
        traffic_lights.add(node_id="2")
    if not DISABLE_RAMP_METER:
        traffic_lights.add(node_id="3")

    additional_net_params = {"scaling": SCALING, "speed_limit": 23}
    net_params = NetParams(inflows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="random",
                                   min_gap=5,
                                   lanes_distribution=float("inf"),
                                   edges_distribution=["2", "3", "4", "5"])

    scenario = BottleneckScenario(name="bay_bridge_toll",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=traffic_lights)

    env = BottleneckEnv(env_params, sim_params, scenario)

    return BottleneckDensityExperiment(env)
def grid_example(render=None):
    """
    Perform a simulation of vehicles on a grid.

    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 and balanced traffic lights on a grid.
    """
    inner_length = 300
    long_length = 500
    short_length = 300
    N_ROWS = 2
    N_COLUMNS = 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) * N_COLUMNS \
        + (num_cars_top + num_cars_bot) * N_ROWS

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

    sim_params = AimsunParams(sim_step=0.5, render=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(veh_id="human", num_vehicles=tot_cars)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    tl_logic = TrafficLightParams(baseline=False)
    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "yellow": "3",
        "state": "GGGrrrGGGrrr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "yellow": "3",
        "state": "yyyrrryyyrrr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "yellow": "3",
        "state": "rrrGGGrrrGGG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "yellow": "3",
        "state": "rrryyyrrryyy"
    }]
    tl_logic.add("center0", phases=phases, programID=1)
    tl_logic.add("center1", phases=phases, programID=1)
    tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1)
    tl_logic.add("center3", phases=phases, programID=1)
    tl_logic.add("center4", phases=phases, programID=1)
    tl_logic.add("center5", tls_type="actuated", phases=phases, programID=1)

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

    initial_config = InitialConfig(spacing='custom')

    scenario = SimpleGridScenario(name="grid-intersection",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    env = AccelEnv(env_params, sim_params, scenario, simulator='aimsun')

    return Experiment(env)
tl_logic = TrafficLightParams(baseline=False)
phases = [{
    "duration": "100000",
    "state": "GGGGr"
}, {
    "duration": "4",
    "state": "yyyyr"
}, {
    "duration": "10",
    "state": "rrrrG"
}, {
    "duration": "10",
    "state": "rrrrr"
}]
tl_logic.add("C", phases=phases, programID="custom", offset="0")


# specify the routes for vehicles in the network
class PedCrossing(Network):
    def specify_routes(self, net_params):
        return {'EC': ['EC', 'CW'], 'WC': ['WC', 'CE']}


flow_params = dict(
    exp_tag='ped_movexy',
    env_name=FmtPedEnvNoLtlReward,
    network=PedCrossing,
    simulator='traci',
    sim=sim_params,
    env=env_params,
Exemple #17
0
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "yryryryryryr"
}, {
    "duration": "31",
    "minDur": "8",
    "maxDur": "45",
    "state": "rGrGrGrGrGrG"
}, {
    "duration": "6",
    "minDur": "3",
    "maxDur": "6",
    "state": "ryryryryryry"
}]
tl_logic.add("center0", phases=phases, programID=1)
tl_logic.add("center1", phases=phases, programID=1)
tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated")

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

if USE_INFLOWS:
    initial_config, net_params = get_flow_params(
        col_num=n_columns,
        row_num=n_rows,
        additional_net_params=additional_net_params)
Exemple #18
0
    veh_type="human",
    edge="1",
    #whats happening here
    vehs_per_hour=flow_rate * (1 - AV_FRAC),
    departLane="random",
    departSpeed=10)
inflow.add(
    veh_type="followerstopper",
    edge="1",
    vehs_per_hour=flow_rate * AV_FRAC,
    departLane="random",
    departSpeed=10)

traffic_lights = TrafficLightParams()
if not DISABLE_TB:
    traffic_lights.add(node_id="2")
if not DISABLE_RAMP_METER:
    traffic_lights.add(node_id="3")

additional_net_params = {"scaling": SCALING, "speed_limit": 23}
net_params = NetParams(
    inflows=inflow,
    additional_params=additional_net_params)

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

    # name of the flow environment the experiment is running on
    env_name="BottleneckDesiredVelocityEnv",
Exemple #19
0
def grid0_baseline(num_runs, render=True):
    """Run script for the grid0 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render : bool, 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']
    vehicles = flow_params['veh']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())

    # define the traffic light logic
    tl_logic = TrafficLightParams(baseline=False)

    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "GrGr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yryr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rGrG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "ryry"
    }]

    for i in range(N_ROWS * N_COLUMNS):
        tl_logic.add('center' + str(i),
                     tls_type='actuated',
                     phases=phases,
                     programID=1)

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

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

    # import the network class
    network_class = flow_params['network']

    # create the network object
    network = network_class(name=exp_tag,
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config,
                            traffic_lights=tl_logic)

    # import the environment class
    env_class = flow_params['env_name']

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

    exp = Experiment(env)

    results = exp.run(num_runs, env_params.horizon)
    total_delay = np.mean(results['returns'])

    return total_delay
def grid1_baseline(num_runs, render=True):
    """Run script for the grid1 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render: bool, optional
            specifies whether to 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']
    vehicles = flow_params['veh']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())

    # define the traffic light logic
    tl_logic = TrafficLightParams(baseline=False)
    phases = [{
        'duration': '31',
        'minDur': '5',
        'maxDur': '45',
        'state': 'GGGrrrGGGrrr'
    }, {
        'duration': '2',
        'minDur': '2',
        'maxDur': '2',
        'state': 'yyyrrryyyrrr'
    }, {
        'duration': '31',
        'minDur': '5',
        'maxDur': '45',
        'state': 'rrrGGGrrrGGG'
    }, {
        'duration': '2',
        'minDur': '2',
        'maxDur': '2',
        'state': 'rrryyyrrryyy'
    }]
    for i in range(N_ROWS * N_COLUMNS):
        tl_logic.add('center' + str(i),
                     tls_type='actuated',
                     phases=phases,
                     programID=1)

    # 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=tl_logic)

    # 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)
    total_delay = np.mean(results['returns'])

    return total_delay
Exemple #21
0
def da_yuan_example(render=None, use_inflows=True):
    """
    Perform a simulation of vehicles on a traffic light grid.

    Parameters
    ----------
    render: bool, optional
        specifies whether to use the gui during execution
    use_inflows : bool, optional
        set to True if you would like to run the experiment with inflows of
        vehicles from the edges, and False otherwise

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles and balanced traffic lights on a traffic light grid.
    """
    v_enter = 10
    sim_params = SumoParams(sim_step=0.05, render=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="human",
        routing_controller=(GridRouter, {}),
        car_following_params=SumoCarFollowingParams(
            min_gap=2.5,
            decel=7.5,  # avoid collisions at emergency stops
        ),
        num_vehicles=6)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    tl_logic = TrafficLightParams(baseline=False)
    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rrGGGG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "rryyyy"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "GGrrrr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yyrrrr"
    }]
    tl_logic.add("inner0", phases=phases, programID=1)
    tl_logic.add("inner1", phases=phases, programID=1)
    tl_logic.add("inner2", phases=phases, programID=1, tls_type="actuated")
    tl_logic.add("inner3", phases=phases, programID=1, tls_type="actuated")

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

    if use_inflows:
        initial_config, net_params = get_flow_params(
            additional_net_params=additional_net_params)
    else:
        initial_config, net_params = get_non_flow_params(enter_speed=v_enter)

    network = DaYuanNetwork(name="grid-intersection",
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config,
                            traffic_lights=tl_logic)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)