コード例 #1
0
args = parser.parse_args()

# create world
world = World(args.config_file, thread_num=args.thread)

# create agents
agents = []
for i in world.intersections:
    action_space = gym.spaces.Discrete(len(i.phases))
    agents.append(
        MaxPressureAgent(
            action_space, i, world,
            LaneVehicleGenerator(world, i, ["lane_count"], in_only=True)))

# create metric
metric = [TravelTimeMetric(world), QueueLengthMetric(world)]
world.subscribe("lane_waiting_time_count")

# create env
env = TSCEnv(world, agents, metric)

# simulate
obs = env.reset()
actions = []
for i in range(args.steps):
    actions = []
    for agent_id, agent in enumerate(agents):
        actions.append(agent.get_action(obs[agent_id]))
    obs, rewards, dones, info = env.step(actions)
    for metric_obj in env.metric:
        metric_obj.update()
コード例 #2
0
    obs = env.reset()
    env.update_metric(met)
    for i in range(args.steps):
        actions = []
        try:
            for agent in agents:
                actions.append(agent.get_action(world))
            obs, rewards, dones, info = env.step(actions)
            for ind_m in range(len(env.metric)):
                env.metric[ind_m].update(done=False)
            if i % 20 == 0:
                print(i, "/", args.steps, "\n")
        except:
            break
    for ind_m in range(len(met_name)):
        print("{} is {:.4f}".format(met_name[ind_m],
                                    env.metric[ind_m].update(done=True)))


metric = [
    TravelTimeMetric(world),
    ThroughputMetric(world),
    FuelMetric(world),
    TotalCostMetric(world)
]
metric_name = [
    "Average Travel Time", "Average throughput", "Average fuel cost",
    "Average total cost"
]
test(metric, metric_name)
コード例 #3
0
def create_colight_env(args, agent="bc_colight"):
    config = json.load(open(args.config_file, "r"))
    road_net_file_path = config["dir"] + config["roadnetFile"]
    res = build_int_intersection_map(road_net_file_path)
    net_node_dict_id2inter = res[0]
    net_node_dict_inter2id = res[1]
    net_edge_dict_id2edge = res[2]
    net_edge_dict_edge2id = res[3]
    node_degree_node = res[4]
    node_degree_edge = res[5]
    node_adjacent_node_matrix = res[6]
    node_adjacent_edge_matrix = res[7]
    edge_adjacent_node_matrix = res[8]

    # create world
    world = World(args.config_file, thread_num=args.thread, silent=True)

    dic_traffic_env_conf = {
        "NUM_INTERSECTIONS": len(net_node_dict_id2inter),  # used
        "NUM_ROADS": len(net_edge_dict_id2edge),  # used
    }

    dic_graph_setting = {
        "NEIGHBOR_NUM": 4,  # standard number of adjacent nodes of each node
        "NEIGHBOR_EDGE_NUM":
        4,  # # standard number of adjacent edges of each node
        "N_LAYERS": 1,  # layers of MPNN
        "INPUT_DIM": [128, 128],
        # input dimension of each layer of multiheadattention, the first value should == the last value of "NODE_EMB_DIM"
        "OUTPUT_DIM": [128, 128],
        # output dimension of each layer of multiheadattention, the first value should == the last value of "NODE_EMB_DIM"
        "NODE_EMB_DIM":
        [128, 128],  # the firsr two layer of dense to embedding the input
        "NUM_HEADS": [5, 5],
        "NODE_LAYER_DIMS_EACH_HEAD": [16, 16],  # [input_dim,output_dim]
        "OUTPUT_LAYERS": [],  #
        "NEIGHBOR_ID":
        node_adjacent_node_matrix,  # adjacent node id of each node
        "ID2INTER_MAPPING":
        net_node_dict_id2inter,  # id ---> intersection mapping
        "INTER2ID_MAPPING":
        net_node_dict_inter2id,  # intersection ----->id mapping
        "NODE_DEGREE_NODE":
        node_degree_node,  # number of adjacent nodes of node
    }
    tmp_agents = []
    observation_generators = []
    for node_dict in world.intersections:
        node_id = node_dict.id
        action_space = gym.spaces.Discrete(len(node_dict.phases))
        node_id_int = net_node_dict_inter2id[node_id]
        tmp_generator = LaneVehicleGenerator(world,
                                             node_dict, ["lane_count"],
                                             in_only=True,
                                             average=None)
        observation_generators.append((node_id_int, tmp_generator))
    sorted(
        observation_generators, key=lambda x: x[0]
    )  # sorted the ob_generator based on its corresponding id_int, increasingly
    # create agent
    action_space = gym.spaces.Discrete(len(world.intersections[0].phases))
    if agent == "bc_colight":
        colightAgent = BCCoLightAgent(action_space, observation_generators,
                                      world, dic_traffic_env_conf,
                                      dic_graph_setting, args)
    elif agent == "colight":
        colightAgent = CoLightAgent(
            action_space, observation_generators,
            LaneVehicleGenerator(world,
                                 world.intersections[0],
                                 ["lane_waiting_count"],
                                 in_only=True,
                                 average="all",
                                 negative=True), world, dic_traffic_env_conf,
            dic_graph_setting, args)
    else:
        colightAgent = None
    # print(colightAgent.ob_length)
    # print(colightAgent.action_space)
    # create metric
    metric = TravelTimeMetric(world)
    agents = [colightAgent]
    # create env
    env = TSCEnv(world, agents, metric)
    return env