Skip to content

Commit

Permalink
Add search
Browse files Browse the repository at this point in the history
  • Loading branch information
paulzcooper committed Feb 18, 2024
1 parent 354de7b commit 09649b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
28 changes: 25 additions & 3 deletions algorithms/test/benchmark/hash_table_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _dehash_neighbor_boxes(self) -> list[list[int]]:

return int_neighbor_boxes

def _put_points_into_boxes(self, points: list[list]) -> dict[list]:
def _put_points_into_boxes(self, points: list[list]) -> dict[int, list]:
"""
Assigns a box index for each point based on its coordinates and returns list of associated points for each box
of the domain.
Expand Down Expand Up @@ -170,6 +170,28 @@ def _put_points_into_boxes(self, points: list[list]) -> dict[list]:

return points_in_boxes

def search(self, points: list[list]) -> list[list]:
"""
Performs the box-based neighbors search on the passed points.
:param points: 2D list of kind [[p0x, p0y], [p1x, p1y], ...]
:return: 2D list of kind [[neighbor_point_index, neighbor_point_index, ...], ...],
where the list index is the index of the according point.
"""
neighbors = [[] for point in range(len(points))]
points_in_boxes_dict = self._put_points_into_boxes(points)

for box_id, box_points_ids in points_in_boxes_dict.items():
curr_box_neighbors_list = self._box_neighbors[box_id]

for neighbor_box_id in curr_box_neighbors_list:
if self.hash2box_id[neighbor_box_id] >= self.hash2box_id[box_id]:
neighbor_box_points = points_in_boxes_dict[neighbor_box_id]

for box_point_id in box_points_ids:
for neighbor_point_id in neighbor_box_points:
dist = dist_sqr(points[box_point_id], points[neighbor_point_id])
if dist - self.search_radius ** 2 < self.epsilon:
neighbors[box_point_id].append(neighbor_point_id)
neighbors[neighbor_point_id].append(box_point_id)

if __name__ == '__main__':
pass
return neighbors
14 changes: 7 additions & 7 deletions algorithms/test/benchmark/neighbors_search_algorithms_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ def setUp(self):

def test_bruteforce(self):
for points, expected_nb in self.test_data:
with self.subTest(points=points):
with self.subTest(points=points, msg="bruteforce"):
bf = BruteForce(search_radius=self.search_radius, epsilon=self.epsilon)
actual_nb = bf.search(points)
actual_nb = [sorted(neighbors) for neighbors in actual_nb]
self.assertEqual(actual_nb, expected_nb)

def test_optimized_bruteforce(self):
for points, expected_nb in self.test_data:
with self.subTest(points=points):
with self.subTest(points=points, msg="optimized_bruteforce"):
bfo = BruteForceOptimized(search_radius=self.search_radius, epsilon=self.epsilon)
actual_nb = bfo.search(points)
actual_nb = [sorted(neighbors) for neighbors in actual_nb]
self.assertEqual(actual_nb, expected_nb)

def test_box_search(self):
for points, expected_nb in self.test_data:
with self.subTest(points=points):
with self.subTest(points=points, msg="box_search"):
box_search = BoxSearch(search_radius=self.search_radius, epsilon=self.epsilon,
domain_size=self.domain_size)
actual_nb = box_search.search(points)
Expand All @@ -117,10 +117,10 @@ def test_box_search(self):

def test_hashed_box_search(self):
for points, expected_nb in self.test_data:
with self.subTest(points=points):
box_search = HashBasedBoxSearch(search_radius=self.search_radius, epsilon=self.epsilon,
domain_size=self.domain_size)
actual_nb = box_search.search(points)
with self.subTest(points=points, msg="hashed_box_search"):
hashed_box_search = HashBasedBoxSearch(search_radius=self.search_radius, epsilon=self.epsilon,
domain_size=self.domain_size)
actual_nb = hashed_box_search.search(points)
actual_nb = [sorted(neighbors) for neighbors in actual_nb]
self.assertEqual(actual_nb, expected_nb)

Expand Down

0 comments on commit 09649b1

Please sign in to comment.