@@ -78,43 +78,43 @@ def boundary_and_obstacles(start, goal, top_vertex, bottom_vertex, obs_number):
7878
7979
8080def find_neighbor (node , ob , closed ):
81- # generate neighbors in certain condition
82- ob_list = ob .tolist ()
83- neighbor : list = []
81+ # Convert obstacles to a set for faster lookup
82+ ob_set = set (map (tuple , ob .tolist ()))
83+ neighbor_set = set ()
84+
85+ # Generate neighbors within the 3x3 grid around the node
8486 for x in range (node .coordinate [0 ] - 1 , node .coordinate [0 ] + 2 ):
8587 for y in range (node .coordinate [1 ] - 1 , node .coordinate [1 ] + 2 ):
86- if [x , y ] not in ob_list :
87- # find all possible neighbor nodes
88- neighbor .append ([x , y ])
89- # remove node violate the motion rule
90- # 1. remove node.coordinate itself
91- neighbor .remove (node .coordinate )
92- # 2. remove neighbor nodes who cross through two diagonal
93- # positioned obstacles since there is no enough space for
94- # robot to go through two diagonal positioned obstacles
95-
96- # top bottom left right neighbors of node
97- top_nei = [node .coordinate [0 ], node .coordinate [1 ] + 1 ]
98- bottom_nei = [node .coordinate [0 ], node .coordinate [1 ] - 1 ]
99- left_nei = [node .coordinate [0 ] - 1 , node .coordinate [1 ]]
100- right_nei = [node .coordinate [0 ] + 1 , node .coordinate [1 ]]
101- # neighbors in four vertex
102- lt_nei = [node .coordinate [0 ] - 1 , node .coordinate [1 ] + 1 ]
103- rt_nei = [node .coordinate [0 ] + 1 , node .coordinate [1 ] + 1 ]
104- lb_nei = [node .coordinate [0 ] - 1 , node .coordinate [1 ] - 1 ]
105- rb_nei = [node .coordinate [0 ] + 1 , node .coordinate [1 ] - 1 ]
106-
107- # remove the unnecessary neighbors
108- if top_nei and left_nei in ob_list and lt_nei in neighbor :
109- neighbor .remove (lt_nei )
110- if top_nei and right_nei in ob_list and rt_nei in neighbor :
111- neighbor .remove (rt_nei )
112- if bottom_nei and left_nei in ob_list and lb_nei in neighbor :
113- neighbor .remove (lb_nei )
114- if bottom_nei and right_nei in ob_list and rb_nei in neighbor :
115- neighbor .remove (rb_nei )
116- neighbor = [x for x in neighbor if x not in closed ]
117- return neighbor
88+ coord = (x , y )
89+ if coord not in ob_set and coord != tuple (node .coordinate ):
90+ neighbor_set .add (coord )
91+
92+ # Define neighbors in cardinal and diagonal directions
93+ top_nei = (node .coordinate [0 ], node .coordinate [1 ] + 1 )
94+ bottom_nei = (node .coordinate [0 ], node .coordinate [1 ] - 1 )
95+ left_nei = (node .coordinate [0 ] - 1 , node .coordinate [1 ])
96+ right_nei = (node .coordinate [0 ] + 1 , node .coordinate [1 ])
97+ lt_nei = (node .coordinate [0 ] - 1 , node .coordinate [1 ] + 1 )
98+ rt_nei = (node .coordinate [0 ] + 1 , node .coordinate [1 ] + 1 )
99+ lb_nei = (node .coordinate [0 ] - 1 , node .coordinate [1 ] - 1 )
100+ rb_nei = (node .coordinate [0 ] + 1 , node .coordinate [1 ] - 1 )
101+
102+ # Remove neighbors that violate diagonal motion rules
103+ if top_nei in ob_set and left_nei in ob_set :
104+ neighbor_set .discard (lt_nei )
105+ if top_nei in ob_set and right_nei in ob_set :
106+ neighbor_set .discard (rt_nei )
107+ if bottom_nei in ob_set and left_nei in ob_set :
108+ neighbor_set .discard (lb_nei )
109+ if bottom_nei in ob_set and right_nei in ob_set :
110+ neighbor_set .discard (rb_nei )
111+
112+ # Filter out neighbors that are in the closed set
113+ closed_set = set (map (tuple , closed ))
114+ neighbor_set -= closed_set
115+
116+ return list (neighbor_set )
117+
118118
119119
120120def find_node_index (coordinate , node_list ):
0 commit comments