diff --git a/docs/images/requirements_graphs/MiniHCraftFourRooms.html b/docs/images/requirements_graphs/MiniHCraftFourRooms.html index 0a281fd3..db14c565 100644 --- a/docs/images/requirements_graphs/MiniHCraftFourRooms.html +++ b/docs/images/requirements_graphs/MiniHCraftFourRooms.html @@ -88,8 +88,8 @@
// parsing and collecting nodes and edges from the python - nodes = new vis.DataSet([{"color": "blue", "id": "zone_item#goal in zone", "image": "https://raw.githubusercontent.com/IRLL/HierarchyCraft/master/src/hcraft/examples/minicraft/resources/items/goal.png", "label": "Goal in zone", "level": 4, "name": "goal", "shape": "image", "size": 10, "title": "Goal in zone", "x": 600.0, "y": 800.0}, {"color": "red", "id": "item#goal", "image": "https://raw.githubusercontent.com/IRLL/HierarchyCraft/master/src/hcraft/examples/minicraft/resources/items/goal.png", "label": "Goal", "level": 5, "name": "goal", "shape": "image", "size": 10, "title": "Goal", "x": 600.0, "y": 1000.0}, {"color": "green", "id": "zone#North-West", "label": "North-west", "level": 4, "name": "North-West", "shape": "dot", "size": 10, "title": "North-west", "x": 0.0, "y": 800.0}, {"color": "green", "id": "zone#South-West", "label": "South-west", "level": 1, "name": "South-West", "shape": "dot", "size": 10, "title": "South-west", "x": 0.0, "y": 200.0}, {"color": "green", "id": "zone#South-East", "label": "South-east", "level": 2, "name": "South-East", "shape": "dot", "size": 10, "title": "South-east", "x": 0.0, "y": 400.0}, {"color": "green", "id": "zone#North-East", "label": "North-east", "level": 3, "name": "North-East", "shape": "dot", "size": 10, "title": "North-east", "x": 0.0, "y": 600.0}, {"color": "black", "font": {"color": "transparent", "strokeColor": "transparent"}, "id": "START#", "label": "START#", "level": 0, "shape": "dot", "size": 10, "title": "Start", "x": 0.0, "y": 0.0}]); - edges = new vis.DataSet([{"arrows": {"middle": {"enabled": true}}, "color": {"color": "#56B4E97F", "highlight": "#56B4E97F", "hover": "#56B4E97F"}, "from": "zone_item#goal in zone", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "item_required_in_zone for reach-goal (transformation 1):\n\nConditions:\nZone(goal\u22651)\nEffects:\n+goal Zone(-goal)", "to": "item#goal", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#CA91617F", "highlight": "#CA91617F", "hover": "#CA91617F"}, "from": "zone#North-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "zone_required for go-to-South-West-from-North-West (transformation 2):\n\nConditions:\n| at North-West\nEffects:\n| at South-West", "to": "zone#South-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#0173B27F", "highlight": "#0173B27F", "hover": "#0173B27F"}, "from": "zone#North-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-North-West-from-North-West (transformation 9):\n\nConditions:\n| at North-West\nEffects:\n| at North-West", "to": "zone#North-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#029E737F", "highlight": "#029E737F", "hover": "#029E737F"}, "from": "zone#South-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-South-East-from-South-East (transformation 5):\n\nConditions:\n| at South-East\nEffects:\n| at South-East", "to": "zone#South-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#FBAFE47F", "highlight": "#FBAFE47F", "hover": "#FBAFE47F"}, "from": "zone#South-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "zone_required for go-to-North-East-from-South-East (transformation 6):\n\nConditions:\n| at South-East\nEffects:\n| at North-East", "to": "zone#North-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#CC78BC7F", "highlight": "#CC78BC7F", "hover": "#CC78BC7F"}, "from": "zone#South-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-South-West-from-South-West (transformation 3):\n\nConditions:\n| at South-West\nEffects:\n| at South-West", "to": "zone#South-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#9494947F", "highlight": "#9494947F", "hover": "#9494947F"}, "from": "zone#South-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "zone_required for go-to-South-East-from-South-West (transformation 4):\n\nConditions:\n| at South-West\nEffects:\n| at South-East", "to": "zone#South-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#D55E007F", "highlight": "#D55E007F", "hover": "#D55E007F"}, "from": "zone#North-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "zone_required for find-goal (transformation 0):\n\nConditions:\nZone(goal\u22640) | at North-East\nEffects:\nZone(+goal)", "to": "zone_item#goal in zone", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#ECE1337F", "highlight": "#ECE1337F", "hover": "#ECE1337F"}, "from": "zone#North-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-North-East-from-North-East (transformation 7):\n\nConditions:\n| at North-East\nEffects:\n| at North-East", "to": "zone#North-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#DE8F057F", "highlight": "#DE8F057F", "hover": "#DE8F057F"}, "from": "zone#North-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "zone_required for go-to-North-West-from-North-East (transformation 8):\n\nConditions:\n| at North-East\nEffects:\n| at North-West", "to": "zone#North-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#0173B27F", "highlight": "#0173B27F", "hover": "#0173B27F"}, "from": "START#", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "Start_zone", "to": "zone#South-West", "width": 1}]); + nodes = new vis.DataSet([{"color": "blue", "id": "zone_item#goal in zone", "image": "https://raw.githubusercontent.com/IRLL/HierarchyCraft/master/src/hcraft/examples/minicraft/resources/items/goal.png", "label": "Goal in zone", "level": 4, "name": "goal", "shape": "image", "size": 10, "title": "Goal in zone", "x": 0.0, "y": 800.0}, {"color": "red", "id": "item#goal", "image": "https://raw.githubusercontent.com/IRLL/HierarchyCraft/master/src/hcraft/examples/minicraft/resources/items/goal.png", "label": "Goal", "level": 5, "name": "goal", "shape": "image", "size": 10, "title": "Goal", "x": 0.0, "y": 1000.0}, {"color": "green", "id": "zone#South-West", "label": "South-west", "level": 1, "name": "South-West", "shape": "dot", "size": 10, "title": "South-west", "x": 0.0, "y": 200.0}, {"color": "green", "id": "zone#South-East", "label": "South-east", "level": 2, "name": "South-East", "shape": "dot", "size": 10, "title": "South-east", "x": 600.0, "y": 400.0}, {"color": "green", "id": "zone#North-West", "label": "North-west", "level": 2, "name": "North-West", "shape": "dot", "size": 10, "title": "North-west", "x": 0.0, "y": 400.0}, {"color": "green", "id": "zone#North-East", "label": "North-east", "level": 3, "name": "North-East", "shape": "dot", "size": 10, "title": "North-east", "x": 0.0, "y": 600.0}, {"color": "black", "font": {"color": "transparent", "strokeColor": "transparent"}, "id": "START#", "label": "START#", "level": 0, "shape": "dot", "size": 10, "title": "Start", "x": 0.0, "y": 0.0}]); + edges = new vis.DataSet([{"arrows": {"middle": {"enabled": true}}, "color": {"color": "#0173B27F", "highlight": "#0173B27F", "hover": "#0173B27F"}, "from": "zone_item#goal in zone", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "item_required_in_zone for reach-goal (transformation 1):\n\nConditions:\nZone(goal\u22651)\nEffects:\n+goal Zone(-goal)", "to": "item#goal", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#D55E007F", "highlight": "#D55E007F", "hover": "#D55E007F"}, "from": "zone#South-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-South-East-from-South-West (transformation 4):\n\nConditions:\n| at South-West\nEffects:\n| at South-East", "to": "zone#South-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#FBAFE47F", "highlight": "#FBAFE47F", "hover": "#FBAFE47F"}, "from": "zone#South-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-North-West-from-South-West (transformation 9):\n\nConditions:\n| at South-West\nEffects:\n| at North-West", "to": "zone#North-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#CA91617F", "highlight": "#CA91617F", "hover": "#CA91617F"}, "from": "zone#North-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "zone_required for find-goal (transformation 0):\n\nConditions:\nZone(goal\u22640) | at North-East\nEffects:\nZone(+goal)", "to": "zone_item#goal in zone", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#9494947F", "highlight": "#9494947F", "hover": "#9494947F"}, "from": "zone#North-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-South-East-from-North-East (transformation 5):\n\nConditions:\n| at North-East\nEffects:\n| at South-East", "to": "zone#South-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#029E737F", "highlight": "#029E737F", "hover": "#029E737F"}, "from": "zone#North-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-North-West-from-North-East (transformation 8):\n\nConditions:\n| at North-East\nEffects:\n| at North-West", "to": "zone#North-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#ECE1337F", "highlight": "#ECE1337F", "hover": "#ECE1337F"}, "from": "zone#North-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-South-West-from-North-West (transformation 2):\n\nConditions:\n| at North-West\nEffects:\n| at South-West", "to": "zone#South-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#CC78BC7F", "highlight": "#CC78BC7F", "hover": "#CC78BC7F"}, "from": "zone#North-West", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-North-East-from-North-West (transformation 7):\n\nConditions:\n| at North-West\nEffects:\n| at North-East", "to": "zone#North-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#56B4E97F", "highlight": "#56B4E97F", "hover": "#56B4E97F"}, "from": "zone#South-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-South-West-from-South-East (transformation 3):\n\nConditions:\n| at South-East\nEffects:\n| at South-West", "to": "zone#South-West", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#DE8F057F", "highlight": "#DE8F057F", "hover": "#DE8F057F"}, "from": "zone#South-East", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": true, "roundness": 0.15, "type": "curvedCW"}, "title": "zone_required for go-to-North-East-from-South-East (transformation 6):\n\nConditions:\n| at South-East\nEffects:\n| at North-East", "to": "zone#North-East", "width": 1}, {"arrows": {"middle": {"enabled": true}}, "color": {"color": "#FBAFE47F", "highlight": "#FBAFE47F", "hover": "#FBAFE47F"}, "from": "START#", "hoverWidth": 0.1, "selectionWidth": 0.1, "smooth": {"enabled": false}, "title": "Start_zone", "to": "zone#South-West", "width": 1}]); nodeColors = {}; allNodes = nodes.get({ returnType: "Object" }); diff --git a/docs/images/requirements_graphs/MiniHCraftFourRooms.png b/docs/images/requirements_graphs/MiniHCraftFourRooms.png index 2bfaf8e7..87a4d0ee 100644 Binary files a/docs/images/requirements_graphs/MiniHCraftFourRooms.png and b/docs/images/requirements_graphs/MiniHCraftFourRooms.png differ diff --git a/src/hcraft/examples/minicraft/fourrooms.py b/src/hcraft/examples/minicraft/fourrooms.py index 6bb194ef..ba367bda 100644 --- a/src/hcraft/examples/minicraft/fourrooms.py +++ b/src/hcraft/examples/minicraft/fourrooms.py @@ -1,4 +1,4 @@ -from typing import List +from typing import Dict, List, TypeVar from hcraft.elements import Item, Zone from hcraft.task import GetItemTask @@ -62,12 +62,11 @@ def build_transformations(self) -> List[Transformation]: ], ) + neighbors = _get_rooms_connections(self.ROOMS) + moves = [] - for room_id, destination in enumerate(self.ROOMS): - for neighbor_room in [ - self.ROOMS[room_id - 1], - self.ROOMS[room_id % len(self.ROOMS)], - ]: + for destination, neighbor_rooms in neighbors.items(): + for neighbor_room in neighbor_rooms: moves.append( Transformation( f"go-to-{destination.name}-from-{neighbor_room.name}", @@ -77,3 +76,13 @@ def build_transformations(self) -> List[Transformation]: ) return [find_goal, reach_goal] + moves + + +Room = TypeVar("Room") + + +def _get_rooms_connections(rooms: List[Room]) -> Dict[Room, List[Room]]: + neighbors = {} + for room_id, destination in enumerate(rooms): + neighbors[destination] = [rooms[room_id - 1], rooms[(room_id + 1) % len(rooms)]] + return neighbors diff --git a/tests/examples/minigrid/__init__.py b/tests/examples/minigrid/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/examples/minigrid/test_fourrooms.py b/tests/examples/minigrid/test_fourrooms.py new file mode 100644 index 00000000..6b755cdc --- /dev/null +++ b/tests/examples/minigrid/test_fourrooms.py @@ -0,0 +1,10 @@ +from hcraft.examples.minicraft.fourrooms import _get_rooms_connections + + +def test_four_rooms_should_be_rightfully_connected(): + assert _get_rooms_connections([0, 1, 2, 3]) == { + 0: [3, 1], + 1: [0, 2], + 2: [1, 3], + 3: [2, 0], + }