使用 Mesa 和 Networkx 可视化代理
Visualizing agents using Mesa & Networkx
我目前正在使用 Mesa 和 Networkx 进行多代理寻路。节点表示在一个时间点只能驻留 1 个代理的位置。边表示节点之间的距离。我如何可视化代理在每个时间步沿边缘的移动?例如在时间步长 = 4 时,代理 A 位于连接节点 1 和 2 的边的中间。
我猜您想使用 networkx
和 matplotlib
绘制一些在节点之间遍历的代理。
import matplotlib.pyplot as plt
import networkx as nx
import matplotlib.animation as animation
import matplotlib
import numpy as np
matplotlib.use('TkAgg')
plt.ion()
H = nx.octahedral_graph() # generate a random graph
pos = nx.spring_layout(H, iterations=200) # find good positions for nodes
为了做到这一点,首先我们需要知道代理在遍历时的每个步骤或帧中的位置。如果我们假设每个节点之间(或每条边上)有 50 步,我们可以编写一个生成器来更新代理在每一帧中的位置:
def traverse(graph, start, end, steps_between_nodes=50):
"""Generate the new position of the agent.
:param graph: the graph you want to put your agent to traverse on.
:param start: the node to start from.
:param end: the node to end at.
:param steps_between_nodes: number of steps on each edge.
"""
steps = np.linspace(0, 1, steps_between_nodes)
# find the best path from start to end
path = nx.shortest_path(graph, source=start, target=end)
stops = np.empty((0, 2))
for i, j in zip(path[1:], path):
# get the position of the agent at each step
new_stops = steps[..., None] * pos[i] + (1 - steps[..., None]) * pos[j]
stops = np.vstack((stops, new_stops))
for s in stops:
yield s
然后我们可以如下动画:
agent_pos = traverse(H, 1, 4) # make an agent traversing from 1 to 4
def update_position(n):
plt.cla()
nx.draw(H, pos, node_size=700, with_labels=True, node_color='green')
c = plt.Circle(next(agent_pos), 0.05, color='purple', zorder=2, alpha=0.7)
plt.gca().add_patch(c)
ani = animation.FuncAnimation(plt.gcf(), update_position, interval=30, repeat=False)
plt.ioff()
plt.show()
最后我们会有这样的东西:
我目前正在使用 Mesa 和 Networkx 进行多代理寻路。节点表示在一个时间点只能驻留 1 个代理的位置。边表示节点之间的距离。我如何可视化代理在每个时间步沿边缘的移动?例如在时间步长 = 4 时,代理 A 位于连接节点 1 和 2 的边的中间。
我猜您想使用 networkx
和 matplotlib
绘制一些在节点之间遍历的代理。
import matplotlib.pyplot as plt
import networkx as nx
import matplotlib.animation as animation
import matplotlib
import numpy as np
matplotlib.use('TkAgg')
plt.ion()
H = nx.octahedral_graph() # generate a random graph
pos = nx.spring_layout(H, iterations=200) # find good positions for nodes
为了做到这一点,首先我们需要知道代理在遍历时的每个步骤或帧中的位置。如果我们假设每个节点之间(或每条边上)有 50 步,我们可以编写一个生成器来更新代理在每一帧中的位置:
def traverse(graph, start, end, steps_between_nodes=50):
"""Generate the new position of the agent.
:param graph: the graph you want to put your agent to traverse on.
:param start: the node to start from.
:param end: the node to end at.
:param steps_between_nodes: number of steps on each edge.
"""
steps = np.linspace(0, 1, steps_between_nodes)
# find the best path from start to end
path = nx.shortest_path(graph, source=start, target=end)
stops = np.empty((0, 2))
for i, j in zip(path[1:], path):
# get the position of the agent at each step
new_stops = steps[..., None] * pos[i] + (1 - steps[..., None]) * pos[j]
stops = np.vstack((stops, new_stops))
for s in stops:
yield s
然后我们可以如下动画:
agent_pos = traverse(H, 1, 4) # make an agent traversing from 1 to 4
def update_position(n):
plt.cla()
nx.draw(H, pos, node_size=700, with_labels=True, node_color='green')
c = plt.Circle(next(agent_pos), 0.05, color='purple', zorder=2, alpha=0.7)
plt.gca().add_patch(c)
ani = animation.FuncAnimation(plt.gcf(), update_position, interval=30, repeat=False)
plt.ioff()
plt.show()
最后我们会有这样的东西: