Skip to content

Commit c89f47f

Browse files
committed
add unit test
1 parent ede80e4 commit c89f47f

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

PathPlanning/TimeBasedPathPlanning/PriorityBasedPlanner.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222

2323
class PriorityBasedPlanner(MultiAgentPlanner):
2424

25-
def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_class: SingleAgentPlanner, verbose: bool) -> list[NodePath]:
26-
25+
def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_class: SingleAgentPlanner, verbose: bool) -> tuple[list[StartAndGoal], list[NodePath]]:
26+
"""
27+
Generate a path from the start to the goal for each agent in the `start_and_goals` list.
28+
Returns the re-ordered StartAndGoal combinations, and a list of path plans. The order of the plans
29+
corresponds to the order of the `start_and_goals` list.
30+
"""
2731
print(f"Using planner: {single_agent_planner_class}")
2832

2933
# Reserve initial positions
@@ -51,7 +55,7 @@ def plan(grid: Grid, start_and_goals: list[StartAndGoal], single_agent_planner_c
5155
grid.reserve_path(path, agent_index)
5256
paths.append(path)
5357

54-
return paths
58+
return (start_and_goals, paths)
5559

5660
verbose = False
5761
show_animation = True
@@ -71,7 +75,9 @@ def main():
7175
)
7276

7377
start_time = time.time()
74-
paths: list[NodePath] = PriorityBasedPlanner.plan(grid, start_and_goals, SafeIntervalPathPlanner, verbose)
78+
start_and_goals: list[StartAndGoal]
79+
paths: list[NodePath]
80+
start_and_goals, paths = PriorityBasedPlanner.plan(grid, start_and_goals, SafeIntervalPathPlanner, verbose)
7581

7682
runtime = time.time() - start_time
7783
print(f"\nPlanning took: {runtime:.5f} seconds")

tests/test_priority_based_planner.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from PathPlanning.TimeBasedPathPlanning.GridWithDynamicObstacles import (
2+
Grid,
3+
NodePath,
4+
ObstacleArrangement,
5+
Position,
6+
)
7+
from PathPlanning.TimeBasedPathPlanning.BaseClasses import StartAndGoal
8+
from PathPlanning.TimeBasedPathPlanning import PriorityBasedPlanner as m
9+
from PathPlanning.TimeBasedPathPlanning.SafeInterval import SafeIntervalPathPlanner
10+
import numpy as np
11+
import conftest
12+
13+
14+
def test_1():
15+
grid_side_length = 21
16+
17+
start_and_goals = [StartAndGoal(i, Position(1, i), Position(19, 19-i)) for i in range(1, 16)]
18+
obstacle_avoid_points = [pos for item in start_and_goals for pos in (item.start, item.goal)]
19+
20+
grid = Grid(
21+
np.array([grid_side_length, grid_side_length]),
22+
num_obstacles=250,
23+
obstacle_avoid_points=obstacle_avoid_points,
24+
obstacle_arrangement=ObstacleArrangement.ARRANGEMENT1,
25+
)
26+
27+
grid_side_length = 21
28+
grid = Grid(
29+
np.array([grid_side_length, grid_side_length]),
30+
obstacle_arrangement=ObstacleArrangement.ARRANGEMENT1,
31+
)
32+
33+
m.show_animation = False
34+
35+
start_and_goals: list[StartAndGoal]
36+
paths: list[NodePath]
37+
start_and_goals, paths = m.PriorityBasedPlanner.plan(grid, start_and_goals, SafeIntervalPathPlanner, False)
38+
39+
# All paths should start at the specified position and reach the goal
40+
for i, start_and_goal in enumerate(start_and_goals):
41+
assert paths[i].path[0].position == start_and_goal.start
42+
assert paths[i].path[-1].position == start_and_goal.goal
43+
44+
if __name__ == "__main__":
45+
conftest.run_this_test(__file__)

0 commit comments

Comments
 (0)