Example #1
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sim_params = SumoParams(sim_step=0.1, render=True)

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

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

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

    initial_config = InitialConfig(spacing="uniform")

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

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

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

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

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=15000,
        max_path_length=horizon,
        n_itr=500,
        # whole_paths=True,
        discount=0.999,
        # step_size=v["step_size"],
    )
    algo.train(),
import collections

# create some default parameters parameters
HORIZON = 3000
env_params = EnvParams(
        horizon=HORIZON,
        sims_per_step=1,
        warmup_steps=0,
        additional_params={
            "max_accel": 3,
            "max_decel": -2,
            "target_velocity": 20,
            "lane_change_duration": 4,
            "num_rl": 5,
        })
initial_config = InitialConfig(edges_distribution=['highway_0'])

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",
Example #3
0
    ),

    # environment related parameters (see flow.core.params.EnvParams)
    env=EnvParams(
        horizon=HORIZON,
        sims_per_step=2,
        warmup_steps=0,
        additional_params={
            "max_accel": 1.5,
            "max_decel": 1.5,
            "target_velocity": 20,
            "num_rl": NUM_RL,
        },
    ),

    # 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(),
)
Example #4
0
            "num_local_lights": 4,
        },
    ),

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

    # 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
    # or reset (see flow.core.params.InitialConfig)
    initial=InitialConfig(
        spacing='custom',
        shuffle=True,
    ),
)

#############################以下为训练部分#################################
def cover_actions(c_a, s_a,num):
    # for i in range(len(c_a)):
    #     if c_a[i] == 1:
    #         s_a[i] = abs(s_a[i] - 1)
    for i in range(num):
        if i == c_a:
            s_a[i] = 1
    return s_a


def data_collection(env, vels, queues):
Example #5
0
        sims_per_step=1,
        horizon=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(
        in_flows=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,
)
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 = 0

    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', 2, True), ('3', 2, True),
                           ('4', 2, True), ('5', 1, False)]
    num_observed_segments = [('1', 1), ('2', 3), ('3', 3), ('4', 3), ('5', 1)]
    if np.isclose(args.av_frac, 0.4):
        q_init = 1000
    else:
        q_init = 600
    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,
        'communicate': args.communicate,
        "centralized_obs": args.central_obs,
        "aggregate_info": args.aggregate_info,
        "av_frac": args.av_frac,
        "congest_penalty_start": args.congest_penalty_start,
        "lc_mode": lc_mode,
        "life_penalty": args.life_penalty,
        'keep_past_actions': args.keep_past_actions,
        "num_sample_seconds": args.num_sample_seconds,
        "speed_reward": args.speed_reward,
        'fair_reward': False,  # This doesn't do anything, remove
        'exit_history_seconds': 0,  # This doesn't do anything, remove

        # parameters for the staggering controller that we imitate
        "n_crit": 8,
        "q_max": 15000,
        "q_min": 200,
        "q_init": q_init,  #
        "feedback_coeff": 1,  #
        'num_imitation_iters': args.num_imitation_iters,
    }

    # 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=23.0)
        inflow.add(veh_type='av',
                   edge='1',
                   vehs_per_hour=flow_rate * args.av_frac,
                   departLane='random',
                   departSpeed=23.0)
    else:
        inflow.add(veh_type='av',
                   edge='1',
                   vehs_per_hour=flow_rate,
                   departLane='random',
                   departSpeed=23.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}

    if args.imitate:
        env_name = 'MultiBottleneckImitationEnv'
    else:
        env_name = 'MultiBottleneckEnv'
    flow_params = dict(
        # name of the experiment
        exp_tag=args.exp_title,

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

        # 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(0 / args.sim_step),
            sims_per_step=2,
            horizon=args.horizon,
            clip_actions=False,
            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
Example #7
0
                 speed_mode="obey_safe_speed",  
             ),
             num_vehicles=10
             )
vehicles.add("human",
             acceleration_controller=(IDMController, {}),
             lane_change_controller=(SimLaneChangeController, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode=25
             ),
             lane_change_params = SumoLaneChangeParams(lane_change_mode=1621),
             num_vehicles=0)

# specify the edges vehicles can originate on
initial_config = InitialConfig(
    edges_distribution=["edge4"]
)

# specify the routes for vehicles in the network
class Network(Network):

    def specify_routes(self, net_params):
        return {
                "edge1": ["edge1","edge2","edge3","edge4","edge5","edge6"],
                #"edge3": ["edge3","edge4","edge5","edge10","edge11","edge12","edge3"],
                "edge4": ["edge4","edge5","edge10","edge11","edge12","edge3","edge4"],
                }


inflow = InFlows()
inflow.add(veh_type="human",
    # length of one rollout
    horizon=horizon, )

# the simulation parameters
sim_params = SumoParams(
    sim_step=sim_step,
    render=True,
    show_radius=True,  # show a circle on top of RL agents
    overtake_right=
    True,  # overtake on right to simulate more aggressive behavior
    emission_path=Params.DATA_DIR,
    restart_instance=False,
)

# setting initial configuration files
initial_config = InitialConfig(shuffle=True, )

# Adding inflows
inflow = InFlows()

human_inflow = dict(
    veh_type="human",
    probability=0.001,
    depart_lane="random",
    depart_speed="random",
    begin=5,  # time in seconds to start the inflow
)

# adding human inflows
inflow_random_edges(inflow, **human_inflow)
Example #9
0
        "target_velocity": 20,
        "sort_vehicles": True,
    },
)

net_params = NetParams(
    inflows=inflow,
    #no_internal_links=False,
    additional_params=additional_net_params,
    template={
        "net": scenario_road_data["net"],  # see above
        "rou": scenario_road_data["rou"],  # see above 
    })

initial_config = InitialConfig(
    #scenario_road_data["edges_distribution"]
    edges_distribution=scenario_road_data["edges_distribution"], )
if __name__ == "__main__":
    scenario = i696Scenario(
        name='i696',
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config,
    )
    #env = AccelEnv(env_params,sumo_params,scenario)
    env = TestEnv(env_params, sumo_params, scenario)
    exp = Experiment(env)
    _ = exp.run(100, 5000)  #,convert_to_csv=True)
    #from IPython import embed
    #embed()
    with open("info.pkl", "wb") as f:
Example #10
0
def figure_eight_baseline(num_runs, render=True):
    """Run script for all figure eight baselines.

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

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

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

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

    # we want no autonomous vehicles in the simulation
    vehicles = Vehicles()
    vehicles.add(veh_id='human',
                 acceleration_controller=(IDMController, {
                     'noise': 0.2
                 }),
                 routing_controller=(ContinuousRouter, {}),
                 sumo_car_following_params=SumoCarFollowingParams(
                     speed_mode='no_collide', ),
                 num_vehicles=14)

    # 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, sumo_params, scenario)

    exp = Experiment(env)

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

    return avg_speed
Example #11
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
    """
    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 = 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

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

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

    return np.mean(results['returns']), np.std(results['returns'])
Example #12
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 sumo's 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.
    """
    sumo_params = SumoParams(sim_step=0.4, overtake_right=True)

    if render is not None:
        sumo_params.render = render

    sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2)
    sumo_lc_params = SumoLaneChangeParams(model="LC2013",
                                          lcCooperative=0.2,
                                          lcSpeedGain=15)

    vehicles = Vehicles()

    vehicles.add(veh_id="human",
                 acceleration_controller=(SumoCarFollowingController, {}),
                 routing_controller=(BayBridgeRouter, {}),
                 speed_mode="all_checks",
                 lane_change_mode="no_lat_collide",
                 sumo_car_following_params=sumo_car_following_params,
                 sumo_lc_params=sumo_lc_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,
                           netfile=NETFILE)

    # download the netfile 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__)), NETFILE),
            "wb+") as f:
        f.write(data_to_write)

    initial_config = InitialConfig(
        spacing="uniform",  # "random",
        min_gap=15)

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

    env = BayBridgeEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
    def __init__(self,
                 name,
                 vehicles,
                 net_params,
                 initial_config=InitialConfig(),
                 traffic_lights=TrafficLightParams()):
        """Initialize a highway with on and off ramps network."""
        for p in ADDITIONAL_NET_PARAMS.keys():
            if p not in net_params.additional_params:
                raise KeyError('Network parameter "{}" not supplied'.format(p))

        # load parameters into class
        params = net_params.additional_params

        self.highway_length = params['highway_length']
        self.on_ramps_length = params['on_ramps_length']
        self.off_ramps_length = params['off_ramps_length']

        self.highway_lanes = params['highway_lanes']
        self.on_ramps_lanes = params['on_ramps_lanes']
        self.off_ramps_lanes = params['off_ramps_lanes']

        self.highway_speed = params['highway_speed']
        self.on_ramps_speed = params['on_ramps_speed']
        self.off_ramps_speed = params['off_ramps_speed']

        self.on_ramps_pos = params['on_ramps_pos']
        self.off_ramps_pos = params['off_ramps_pos']

        self.p = params['next_off_ramp_proba']

        self.angle_on_ramps = params['angle_on_ramps']
        self.angle_off_ramps = params['angle_off_ramps']

        # generate position of all network nodes
        self.ramps_pos = sorted(self.on_ramps_pos + self.off_ramps_pos)
        self.nodes_pos = sorted(
            list(set([0] + self.ramps_pos + [self.highway_length])))

        # highway_pos[x] = id of the highway node whose starting position is x
        self.highway_pos = {x: i for i, x in enumerate(self.nodes_pos)}
        # ramp_pos[x] = id of the ramp node whose intersection with the highway
        # is at position x
        self.ramp_pos = {
            x: "on_ramp_{}".format(i)
            for i, x in enumerate(self.on_ramps_pos)
        }
        self.ramp_pos.update({
            x: "off_ramp_{}".format(i)
            for i, x in enumerate(self.off_ramps_pos)
        })

        # make sure network is constructable
        if (len(self.ramps_pos) > 0
                and (min(self.ramps_pos) <= 0
                     or max(self.ramps_pos) >= self.highway_length)):
            raise ValueError('All ramps positions should be positive and less '
                             'than highway length. Current ramps positions: {}'
                             '. Current highway length: {}.'.format(
                                 self.ramps_pos, self.highway_length))
        if len(self.ramps_pos) != len(list(set(self.ramps_pos))):
            raise ValueError('Two ramps positions cannot be equal.')

        super().__init__(name, vehicles, net_params, initial_config,
                         traffic_lights)
Example #14
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sim_params = SumoParams(sim_step=0.2, render=True)

    # 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, {
            "noise": 0.2
        }),
        lane_change_controller=(SimLaneChangeController, {}),
        routing_controller=(ContinuousRouter, {}),
        num_vehicles=10,
        car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5),
        lane_change_params=SumoLaneChangeParams())

    env_params = EnvParams(
        horizon=HORIZON,
        additional_params={
            "target_velocity": 10,
            "max_accel": 3,
            "max_decel": 3,
            "sort_vehicles": False
        })

    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")

    scenario = TwoLoopsOneMergingScenario(
        name=exp_tag,
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config)

    env_name = "AccelEnv"
    pass_params = (env_name, sim_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()
Example #15
0
    def __init__(self,
                 name,
                 generator_class,
                 vehicles,
                 net_params,
                 initial_config=InitialConfig(),
                 traffic_lights=TrafficLights()):
        """Base scenario class.

        Initializes a new scenario. Scenarios are used to specify features of
        a network, including the positions of nodes, properties of the edges
        and junctions connecting these nodes, properties of vehicles and
        traffic lights, and other features as well.

        Several network specific features can be acquired from this class via a
        plethora of get methods (see documentation).

        This class can be instantiated once and reused in multiple experiments.
        Note that this function stores all the relevant parameters. The
        generate() function still needs to be called separately.

        Attributes
        ----------
        name: str
            A tag associated with the scenario
        generator_class: Generator type
            Class for generating configuration and net files with placed
            vehicles, e.g. CircleGenerator
        vehicles: Vehicles type
            see flow/core/vehicles.py
        net_params: NetParams type
            see flow/core/params.py
        initial_config: InitialConfig type
            see flow/core/params.py
        traffic_lights: flow.core.traffic_lights.TrafficLights type
            see flow/core/traffic_lights.py
        """
        # Invoke serializable if using rllab
        if Serializable is not object:
            Serializable.quick_init(self, locals())

        self.name = name
        self.generator_class = generator_class
        self.vehicles = vehicles
        self.net_params = net_params
        self.initial_config = initial_config
        self.traffic_lights = traffic_lights

        # create a generator instance
        self.generator = self.generator_class(self.net_params, self.name)

        # create the network configuration file from the generator
        self._edges, self._connections = self.generator.generate_net(
            self.net_params, self.traffic_lights)

        # list of edges and internal links (junctions)
        self._edge_list = [
            edge_id for edge_id in self._edges.keys() if edge_id[0] != ":"
        ]
        self._junction_list = list(
            set(self._edges.keys()) - set(self._edge_list))

        # parameters to be specified under each unique subclass's
        # __init__() function
        self.edgestarts = self.specify_edge_starts()

        # these optional parameters need only be used if "no-internal-links"
        # is set to "false" while calling sumo's netconvert function
        self.internal_edgestarts = self.specify_internal_edge_starts()
        self.intersection_edgestarts = self.specify_intersection_edge_starts()

        # in case the user did not write the intersection edge-starts in
        # internal edge-starts as well (because of redundancy), merge the two
        # together
        self.internal_edgestarts += self.intersection_edgestarts
        seen = set()
        self.internal_edgestarts = \
            [item for item in self.internal_edgestarts
             if item[1] not in seen and not seen.add(item[1])]
        self.internal_edgestarts_dict = dict(self.internal_edgestarts)

        # total_edgestarts and total_edgestarts_dict contain all of the above
        # edges, with the former being ordered by position
        if self.net_params.no_internal_links:
            self.total_edgestarts = self.edgestarts
        else:
            self.total_edgestarts = self.edgestarts + self.internal_edgestarts
        self.total_edgestarts.sort(key=lambda tup: tup[1])

        self.total_edgestarts_dict = dict(self.total_edgestarts)

        # length of the network, or the portion of the network in
        # which cars are meant to be distributed
        # (may be overridden by subclass __init__())
        if not hasattr(self, "length"):
            self.length = sum([
                self.edge_length(edge_id) for edge_id in self.get_edge_list()
            ])

        # generate starting position for vehicles in the network
        if self.initial_config.positions is None:
            self.initial_config.positions, self.initial_config.lanes = \
                self.generate_starting_positions()

        # create the sumo configuration files using the generator class
        cfg_name = self.generator.generate_cfg(self.net_params)
        self.generator.make_routes(self, self.initial_config)

        # specify the location of the sumo configuration file
        self.cfg = self.generator.cfg_path + cfg_name
Example #16
0
def bottleneck0_baseline(num_runs, render=True):
    """Run script for the bottleneck0 baseline.

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

    Returns
    -------
        SumoExperiment
            class needed to run simulations
    """
    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 speed_mode=9,
                 routing_controller=(ContinuousRouter, {}),
                 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": [1000, 2000]
    }

    # flow rate
    flow_rate = 1900 * SCALING

    # percentage of flow coming out of each lane
    inflow = InFlows()
    inflow.add(veh_type="human", edge="1",
               vehs_per_hour=flow_rate,
               departLane="random", departSpeed=10)

    traffic_lights = TrafficLights()
    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}
    net_params = NetParams(inflows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

    sumo_params = SumoParams(
        sim_step=0.5,
        render=render,
        print_warnings=False,
        restart_instance=False,
    )

    env_params = EnvParams(
        evaluate=True,  # Set to True to evaluate traffic metrics
        warmup_steps=40,
        sims_per_step=1,
        horizon=HORIZON,
        additional_params=additional_env_params,
    )

    initial_config = InitialConfig(
        spacing="uniform",
        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, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, HORIZON)
    return np.mean(results["returns"]), np.std(results["returns"])
Example #17
0
def grid1_baseline(num_runs, sumo_binary="sumo-gui"):
    """Run script for the grid1 baseline.

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

    Returns
    -------
        SumoExperiment
            class needed to run simulations
    """
    # we place a sufficient number of vehicles to ensure they confirm with the
    # total number specified above. We also use a "right_of_way" speed mode to
    # support traffic light compliance
    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(SumoCarFollowingController, {}),
                 sumo_car_following_params=SumoCarFollowingParams(
                     min_gap=2.5,
                     max_speed=V_ENTER,
                 ),
                 routing_controller=(GridRouter, {}),
                 num_vehicles=(N_LEFT+N_RIGHT)*N_COLUMNS +
                              (N_BOTTOM+N_TOP)*N_ROWS,
                 speed_mode="right_of_way")

    # inflows of vehicles are place on all outer edges (listed here)
    outer_edges = []
    outer_edges += ["left{}_{}".format(N_ROWS, i) for i in range(N_COLUMNS)]
    outer_edges += ["right0_{}".format(i) for i in range(N_ROWS)]
    outer_edges += ["bot{}_0".format(i) for i in range(N_ROWS)]
    outer_edges += ["top{}_{}".format(i, N_COLUMNS) for i in range(N_ROWS)]

    # equal inflows for each edge (as dictate by the EDGE_INFLOW constant)
    inflow = InFlows()
    for edge in outer_edges:
        inflow.add(veh_type="human", edge=edge, vehs_per_hour=EDGE_INFLOW,
                   departLane="free", departSpeed="max")

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

    net_params = NetParams(
            in_flows=inflow,
            no_internal_links=False,
            additional_params={
                "speed_limit": V_ENTER + 5,
                "grid_array": {
                    "short_length": SHORT_LENGTH,
                    "inner_length": INNER_LENGTH,
                    "long_length": LONG_LENGTH,
                    "row_num": N_ROWS,
                    "col_num": N_COLUMNS,
                    "cars_left": N_LEFT,
                    "cars_right": N_RIGHT,
                    "cars_top": N_TOP,
                    "cars_bot": N_BOTTOM,
                },
                "horizontal_lanes": 1,
                "vertical_lanes": 1,
            },
        )

    sumo_params = SumoParams(
            restart_instance=False,
            sim_step=1,
            sumo_binary=sumo_binary,
        )

    env_params = EnvParams(
            evaluate=True,  # Set to True to evaluate traffic metrics
            horizon=HORIZON,
            additional_params={
                "switch_time": 2.0,
                "num_observed": 2,
                "tl_type": "actuated",
            },
        )

    initial_config = InitialConfig(shuffle=True)

    scenario = SimpleGridScenario(name="grid",
                                  generator_class=SimpleGridGenerator,
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    env = PO_TrafficLightGridEnv(env_params, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, HORIZON)
    total_delay = np.mean(results["returns"])

    return total_delay
Example #18
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 sumo's gui during execution

    Returns
    -------
        SumoExperiment
            class needed to run simulations
    """
    exp_tag = flow_params['exp_tag']
    sumo_params = flow_params['sumo']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())
    traffic_lights = flow_params.get('tls', TrafficLights())

    # we want no autonomous vehicles in the simulation
    vehicles = Vehicles()
    vehicles.add(veh_id='human',
                 speed_mode=9,
                 routing_controller=(ContinuousRouter, {}),
                 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
    sumo_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, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

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

    return np.mean(results['returns']), np.std(results['returns'])
Example #19
0
def main():
    model_logs_path = args.model_logs_path
    rewards_logs_path = args.rewards_logs_path
    label = args.label
    load_path = args.load_path
    print('LOAD PATH 	--	main:', load_path)
    train = True

    experiments = 2
    runs = 5000
    steps_per_run = 250

    #1. Set the logs object, creating the logs paths, if it does not exists yet, and the experiments logs path
    save_logs = SaveLogs(label, experiments, runs, steps_per_run)

    save_logs.create_logs_path()
    save_logs.create_experiments_logs_path()

    #2. Iniciate the variables used to store informations from the simulation
    performance = np.zeros(runs)
    collisions = np.zeros(runs)
    rewards = np.zeros(runs)

    #3. Run a set number of experiments
    for i in range(experiments):
        #1. Create a Vehicle object containing the vehicles that will be in the simulation
        # The tau parameter must be lower than the simulation step in order to allow collisions
        sumo_car_following_params = SumoCarFollowingParams(sigma=1,
                                                           security=False,
                                                           tau=0.1)
        vehicles = Vehicles()
        # The speed mode parameter controlls how the vehicle controlls the speed. All the options for this parameter
        #can be found in: http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {
                         'T': 0.1,
                         's0': 0.1
                     }),
                     routing_controller=(GridRouter, {}),
                     num_vehicles=4,
                     speed_mode=1100,
                     lane_change_mode='aggressive',
                     sumo_car_following_params=sumo_car_following_params)
        vehicles.add(veh_id="rl",
                     acceleration_controller=(RLController, {}),
                     routing_controller=(GridRouter, {}),
                     num_vehicles=1,
                     speed_mode=0000,
                     lane_change_mode='aggressive',
                     sumo_car_following_params=sumo_car_following_params)

        #2. Initite the parameters for a sumo simulation and the initial configurations of the simulation
        sumo_params = SumoParams(sim_step=0.5, render=True)

        edges_distribution = [
            'bottom', 'right'
        ]  #set the vehicles to just star on the bottom and right edges
        initial_config = InitialConfig(edges_distribution=edges_distribution,
                                       spacing='custom')

        #3. Set the environment parameter
        env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

        #4. Set the netfile parameters with the path to the .net.xml network file
        net_params = NetParams(netfile=os.getcwd() +
                               '/sumo/one_junction.net.xml')

        #5. Create instances of the scenario and environment
        scenario = OneJunctionScenario(  # we use the NetFileScenario scenario class for one junction scenario... 
            name="test_NetFile_scenario",
            generator_class=
            OneJunctionGenerator,  # ... as well as the newly netfile generator class
            vehicles=vehicles,
            net_params=net_params,
            initial_config=initial_config)

        env = OneJuntionCrashEnv(env_params, sumo_params, scenario)

        #6. create a instance of a sumo experiment
        exp = Experiment(env, scenario)

        #7. Run the sumo simulation for a set number of runs and time steps per run
        if train:
            info = exp.run_train(runs,
                                 steps_per_run,
                                 run=i,
                                 saveLogs=save_logs,
                                 train=True,
                                 load_path=load_path)
        else:
            info = exp.run_eval(runs,
                                steps_per_run,
                                run=i,
                                saveLogs=save_logs,
                                train=False,
                                load_path=load_path)

        performance = performance + info['performance']
        collisions = collisions + info['collisions']
        rewards = rewards + info['returns']

        save_logs.save_graph(label, 'exp' + str(i), performance, collisions,
                             rewards)

    #4. Average the total performance of the experiments
    performance = performance / experiments
    collisions = collisions / experiments
    rewards = rewards / experiments

    #5. Store all the statitics of the simulation
    save_logs.save_config_and_statistics()

    #6. Save the graphs produced
    save_logs.save_graph(label, 'final', performance, collisions, rewards)
Example #20
0
    # scenario's documentation or ADDITIONAL_NET_PARAMS component)
    net=NetParams(
        inflows=inflow,
        no_internal_links=False,
        additional_params={
            "speed_limit": V_ENTER + 5,
            "grid_array": {
                "short_length": SHORT_LENGTH,
                "inner_length": INNER_LENGTH,
                "long_length": LONG_LENGTH,
                "row_num": N_ROWS,
                "col_num": N_COLUMNS,
                "cars_left": N_LEFT,
                "cars_right": N_RIGHT,
                "cars_top": N_TOP,
                "cars_bot": N_BOTTOM,
            },
            "horizontal_lanes": 1,
            "vertical_lanes": 1,
        },
    ),

    # 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(shuffle=True),
)
Example #21
0
            "main_human": MAIN_HUMAN,
            "merge_human": MERGE_HUMAN,
            #"use_seeds":"/home/cuijiaxun/flow_2020_07_14_19_32_55.589614/seeds.pkl",
        },
    ),

    # 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()
    initial=InitialConfig(
        spacing='uniform',
        #edges_distribution=['left', 'inflow_highway']
        edges_distribution={
            "inflow_highway": MAIN_HUMAN / 5 + MAIN_RL / 5,
            "left": MAIN_HUMAN * 4 / 5 + MAIN_RL * 4 / 5,
            'bottom': MERGE_HUMAN * 4 / 5,
            'inflow_merge': MERGE_HUMAN / 5,
        }),
)
    # environment related parameters (see flow.core.params.EnvParams)
    env=EnvParams(
        horizon=HORIZON,
        sims_per_step=1,
        warmup_steps=0,
        additional_params={
            "max_accel": 3,
            "max_decel": -2,
            "target_velocity": 20,
            "lane_change_duration": 4,
            "num_rl": 5,
        },
    ),

    # 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(edges_distribution=['highway_0']),
)
    net=NetParams(
        additional_params={
            'length': 230,
            'lanes': 1,
            'speed_limit': 30,
            'resolution': 40,
            'num_rings': NUM_RINGS
        }, ),

    # 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(bunching=20.0, spacing='custom'),
)


create_env, env_name = make_create_env(params=flow_params, version=0)

# Register as rllib env
register_env(env_name, create_env)

test_env = create_env()
obs_space = test_env.observation_space
act_space = test_env.action_space


def gen_policy():
    """Generate a policy in RLlib."""
def run_task(*_):
    auton_cars = 20

    sumo_params = SumoParams(time_step=0.1,
                             human_speed_mode="no_collide",
                             rl_speed_mode="no_collide",
                             sumo_binary="sumo-gui")

    vehicles = Vehicles()
    vehicles.add_vehicles("idm", (RLController, {}), None, None, 0, 20)

    intensity = .2
    v_enter = 10
    env_params = EnvParams(additional_params={
        "target_velocity": v_enter,
        "control-length": 150,
        "max_speed": v_enter
    })

    additional_net_params = {
        "horizontal_length_in": 400,
        "horizontal_length_out": 800,
        "horizontal_lanes": 1,
        "vertical_length_in": 400,
        "vertical_length_out": 800,
        "vertical_lanes": 1,
        "speed_limit": {
            "horizontal": v_enter,
            "vertical": v_enter
        }
    }
    net_params = NetParams(no_internal_links=False,
                           additional_params=additional_net_params)

    cfg_params = {"start_time": 0, "end_time": 3000, "cfg_path": "debug/cfg/"}

    initial_config = InitialConfig(spacing="custom",
                                   additional_params={
                                       "intensity": intensity,
                                       "enter_speed": v_enter
                                   })

    scenario = TwoWayIntersectionScenario("two-way-intersection",
                                          TwoWayIntersectionGenerator,
                                          vehicles,
                                          net_params,
                                          initial_config=initial_config)

    env = TwoIntersectionEnvironment(env_params, sumo_params, scenario)
    env_name = "TwoIntersectionEnvironment"
    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)
    logging.info("Experiment Set Up complete")

    print("experiment initialized")

    env = normalize(env)

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

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=30000,
        max_path_length=horizon,
        # whole_paths=True,
        n_itr=200,
        discount=0.999,
        # step_size=0.01,
    )
    algo.train()
        inflows=inflow,
        #no_internal_links=False,
        additional_params=additional_net_params,
        template={
            "net": scenario_road_data["net"],  # see above
            "rou": scenario_road_data["rou"],  # see above 
        }),

    # 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(
        # Distributing only at the beginning of routes
        scenario_road_data["edges_distribution"]),
)
'''
def setup_exps(seeds_file=None):

    alg_run = "PPO"

    agent_cls = get_agent_class(alg_run)
    config = agent_cls._default_config.copy()
    config["num_workers"] = N_CPUS
    config["train_batch_size"] = HORIZON * N_ROLLOUTS
    config["gamma"] = 0.999  # discount rate
    config["model"].update({"fcnet_hiddens": [32, 32, 32]})
    config["use_gae"] = True
    config["lambda"] = 0.97
Example #26
0
    "vertical_length_in": 1500,
    "vertical_length_out": 1500,
    "vertical_lanes": 2,
    "speed_limit": {
        "horizontal": v_enter,
        "vertical": v_enter
    }
}
net_params = NetParams(no_internal_links=False,
                       additional_params=additional_net_params)

cfg_params = {"start_time": 0, "end_time": 3000, "cfg_path": "debug/cfg/"}

initial_config = InitialConfig(spacing="custom",
                               additional_params={
                                   "intensity": intensity,
                                   "enter_speed": v_enter
                               })

scenario = MyTwoWayIntersectionScenario("two-way-intersection",
                                        MyTwoWayIntersectionGenerator,
                                        vehicles,
                                        net_params,
                                        initial_config=initial_config)

env = TwoIntersectionEnvironment(env_params, sumo_params, scenario)

exp = SumoExperiment(env, scenario)

logging.info("Experiment Set Up complete")
Example #27
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)
Example #28
0
def grid_example(render=None):
    """
    Perform a simulation of vehicles on a grid.

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

    Returns
    -------
    exp: flow.core.SumoExperiment type
        A non-rl experiment demonstrating the performance of human-driven
        vehicles and balanced traffic lights on a grid.
    """
    inner_length = 300
    long_length = 500
    short_length = 300
    n = 2
    m = 3
    num_cars_left = 20
    num_cars_right = 20
    num_cars_top = 20
    num_cars_bot = 20
    tot_cars = (num_cars_left + num_cars_right) * m \
        + (num_cars_top + num_cars_bot) * n

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

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

    if render is not None:
        sumo_params.render = render

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

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    tl_logic = TrafficLights(baseline=False)
    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "GGGrrrGGGrrr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yyyrrryyyrrr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rrrGGGrrrGGG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "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)

    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()

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

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Example #29
0
# all other imports are standard
from flow.core.params import VehicleParams
from flow.core.params import NetParams
from flow.core.params import InitialConfig
from flow.core.params import EnvParams
from flow.core.params import SumoParams

from flow.networks import Network

net_params = NetParams(osm_path='map.osm')

# create the remainding parameters
env_params = EnvParams()
sim_params = SumoParams(render=True)
initial_config = InitialConfig()
vehicles = VehicleParams()
vehicles.add('human', num_vehicles=1000)

flow_params = dict(
    exp_tag='bay_bridge',
    env_name=TestEnv,
    network=Network,
    simulator='traci',
    sim=sim_params,
    env=env_params,
    net=net_params,
    veh=vehicles,
    initial=initial_config,
)
Example #30
0
    def __init__(self,
                 name,
                 vehicles,
                 net_params,
                 initial_config=InitialConfig(),
                 traffic_lights=TrafficLightParams(),
                 detector_params=None):
        """Instantiate the base network class.

        Attributes
        ----------
        name : str
            A tag associated with the network
        vehicles : flow.core.params.VehicleParams
            see flow/core/params.py
        net_params : flow.core.params.NetParams
            see flow/core/params.py
        initial_config : flow.core.params.InitialConfig
            see flow/core/params.py
        traffic_lights : flow.core.params.TrafficLightParams
            see flow/core/params.py
        """
        self.orig_name = name  # To avoid repeated concatenation upon reset
        self.name = name + time.strftime('_%Y%m%d-%H%M%S') + str(time.time())

        self.vehicles = vehicles
        self.net_params = net_params
        self.initial_config = initial_config
        self.traffic_lights = traffic_lights
        self.detector_params = detector_params if detector_params else DetectorParams(
        )

        # specify routes vehicles can take
        self.routes = self.specify_routes(net_params)

        if net_params.template is None and net_params.osm_path is None:
            # specify the attributes of the nodes
            self.nodes = self.specify_nodes(net_params)
            # collect the attributes of each edge
            self.edges = self.specify_edges(net_params)
            # specify the types attributes (default is None)
            self.types = self.specify_types(net_params)
            # specify the connection attributes (default is None)
            self.connections = self.specify_connections(net_params)

        # this is to be used if file paths other than the the network geometry
        # file is specified
        elif type(net_params.template) is dict:
            if 'rou' in net_params.template:
                veh, rou = self._vehicle_infos(net_params.template['rou'])

                vtypes = self._vehicle_type(net_params.template.get('vtype'))
                cf = self._get_cf_params(vtypes)
                lc = self._get_lc_params(vtypes)

                # add the vehicle types to the VehicleParams object
                for t in vtypes:
                    vehicles.add(veh_id=t,
                                 length=vtypes[t]['length'],
                                 car_following_params=cf[t],
                                 lane_change_params=lc[t],
                                 num_vehicles=0)

                # add the routes of the vehicles that will be departed later
                # under the name of the vehicle. This will later be identified
                # by k.vehicles._add_departed
                self.routes = rou

                # vehicles to be added with different departure times
                self.template_vehicles = veh

            if 'add' in net_params.template:
                e1Detectors, e2Detectors = self._get_detector_params(
                    net_params.template['add'])
                for e1Detector in e1Detectors:
                    self.detector_params.add_induction_loop_detector(
                        name=e1Detector['id'],
                        lane_id=e1Detector['lane'],
                        position=e1Detector['pos'],
                        frequency=e1Detector['freq'],
                        storage_file=e1Detector['file'],
                    )
                for e2Detector in e2Detectors:
                    self.detector_params.add_lane_area_detector(
                        name=e2Detector['id'],
                        lane_id=e2Detector['lane'],
                        position=e2Detector['pos'],
                        frequency=e2Detector['freq'],
                        storage_file=e2Detector['file'],
                    )

            self.types = None
            self.nodes = None
            self.edges = None
            self.connections = None

        # osm_path or template as type str
        else:
            self.nodes = None
            self.edges = None
            self.types = None
            self.connections = None

        # optional parameters, used to get positions from some global reference
        self.edge_starts = self.specify_edge_starts()
        self.internal_edge_starts = self.specify_internal_edge_starts()
        self.intersection_edge_starts = []  # this will be deprecated