gridworldpy 0.2.0__tar.gz → 0.4.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.4.0/.gitignore +100 -0
- {gridworldpy-0.2.0/gridworldpy.egg-info → gridworldpy-0.4.0}/PKG-INFO +23 -12
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/README.md +13 -2
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/examples/q_learning.py +0 -1
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/examples/sarsa.py +1 -2
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/pyproject.toml +8 -6
- {gridworldpy-0.2.0/examples → gridworldpy-0.4.0/src}/gridworldpy/__init__.py +1 -1
- {gridworldpy-0.2.0/examples → gridworldpy-0.4.0/src}/gridworldpy/gridworld.py +31 -25
- {gridworldpy-0.2.0/examples → gridworldpy-0.4.0/src}/gridworldpy/q_table.py +1 -1
- gridworldpy-0.4.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.4.0}/LICENSE +0 -0
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/examples/basic_usage.py +0 -0
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/examples/policy_evaluation.py +0 -0
- {gridworldpy-0.2.0 → gridworldpy-0.4.0}/examples/value_iteration.py +0 -0
- {gridworldpy-0.2.0/examples → gridworldpy-0.4.0/src}/gridworldpy/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.4.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,17 +25,23 @@ 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
|
|
|
35
35
|
[](https://badge.fury.io/py/gridworldpy)
|
|
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
|
+
安装后使用 `from gridworldpy import GridWorldEnv` 导入。
|
|
64
|
+
|
|
58
65
|
或者从源码安装:
|
|
59
66
|
|
|
60
67
|
```bash
|
|
@@ -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`时
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
# GridWorldPy
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/py/gridworldpy)
|
|
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
|
+
安装后使用 `from gridworldpy import GridWorldEnv` 导入。
|
|
32
|
+
|
|
26
33
|
或者从源码安装:
|
|
27
34
|
|
|
28
35
|
```bash
|
|
@@ -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,7 +1,6 @@
|
|
|
1
1
|
"""SARSA 算法 """
|
|
2
2
|
from gridworldpy import GridWorldEnv, QTable
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
# 1. 环境设置
|
|
6
5
|
grid_size = (7, 7)
|
|
7
6
|
# 奖励设置
|
|
@@ -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.4.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/gridworldpy"]
|
|
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
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import time
|
|
1
2
|
import tkinter as tk
|
|
3
|
+
|
|
2
4
|
import numpy as np
|
|
3
|
-
|
|
5
|
+
|
|
4
6
|
from .utils import *
|
|
5
7
|
|
|
6
8
|
|
|
@@ -12,7 +14,7 @@ class GridWorldEnv:
|
|
|
12
14
|
|
|
13
15
|
def __init__(self, grid_size=(5, 5), enable_keep=False, start_state=(0, 0), target_state=None,
|
|
14
16
|
render_mode="human", keyboard_control=False, max_steps=100000000,
|
|
15
|
-
cell_size=135, circle_radius=35, font_size=
|
|
17
|
+
cell_size=135, circle_radius=35, font_size=14, max_arrow_length=30, color_alpha=0.0,
|
|
16
18
|
show_cell_pos=False, show_step_num=False):
|
|
17
19
|
"""
|
|
18
20
|
Args:
|
|
@@ -93,6 +95,7 @@ class GridWorldEnv:
|
|
|
93
95
|
|
|
94
96
|
# 初始化策略矩阵 - 每个cell的动作概率分布 [停留,上,下,左,右]
|
|
95
97
|
self.policy = None # 初始化放在 set_policy 方法之中
|
|
98
|
+
self._configured_policy = None
|
|
96
99
|
|
|
97
100
|
# 初始化禁用状态集合
|
|
98
101
|
self.disabled_states = set()
|
|
@@ -187,8 +190,8 @@ class GridWorldEnv:
|
|
|
187
190
|
"""
|
|
188
191
|
action_num = 5 if self.enable_keep else 4
|
|
189
192
|
# 若策略未初始化,则使用均匀分布
|
|
190
|
-
if self.
|
|
191
|
-
self.
|
|
193
|
+
if self._configured_policy is None:
|
|
194
|
+
self._configured_policy = np.ones((self.grid_size[0], self.grid_size[1], action_num)) * 1/action_num
|
|
192
195
|
|
|
193
196
|
if policy_config == 'random':
|
|
194
197
|
# 为每个cell随机生成概率分布
|
|
@@ -196,7 +199,7 @@ class GridWorldEnv:
|
|
|
196
199
|
for j in range(self.grid_size[1]):
|
|
197
200
|
probs = np.random.uniform(0, 1, action_num)
|
|
198
201
|
probs = probs / np.sum(probs) # 归一化
|
|
199
|
-
self.
|
|
202
|
+
self._configured_policy[i, j] = probs
|
|
200
203
|
elif isinstance(policy_config, (list, dict)):
|
|
201
204
|
# 兼容list和dict两种输入格式
|
|
202
205
|
policy_config = policy_config if isinstance(policy_config, list) else policy_config.items()
|
|
@@ -204,10 +207,10 @@ class GridWorldEnv:
|
|
|
204
207
|
assert len(probs) == action_num, f"Policy must have {action_num} values when `eable_keep` is {self.enable_keep}"
|
|
205
208
|
probs = np.array(probs)
|
|
206
209
|
if np.sum(probs) > 0:
|
|
207
|
-
self.
|
|
210
|
+
self._configured_policy[x, y] = probs / np.sum(probs) # 归一化
|
|
208
211
|
else:
|
|
209
212
|
# 所有概率都为0,保持为0
|
|
210
|
-
self.
|
|
213
|
+
self._configured_policy[x, y] = probs
|
|
211
214
|
|
|
212
215
|
# 调整禁用状态的概率分布
|
|
213
216
|
self._adjust_policy_for_disabled_states()
|
|
@@ -233,14 +236,15 @@ class GridWorldEnv:
|
|
|
233
236
|
self.disabled_states.add((x, y))
|
|
234
237
|
|
|
235
238
|
# 调整策略概率分布
|
|
236
|
-
if self.
|
|
239
|
+
if self._configured_policy is not None:
|
|
237
240
|
self._adjust_policy_for_disabled_states()
|
|
238
241
|
|
|
239
242
|
def _adjust_policy_for_disabled_states(self):
|
|
240
243
|
"""调整策略:
|
|
241
|
-
当enable_keep=True
|
|
242
|
-
|
|
244
|
+
当enable_keep=True时,将转移到禁用状态的概率添加到停留动作上;
|
|
245
|
+
否则将概率分配给其余动作。撞墙动作保持不变。
|
|
243
246
|
"""
|
|
247
|
+
self.policy = self._configured_policy.copy()
|
|
244
248
|
for i in range(self.grid_size[0]):
|
|
245
249
|
for j in range(self.grid_size[1]):
|
|
246
250
|
if (i, j) in self.disabled_states:
|
|
@@ -254,7 +258,7 @@ class GridWorldEnv:
|
|
|
254
258
|
|
|
255
259
|
# 检查上方动作 (index 1)
|
|
256
260
|
next_x = i - 1
|
|
257
|
-
if
|
|
261
|
+
if (next_x, j) in self.disabled_states:
|
|
258
262
|
p_id = 1 if self.enable_keep else 0
|
|
259
263
|
blocked_prob += current_policy[p_id]
|
|
260
264
|
current_policy[p_id] = 0
|
|
@@ -262,7 +266,7 @@ class GridWorldEnv:
|
|
|
262
266
|
|
|
263
267
|
# 检查下方动作 (index 2)
|
|
264
268
|
next_x = i + 1
|
|
265
|
-
if
|
|
269
|
+
if (next_x, j) in self.disabled_states:
|
|
266
270
|
p_id = 2 if self.enable_keep else 1
|
|
267
271
|
blocked_prob += current_policy[p_id]
|
|
268
272
|
current_policy[p_id] = 0
|
|
@@ -270,7 +274,7 @@ class GridWorldEnv:
|
|
|
270
274
|
|
|
271
275
|
# 检查左方动作 (index 3)
|
|
272
276
|
next_y = j - 1
|
|
273
|
-
if
|
|
277
|
+
if (i, next_y) in self.disabled_states:
|
|
274
278
|
p_id = 3 if self.enable_keep else 2
|
|
275
279
|
blocked_prob += current_policy[p_id]
|
|
276
280
|
current_policy[p_id] = 0
|
|
@@ -278,7 +282,7 @@ class GridWorldEnv:
|
|
|
278
282
|
|
|
279
283
|
# 检查右方动作 (index 4)
|
|
280
284
|
next_y = j + 1
|
|
281
|
-
if
|
|
285
|
+
if (i, next_y) in self.disabled_states:
|
|
282
286
|
p_id = 4 if self.enable_keep else 3
|
|
283
287
|
blocked_prob += current_policy[p_id]
|
|
284
288
|
current_policy[p_id] = 0
|
|
@@ -337,11 +341,10 @@ class GridWorldEnv:
|
|
|
337
341
|
elif _action == 4: # 右
|
|
338
342
|
new_y = min(y+1, self.grid_size[1]-1)
|
|
339
343
|
|
|
340
|
-
#
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
effective_action = True
|
|
344
|
+
# 进入禁用状态的动作留在原地,奖励为零。
|
|
345
|
+
effective_action = (new_x, new_y) not in self.disabled_states
|
|
346
|
+
if not effective_action:
|
|
347
|
+
new_x, new_y = self.state
|
|
345
348
|
|
|
346
349
|
self.step_count += 1 # 更新步数
|
|
347
350
|
|
|
@@ -354,14 +357,17 @@ class GridWorldEnv:
|
|
|
354
357
|
|
|
355
358
|
# 离开当前状态得到的奖励
|
|
356
359
|
received_reward = self.rewards[self.state] if self.rewards is not None else None
|
|
360
|
+
if not effective_action:
|
|
361
|
+
received_reward = 0.0
|
|
357
362
|
|
|
358
363
|
# 判断是否终止
|
|
359
364
|
done = False
|
|
360
|
-
if self.goal_pos is not None:
|
|
365
|
+
if effective_action and self.goal_pos is not None and self.next_state == self.goal_pos:
|
|
361
366
|
# 基于目标位置的终止
|
|
362
|
-
done =
|
|
363
|
-
|
|
364
|
-
|
|
367
|
+
done = True
|
|
368
|
+
info['exit_reason'] = 'done'
|
|
369
|
+
if self.rewards is not None and self.state != self.goal_pos:
|
|
370
|
+
received_reward += self.rewards[self.goal_pos]
|
|
365
371
|
elif self.max_steps is not None:
|
|
366
372
|
# 基于最大步数的终止
|
|
367
373
|
done = self.step_count >= self.max_steps
|
|
@@ -627,13 +633,13 @@ class GridWorldEnv:
|
|
|
627
633
|
|
|
628
634
|
# 第一行(奖励)
|
|
629
635
|
self.canvas.create_text(
|
|
630
|
-
center_x, center_y - font_size//2,
|
|
636
|
+
center_x, center_y - font_size//2 - 2,
|
|
631
637
|
text=reward_text, fill=text_color, font=('Arial', font_size)
|
|
632
638
|
)
|
|
633
639
|
|
|
634
640
|
# 第二行(状态价值)
|
|
635
641
|
self.canvas.create_text(
|
|
636
|
-
center_x, center_y + font_size//2,
|
|
642
|
+
center_x, center_y + font_size//2 + 2,
|
|
637
643
|
text=value_text, fill=text_color, font=('Arial', font_size)
|
|
638
644
|
)
|
|
639
645
|
else:
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from gridworldpy 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_*
|