Skip to content

Commit d8f5cc2

Browse files
authored
Merge pull request #56 from ManifoldRG/55-fix-final-position-recentering-and-remove-negative-ranges
Removed negative ranges, recentered final module positions, added tes…
2 parents 35eda04 + d8ce15e commit d8f5cc2

File tree

2 files changed

+105
-14
lines changed

2 files changed

+105
-14
lines changed

ogm/occupancy_grid_map.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def recenter_initial_positions(self, module_positions, final_module_positions, g
7979
raise ValueError("Module 1 must exist in the module positions dictionary")
8080

8181
module1_pos = module_positions[1]
82+
module1_final_pos = final_module_positions[1]
8283

8384
# Calculate offset to move module 1 to grid center
8485
offset = (
@@ -96,13 +97,20 @@ def recenter_initial_positions(self, module_positions, final_module_positions, g
9697
pos[2] + offset[2]
9798
)
9899

100+
# Calculate the "final" offset to move module 1 to grid center (in the case that module 1's final position deviates from its initial position)
101+
final_offset = (
102+
grid_center - module1_final_pos[0],
103+
grid_center - module1_final_pos[1],
104+
grid_center - module1_final_pos[2]
105+
)
106+
99107
# Apply offset to all final module positions
100108
recentered_final_positions = {}
101109
for module, pos in final_module_positions.items():
102110
recentered_final_positions[module] = (
103-
pos[0] + offset[0],
104-
pos[1] + offset[1],
105-
pos[2] + offset[2]
111+
pos[0] + final_offset[0],
112+
pos[1] + final_offset[1],
113+
pos[2] + final_offset[2]
106114
)
107115

108116
return recentered_positions, recentered_final_positions
@@ -228,11 +236,6 @@ def init_actions(self):
228236
48: np.array([[0,0], [-1,1], [-2,0]]) # does the negative stuff work? # now switch which dimension stays the same
229237
}
230238

231-
self.negative_y_ranges = {7,8,15,16}
232-
self.negative_y_ranges = {}
233-
self.negative_z_ranges = {23,24,31,32,39,40,47,48}
234-
self.negative_z_ranges = {}
235-
236239
def calc_possible_actions(self): # need to check now that neighbor is free
237240
self.possible_actions = {}
238241
self.articulation_points = set(self.articulationPoints(len(self.modules), self.edges))
@@ -254,12 +257,7 @@ def calc_possible_actions(self): # need to check now that neighbor is free
254257
offset_y = module_position[1] + rangethingy[1]
255258
offset_z = module_position[2] + rangethingy[2]
256259

257-
if p in self.negative_y_ranges:
258-
sliced = self.curr_grid_map[offset_x[0]:(offset_x[1] + 1), offset_y[0]:(None if offset_y[1]==0 else offset_y[1] - 1):-1, offset_z[0]:(offset_z[1] + 1)]
259-
elif p in self.negative_z_ranges:
260-
sliced = self.curr_grid_map[offset_x[0]:(offset_x[1] + 1), offset_y[0]:(offset_y[1] + 1), offset_z[0]:(None if offset_z[1]==0 else offset_z[1] - 1):-1]
261-
else:
262-
sliced = self.curr_grid_map[offset_x[0]:(offset_x[1] + 1), offset_y[0]:(offset_y[1] + 1), offset_z[0]:(offset_z[1] + 1)]
260+
sliced = self.curr_grid_map[offset_x[0]:(offset_x[1] + 1), offset_y[0]:(offset_y[1] + 1), offset_z[0]:(offset_z[1] + 1)]
263261

264262
booled = np.squeeze(sliced > 0)
265263
pa = self.possible_actions[m]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)