0

@Ashkan I saw your answer on that problem (How to write Get_state() return based in multi-agent based on agent-id?)

You gave some example codes: def get_state(self):

    agent_state_dict = {}
    i = 0
    for intersection, edges in self.scenario.get_node_mapping():
        i = i + 1
        agent_id = self.agent_name_prefix + str(i) # self.agent_name_prefix is defined as string "intersection"

        speeds = []
        dist_to_intersec = []
        traffic_light_states = []

        ..... code .....

        # construct the state (observation) for each agent
        observation = np.array(
            np.concatenate([
                speeds, dist_to_intersec, traffic_light_states  

        # each intersection is an agent, so we will make a dictionary that maps form "self.agent_name_prefix+'i'" to the state of that agent.
        agent_state_dict.update({agent_id: observation})

    return agent_state_dict

I have some question about your code:

  1. on the 'for' loop, you use intersection and edges one time, and there is no other usage of intersection and edges, what the function of intersection and edges at here?
  2. On the dist_to_intesec[], based on green_wave_env.py, it will returns all vehicles' distance to all intersections, not return the vehicles' distance to a special/individual intersection, I do not very understand your dist_to_intersec[] at here, can you explain it?

  3. How to check the data of get_state(), for example, I want to get the data of dist_to_intersec.

On my project: Based grid network, I want to get the number of how much vehicles is on the circle of an intersection(for example, the radius of that circle is 100m, the intersection is the center point) on a horizon time. So your reply will help me a lot. @Ashkan

xiaoToby
  • 1
  • 2
  • the call to self.scenario.get_node_mapping()returns a dict of nodes to the edges that head toward it – xiaoToby Aug 14 '19 at 15:28

1 Answers1

1

In that post, I had just posted a skeleton of the code in for better understanding, and the full code is not show there.

Here are the answers to your question:

  1. The intersection is used later in self.k.traffic_light.get_state(intersection) to get the state of the traffic lights in the intersection. The edges is used as an input to observed_vehicle_ids = self.k_closest_to_intersection_edge(edges, self.num_closest_vehicles_onbound) function to get the cars on specified edges.

  2. dist_to_intersec is a variable that simply will store the distances of the observed vehicles (not all vehicles' distance to all intersections) that are stored in observed_vehicle_ids:

for veh_id in observed_vehicle_ids:
    if veh_id == 0:
        dist_to_intersec.append(-1)
        speeds.append(-1)
    else:
        dist_to_intersec.append(
            (self.k.scenario.edge_length(
                self.k.vehicle.get_edge(veh_id))
                - self.k.vehicle.get_position(veh_id)) / max_dist
        )
        speeds.append(
            self.k.vehicle.get_speed(veh_id) / max_speed
        )
  1. You can print the state.

To get the fixed number of vehicles on each edge of an intersection, you can try this:

def k_closest_to_intersection_edge(self, edges, k):
    """
    Return the veh_id of the 4*k closest vehicles to an intersection for
    each edge (k closest vehicles on each edge). 
    """
    if k < 0:
        raise IndexError("k must be greater than 0")
    ids = []

    def sort_lambda(veh_id):
        return self.get_distance_to_intersection(veh_id)

    for edge in edges:
        vehicles = self.k.vehicle.get_ids_by_edge(edge)
        veh_ids_per_bound = sorted(
            vehicles,
            key=sort_lambda
        )
        if len(veh_ids_per_bound) >= k: # we have more than k vehicles, and we need to cut
            ids += veh_ids_per_bound[:k]
        else: # we have less than k vehicles, and we need to pad
            padding = k - len(veh_ids_per_bound)
            ids += (veh_ids_per_bound + [0]*padding)

    return ids
Ashkan
  • 238
  • 1
  • 10
  • For the first answer: I am confused with your functions: self.k.traffic_light.get_state(intersection) and num_closest_vehicles_onbound.Because I didn't see any code about these functions on Flow's code files. So these functions are created by yourself, if yes, I think maybe you can supply the full code of these function will be helpful – xiaoToby Aug 15 '19 at 09:20
  • For the third function, I am sure that the state can be printed, but my problem is how to print it, for example, On the get_state() function of green_wave_env.py , I wrote print(dist_to_intersec), when I ran green_wave.py, I cannot see any data of the print function. But it cannot work@Ashkan Hope your reply, it will help me a lot – xiaoToby Aug 15 '19 at 09:26
  • And also about my project vision(I wrote at the last paragraph),the object is the traffic light,not the vehicle. By my understanding, now all the above information the object is vehicle,right?(because the num_observed_id[] are vehicles' ids), if I want to monitor how many vehicles are on a circle of an intersection(for example, radius is 100m), I should add all the vehicles into the num_observed_id[], it is will be a very troublem procedure of that, so my understanding is right? And If i set the observed object is the intersection, and collect vehicle number on a horizon,it is feasible? – xiaoToby Aug 15 '19 at 11:29
  • Now, I want to collect the vehicle number of an intersection on a horizon time, I made some changes of k_closest_to_intersection(self,edges,k) function: veh_ids_ordered = sorted(self.k.vehicle.get_ids_by_edge(edges), key = lambda k: for id in veh_id: get_distance_to_intersection(id)< k). At here, the k value is the radius of an intersection(for example: 100 meters ) and finally return veh_ids_ordered[]. it is correct? I hope you reply with it, it will help me a lot, thanks. – xiaoToby Aug 15 '19 at 16:00
  • ,hi, could you answer my questions?@Ashkan – xiaoToby Aug 16 '19 at 04:51
  • @xiaoToby 1- `num_closest_vehicles_onbound` is simply a constant. I define it to be 3. The function `self.k.traffic_light.get_state` is also defined in Flow. Feel free to use it. 3- I can easily print the `print(dist_to_intersec)` and it prints the distance of the 3 closes vehicles to an intersection (so total of 12). For instance, `[-1, -1, -1, 0.049377359690088836, 0.10316493580094903, 0.15412161437066102, -1, -1, -1, 0.049377359690088836, 0.10316493580094903, 0.15412161437066102]` – Ashkan Aug 18 '19 at 00:12
  • I am not sure about what you exactly mean. But, you are free to define your problem as how ever you like. about your function that you defined, I tried to understand it, and it seems correct to me! I was not able to verify it fully, since you used lambda function and wrote the whole thing in one line; but, your logic is interesting. You can test small and see if it works. – Ashkan Aug 18 '19 at 00:26