Skip to content

Commit bd5db9d

Browse files
committed
some speedup + bugfix
1 parent e73ab12 commit bd5db9d

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

PathPlanning/TimeBasedPathPlanning/ConflictBasedSearch.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_c
6666

6767
# TODO: contents of this loop should probably be in a helper?
6868
for constrained_agent in constraint_tree_node.constraint.constrained_agents:
69-
paths: dict[AgentId, NodePath] = {}
70-
7169
if verbose:
7270
print(f"\nOuter loop step for agent {constrained_agent}")
7371

@@ -90,37 +88,31 @@ def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_c
9088
grid.clear_constraint_points()
9189
grid.apply_constraint_points(all_constraints)
9290

93-
failed_to_plan = False
94-
for agent_idx, start_and_goal in enumerate(start_and_goals):
95-
# print("planning for:", agent_idx)
96-
try:
97-
path = single_agent_planner_class.plan(grid, start_and_goal.start, start_and_goal.goal, agent_idx, verbose)
98-
except Exception as e:
99-
print(f"Failed to plan with constraints: {all_constraints}")
100-
failed_to_plan = True
101-
102-
if path is None:
103-
raise RuntimeError(f"Failed to find path for {start_and_goal}")
104-
paths[AgentId(start_and_goal.index)] = path
105-
106-
if failed_to_plan:
107-
print(f"Failed to plan with constraints: {all_constraints}")
91+
# Just plan for agent with new constraint
92+
start_and_goal = start_and_goals[constrained_agent.agent]
93+
try:
94+
new_path = single_agent_planner_class.plan(grid, start_and_goal.start, start_and_goal.goal, start_and_goal.index, verbose)
95+
except Exception as e:
10896
continue
10997

98+
applied_constraint_parent = deepcopy(constraint_tree_node) #TODO: not sure if deepcopy is actually needed
99+
paths: dict[AgentId, NodePath] = deepcopy(applied_constraint_parent.paths) # TODO: not sure if deepcopy is actually needed
100+
paths[constrained_agent.agent] = new_path
101+
110102
# for (agent_idx, path) in paths.items():
111103
# print(f"\nAgent {agent_idx} path:\n {path}")
112104

113-
applied_constraint_parent = deepcopy(constraint_tree_node) #TODO: not sure if deepcopy is actually needed
114105
applied_constraint_parent.constraint = applied_constraint
115106
parent_idx = constraint_tree.add_expanded_node(applied_constraint_parent)
116107

117-
new_constraint_tree_node = ConstraintTreeNode(paths, parent_idx)
108+
new_constraint_tree_node = ConstraintTreeNode(deepcopy(paths), parent_idx, all_constraints)
118109
if new_constraint_tree_node.constraint is None:
119110
# This means we found a solution!
120111
print("Found a path with constraints:")
121112
for constraint in all_constraints:
122113
print(f"\t{constraint}")
123-
return (start_and_goals, [paths[AgentId(i)] for i in range(len(start_and_goals))])
114+
# return (start_and_goals, [paths[AgentId(i)] for i in range(len(start_and_goals))])
115+
return (start_and_goals, paths.values())
124116

125117
# if verbose:
126118
print(f"Adding new constraint tree node with constraint: {new_constraint_tree_node.constraint}")
@@ -140,13 +132,13 @@ def main():
140132

141133
# TODO: bug somewhere where it expects agent ids to match indices
142134
# start_and_goals = [StartAndGoal(i, Position(1, i), Position(19, 19-i)) for i in range(1, 12)]
143-
# start_and_goals = [StartAndGoal(i, Position(1, 8+i), Position(19, 19-i)) for i in range(6)]
135+
start_and_goals = [StartAndGoal(i, Position(1, 8+i), Position(19, 19-i)) for i in range(6)]
144136
# start_and_goals = [StartAndGoal(i, Position(1, 2*i), Position(19, 19-i)) for i in range(4)]
145137

146138
# hallway cross
147-
start_and_goals = [StartAndGoal(0, Position(6, 10), Position(13, 10)),
148-
StartAndGoal(1, Position(12, 10), Position(7, 10)),
149-
StartAndGoal(2, Position(11, 10), Position(6, 10))]
139+
# start_and_goals = [StartAndGoal(0, Position(6, 10), Position(13, 10)),
140+
# StartAndGoal(1, Position(13, 10), Position(7, 10)),
141+
# StartAndGoal(2, Position(11, 10), Position(6, 10))]
150142

151143
# temporary obstacle
152144
# start_and_goals = [StartAndGoal(0, Position(15, 14), Position(15, 16))]
@@ -160,8 +152,8 @@ def main():
160152
num_obstacles=250,
161153
obstacle_avoid_points=obstacle_avoid_points,
162154
# obstacle_arrangement=ObstacleArrangement.TEMPORARY_OBSTACLE,
163-
obstacle_arrangement=ObstacleArrangement.HALLWAY,
164-
# obstacle_arrangement=ObstacleArrangement.NARROW_CORRIDOR,
155+
# obstacle_arrangement=ObstacleArrangement.HALLWAY,
156+
obstacle_arrangement=ObstacleArrangement.NARROW_CORRIDOR,
165157
# obstacle_arrangement=ObstacleArrangement.ARRANGEMENT1,
166158
# obstacle_arrangement=ObstacleArrangement.RANDOM,
167159
)

PathPlanning/TimeBasedPathPlanning/ConstraintTree.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ class ConstraintTreeNode:
3333
paths: dict[AgentId, NodePath]
3434
cost: int
3535

36-
def __init__(self, paths: dict[AgentId, NodePath], parent_idx: int):
36+
def __init__(self, paths: dict[AgentId, NodePath], parent_idx: int, all_constraints: list[AppliedConstraint]):
3737
self.paths = paths
3838
self.cost = sum(path.goal_reached_time() for path in paths.values())
3939
self.parent_idx = parent_idx
4040
self.constraint = self.get_constraint_point()
41+
self.all_constraints = all_constraints
4142

4243
def __lt__(self, other) -> bool:
44+
if self.cost == other.cost:
45+
return len(self.all_constraints) < len(other.all_constraints)
4346
return self.cost < other.cost
4447

4548
def get_constraint_point(self, verbose = False) -> Optional[ForkingConstraint]:
@@ -88,8 +91,6 @@ def get_constraint_point(self, verbose = False) -> Optional[ForkingConstraint]:
8891
))
8992
print(f"new constraint: {new_constraint}")
9093
return new_constraint
91-
else:
92-
positions_at_time[new_position_at_last_time] = AgentId(agent_id)
9394
return None
9495

9596

@@ -102,7 +103,7 @@ class ConstraintTree:
102103
def __init__(self, initial_solution: dict[AgentId, NodePath]):
103104
self.nodes_to_expand = []
104105
self.expanded_nodes = {}
105-
heapq.heappush(self.nodes_to_expand, ConstraintTreeNode(initial_solution, -1))
106+
heapq.heappush(self.nodes_to_expand, ConstraintTreeNode(initial_solution, -1, []))
106107

107108
def get_next_node_to_expand(self) -> Optional[ConstraintTreeNode]:
108109
if not self.nodes_to_expand:

0 commit comments

Comments
 (0)