gridworldpy 0.2.0__tar.gz → 0.3.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.
- gridworldpy-0.3.0/.gitignore +100 -0
- {gridworldpy-0.2.0/gridworldpy.egg-info → gridworldpy-0.3.0}/PKG-INFO +24 -13
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/README.md +14 -3
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/examples/basic_usage.py +1 -1
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/examples/policy_evaluation.py +1 -1
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/examples/q_learning.py +1 -1
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/examples/sarsa.py +2 -3
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/examples/value_iteration.py +1 -1
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/pyproject.toml +8 -6
- {gridworldpy-0.2.0/examples/gridworldpy → gridworldpy-0.3.0/src}/__init__.py +1 -1
- {gridworldpy-0.2.0/examples/gridworldpy → gridworldpy-0.3.0/src}/gridworld.py +25 -21
- {gridworldpy-0.2.0/examples/gridworldpy → gridworldpy-0.3.0/src}/q_table.py +1 -1
- gridworldpy-0.3.0/tests/test_gridworld.py +72 -0
- gridworldpy-0.2.0/MANIFEST.in +0 -19
- gridworldpy-0.2.0/PKG-INFO +0 -240
- gridworldpy-0.2.0/examples/test.py +0 -32
- gridworldpy-0.2.0/gridworldpy/__init__.py +0 -16
- gridworldpy-0.2.0/gridworldpy/gridworld.py +0 -680
- gridworldpy-0.2.0/gridworldpy/q_table.py +0 -65
- gridworldpy-0.2.0/gridworldpy/utils.py +0 -177
- gridworldpy-0.2.0/gridworldpy.egg-info/SOURCES.txt +0 -24
- gridworldpy-0.2.0/gridworldpy.egg-info/dependency_links.txt +0 -1
- gridworldpy-0.2.0/gridworldpy.egg-info/requires.txt +0 -4
- gridworldpy-0.2.0/gridworldpy.egg-info/top_level.txt +0 -1
- gridworldpy-0.2.0/setup.cfg +0 -4
- gridworldpy-0.2.0/setup.py +0 -45
- {gridworldpy-0.2.0 → gridworldpy-0.3.0}/LICENSE +0 -0
- {gridworldpy-0.2.0/examples/gridworldpy → gridworldpy-0.3.0/src}/utils.py +0 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
.gitignore
|
|
2
|
+
|
|
3
|
+
# Jupyter Notebook
|
|
4
|
+
.ipynb_checkpoints/
|
|
5
|
+
*.ipynb_convert/
|
|
6
|
+
|
|
7
|
+
# Python cache/files
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.py[cod]
|
|
10
|
+
*.pyo
|
|
11
|
+
*.pyd
|
|
12
|
+
*.so
|
|
13
|
+
|
|
14
|
+
# Virtual environments
|
|
15
|
+
.env
|
|
16
|
+
.venv
|
|
17
|
+
venv/
|
|
18
|
+
env/
|
|
19
|
+
ENV/
|
|
20
|
+
pip-wheel-metadata/
|
|
21
|
+
pip-log.txt
|
|
22
|
+
poetry.lock
|
|
23
|
+
|
|
24
|
+
# Package/build artifacts
|
|
25
|
+
build/
|
|
26
|
+
dist/
|
|
27
|
+
*.egg-info/
|
|
28
|
+
*.egg
|
|
29
|
+
.eggs/
|
|
30
|
+
|
|
31
|
+
# Tool caches
|
|
32
|
+
.mypy_cache/
|
|
33
|
+
.pytest_cache/
|
|
34
|
+
.pyre/
|
|
35
|
+
.pytype/
|
|
36
|
+
.cache/
|
|
37
|
+
.nox/
|
|
38
|
+
.coverage
|
|
39
|
+
coverage.xml
|
|
40
|
+
htmlcov/
|
|
41
|
+
|
|
42
|
+
# Editors / IDE
|
|
43
|
+
.vscode/
|
|
44
|
+
.idea/
|
|
45
|
+
|
|
46
|
+
# OS files
|
|
47
|
+
.DS_Store
|
|
48
|
+
Thumbs.db
|
|
49
|
+
|
|
50
|
+
# Logs
|
|
51
|
+
*.log
|
|
52
|
+
|
|
53
|
+
# Jupyter/analysis outputs (unversioned large data)
|
|
54
|
+
*.h5
|
|
55
|
+
*.hdf5
|
|
56
|
+
*.sqlite
|
|
57
|
+
*.db
|
|
58
|
+
|
|
59
|
+
# Common data folders (keep structure with .gitkeep if needed)
|
|
60
|
+
data/raw/
|
|
61
|
+
data/interim/
|
|
62
|
+
data/temp/
|
|
63
|
+
data/tmp/
|
|
64
|
+
outputs/
|
|
65
|
+
results/
|
|
66
|
+
|
|
67
|
+
# Credentials / configs
|
|
68
|
+
*.env
|
|
69
|
+
*.secret
|
|
70
|
+
*.key
|
|
71
|
+
*.pem
|
|
72
|
+
config.local.*
|
|
73
|
+
secrets.*
|
|
74
|
+
|
|
75
|
+
# Misc
|
|
76
|
+
*.bak
|
|
77
|
+
*.swp
|
|
78
|
+
*.orig
|
|
79
|
+
|
|
80
|
+
# LIC
|
|
81
|
+
.vscode
|
|
82
|
+
test
|
|
83
|
+
test.py
|
|
84
|
+
tmp.py
|
|
85
|
+
tmp*
|
|
86
|
+
cmds.txt
|
|
87
|
+
checkpoints
|
|
88
|
+
checkpoints/*
|
|
89
|
+
datasets
|
|
90
|
+
dataset
|
|
91
|
+
datasets/*
|
|
92
|
+
logs
|
|
93
|
+
logs/*
|
|
94
|
+
data_flow.drawio
|
|
95
|
+
wandb
|
|
96
|
+
.DS_Store
|
|
97
|
+
|
|
98
|
+
examples/gridworldpy
|
|
99
|
+
|
|
100
|
+
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: gridworldpy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A flexible and interactive grid world environment for reinforcement learning
|
|
5
|
-
Home-page: https://github.com/hitlic/gridworldpy
|
|
6
|
-
Author: LIC
|
|
7
|
-
Author-email: LIC <liuchen.lic@gmail.com>
|
|
8
|
-
Maintainer-email: LIC <liuchen.lic@gmail.com>
|
|
9
|
-
License: MIT
|
|
10
5
|
Project-URL: Homepage, https://github.com/hitlic/gridworldpy
|
|
11
6
|
Project-URL: Repository, https://github.com/hitlic/gridworldpy
|
|
12
7
|
Project-URL: Documentation, https://github.com/hitlic/gridworldpy#readme
|
|
13
8
|
Project-URL: Bug Reports, https://github.com/hitlic/gridworldpy/issues
|
|
14
|
-
|
|
9
|
+
Author-email: LIC <liuchen.lic@gmail.com>
|
|
10
|
+
Maintainer-email: LIC <liuchen.lic@gmail.com>
|
|
11
|
+
License: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: AI,RL,environment,grid world,machine learning,reinforcement learning,visualization
|
|
15
14
|
Classifier: Development Status :: 4 - Beta
|
|
16
15
|
Classifier: Intended Audience :: Developers
|
|
17
16
|
Classifier: Intended Audience :: Education
|
|
@@ -26,9 +25,10 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
26
25
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
27
26
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
28
27
|
Requires-Python: >=3.8
|
|
29
|
-
|
|
28
|
+
Requires-Dist: numpy>=1.21.0
|
|
30
29
|
Provides-Extra: examples
|
|
31
|
-
|
|
30
|
+
Requires-Dist: matplotlib>=3.5.0; extra == 'examples'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
32
|
|
|
33
33
|
# GridWorldPy
|
|
34
34
|
|
|
@@ -36,7 +36,12 @@ License-File: LICENSE
|
|
|
36
36
|
[](https://www.python.org/downloads/)
|
|
37
37
|
[](https://opensource.org/licenses/MIT)
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
一个灵活且交互式的网格世界环境,专为强化学习实验和教学目的设计。
|
|
40
|
+
|
|
41
|
+
<div align="center">
|
|
42
|
+
<img src="./img.png" width="50%">
|
|
43
|
+
</div>
|
|
44
|
+
|
|
40
45
|
|
|
41
46
|
## ✨ 特性
|
|
42
47
|
|
|
@@ -55,6 +60,8 @@ License-File: LICENSE
|
|
|
55
60
|
pip install gridworldpy
|
|
56
61
|
```
|
|
57
62
|
|
|
63
|
+
安装后的 Python 包名为 `src`,使用 `from src import GridWorldEnv` 导入。
|
|
64
|
+
|
|
58
65
|
或者从源码安装:
|
|
59
66
|
|
|
60
67
|
```bash
|
|
@@ -68,7 +75,7 @@ pip install -e .
|
|
|
68
75
|
### 基本使用
|
|
69
76
|
|
|
70
77
|
```python
|
|
71
|
-
from
|
|
78
|
+
from src import GridWorldEnv
|
|
72
79
|
|
|
73
80
|
# 创建一个5x5的网格世界
|
|
74
81
|
env = GridWorldEnv(grid_size=(5, 5), keyboard_control=True)
|
|
@@ -86,7 +93,7 @@ obs, reward, done, is_effective, info = env.step(1) # 向上移动
|
|
|
86
93
|
### 自定义配置
|
|
87
94
|
|
|
88
95
|
```python
|
|
89
|
-
from
|
|
96
|
+
from src import GridWorldEnv
|
|
90
97
|
|
|
91
98
|
# 创建带自定义配置的环境
|
|
92
99
|
env = GridWorldEnv(
|
|
@@ -193,6 +200,10 @@ GridWorldEnv(
|
|
|
193
200
|
- `set_disable_states(disabled_poses)`: 禁用指定状态
|
|
194
201
|
- `close()`: 关闭环境
|
|
195
202
|
|
|
203
|
+
每次动作计入最大步数,包括尝试进入禁用状态的动作;被禁用的动作使智能体留在原地,奖励为 0。普通动作获得离开状态的奖励,进入目标状态时结束回合,并额外获得目标状态的奖励。目标与最大步数同时达到时,终止原因记为目标。
|
|
204
|
+
|
|
205
|
+
策略中的撞墙动作使智能体留在原地。指向禁用状态的概率会被重新分配;修改禁用状态后,策略会根据原始设置重新计算。
|
|
206
|
+
|
|
196
207
|
#### 动作空间
|
|
197
208
|
|
|
198
209
|
- 当`enable_keep=True`时
|
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
[](https://www.python.org/downloads/)
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
一个灵活且交互式的网格世界环境,专为强化学习实验和教学目的设计。
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
<img src="./img.png" width="50%">
|
|
11
|
+
</div>
|
|
12
|
+
|
|
8
13
|
|
|
9
14
|
## ✨ 特性
|
|
10
15
|
|
|
@@ -23,6 +28,8 @@
|
|
|
23
28
|
pip install gridworldpy
|
|
24
29
|
```
|
|
25
30
|
|
|
31
|
+
安装后的 Python 包名为 `src`,使用 `from src import GridWorldEnv` 导入。
|
|
32
|
+
|
|
26
33
|
或者从源码安装:
|
|
27
34
|
|
|
28
35
|
```bash
|
|
@@ -36,7 +43,7 @@ pip install -e .
|
|
|
36
43
|
### 基本使用
|
|
37
44
|
|
|
38
45
|
```python
|
|
39
|
-
from
|
|
46
|
+
from src import GridWorldEnv
|
|
40
47
|
|
|
41
48
|
# 创建一个5x5的网格世界
|
|
42
49
|
env = GridWorldEnv(grid_size=(5, 5), keyboard_control=True)
|
|
@@ -54,7 +61,7 @@ obs, reward, done, is_effective, info = env.step(1) # 向上移动
|
|
|
54
61
|
### 自定义配置
|
|
55
62
|
|
|
56
63
|
```python
|
|
57
|
-
from
|
|
64
|
+
from src import GridWorldEnv
|
|
58
65
|
|
|
59
66
|
# 创建带自定义配置的环境
|
|
60
67
|
env = GridWorldEnv(
|
|
@@ -161,6 +168,10 @@ GridWorldEnv(
|
|
|
161
168
|
- `set_disable_states(disabled_poses)`: 禁用指定状态
|
|
162
169
|
- `close()`: 关闭环境
|
|
163
170
|
|
|
171
|
+
每次动作计入最大步数,包括尝试进入禁用状态的动作;被禁用的动作使智能体留在原地,奖励为 0。普通动作获得离开状态的奖励,进入目标状态时结束回合,并额外获得目标状态的奖励。目标与最大步数同时达到时,终止原因记为目标。
|
|
172
|
+
|
|
173
|
+
策略中的撞墙动作使智能体留在原地。指向禁用状态的概率会被重新分配;修改禁用状态后,策略会根据原始设置重新计算。
|
|
174
|
+
|
|
164
175
|
#### 动作空间
|
|
165
176
|
|
|
166
177
|
- 当`enable_keep=True`时
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"""SARSA 算法 """
|
|
2
|
-
from
|
|
3
|
-
|
|
2
|
+
from src import GridWorldEnv, QTable
|
|
4
3
|
|
|
5
4
|
# 1. 环境设置
|
|
6
5
|
grid_size = (7, 7)
|
|
@@ -18,7 +17,7 @@ disable_def = []
|
|
|
18
17
|
gamma = 0.8 # 折扣因子
|
|
19
18
|
alpha = 0.05 # 学习率
|
|
20
19
|
epsilon = 0.99 # ε-贪婪策略中的探索率
|
|
21
|
-
min_epsilon = 0.
|
|
20
|
+
min_epsilon = 0.3 # ε的最小值
|
|
22
21
|
decay_rate = 0.99 # ε的衰减率
|
|
23
22
|
num_episodes = 1000 # 训练的回合数
|
|
24
23
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["
|
|
3
|
-
build-backend = "
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "gridworldpy"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.3.0"
|
|
8
8
|
description = "A flexible and interactive grid world environment for reinforcement learning"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = {text = "MIT"}
|
|
@@ -46,9 +46,11 @@ Repository = "https://github.com/hitlic/gridworldpy"
|
|
|
46
46
|
Documentation = "https://github.com/hitlic/gridworldpy#readme"
|
|
47
47
|
"Bug Reports" = "https://github.com/hitlic/gridworldpy/issues"
|
|
48
48
|
|
|
49
|
-
[tool.
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["src"]
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.sdist]
|
|
53
|
+
include = ["/src", "/examples", "/tests", "/README.md", "/LICENSE", "/pyproject.toml"]
|
|
52
54
|
|
|
53
55
|
[tool.black]
|
|
54
56
|
line-length = 88
|
|
@@ -93,6 +93,7 @@ class GridWorldEnv:
|
|
|
93
93
|
|
|
94
94
|
# 初始化策略矩阵 - 每个cell的动作概率分布 [停留,上,下,左,右]
|
|
95
95
|
self.policy = None # 初始化放在 set_policy 方法之中
|
|
96
|
+
self._configured_policy = None
|
|
96
97
|
|
|
97
98
|
# 初始化禁用状态集合
|
|
98
99
|
self.disabled_states = set()
|
|
@@ -187,8 +188,8 @@ class GridWorldEnv:
|
|
|
187
188
|
"""
|
|
188
189
|
action_num = 5 if self.enable_keep else 4
|
|
189
190
|
# 若策略未初始化,则使用均匀分布
|
|
190
|
-
if self.
|
|
191
|
-
self.
|
|
191
|
+
if self._configured_policy is None:
|
|
192
|
+
self._configured_policy = np.ones((self.grid_size[0], self.grid_size[1], action_num)) * 1/action_num
|
|
192
193
|
|
|
193
194
|
if policy_config == 'random':
|
|
194
195
|
# 为每个cell随机生成概率分布
|
|
@@ -196,7 +197,7 @@ class GridWorldEnv:
|
|
|
196
197
|
for j in range(self.grid_size[1]):
|
|
197
198
|
probs = np.random.uniform(0, 1, action_num)
|
|
198
199
|
probs = probs / np.sum(probs) # 归一化
|
|
199
|
-
self.
|
|
200
|
+
self._configured_policy[i, j] = probs
|
|
200
201
|
elif isinstance(policy_config, (list, dict)):
|
|
201
202
|
# 兼容list和dict两种输入格式
|
|
202
203
|
policy_config = policy_config if isinstance(policy_config, list) else policy_config.items()
|
|
@@ -204,10 +205,10 @@ class GridWorldEnv:
|
|
|
204
205
|
assert len(probs) == action_num, f"Policy must have {action_num} values when `eable_keep` is {self.enable_keep}"
|
|
205
206
|
probs = np.array(probs)
|
|
206
207
|
if np.sum(probs) > 0:
|
|
207
|
-
self.
|
|
208
|
+
self._configured_policy[x, y] = probs / np.sum(probs) # 归一化
|
|
208
209
|
else:
|
|
209
210
|
# 所有概率都为0,保持为0
|
|
210
|
-
self.
|
|
211
|
+
self._configured_policy[x, y] = probs
|
|
211
212
|
|
|
212
213
|
# 调整禁用状态的概率分布
|
|
213
214
|
self._adjust_policy_for_disabled_states()
|
|
@@ -233,14 +234,15 @@ class GridWorldEnv:
|
|
|
233
234
|
self.disabled_states.add((x, y))
|
|
234
235
|
|
|
235
236
|
# 调整策略概率分布
|
|
236
|
-
if self.
|
|
237
|
+
if self._configured_policy is not None:
|
|
237
238
|
self._adjust_policy_for_disabled_states()
|
|
238
239
|
|
|
239
240
|
def _adjust_policy_for_disabled_states(self):
|
|
240
241
|
"""调整策略:
|
|
241
|
-
当enable_keep=True
|
|
242
|
-
|
|
242
|
+
当enable_keep=True时,将转移到禁用状态的概率添加到停留动作上;
|
|
243
|
+
否则将概率分配给其余动作。撞墙动作保持不变。
|
|
243
244
|
"""
|
|
245
|
+
self.policy = self._configured_policy.copy()
|
|
244
246
|
for i in range(self.grid_size[0]):
|
|
245
247
|
for j in range(self.grid_size[1]):
|
|
246
248
|
if (i, j) in self.disabled_states:
|
|
@@ -254,7 +256,7 @@ class GridWorldEnv:
|
|
|
254
256
|
|
|
255
257
|
# 检查上方动作 (index 1)
|
|
256
258
|
next_x = i - 1
|
|
257
|
-
if
|
|
259
|
+
if (next_x, j) in self.disabled_states:
|
|
258
260
|
p_id = 1 if self.enable_keep else 0
|
|
259
261
|
blocked_prob += current_policy[p_id]
|
|
260
262
|
current_policy[p_id] = 0
|
|
@@ -262,7 +264,7 @@ class GridWorldEnv:
|
|
|
262
264
|
|
|
263
265
|
# 检查下方动作 (index 2)
|
|
264
266
|
next_x = i + 1
|
|
265
|
-
if
|
|
267
|
+
if (next_x, j) in self.disabled_states:
|
|
266
268
|
p_id = 2 if self.enable_keep else 1
|
|
267
269
|
blocked_prob += current_policy[p_id]
|
|
268
270
|
current_policy[p_id] = 0
|
|
@@ -270,7 +272,7 @@ class GridWorldEnv:
|
|
|
270
272
|
|
|
271
273
|
# 检查左方动作 (index 3)
|
|
272
274
|
next_y = j - 1
|
|
273
|
-
if
|
|
275
|
+
if (i, next_y) in self.disabled_states:
|
|
274
276
|
p_id = 3 if self.enable_keep else 2
|
|
275
277
|
blocked_prob += current_policy[p_id]
|
|
276
278
|
current_policy[p_id] = 0
|
|
@@ -278,7 +280,7 @@ class GridWorldEnv:
|
|
|
278
280
|
|
|
279
281
|
# 检查右方动作 (index 4)
|
|
280
282
|
next_y = j + 1
|
|
281
|
-
if
|
|
283
|
+
if (i, next_y) in self.disabled_states:
|
|
282
284
|
p_id = 4 if self.enable_keep else 3
|
|
283
285
|
blocked_prob += current_policy[p_id]
|
|
284
286
|
current_policy[p_id] = 0
|
|
@@ -337,11 +339,10 @@ class GridWorldEnv:
|
|
|
337
339
|
elif _action == 4: # 右
|
|
338
340
|
new_y = min(y+1, self.grid_size[1]-1)
|
|
339
341
|
|
|
340
|
-
#
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
effective_action = True
|
|
342
|
+
# 进入禁用状态的动作留在原地,奖励为零。
|
|
343
|
+
effective_action = (new_x, new_y) not in self.disabled_states
|
|
344
|
+
if not effective_action:
|
|
345
|
+
new_x, new_y = self.state
|
|
345
346
|
|
|
346
347
|
self.step_count += 1 # 更新步数
|
|
347
348
|
|
|
@@ -354,14 +355,17 @@ class GridWorldEnv:
|
|
|
354
355
|
|
|
355
356
|
# 离开当前状态得到的奖励
|
|
356
357
|
received_reward = self.rewards[self.state] if self.rewards is not None else None
|
|
358
|
+
if not effective_action:
|
|
359
|
+
received_reward = 0.0
|
|
357
360
|
|
|
358
361
|
# 判断是否终止
|
|
359
362
|
done = False
|
|
360
|
-
if self.goal_pos is not None:
|
|
363
|
+
if effective_action and self.goal_pos is not None and self.next_state == self.goal_pos:
|
|
361
364
|
# 基于目标位置的终止
|
|
362
|
-
done =
|
|
363
|
-
|
|
364
|
-
|
|
365
|
+
done = True
|
|
366
|
+
info['exit_reason'] = 'done'
|
|
367
|
+
if self.rewards is not None and self.state != self.goal_pos:
|
|
368
|
+
received_reward += self.rewards[self.goal_pos]
|
|
365
369
|
elif self.max_steps is not None:
|
|
366
370
|
# 基于最大步数的终止
|
|
367
371
|
done = self.step_count >= self.max_steps
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from src import GridWorldEnv, policy_to_transition_matrix
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class GridWorldTests(unittest.TestCase):
|
|
9
|
+
def make_env(self, **kwargs):
|
|
10
|
+
return GridWorldEnv(grid_size=(2, 2), render_mode=None, **kwargs)
|
|
11
|
+
|
|
12
|
+
def test_reaching_goal_returns_rewards_and_ends_episode(self):
|
|
13
|
+
env = self.make_env(target_state=(0, 1), max_steps=1)
|
|
14
|
+
env.set_rewards({(0, 0): -0.1, (0, 1): 1.0})
|
|
15
|
+
state, reward, done, effective, info = env.step(3)
|
|
16
|
+
self.assertEqual(state, (0, 1))
|
|
17
|
+
self.assertAlmostEqual(reward, 0.9)
|
|
18
|
+
self.assertTrue(done)
|
|
19
|
+
self.assertTrue(effective)
|
|
20
|
+
self.assertEqual(info['exit_reason'], 'done')
|
|
21
|
+
self.assertEqual(info['state'], (0, 0))
|
|
22
|
+
|
|
23
|
+
def test_step_limit_with_goal(self):
|
|
24
|
+
env = self.make_env(target_state=(1, 1), max_steps=2)
|
|
25
|
+
self.assertFalse(env.step(0)[2])
|
|
26
|
+
state, _, done, _, info = env.step(0)
|
|
27
|
+
self.assertEqual(state, (0, 0))
|
|
28
|
+
self.assertTrue(done)
|
|
29
|
+
self.assertEqual(info['exit_reason'], 'max_steps')
|
|
30
|
+
|
|
31
|
+
def test_blocked_actions_count_towards_limit(self):
|
|
32
|
+
env = self.make_env(max_steps=2)
|
|
33
|
+
env.set_disable_states([(0, 1)])
|
|
34
|
+
self.assertEqual(env.step(3)[:4], ((0, 0), 0.0, False, False))
|
|
35
|
+
state, reward, done, effective, info = env.step(3)
|
|
36
|
+
self.assertEqual((state, reward, done, effective), ((0, 0), 0.0, True, False))
|
|
37
|
+
self.assertEqual(info['step_count'], 2)
|
|
38
|
+
self.assertEqual(info['exit_reason'], 'max_steps')
|
|
39
|
+
|
|
40
|
+
def test_wall_action_remains_in_policy_and_transition_matrix(self):
|
|
41
|
+
for enable_keep in (False, True):
|
|
42
|
+
with self.subTest(enable_keep=enable_keep):
|
|
43
|
+
env = self.make_env(enable_keep=enable_keep)
|
|
44
|
+
up = 1 if enable_keep else 0
|
|
45
|
+
probs = [0] * env.action_num
|
|
46
|
+
probs[up] = 1
|
|
47
|
+
env.set_policy({(0, 0): probs})
|
|
48
|
+
np.testing.assert_array_equal(env.get_current_policy(), probs)
|
|
49
|
+
matrix = policy_to_transition_matrix(env.get_policy(), env.grid_size)
|
|
50
|
+
self.assertEqual(matrix[0, 0], 1)
|
|
51
|
+
self.assertEqual(env.step(up)[0], (0, 0))
|
|
52
|
+
|
|
53
|
+
def test_policy_restored_after_obstacle_changes(self):
|
|
54
|
+
for enable_keep in (False, True):
|
|
55
|
+
with self.subTest(enable_keep=enable_keep):
|
|
56
|
+
env = self.make_env(enable_keep=enable_keep)
|
|
57
|
+
right = 4 if enable_keep else 3
|
|
58
|
+
probs = [0] * env.action_num
|
|
59
|
+
probs[right] = 1
|
|
60
|
+
env.set_policy({(0, 0): probs, (0, 1): probs})
|
|
61
|
+
env.set_disable_states([(0, 1)])
|
|
62
|
+
self.assertEqual(env.get_current_policy()[right], 0)
|
|
63
|
+
env.set_policy({(1, 0): probs})
|
|
64
|
+
env.set_disable_states([])
|
|
65
|
+
policy = dict(env.get_policy())
|
|
66
|
+
self.assertEqual(policy[(0, 0)], probs)
|
|
67
|
+
self.assertEqual(policy[(0, 1)], probs)
|
|
68
|
+
self.assertEqual(policy[(1, 0)], probs)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if __name__ == '__main__':
|
|
72
|
+
unittest.main()
|
gridworldpy-0.2.0/MANIFEST.in
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# Include important files in the source distribution
|
|
2
|
-
include README.md
|
|
3
|
-
include LICENSE
|
|
4
|
-
include PUBLISHING_GUIDE.md
|
|
5
|
-
include pyproject.toml
|
|
6
|
-
|
|
7
|
-
# Include examples
|
|
8
|
-
recursive-include examples *.py
|
|
9
|
-
|
|
10
|
-
# Exclude compiled files and cache
|
|
11
|
-
global-exclude *.pyc
|
|
12
|
-
global-exclude __pycache__
|
|
13
|
-
global-exclude .DS_Store
|
|
14
|
-
global-exclude *.egg-info
|
|
15
|
-
|
|
16
|
-
# Exclude test and development files
|
|
17
|
-
exclude .gitignore
|
|
18
|
-
exclude *.log
|
|
19
|
-
exclude test_*
|