Skip to content

Commit 075d131

Browse files
committed
mypy
1 parent 93a0edd commit 075d131

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

PathPlanning/TimeBasedPathPlanning/ConflictBasedSearch.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
ObstacleArrangement,
2121
Position,
2222
)
23+
import time
24+
2325
from PathPlanning.TimeBasedPathPlanning.BaseClasses import MultiAgentPlanner, StartAndGoal
2426
from PathPlanning.TimeBasedPathPlanning.Node import NodePath
2527
from PathPlanning.TimeBasedPathPlanning.BaseClasses import SingleAgentPlanner
2628
from PathPlanning.TimeBasedPathPlanning.SafeInterval import SafeIntervalPathPlanner
2729
from PathPlanning.TimeBasedPathPlanning.SpaceTimeAStar import SpaceTimeAStar
2830
from PathPlanning.TimeBasedPathPlanning.Plotting import PlotNodePaths
29-
from PathPlanning.TimeBasedPathPlanning.ConstraintTree import AgentId, AppliedConstraint, ConstraintTree, ConstraintTreeNode, ForkingConstraint
30-
import time
31+
from PathPlanning.TimeBasedPathPlanning.ConstraintTree import AgentId, AppliedConstraint, ConstrainedAgent, ConstraintTree, ConstraintTreeNode, ForkingConstraint
3132

3233
class ConflictBasedSearch(MultiAgentPlanner):
3334

@@ -56,13 +57,16 @@ def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_c
5657

5758
while constraint_tree.nodes_to_expand:
5859
constraint_tree_node = constraint_tree.get_next_node_to_expand()
60+
if constraint_tree_node is None:
61+
raise RuntimeError("No more nodes to expand in the constraint tree.")
62+
5963
ancestor_constraints = constraint_tree.get_ancestor_constraints(constraint_tree_node.parent_idx)
6064

6165
if verbose:
6266
print(f"Expanding node with constraint {constraint_tree_node.constraint} and parent {constraint_tree_node.parent_idx}")
6367
print(f"\tCOST: {constraint_tree_node.cost}")
6468

65-
if constraint_tree_node is None:
69+
if constraint_tree_node.constraint is None:
6670
raise RuntimeError("No more nodes to expand in the constraint tree.")
6771
if not constraint_tree_node.constraint:
6872
# This means we found a solution!
@@ -83,7 +87,16 @@ def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_c
8387
all_constraints = ancestor_constraints
8488
all_constraints.append(applied_constraint)
8589

86-
new_path = ConflictBasedSearch.plan_for_agent(constrained_agent, all_constraints, constraint_tree, attempted_constraint_combos, grid, single_agent_planner_class, start_and_goals, verbose)
90+
new_path = ConflictBasedSearch.plan_for_agent(
91+
constrained_agent,
92+
all_constraints,
93+
constraint_tree,
94+
attempted_constraint_combos,
95+
grid,
96+
single_agent_planner_class,
97+
start_and_goals,
98+
verbose
99+
)
87100
if not new_path:
88101
continue
89102

@@ -116,14 +129,14 @@ def get_agents_start_and_goal(start_and_goal_list: list[StartAndGoal], target_in
116129
raise RuntimeError(f"Could not find agent with index {target_index} in {start_and_goal_list}")
117130

118131
@staticmethod
119-
def plan_for_agent(constrained_agent: ConstraintTreeNode,
132+
def plan_for_agent(constrained_agent: ConstrainedAgent,
120133
all_constraints: list[AppliedConstraint],
121134
constraint_tree: ConstraintTree,
122135
attempted_constraint_combos: set,
123136
grid: Grid,
124137
single_agent_planner_class: SingleAgentPlanner,
125138
start_and_goals: list[StartAndGoal],
126-
verbose: False) -> tuple[list[StartAndGoal], list[NodePath]] | None:
139+
verbose: bool) -> NodePath | None:
127140
"""
128141
Attempt to generate a path plan for a single agent
129142
"""

PathPlanning/TimeBasedPathPlanning/ConstraintTree.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def check_for_constraints_at_time(self, positions_at_time: dict[PositionAtTime,
9898
if t == 0:
9999
continue
100100
last_position = path.get_position(t - 1)
101-
if not position:
101+
if not last_position:
102102
raise RuntimeError(f"Failed to get position for agent {agent_id} at time {t-1}")
103103
new_position_at_last_time = PositionAtTime(position, t-1)
104104
old_position_at_new_time = PositionAtTime(last_position, t)
@@ -123,10 +123,11 @@ class ConstraintTree:
123123
# Child nodes have been created (Maps node_index to ConstraintTreeNode)
124124
expanded_nodes: dict[int, ConstraintTreeNode]
125125
# Need to solve and generate children
126-
nodes_to_expand = list[ConstraintTreeNode]
126+
nodes_to_expand: list[ConstraintTreeNode]
127127

128128
def __init__(self, initial_solution: dict[AgentId, NodePath]):
129129
self.expanded_nodes = {}
130+
self.nodes_to_expand = []
130131
heapq.heappush(self.nodes_to_expand, ConstraintTreeNode(initial_solution, -1, []))
131132

132133
def get_next_node_to_expand(self) -> ConstraintTreeNode | None:
@@ -137,7 +138,7 @@ def get_next_node_to_expand(self) -> ConstraintTreeNode | None:
137138
"""
138139
Add a node to the tree and generate children if needed. Returns true if the node is a solution, false otherwise.
139140
"""
140-
def add_node_to_tree(self, node: ConstraintTreeNode) -> bool:
141+
def add_node_to_tree(self, node: ConstraintTreeNode):
141142
heapq.heappush(self.nodes_to_expand, node)
142143

143144
"""

0 commit comments

Comments
 (0)