@阿什坎 我看到了您对这个问题的回答(How to write Get_state() return based in multi-agent based on agent-id?)
您提供了一些示例代码: 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
我对您的代码有疑问:
在dist_to_intesec []上,基于green_wave_env.py,它将把所有车辆的距离返回到所有路口,而不是将车辆的距离返回到特殊/单个路口,我不太了解您的dist_to_intersec [ ]在这里,您能解释一下吗?
如何检查get_state()的数据,例如,我想获取dist_to_intersec的数据。
在我的项目中: 基于网格网络,我想获取地平线时间上交叉路口圆上的车辆数量(例如,该圆的半径为100m,交叉路口为中心点)。 因此,您的回复将对我有很大帮助。 @Ashkan
答案 0 :(得分:1)
在那篇文章中,我只是张贴了代码的骨架以更好地理解,这里没有显示完整的代码。
以下是您问题的答案:
稍后在intersection
中使用self.k.traffic_light.get_state(intersection)
来获取交叉路口的交通灯状态。 edges
用作observed_vehicle_ids = self.k_closest_to_intersection_edge(edges, self.num_closest_vehicles_onbound)
函数的输入,以使汽车位于指定的边缘。
dist_to_intersec
是一个变量,将仅存储存储在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
)
要在交叉路口的每个边上获取固定数量的车辆,可以尝试以下操作:
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