Agents and spatial¶
Graphs, shortest-path algorithms, and an Agent that traverses them.
Graphs¶
grid_graph returns a simweave.spatial.Graph whose nodes are
(row, col) tuples. Bring your own graph by either constructing
Graph directly or using a NetworkX graph with the [graph] extra.
Path planners¶
a_star(graph, start, goal, heuristic)dijkstra(graph, start, goal)- Heuristics:
manhattan,euclidean,chebyshev - Errors:
NoPathErrorif no route exists
Agents¶
agent = sw.Agent(
graph=g,
start_node=(0, 0),
tasks=[(7, 11), (3, 4), (0, 11)],
speed=2.0,
heuristic=sw.manhattan,
name="rover",
)
env = sw.SimEnvironment(dt=0.5, end=50.0)
env.register(agent)
env.run()
sw.plot_agent_path(agent, graph=g).write_html("path.html",
include_plotlyjs="cdn")
Compass is a small enum for cardinal-direction logic if you need it
when authoring custom agent behaviours.
API: simweave.agents¶
Agents, routing, and orientation primitives.
Compass
¶
Discrete compass quantised to a fixed number of points.
Source code in src/simweave/agents/compass.py
NoPathError
¶
Bases: ValueError
Raised when A* cannot reach the goal from the start node.
Agent
¶
Agent(graph: Any, start_node: Node, tasks: Sequence[Node] | None = None, speed: float = 1.0, heuristic: Callable[[Node, Node], float] | None = None, on_arrive: Callable[['Agent', Node, Any], None] | None = None, weight_attr: str = 'weight', name: str | None = None)
Bases: Entity
Graph-routing agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Any
|
A simweave Graph, networkx graph, or dict-of-dict adjacency. |
required |
start_node
|
Node
|
Node at which the agent spawns. |
required |
tasks
|
Sequence[Node] | None
|
Ordered list of target nodes to visit. |
None
|
speed
|
float
|
Distance per unit time. |
1.0
|
heuristic
|
Callable[[Node, Node], float] | None
|
Optional heuristic for A. If None, A degenerates to Dijkstra. |
None
|
on_arrive
|
Callable[['Agent', Node, Any], None] | None
|
Optional callback |
None
|
name
|
str | None
|
Optional agent name. |
None
|
Source code in src/simweave/agents/agent.py
plan_to
¶
Replan to target from the current position using A*.
Source code in src/simweave/agents/agent.py
a_star
¶
a_star(graph: Any, start: Node, goal: Node, heuristic: Heuristic | None = None, weight_attr: str = 'weight') -> list[Node]
Compute shortest path from start to goal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Any
|
Any object understood by :func: |
required |
heuristic
|
Heuristic | None
|
Admissible heuristic |
None
|
weight_attr
|
str
|
Edge attribute to treat as weight. Defaults to |
'weight'
|
Returns:
| Type | Description |
|---|---|
list[Node]
|
Path from start to goal inclusive. |
Source code in src/simweave/agents/routing.py
dijkstra
¶
Convenience wrapper -- A* with zero heuristic.
API: simweave.spatial¶
Graphs and spatial primitives.
Graph
¶
adj_view
¶
Return {neighbor: edge_data_dict} for either a simweave Graph or a networkx graph.
Source code in src/simweave/spatial/graph.py
grid_graph
¶
grid_graph(nrows: int, ncols: int, *, diagonal: bool = False, weight: float = 1.0, diagonal_weight: float | None = None) -> Graph
Build a 2D lattice graph with nodes (r, c).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diagonal
|
bool
|
If True, add 45-degree edges (cost defaults to |
False
|