88import matplotlib .pyplot as plt
99import numpy as np
1010
11- def test_astar_planning ():
12- heuristic = lambda x ,y : np .linalg .norm (np .array (x )- np .array (y ))
13- test_configs = ((0 ,0 ,0 ), (5 ,5 ,0 )) #start and goal configurations
11+ def plot_path (start_config , goal_config , path = None ):
12+ """Helper function to visualize the path"""
13+ plt .figure (figsize = (8 , 8 ))
14+
15+ # Plot start and goal
16+ plt .plot (start_config [0 ], start_config [1 ], 'go' , markersize = 10 , label = 'Start' )
17+ plt .plot (goal_config [0 ], goal_config [1 ], 'ro' , markersize = 10 , label = 'Goal' )
18+
19+ # Plot path if available
20+ if path :
21+ path = np .array (path )
22+ plt .plot (path [:, 0 ], path [:, 1 ], 'b.-' , label = 'Path' )
23+
24+ plt .grid (True )
25+ plt .axis ('equal' )
26+ plt .legend ()
27+ plt .title ('A* Path Planning Test' )
28+ plt .xlabel ('X (meters)' )
29+ plt .ylabel ('Y (meters)' )
30+ plt .show ()
1431
15- planner = Astar (None , None , heuristic )
32+ def euclidean_heuristic (current , goal ):
33+ """Simple Euclidean distance heuristic"""
34+ return np .linalg .norm (np .array (current ) - np .array (goal ))
1635
17- test_traj = planner (* test_configs )
18- print (test_traj )
36+ def test_straight_line ():
37+ """Test 1: Simple straight line path"""
38+ print ("\n Test 1: Straight Line Path" )
39+ start = (0 , 0 , 0 )
40+ goal = (5 , 0 , 0 )
41+
42+ planner = Astar (start , goal , euclidean_heuristic )
43+ path = planner (start , goal )
44+
45+ assert path is not False , "Planner failed to find path"
46+ assert len (path ) > 0 , "Path is empty"
47+
48+ print ("Path found:" , path )
49+ plot_path (start , goal , path )
50+ return path
1951
52+ def test_diagonal_path ():
53+ """Test 2: Diagonal path"""
54+ print ("\n Test 2: Diagonal Path" )
55+ start = (0 , 0 , 0 )
56+ goal = (3 , 3 , 0 )
57+
58+ planner = Astar (start , goal , euclidean_heuristic )
59+ path = planner (start , goal )
60+
61+ assert path is not False , "Planner failed to find path"
62+ assert len (path ) > 0 , "Path is empty"
63+
64+ print ("Path found:" , path )
65+ plot_path (start , goal , path )
66+ return path
67+
68+ def test_path_length ():
69+ """Test 3: Verify path length is reasonable"""
70+ print ("\n Test 3: Path Length Test" )
71+ start = (0 , 0 , 0 )
72+ goal = (2 , 2 , 0 )
73+
74+ planner = Astar (start , goal , euclidean_heuristic )
75+ path = planner (start , goal )
76+
77+ # Check if path exists
78+ assert path is not False , "Planner failed to find path"
79+
80+ # Check if path length is reasonable
81+ path_length = 0
82+ for i in range (len (path )- 1 ):
83+ path_length += euclidean_heuristic (path [i ], path [i + 1 ])
84+
85+ # Path length should be close to the direct distance
86+ direct_distance = euclidean_heuristic (start , goal )
87+ assert path_length <= direct_distance * 1.5 , "Path is too long"
88+
89+ print (f"Path length: { path_length :.2f} " )
90+ print (f"Direct distance: { direct_distance :.2f} " )
91+ plot_path (start , goal , path )
92+ return path
93+
94+ def test_start_goal_same ():
95+ """Test 4: Start and goal at same position"""
96+ print ("\n Test 4: Start and Goal Same Position" )
97+ start = (1 , 1 , 0 )
98+ goal = (1 , 1 , 0 )
99+
100+ planner = Astar (start , goal , euclidean_heuristic )
101+ path = planner (start , goal )
102+
103+ assert path is not False , "Planner failed to find path"
104+ assert len (path ) > 0 , "Path is empty"
105+ assert path [0 ] == start and path [- 1 ] == goal , "Path should contain at least start/goal"
106+
107+ print ("Path found:" , path )
108+ plot_path (start , goal , path )
109+ return path
110+
111+ def run_all_tests ():
112+ """Run all test cases"""
113+ try :
114+ test_straight_line ()
115+ test_diagonal_path ()
116+ test_path_length ()
117+ test_start_goal_same ()
118+ print ("\n All tests passed successfully!" )
119+ except AssertionError as e :
120+ print (f"\n Test failed: { str (e )} " )
121+ except Exception as e :
122+ print (f"\n Unexpected error: { str (e )} " )
20123
21124if __name__ == '__main__' :
22- test_astar_planning ()
125+ run_all_tests ()
0 commit comments