miniworlds-robot 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- miniworlds_robot-0.1.0/LICENSE +21 -0
- miniworlds_robot-0.1.0/PKG-INFO +41 -0
- miniworlds_robot-0.1.0/README.md +25 -0
- miniworlds_robot-0.1.0/pyproject.toml +29 -0
- miniworlds_robot-0.1.0/setup.cfg +4 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/__init__.py +21 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/config.py +112 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/loader.py +57 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/py.typed +1 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/robot.py +76 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/tasks.py +20 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot/world.py +121 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot.egg-info/PKG-INFO +41 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot.egg-info/SOURCES.txt +17 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot.egg-info/dependency_links.txt +1 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot.egg-info/requires.txt +1 -0
- miniworlds_robot-0.1.0/source/miniworlds_robot.egg-info/top_level.txt +1 -0
- miniworlds_robot-0.1.0/test/test_examples.py +36 -0
- miniworlds_robot-0.1.0/test/test_robot_library.py +105 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andreas Siebel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: miniworlds-robot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Actor-based robot worlds for miniworlds
|
|
5
|
+
Author: Andreas Siebel
|
|
6
|
+
Classifier: Development Status :: 3 - Alpha
|
|
7
|
+
Classifier: Intended Audience :: Education
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Topic :: Education
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: miniworlds
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# miniworlds-robot
|
|
18
|
+
|
|
19
|
+
`miniworlds-robot` is a small extension library for miniworlds. It provides
|
|
20
|
+
robot worlds with a restricted, configuration-driven API for learners.
|
|
21
|
+
|
|
22
|
+
The library builds everything from miniworlds worlds and actors. It does not
|
|
23
|
+
load pygame sprites or expose pygame primitives.
|
|
24
|
+
|
|
25
|
+
The package is intentionally kept separate from the miniworlds core package so
|
|
26
|
+
it can later live in its own repository and be published on PyPI.
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from miniworlds_robot import Loader
|
|
32
|
+
|
|
33
|
+
world = Loader.load_world("basic")
|
|
34
|
+
robot = Loader.load_robot("standard", world, position=(1, 1))
|
|
35
|
+
|
|
36
|
+
robot.step()
|
|
37
|
+
robot.turn_left()
|
|
38
|
+
|
|
39
|
+
print(world.is_solved())
|
|
40
|
+
world.run()
|
|
41
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# miniworlds-robot
|
|
2
|
+
|
|
3
|
+
`miniworlds-robot` is a small extension library for miniworlds. It provides
|
|
4
|
+
robot worlds with a restricted, configuration-driven API for learners.
|
|
5
|
+
|
|
6
|
+
The library builds everything from miniworlds worlds and actors. It does not
|
|
7
|
+
load pygame sprites or expose pygame primitives.
|
|
8
|
+
|
|
9
|
+
The package is intentionally kept separate from the miniworlds core package so
|
|
10
|
+
it can later live in its own repository and be published on PyPI.
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from miniworlds_robot import Loader
|
|
16
|
+
|
|
17
|
+
world = Loader.load_world("basic")
|
|
18
|
+
robot = Loader.load_robot("standard", world, position=(1, 1))
|
|
19
|
+
|
|
20
|
+
robot.step()
|
|
21
|
+
robot.turn_left()
|
|
22
|
+
|
|
23
|
+
print(world.is_solved())
|
|
24
|
+
world.run()
|
|
25
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "miniworlds-robot"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Actor-based robot worlds for miniworlds"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"miniworlds",
|
|
13
|
+
]
|
|
14
|
+
authors = [
|
|
15
|
+
{ name = "Andreas Siebel" },
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Education",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Programming Language :: Python",
|
|
22
|
+
"Topic :: Education",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
where = ["source"]
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.package-data]
|
|
29
|
+
miniworlds_robot = ["py.typed"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from miniworlds_robot.config import ObjectConfig, RobotConfig, TargetConfig, WorldConfig
|
|
2
|
+
from miniworlds_robot.loader import Loader, load_robot, load_world
|
|
3
|
+
from miniworlds_robot.robot import Robot
|
|
4
|
+
from miniworlds_robot.tasks import task
|
|
5
|
+
from miniworlds_robot.world import Leaf, Mushroom, RobotWorld, Tree
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"Leaf",
|
|
9
|
+
"Loader",
|
|
10
|
+
"Mushroom",
|
|
11
|
+
"ObjectConfig",
|
|
12
|
+
"Robot",
|
|
13
|
+
"RobotConfig",
|
|
14
|
+
"RobotWorld",
|
|
15
|
+
"TargetConfig",
|
|
16
|
+
"Tree",
|
|
17
|
+
"WorldConfig",
|
|
18
|
+
"load_robot",
|
|
19
|
+
"load_world",
|
|
20
|
+
"task",
|
|
21
|
+
]
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import Mapping, Tuple
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Color = Tuple[int, int, int] | Tuple[int, int, int, int]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class RobotConfig:
|
|
12
|
+
name: str
|
|
13
|
+
costume: Color = (214, 37, 37, 255)
|
|
14
|
+
direction: int = 90
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class ObjectConfig:
|
|
19
|
+
kind: str
|
|
20
|
+
position: Tuple[int, int]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class TargetConfig:
|
|
25
|
+
robot_position: Tuple[int, int] | None = None
|
|
26
|
+
robot_direction: int | None = None
|
|
27
|
+
robot_steps: int | None = None
|
|
28
|
+
objects: tuple[ObjectConfig, ...] | None = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class WorldConfig:
|
|
33
|
+
name: str
|
|
34
|
+
columns: int = 10
|
|
35
|
+
rows: int = 10
|
|
36
|
+
tile_size: int = 40
|
|
37
|
+
background: Color = (221, 236, 203, 255)
|
|
38
|
+
objects: tuple[ObjectConfig, ...] = ()
|
|
39
|
+
target: TargetConfig = field(default_factory=TargetConfig)
|
|
40
|
+
robot_abilities: frozenset[str] = field(
|
|
41
|
+
default_factory=lambda: frozenset({"step", "turn_left", "turn_right"})
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
WORLD_CONFIGS: Mapping[str, WorldConfig] = {
|
|
46
|
+
"basic": WorldConfig(name="basic"),
|
|
47
|
+
"sequence_path": WorldConfig(
|
|
48
|
+
name="sequence_path",
|
|
49
|
+
columns=6,
|
|
50
|
+
rows=4,
|
|
51
|
+
target=TargetConfig(robot_position=(4, 2), robot_direction=90, robot_steps=4),
|
|
52
|
+
),
|
|
53
|
+
"variables_path": WorldConfig(
|
|
54
|
+
name="variables_path",
|
|
55
|
+
columns=6,
|
|
56
|
+
rows=4,
|
|
57
|
+
target=TargetConfig(robot_position=(3, 1), robot_direction=-90, robot_steps=5),
|
|
58
|
+
),
|
|
59
|
+
"function_path": WorldConfig(
|
|
60
|
+
name="function_path",
|
|
61
|
+
columns=6,
|
|
62
|
+
rows=4,
|
|
63
|
+
target=TargetConfig(robot_position=(1, 2), robot_direction=-90, robot_steps=4),
|
|
64
|
+
),
|
|
65
|
+
"loop_square": WorldConfig(
|
|
66
|
+
name="loop_square",
|
|
67
|
+
columns=6,
|
|
68
|
+
rows=6,
|
|
69
|
+
target=TargetConfig(robot_position=(1, 1), robot_direction=90, robot_steps=8),
|
|
70
|
+
),
|
|
71
|
+
"leaf_line": WorldConfig(
|
|
72
|
+
name="leaf_line",
|
|
73
|
+
columns=7,
|
|
74
|
+
rows=3,
|
|
75
|
+
objects=(
|
|
76
|
+
ObjectConfig("leaf", (1, 1)),
|
|
77
|
+
ObjectConfig("leaf", (3, 1)),
|
|
78
|
+
ObjectConfig("leaf", (5, 1)),
|
|
79
|
+
),
|
|
80
|
+
target=TargetConfig(robot_position=(6, 1), robot_direction=90, robot_steps=6, objects=()),
|
|
81
|
+
robot_abilities=frozenset(
|
|
82
|
+
{"step", "turn_left", "turn_right", "on_leaf", "remove_leaf"}
|
|
83
|
+
),
|
|
84
|
+
),
|
|
85
|
+
"obstacle_garden": WorldConfig(
|
|
86
|
+
name="obstacle_garden",
|
|
87
|
+
columns=7,
|
|
88
|
+
rows=5,
|
|
89
|
+
objects=(
|
|
90
|
+
ObjectConfig("tree", (3, 1)),
|
|
91
|
+
ObjectConfig("mushroom", (3, 2)),
|
|
92
|
+
ObjectConfig("tree", (3, 3)),
|
|
93
|
+
),
|
|
94
|
+
target=TargetConfig(
|
|
95
|
+
objects=(
|
|
96
|
+
ObjectConfig("tree", (3, 1)),
|
|
97
|
+
ObjectConfig("mushroom", (3, 2)),
|
|
98
|
+
ObjectConfig("tree", (3, 3)),
|
|
99
|
+
)
|
|
100
|
+
),
|
|
101
|
+
),
|
|
102
|
+
"with_position": WorldConfig(
|
|
103
|
+
name="with_position",
|
|
104
|
+
robot_abilities=frozenset({"step", "turn_left", "turn_right", "position"}),
|
|
105
|
+
),
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
ROBOT_CONFIGS: Mapping[str, RobotConfig] = {
|
|
110
|
+
"standard": RobotConfig(name="standard"),
|
|
111
|
+
"blue": RobotConfig(name="blue", costume=(38, 112, 201, 255)),
|
|
112
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from miniworlds_robot.config import ROBOT_CONFIGS, WORLD_CONFIGS, RobotConfig, WorldConfig
|
|
4
|
+
from miniworlds_robot.robot import Robot, create_robot
|
|
5
|
+
from miniworlds_robot.world import Leaf, Mushroom, Position, RobotWorld, Tree
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
OBJECT_TYPES = {
|
|
9
|
+
"leaf": Leaf,
|
|
10
|
+
"mushroom": Mushroom,
|
|
11
|
+
"tree": Tree,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _resolve_world_config(config: str | WorldConfig) -> WorldConfig:
|
|
16
|
+
if isinstance(config, WorldConfig):
|
|
17
|
+
return config
|
|
18
|
+
try:
|
|
19
|
+
return WORLD_CONFIGS[config]
|
|
20
|
+
except KeyError:
|
|
21
|
+
raise ValueError(f"Unknown RobotWorld config: {config!r}") from None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _resolve_robot_config(config: str | RobotConfig) -> RobotConfig:
|
|
25
|
+
if isinstance(config, RobotConfig):
|
|
26
|
+
return config
|
|
27
|
+
try:
|
|
28
|
+
return ROBOT_CONFIGS[config]
|
|
29
|
+
except KeyError:
|
|
30
|
+
raise ValueError(f"Unknown Robot config: {config!r}") from None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def load_world(config: str | WorldConfig = "basic", **overrides) -> RobotWorld:
|
|
34
|
+
world_config = _resolve_world_config(config)
|
|
35
|
+
world = RobotWorld(world_config, **overrides)
|
|
36
|
+
for obj in world_config.objects:
|
|
37
|
+
try:
|
|
38
|
+
object_cls = OBJECT_TYPES[obj.kind]
|
|
39
|
+
except KeyError:
|
|
40
|
+
raise ValueError(f"Unknown RobotWorld object kind: {obj.kind!r}") from None
|
|
41
|
+
world.add_object(object_cls, obj.position)
|
|
42
|
+
return world
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def load_robot(
|
|
46
|
+
config: str | RobotConfig = "standard",
|
|
47
|
+
world: RobotWorld | None = None,
|
|
48
|
+
*,
|
|
49
|
+
position: Position = (0, 0),
|
|
50
|
+
) -> Robot:
|
|
51
|
+
world = world or load_world()
|
|
52
|
+
return create_robot(_resolve_robot_config(config), world, position)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Loader:
|
|
56
|
+
load_world = staticmethod(load_world)
|
|
57
|
+
load_robot = staticmethod(load_robot)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from miniworlds import Actor
|
|
4
|
+
|
|
5
|
+
from miniworlds_robot.config import RobotConfig
|
|
6
|
+
from miniworlds_robot.world import Leaf, Position, RobotWorld
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _RobotBody(Actor):
|
|
10
|
+
def __init__(self, position: Position, *, world: RobotWorld, config: RobotConfig):
|
|
11
|
+
super().__init__(position, world=world)
|
|
12
|
+
self.is_robot_body = True
|
|
13
|
+
self.robot_steps = 0
|
|
14
|
+
self.size = (1, 1)
|
|
15
|
+
self.direction = config.direction
|
|
16
|
+
self.add_costume(config.costume)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Robot:
|
|
20
|
+
"""Restricted public facade for the internal miniworlds actor."""
|
|
21
|
+
|
|
22
|
+
_ABILITY_METHODS: dict[str, str] = {
|
|
23
|
+
"step": "_step",
|
|
24
|
+
"turn_left": "_turn_left",
|
|
25
|
+
"turn_right": "_turn_right",
|
|
26
|
+
"on_leaf": "_on_leaf",
|
|
27
|
+
"remove_leaf": "_remove_leaf",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
def __init__(self, actor: _RobotBody, world: RobotWorld):
|
|
31
|
+
self._actor = actor
|
|
32
|
+
self._world = world
|
|
33
|
+
|
|
34
|
+
def __getattr__(self, name: str):
|
|
35
|
+
if name == "position" and self._has_ability("position"):
|
|
36
|
+
return self._actor.position
|
|
37
|
+
method_name = self._ABILITY_METHODS.get(name)
|
|
38
|
+
if method_name and self._has_ability(name):
|
|
39
|
+
return getattr(self, method_name)
|
|
40
|
+
raise AttributeError(f"Robot has no ability {name!r} in this world")
|
|
41
|
+
|
|
42
|
+
def _has_ability(self, name: str) -> bool:
|
|
43
|
+
return name in self._world.robot_abilities
|
|
44
|
+
|
|
45
|
+
def _step(self):
|
|
46
|
+
destination = self._actor.sensor_manager.get_destination(
|
|
47
|
+
self._actor.position, self._actor.direction, 1
|
|
48
|
+
)
|
|
49
|
+
if self._world.is_blocked(destination):
|
|
50
|
+
return self._actor
|
|
51
|
+
self._actor.robot_steps += 1
|
|
52
|
+
return self._actor.move(1)
|
|
53
|
+
|
|
54
|
+
def _turn_left(self):
|
|
55
|
+
return self._actor.turn_left(90)
|
|
56
|
+
|
|
57
|
+
def _turn_right(self):
|
|
58
|
+
return self._actor.turn_right(90)
|
|
59
|
+
|
|
60
|
+
def _on_leaf(self) -> bool:
|
|
61
|
+
return any(
|
|
62
|
+
isinstance(actor, Leaf)
|
|
63
|
+
for actor in self._world.detect_actors(self._actor.position)
|
|
64
|
+
if actor is not self._actor
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
def _remove_leaf(self) -> bool:
|
|
68
|
+
for actor in self._world.detect_actors(self._actor.position):
|
|
69
|
+
if isinstance(actor, Leaf):
|
|
70
|
+
actor.remove()
|
|
71
|
+
return True
|
|
72
|
+
return False
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def create_robot(config: RobotConfig, world: RobotWorld, position: Position = (0, 0)) -> Robot:
|
|
76
|
+
return Robot(_RobotBody(position, world=world, config=config), world)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from miniworlds_robot.loader import load_robot, load_world
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def task(name: str, robot: str = "standard"):
|
|
7
|
+
world = load_world(name)
|
|
8
|
+
robot_actor = load_robot(robot, world, position=_start_position(name))
|
|
9
|
+
return world, robot_actor
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _start_position(name: str) -> tuple[int, int]:
|
|
13
|
+
starts = {
|
|
14
|
+
"sequence_path": (1, 1),
|
|
15
|
+
"variables_path": (0, 1),
|
|
16
|
+
"function_path": (1, 2),
|
|
17
|
+
"loop_square": (1, 1),
|
|
18
|
+
"leaf_line": (0, 1),
|
|
19
|
+
}
|
|
20
|
+
return starts.get(name, (0, 0))
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Tuple, Type
|
|
4
|
+
|
|
5
|
+
from miniworlds import Actor, TiledWorld
|
|
6
|
+
|
|
7
|
+
from miniworlds_robot.config import WorldConfig
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
Position = Tuple[float, float]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class RobotWorld(TiledWorld):
|
|
14
|
+
"""A miniworlds tiled world with a configuration-driven Robot API."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, config: WorldConfig | None = None, **overrides):
|
|
17
|
+
config = config or WorldConfig(name="custom")
|
|
18
|
+
self.robot_config = config
|
|
19
|
+
columns = overrides.pop("columns", config.columns)
|
|
20
|
+
rows = overrides.pop("rows", config.rows)
|
|
21
|
+
tile_size = overrides.pop("tile_size", config.tile_size)
|
|
22
|
+
super().__init__(columns, rows, tile_size=tile_size, **overrides)
|
|
23
|
+
self.tile_size = tile_size
|
|
24
|
+
self.grid = True
|
|
25
|
+
self.background.fill_color = config.background
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def robot_abilities(self) -> frozenset[str]:
|
|
29
|
+
return self.robot_config.robot_abilities
|
|
30
|
+
|
|
31
|
+
def is_blocked(self, position: Position) -> bool:
|
|
32
|
+
if not self.is_tile(position):
|
|
33
|
+
return True
|
|
34
|
+
return any(getattr(actor, "blocks_robot", False) for actor in self.detect_actors(position))
|
|
35
|
+
|
|
36
|
+
def add_object(self, actor_cls: Type[Actor], position: Position) -> Actor:
|
|
37
|
+
return actor_cls(position, world=self)
|
|
38
|
+
|
|
39
|
+
def is_solved(self) -> bool:
|
|
40
|
+
"""Return whether the current world state matches the task target."""
|
|
41
|
+
target = self.robot_config.target
|
|
42
|
+
if target.robot_position is not None and self._robot_positions() != (target.robot_position,):
|
|
43
|
+
return False
|
|
44
|
+
if target.robot_direction is not None and self._robot_directions() != (target.robot_direction,):
|
|
45
|
+
return False
|
|
46
|
+
if target.robot_steps is not None and self._robot_steps() != (target.robot_steps,):
|
|
47
|
+
return False
|
|
48
|
+
if target.objects is not None and self._object_state() != self._object_target_state(target.objects):
|
|
49
|
+
return False
|
|
50
|
+
return True
|
|
51
|
+
|
|
52
|
+
def _robot_positions(self) -> tuple[tuple[int, int], ...]:
|
|
53
|
+
return tuple(
|
|
54
|
+
sorted(
|
|
55
|
+
actor.position
|
|
56
|
+
for actor in self.actors
|
|
57
|
+
if getattr(actor, "is_robot_body", False)
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def _robot_directions(self) -> tuple[int, ...]:
|
|
62
|
+
return tuple(
|
|
63
|
+
sorted(
|
|
64
|
+
actor.direction
|
|
65
|
+
for actor in self.actors
|
|
66
|
+
if getattr(actor, "is_robot_body", False)
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def _robot_steps(self) -> tuple[int, ...]:
|
|
71
|
+
return tuple(
|
|
72
|
+
sorted(
|
|
73
|
+
actor.robot_steps
|
|
74
|
+
for actor in self.actors
|
|
75
|
+
if getattr(actor, "is_robot_body", False)
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def _object_state(self) -> tuple[tuple[str, tuple[int, int]], ...]:
|
|
80
|
+
return tuple(
|
|
81
|
+
sorted(
|
|
82
|
+
(actor.robot_object_kind, actor.position)
|
|
83
|
+
for actor in self.actors
|
|
84
|
+
if hasattr(actor, "robot_object_kind")
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def _object_target_state(objects) -> tuple[tuple[str, tuple[int, int]], ...]:
|
|
90
|
+
return tuple(sorted((obj.kind, obj.position) for obj in objects))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class RobotObject(Actor):
|
|
94
|
+
blocks_robot = False
|
|
95
|
+
collectable = False
|
|
96
|
+
costume_color = (255, 255, 255, 255)
|
|
97
|
+
robot_object_kind = "object"
|
|
98
|
+
|
|
99
|
+
def __init__(self, position: Position = (0, 0), *, world=None):
|
|
100
|
+
super().__init__(position, world=world)
|
|
101
|
+
self.size = (1, 1)
|
|
102
|
+
self.is_blocking = self.blocks_robot
|
|
103
|
+
self.add_costume(self.costume_color)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Tree(RobotObject):
|
|
107
|
+
blocks_robot = True
|
|
108
|
+
costume_color = (33, 120, 58, 255)
|
|
109
|
+
robot_object_kind = "tree"
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class Mushroom(RobotObject):
|
|
113
|
+
blocks_robot = True
|
|
114
|
+
costume_color = (192, 43, 48, 255)
|
|
115
|
+
robot_object_kind = "mushroom"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class Leaf(RobotObject):
|
|
119
|
+
collectable = True
|
|
120
|
+
costume_color = (74, 159, 65, 255)
|
|
121
|
+
robot_object_kind = "leaf"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: miniworlds-robot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Actor-based robot worlds for miniworlds
|
|
5
|
+
Author: Andreas Siebel
|
|
6
|
+
Classifier: Development Status :: 3 - Alpha
|
|
7
|
+
Classifier: Intended Audience :: Education
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Topic :: Education
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: miniworlds
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# miniworlds-robot
|
|
18
|
+
|
|
19
|
+
`miniworlds-robot` is a small extension library for miniworlds. It provides
|
|
20
|
+
robot worlds with a restricted, configuration-driven API for learners.
|
|
21
|
+
|
|
22
|
+
The library builds everything from miniworlds worlds and actors. It does not
|
|
23
|
+
load pygame sprites or expose pygame primitives.
|
|
24
|
+
|
|
25
|
+
The package is intentionally kept separate from the miniworlds core package so
|
|
26
|
+
it can later live in its own repository and be published on PyPI.
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from miniworlds_robot import Loader
|
|
32
|
+
|
|
33
|
+
world = Loader.load_world("basic")
|
|
34
|
+
robot = Loader.load_robot("standard", world, position=(1, 1))
|
|
35
|
+
|
|
36
|
+
robot.step()
|
|
37
|
+
robot.turn_left()
|
|
38
|
+
|
|
39
|
+
print(world.is_solved())
|
|
40
|
+
world.run()
|
|
41
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
source/miniworlds_robot/__init__.py
|
|
5
|
+
source/miniworlds_robot/config.py
|
|
6
|
+
source/miniworlds_robot/loader.py
|
|
7
|
+
source/miniworlds_robot/py.typed
|
|
8
|
+
source/miniworlds_robot/robot.py
|
|
9
|
+
source/miniworlds_robot/tasks.py
|
|
10
|
+
source/miniworlds_robot/world.py
|
|
11
|
+
source/miniworlds_robot.egg-info/PKG-INFO
|
|
12
|
+
source/miniworlds_robot.egg-info/SOURCES.txt
|
|
13
|
+
source/miniworlds_robot.egg-info/dependency_links.txt
|
|
14
|
+
source/miniworlds_robot.egg-info/requires.txt
|
|
15
|
+
source/miniworlds_robot.egg-info/top_level.txt
|
|
16
|
+
test/test_examples.py
|
|
17
|
+
test/test_robot_library.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
miniworlds
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
miniworlds_robot
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
from importlib.util import module_from_spec, spec_from_file_location
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from unittest.mock import patch
|
|
5
|
+
|
|
6
|
+
from miniworlds_robot import RobotWorld
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
10
|
+
EXAMPLES_DIR = ROOT / "examples"
|
|
11
|
+
SOLUTIONS_DIR = ROOT / "solutions"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _load_file(path):
|
|
15
|
+
spec = spec_from_file_location(path.stem, path)
|
|
16
|
+
module = module_from_spec(spec)
|
|
17
|
+
assert spec.loader is not None
|
|
18
|
+
with warnings.catch_warnings():
|
|
19
|
+
warnings.filterwarnings("ignore", message=r"\[world_name\]\.run\(\) was not found")
|
|
20
|
+
with patch.object(RobotWorld, "run", lambda world, *args, **kwargs: None):
|
|
21
|
+
spec.loader.exec_module(module)
|
|
22
|
+
return module
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_student_examples_are_importable_but_not_solved_yet():
|
|
26
|
+
for path in sorted(EXAMPLES_DIR.glob("[0-9][0-9]_*.py")):
|
|
27
|
+
module = _load_file(path)
|
|
28
|
+
|
|
29
|
+
assert module.world.is_solved() is False
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_solutions_match_target_world_configuration():
|
|
33
|
+
for path in sorted(SOLUTIONS_DIR.glob("[0-9][0-9]_*.py")):
|
|
34
|
+
module = _load_file(path)
|
|
35
|
+
|
|
36
|
+
assert module.world.is_solved() is True
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from unittest.mock import patch
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from miniworlds import Actor, TiledWorld
|
|
6
|
+
from miniworlds_robot import Leaf, Loader, ObjectConfig, RobotConfig, WorldConfig, load_robot, load_world
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_loader_creates_world_and_robot_with_restricted_default_api():
|
|
10
|
+
world = load_world("basic")
|
|
11
|
+
robot = load_robot("standard", world, position=(1, 1))
|
|
12
|
+
|
|
13
|
+
robot.step()
|
|
14
|
+
robot.turn_left()
|
|
15
|
+
robot.turn_right()
|
|
16
|
+
|
|
17
|
+
with pytest.raises(AttributeError):
|
|
18
|
+
robot.position
|
|
19
|
+
|
|
20
|
+
assert all(isinstance(actor, Actor) for actor in world.actors)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_world_config_can_expose_position():
|
|
24
|
+
world = load_world("with_position")
|
|
25
|
+
robot = load_robot("standard", world, position=(2, 3))
|
|
26
|
+
|
|
27
|
+
assert robot.position == (2, 3)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_custom_world_config_controls_leaf_abilities():
|
|
31
|
+
config = WorldConfig(
|
|
32
|
+
name="leaf-world",
|
|
33
|
+
robot_abilities=frozenset({"on_leaf", "remove_leaf"}),
|
|
34
|
+
)
|
|
35
|
+
world = Loader.load_world(config)
|
|
36
|
+
robot = Loader.load_robot(RobotConfig(name="green", costume=(40, 160, 80, 255)), world, position=(1, 1))
|
|
37
|
+
Leaf((1, 1), world=world)
|
|
38
|
+
|
|
39
|
+
assert robot.on_leaf()
|
|
40
|
+
assert robot.remove_leaf()
|
|
41
|
+
assert not robot.on_leaf()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_loader_places_configured_world_objects():
|
|
45
|
+
world = load_world(
|
|
46
|
+
WorldConfig(
|
|
47
|
+
name="configured-objects",
|
|
48
|
+
objects=(
|
|
49
|
+
ObjectConfig("tree", (1, 1)),
|
|
50
|
+
ObjectConfig("mushroom", (2, 1)),
|
|
51
|
+
ObjectConfig("leaf", (3, 1)),
|
|
52
|
+
),
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
assert world.is_blocked((1, 1))
|
|
57
|
+
assert world.is_blocked((2, 1))
|
|
58
|
+
assert not world.is_blocked((3, 1))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_robot_cannot_step_onto_a_blocking_object_or_count_the_attempt():
|
|
62
|
+
world = load_world("obstacle_garden")
|
|
63
|
+
robot = load_robot(world=world, position=(2, 1))
|
|
64
|
+
|
|
65
|
+
robot.step()
|
|
66
|
+
|
|
67
|
+
assert robot._actor.position == (2, 1)
|
|
68
|
+
assert robot._actor.robot_steps == 0
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_robot_cannot_leave_the_world_or_count_the_attempt():
|
|
72
|
+
world = load_world(WorldConfig(name="small", columns=2, rows=2))
|
|
73
|
+
robot = load_robot(RobotConfig(name="left", direction=-90), world, position=(0, 0))
|
|
74
|
+
|
|
75
|
+
robot.step()
|
|
76
|
+
|
|
77
|
+
assert robot._actor.position == (0, 0)
|
|
78
|
+
assert robot._actor.robot_steps == 0
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_successful_step_moves_robot_and_counts_once():
|
|
82
|
+
world = load_world("basic")
|
|
83
|
+
robot = load_robot(world=world, position=(0, 0))
|
|
84
|
+
|
|
85
|
+
robot.step()
|
|
86
|
+
|
|
87
|
+
assert robot._actor.position == (1, 0)
|
|
88
|
+
assert robot._actor.robot_steps == 1
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_run_keeps_tiled_world_runtime_behavior():
|
|
92
|
+
world = load_world("basic")
|
|
93
|
+
|
|
94
|
+
with patch.object(TiledWorld, "run") as run:
|
|
95
|
+
world.run(fullscreen=True)
|
|
96
|
+
|
|
97
|
+
run.assert_called_once_with(fullscreen=True)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_unknown_configs_raise_clear_errors():
|
|
101
|
+
with pytest.raises(ValueError, match="Unknown RobotWorld config"):
|
|
102
|
+
load_world("missing")
|
|
103
|
+
|
|
104
|
+
with pytest.raises(ValueError, match="Unknown Robot config"):
|
|
105
|
+
load_robot("missing")
|