|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | +from ogm.occupancy_grid_map import OccupancyGridMap |
| 4 | +from agent.random_search_agent import RandomSearchAgent |
| 5 | +import networkx as nx |
| 6 | + |
| 7 | +class TestOGMFinalPositionRecentering(unittest.TestCase): |
| 8 | + |
| 9 | + def assert_recentered_positions_match(self, module_positions, final_module_positions, expected_recentered_module_positions, expected_recentered_final_module_positions): |
| 10 | + # Create occupancy grid map with 3 modules |
| 11 | + ogm = OccupancyGridMap(module_positions, final_module_positions, 3) |
| 12 | + |
| 13 | + # Print grid size and module positions |
| 14 | + print(f"Grid size: {ogm.grid_map.shape}") |
| 15 | + print(f"Original module 1 position: {module_positions[1]}") |
| 16 | + print(f"Recentered module 1 position: {ogm.module_positions[1]}") |
| 17 | + print(f"All recentered module positions: {ogm.module_positions}") |
| 18 | + print(f"Original module 1 final position: {final_module_positions[1]}") |
| 19 | + print(f"Recentered module 1 final position: {ogm.final_module_positions[1]}") |
| 20 | + print(f"All recentered module final positions: {ogm.final_module_positions}") |
| 21 | + |
| 22 | + # Compare with expected positions |
| 23 | + self.assertEqual( |
| 24 | + ogm.module_positions, expected_recentered_module_positions, |
| 25 | + msg=f"\nExpected recentered module positions: {expected_recentered_module_positions}\nActual recentered module positions: {ogm.module_positions}" |
| 26 | + ) |
| 27 | + |
| 28 | + self.assertEqual( |
| 29 | + ogm.final_module_positions, expected_recentered_final_module_positions, |
| 30 | + msg=f"\nExpected recentered final module positions: {expected_recentered_final_module_positions}\nActual recentered final module positions: {ogm.final_module_positions}" |
| 31 | + ) |
| 32 | + |
| 33 | + def assert_random_agent_success(self): |
| 34 | + matrix = np.zeros((9,9,9)) |
| 35 | + matrix[4, 4, 4] = 1 |
| 36 | + matrix[4, 5, 4] = 2 |
| 37 | + matrix[5,5,4] = 3 |
| 38 | + module_positions = {1: (4,4,4), 2: (4,5,4), 3: (5,5,4)} |
| 39 | + final_matrix = np.zeros((9,9,9)) |
| 40 | + final_matrix[4,4,4] = 1 |
| 41 | + final_matrix[3,5,4] = 2 |
| 42 | + final_matrix[4,5,4] = 3 |
| 43 | + final_module_positions = {1: (4, 4, 5), 2: (3, 5, 5), 3: (4, 5, 5)} |
| 44 | + ogm = OccupancyGridMap(module_positions, final_module_positions, 3) |
| 45 | + agent = RandomSearchAgent(max_steps=1000) |
| 46 | + |
| 47 | + success = agent.search(ogm) |
| 48 | + |
| 49 | + print(f"Search {'succeeded' if success else 'failed'} after {agent.steps_taken} steps") |
| 50 | + |
| 51 | + self.assertTrue(success, msg=f"\nSuccess: {success}") |
| 52 | + |
| 53 | + def test_configuration_case_1(self): |
| 54 | + """ |
| 55 | + Basic configuration: |
| 56 | + Module 1 — (4,4,4) |
| 57 | + Module 2 — (4,5,4) <- articulation point |
| 58 | + Module 3 — (5,5,4) |
| 59 | +
|
| 60 | + - Module 2 connects the other two; its removal disconnects the graph. |
| 61 | + - Only modules 1 and 3 should have pivots. |
| 62 | + """ |
| 63 | + # Define initial module positions |
| 64 | + module_positions = { |
| 65 | + 1: (19, 3, 5), # This will be recentered to the grid center |
| 66 | + 2: (19, 4, 5), |
| 67 | + 3: (19, 3, 6) |
| 68 | + } |
| 69 | + |
| 70 | + # Define final module positions |
| 71 | + final_module_positions = { |
| 72 | + 1: (19, 3, 6), |
| 73 | + 2: (19, 3, 5), |
| 74 | + 3: (20, 3, 6) |
| 75 | + } |
| 76 | + |
| 77 | + expected_recentered_module_positions = { |
| 78 | + 1: (4, 4, 4), |
| 79 | + 2: (4, 5, 4), |
| 80 | + 3: (4, 4, 5) |
| 81 | + } |
| 82 | + |
| 83 | + expected_recentered_final_module_positions = { |
| 84 | + 1: (4, 4, 4), |
| 85 | + 2: (4, 4, 3), |
| 86 | + 3: (5, 4, 4) |
| 87 | + } |
| 88 | + |
| 89 | + self.assert_recentered_positions_match(module_positions, final_module_positions, expected_recentered_module_positions, expected_recentered_final_module_positions) |
| 90 | + self.assert_random_agent_success() |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + unittest.main() |
0 commit comments