gridworldpy 0.1.2__tar.gz → 0.2.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.
Files changed (26) hide show
  1. {gridworldpy-0.1.2/gridworldpy.egg-info → gridworldpy-0.2.0}/PKG-INFO +25 -12
  2. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/README.md +24 -11
  3. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/gridworldpy/__init__.py +1 -1
  4. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/gridworldpy/gridworld.py +5 -4
  5. {gridworldpy-0.1.2 → gridworldpy-0.2.0/examples}/gridworldpy/q_table.py +4 -0
  6. gridworldpy-0.2.0/examples/q_learning.py +84 -0
  7. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/sarsa.py +15 -18
  8. gridworldpy-0.2.0/examples/test.py +32 -0
  9. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy/__init__.py +1 -1
  10. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy/gridworld.py +5 -4
  11. {gridworldpy-0.1.2/examples → gridworldpy-0.2.0}/gridworldpy/q_table.py +4 -0
  12. {gridworldpy-0.1.2 → gridworldpy-0.2.0/gridworldpy.egg-info}/PKG-INFO +25 -12
  13. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy.egg-info/SOURCES.txt +2 -0
  14. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/pyproject.toml +1 -1
  15. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/LICENSE +0 -0
  16. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/MANIFEST.in +0 -0
  17. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/basic_usage.py +0 -0
  18. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/gridworldpy/utils.py +0 -0
  19. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/policy_evaluation.py +0 -0
  20. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/examples/value_iteration.py +0 -0
  21. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy/utils.py +0 -0
  22. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy.egg-info/dependency_links.txt +0 -0
  23. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy.egg-info/requires.txt +0 -0
  24. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/gridworldpy.egg-info/top_level.txt +0 -0
  25. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/setup.cfg +0 -0
  26. {gridworldpy-0.1.2 → gridworldpy-0.2.0}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gridworldpy
3
- Version: 0.1.2
3
+ Version: 0.2.0
4
4
  Summary: A flexible and interactive grid world environment for reinforcement learning
5
5
  Home-page: https://github.com/hitlic/gridworldpy
6
6
  Author: LIC
@@ -92,7 +92,8 @@ from gridworldpy import GridWorldEnv
92
92
  env = GridWorldEnv(
93
93
  grid_size=(4, 4),
94
94
  keyboard_control=True, # 启用键盘控制
95
- terminal_condition=(3, 3), # 目标位置为(3,3)
95
+ enable_keep= True, # 启用停留动作
96
+ target_state=(3, 3), # 目标位置为(3,3)
96
97
  cell_size=150, # 每个格子150像素
97
98
  circle_radius=40, # 奖励圆圈半径40像素
98
99
  )
@@ -113,7 +114,7 @@ policy = [
113
114
  env.set_policy(policy)
114
115
 
115
116
  # 禁用某些状态(创建障碍物)
116
- env.disable_states([(1, 2), (2, 1)])
117
+ env.set_disable_states([(1, 2), (2, 1)])
117
118
 
118
119
  # 渲染环境
119
120
  env.render()
@@ -167,34 +168,44 @@ for step in range(10):
167
168
  ```python
168
169
  GridWorldEnv(
169
170
  grid_size=(5, 5), # 网格大小 (行, 列)
171
+ enable_keep=False, # 是否允许停留当前状态
172
+ start_state=(0, 0), # 初始状态
170
173
  render_mode="human", # 渲染模式
171
174
  keyboard_control=True, # 是否启用键盘控制
172
- terminal_condition=None, # 终止条件
175
+ max_steps=100000, # 最大步数
173
176
  cell_size=130, # 每个格子的像素大小
174
177
  circle_radius=35, # 奖励圆圈半径
175
178
  font_size=16, # 字体大小
176
179
  max_arrow_length=50, # 策略箭头最大长度
180
+ color_alpha=0.0, # 取值[0, 1),显示颜色对数值的灵敏度
177
181
  show_cell_pos=True, # 在每个单元上显示位置坐标
178
- color_alpha=0.0 # 控制色彩敏感度
182
+ show_step_num=False # 是否显示步数
179
183
  )
180
184
  ```
181
185
 
182
186
  #### 主要方法
183
187
 
184
- - `step(action)`: 执行动作,返回(observation, reward, done, info)
188
+ - `step(action)`: 执行动作,返回 `(next_state, reward, done, valid_action, info)`
189
+ - `reset()`:重置环境,返回初始状态
185
190
  - `render(state_values=None, policy_config=None, reward_config=None)`: 渲染环境
186
191
  - `set_rewards(reward_config)`: 设置奖励配置
187
192
  - `set_policy(policy_config)`: 设置策略配置
188
- - `disable_states(disabled_poses)`: 禁用指定状态
193
+ - `set_disable_states(disabled_poses)`: 禁用指定状态
189
194
  - `close()`: 关闭环境
190
195
 
191
196
  #### 动作空间
192
197
 
193
- - 0: 停留在当前位置
194
- - 1: 向上移动
195
- - 2: 向下移动
196
- - 3: 向左移动
197
- - 4: 向右移动
198
+ - 当`enable_keep=True`时
199
+ - 0: 停留在当前位置
200
+ - 1: 向上移动
201
+ - 2: 向下移动
202
+ - 3: 向左移动
203
+ - 4: 向右移动
204
+ - 当`enable_keep=False`时
205
+ - 0: 向上移动
206
+ - 1: 向下移动
207
+ - 2: 向左移动
208
+ - 3: 向右移动
198
209
 
199
210
  ## 🎨 可视化说明
200
211
 
@@ -221,6 +232,8 @@ GridWorldEnv(
221
232
  - `basic_usage.py`:基本使用示例
222
233
  - `policy_evaluation.py`:蒙特卡罗法策略评估
223
234
  - `value_iteration.py`:价值迭代寻找最优策略
235
+ - `sarsa.py`:SARSA算法实现
236
+ - `q_learning.py`:Q-Learning算法实现
224
237
 
225
238
  ## 📄 许可
226
239
 
@@ -60,7 +60,8 @@ from gridworldpy import GridWorldEnv
60
60
  env = GridWorldEnv(
61
61
  grid_size=(4, 4),
62
62
  keyboard_control=True, # 启用键盘控制
63
- terminal_condition=(3, 3), # 目标位置为(3,3)
63
+ enable_keep= True, # 启用停留动作
64
+ target_state=(3, 3), # 目标位置为(3,3)
64
65
  cell_size=150, # 每个格子150像素
65
66
  circle_radius=40, # 奖励圆圈半径40像素
66
67
  )
@@ -81,7 +82,7 @@ policy = [
81
82
  env.set_policy(policy)
82
83
 
83
84
  # 禁用某些状态(创建障碍物)
84
- env.disable_states([(1, 2), (2, 1)])
85
+ env.set_disable_states([(1, 2), (2, 1)])
85
86
 
86
87
  # 渲染环境
87
88
  env.render()
@@ -135,34 +136,44 @@ for step in range(10):
135
136
  ```python
136
137
  GridWorldEnv(
137
138
  grid_size=(5, 5), # 网格大小 (行, 列)
139
+ enable_keep=False, # 是否允许停留当前状态
140
+ start_state=(0, 0), # 初始状态
138
141
  render_mode="human", # 渲染模式
139
142
  keyboard_control=True, # 是否启用键盘控制
140
- terminal_condition=None, # 终止条件
143
+ max_steps=100000, # 最大步数
141
144
  cell_size=130, # 每个格子的像素大小
142
145
  circle_radius=35, # 奖励圆圈半径
143
146
  font_size=16, # 字体大小
144
147
  max_arrow_length=50, # 策略箭头最大长度
148
+ color_alpha=0.0, # 取值[0, 1),显示颜色对数值的灵敏度
145
149
  show_cell_pos=True, # 在每个单元上显示位置坐标
146
- color_alpha=0.0 # 控制色彩敏感度
150
+ show_step_num=False # 是否显示步数
147
151
  )
148
152
  ```
149
153
 
150
154
  #### 主要方法
151
155
 
152
- - `step(action)`: 执行动作,返回(observation, reward, done, info)
156
+ - `step(action)`: 执行动作,返回 `(next_state, reward, done, valid_action, info)`
157
+ - `reset()`:重置环境,返回初始状态
153
158
  - `render(state_values=None, policy_config=None, reward_config=None)`: 渲染环境
154
159
  - `set_rewards(reward_config)`: 设置奖励配置
155
160
  - `set_policy(policy_config)`: 设置策略配置
156
- - `disable_states(disabled_poses)`: 禁用指定状态
161
+ - `set_disable_states(disabled_poses)`: 禁用指定状态
157
162
  - `close()`: 关闭环境
158
163
 
159
164
  #### 动作空间
160
165
 
161
- - 0: 停留在当前位置
162
- - 1: 向上移动
163
- - 2: 向下移动
164
- - 3: 向左移动
165
- - 4: 向右移动
166
+ - 当`enable_keep=True`时
167
+ - 0: 停留在当前位置
168
+ - 1: 向上移动
169
+ - 2: 向下移动
170
+ - 3: 向左移动
171
+ - 4: 向右移动
172
+ - 当`enable_keep=False`时
173
+ - 0: 向上移动
174
+ - 1: 向下移动
175
+ - 2: 向左移动
176
+ - 3: 向右移动
166
177
 
167
178
  ## 🎨 可视化说明
168
179
 
@@ -189,6 +200,8 @@ GridWorldEnv(
189
200
  - `basic_usage.py`:基本使用示例
190
201
  - `policy_evaluation.py`:蒙特卡罗法策略评估
191
202
  - `value_iteration.py`:价值迭代寻找最优策略
203
+ - `sarsa.py`:SARSA算法实现
204
+ - `q_learning.py`:Q-Learning算法实现
192
205
 
193
206
  ## 📄 许可
194
207
 
@@ -9,7 +9,7 @@ from .gridworld import GridWorldEnv
9
9
  from .utils import policy_to_transition_matrix, matrix_to_rewards
10
10
  from .q_table import QTable
11
11
 
12
- __version__ = "0.1.2"
12
+ __version__ = "0.2.0"
13
13
  __author__ = "LIC"
14
14
  __email__ = "liuchen.lic@gmail.com"
15
15
 
@@ -10,8 +10,8 @@ class GridWorldEnv:
10
10
  使用tkinter进行可视化
11
11
  """
12
12
 
13
- def __init__(self, grid_size=(5, 5), enable_keep=False, start_state=(0, 0), render_mode="human",
14
- keyboard_control=False, target_state=None, max_steps=100000000,
13
+ def __init__(self, grid_size=(5, 5), enable_keep=False, start_state=(0, 0), target_state=None,
14
+ render_mode="human", keyboard_control=False, max_steps=100000000,
15
15
  cell_size=135, circle_radius=35, font_size=16, max_arrow_length=30, color_alpha=0.0,
16
16
  show_cell_pos=False, show_step_num=False):
17
17
  """
@@ -19,9 +19,9 @@ class GridWorldEnv:
19
19
  grid_size: (rows, cols) 网格大小
20
20
  enable_keep: 是否允许停留在当前状态,True表示5状态动作,False表示4状态动作
21
21
  start_state: (x, y) 初始状态位置
22
+ target_state: 目标位置坐标 (x, y),如果为 None 则表示无目标位置
22
23
  render_mode: 渲染模式,"human"表示可视化
23
24
  keyboard_control: 是否启用键盘控制
24
- target_state: 目标位置坐标 (x, y),如果为 None 则表示无目标位置
25
25
  max_steps: 最大步数限制
26
26
  cell_size: 每个单元格的像素尺寸
27
27
  circle_radius: 显示奖励/价值的圆半径
@@ -620,7 +620,8 @@ class GridWorldEnv:
620
620
 
621
621
  if state_value is not None:
622
622
  # 显示两行:奖励和状态价值
623
- num_width = max(len(f"{reward_val:.2f}"), len(f"{state_value:.2f}"))
623
+
624
+ num_width = max(len(f"{0 if reward_val is None else reward_val:.2f}"), len(f"{state_value:.2f}"))
624
625
  reward_text = '' if reward_val is None else f"R: {reward_val:{num_width}.2f}"
625
626
  value_text = f"V: {state_value:{num_width}.2f}"
626
627
 
@@ -40,6 +40,10 @@ class QTable:
40
40
  return state, policy
41
41
  return [f(idx) for idx in range(len(self.table))]
42
42
 
43
+ def best_q_value(self, state):
44
+ idx = self.state2idx(state)
45
+ return np.max(self.table[idx])
46
+
43
47
  def _id2policy(self, action_idx):
44
48
  p = [0, 0, 0, 0, 0] if self.enable_keep else [0, 0, 0, 0]
45
49
  p[action_idx] = 1
@@ -0,0 +1,84 @@
1
+ """Q-learning 算法实现
2
+
3
+ Q-learning 是一种异策略(off-policy)算法。
4
+ 它的更新规则不依赖于下一个实际执行的动作,而是直接使用下一状态能带来的最大化收益来更新 Q 值。
5
+
6
+ Q-learning 更新规则:
7
+ Q(s, a) <- Q(s, a) + alpha * (reward + gamma * max_{a'} Q(s', a') - Q(s, a))
8
+ """
9
+ from gridworldpy import GridWorldEnv, QTable
10
+
11
+
12
+ # 1. 环境设置
13
+ grid_size = (7, 7)
14
+ # 奖励设置
15
+ rewards_def = [
16
+ ((6, 6), 1),
17
+ ((4, 0), -1),
18
+ ((0, 4), -1)
19
+ ]
20
+
21
+ disable_def = [(2, 2), (3, 2), (4, 2), (2, 3), (2, 4)]
22
+
23
+ # 2. 超参数设置
24
+ gamma = 0.8 # 折扣因子
25
+ alpha = 0.05 # 学习率
26
+ epsilon = 0.99 # ε-贪婪策略中的探索率
27
+ min_epsilon = 0.15 # ε的最小值
28
+ decay_rate = 0.99 # ε的衰减率
29
+ num_episodes = 1000 # 训练的回合数
30
+
31
+ # 3. 初始化环境与 Q-table
32
+ env = GridWorldEnv(grid_size=grid_size, enable_keep=False, start_state=(0, 0), target_state=(6, 6),
33
+ cell_size=80, show_cell_pos=True, max_arrow_length=15, circle_radius=20, font_size=10,
34
+ max_steps=500, show_step_num=True,
35
+ )
36
+
37
+
38
+ q_table = QTable(grid_size, enable_keep=env.enable_keep)
39
+
40
+
41
+ # 4. Q-learning 算法主循环
42
+ for episode in range(num_episodes):
43
+ print(f"第 {episode + 1} 回合, epsilon: {epsilon:.4}")
44
+ state = env.reset()
45
+ env.set_rewards(rewards_def)
46
+ env.set_disable_states(disable_def)
47
+
48
+ while True:
49
+ # 根据当前状态选择动作(ε-贪婪策略)
50
+ action = q_table.epsilon_greedy_action(state, epsilon)
51
+
52
+ # 执行动作,获取新状态和奖励
53
+ next_state, reward, done, _, info = env.step(action)
54
+ reward = reward if reward is not None else 0.0
55
+
56
+ # Q-learning 更新规则
57
+ current_q = q_table.get(state, action)
58
+ # 核心步骤:使用下一状态的最大Q值,而不是下一个实际动作的Q值
59
+ next_max_q = q_table.best_q_value(next_state) if not done else 0.0
60
+ td_target = reward + gamma * next_max_q
61
+ td_error = td_target - current_q
62
+ q_table.update(state, action, current_q + alpha * td_error)
63
+
64
+ # 如果到达终止状态,则结束当前回合
65
+ if done:
66
+ break
67
+
68
+ # 更新状态
69
+ state = next_state
70
+
71
+ optimal_policy = q_table.best_policy()
72
+ env.set_policy(optimal_policy)
73
+ env.render(q_table.best_state_values())
74
+
75
+ # 更新 ε
76
+ epsilon = max(min_epsilon, epsilon * decay_rate)
77
+
78
+
79
+ optimal_policy = q_table.best_policy()
80
+ env.set_policy(optimal_policy)
81
+ env.render(q_table.best_state_values())
82
+
83
+ input("按回车键退出...")
84
+ env.close()
@@ -1,4 +1,4 @@
1
- """SARSA 算法"""
1
+ """SARSA 算法 """
2
2
  from gridworldpy import GridWorldEnv, QTable
3
3
 
4
4
 
@@ -6,25 +6,26 @@ from gridworldpy import GridWorldEnv, QTable
6
6
  grid_size = (7, 7)
7
7
  # 奖励设置
8
8
  rewards_def = [
9
- ((3, 5), 1),
10
- # ((4, 0), -1),
11
- # ((0, 4), -1)
9
+ ((6, 6), 1),
10
+ ((4, 0), -1),
11
+ ((0, 4), -1)
12
12
  ]
13
13
 
14
- disable_def = [(3, 1), (3, 2), (1, 4), (2, 4), (3, 4), (4, 4)]
14
+ disable_def = []
15
+ # disable_def = [(2, 3), (2, 4)]
15
16
 
16
17
  # 2. 超参数设置
17
- gamma = 0.8 # 折扣因子
18
- alpha = 0.05 # 学习率
19
- epsilon = 0.99 # ε-贪婪策略中的探索率
20
- min_epsilon = 0.15 # ε的最小值
21
- decay_rate = 0.99 # ε的衰减率
18
+ gamma = 0.8 # 折扣因子
19
+ alpha = 0.05 # 学习率
20
+ epsilon = 0.99 # ε-贪婪策略中的探索率
21
+ min_epsilon = 0.15 # ε的最小值
22
+ decay_rate = 0.99 # ε的衰减率
22
23
  num_episodes = 1000 # 训练的回合数
23
24
 
24
25
  # 3. 初始化环境与 Q-table
25
- env = GridWorldEnv(grid_size=grid_size, enable_keep=False, start_state=(0, 0), target_state=(3, 5),
26
+ env = GridWorldEnv(grid_size=grid_size, enable_keep=False, start_state=(0, 0), target_state=(6, 6),
26
27
  cell_size=80, show_cell_pos=True, max_arrow_length=15, circle_radius=20, font_size=10,
27
- max_steps=500, color_alpha=0.6, show_step_num=True,
28
+ max_steps=500, show_step_num=True,
28
29
  )
29
30
 
30
31
  q_table = QTable(grid_size, enable_keep=env.enable_keep)
@@ -45,12 +46,8 @@ for episode in range(num_episodes):
45
46
  next_state, reward, done, _, info = env.step(action)
46
47
  reward = reward if reward is not None else 0.0
47
48
  # 在新状态下,根据策略选择下一个动作
48
- if done:
49
- next_action = None
50
- next_q = 0.0
51
- else:
52
- next_action = q_table.epsilon_greedy_action(next_state, epsilon)
53
- next_q = q_table.get(next_state, next_action)
49
+ next_action = q_table.epsilon_greedy_action(next_state, epsilon) if not done else None
50
+ next_q = q_table.get(next_state, next_action) if not done else 0.0
54
51
 
55
52
  # SARSA 更新规则
56
53
  current_q = q_table.get(state, action)
@@ -0,0 +1,32 @@
1
+ from gridworldpy import GridWorldEnv
2
+
3
+ # 创建带自定义配置的环境
4
+ env = GridWorldEnv(
5
+ grid_size=(4, 4),
6
+ keyboard_control=True, # 启用键盘控制
7
+ enable_keep= True, # 启用停留动作
8
+ target_state=(3, 3), # 目标位置为(3,3)
9
+ cell_size=150, # 每个格子150像素
10
+ circle_radius=40, # 奖励圆圈半径40像素
11
+ )
12
+
13
+ # 设置特定位置的奖励
14
+ rewards = [
15
+ ((0, 0), -0.1), # 起始位置小负奖励
16
+ ((1, 1), -1.0), # 陷阱:大负奖励
17
+ ((3, 3), 1.0), # 目标:大正奖励
18
+ ]
19
+ env.set_rewards(rewards)
20
+
21
+ # 设置特定的策略
22
+ policy = [
23
+ ((0, 0), [0.1, 0.2, 0.2, 0.2, 0.3]), # 位置(0,0)的动作概率
24
+ ((1, 0), [0.0, 0.0, 0.5, 0.0, 0.5]), # 位置(1,0)的动作概率
25
+ ]
26
+ env.set_policy(policy)
27
+
28
+ # 禁用某些状态(创建障碍物)
29
+ env.set_disable_states([(1, 2), (2, 1)])
30
+
31
+ # 渲染环境
32
+ env.render()
@@ -9,7 +9,7 @@ from .gridworld import GridWorldEnv
9
9
  from .utils import policy_to_transition_matrix, matrix_to_rewards
10
10
  from .q_table import QTable
11
11
 
12
- __version__ = "0.1.2"
12
+ __version__ = "0.2.0"
13
13
  __author__ = "LIC"
14
14
  __email__ = "liuchen.lic@gmail.com"
15
15
 
@@ -10,8 +10,8 @@ class GridWorldEnv:
10
10
  使用tkinter进行可视化
11
11
  """
12
12
 
13
- def __init__(self, grid_size=(5, 5), enable_keep=False, start_state=(0, 0), render_mode="human",
14
- keyboard_control=False, target_state=None, max_steps=100000000,
13
+ def __init__(self, grid_size=(5, 5), enable_keep=False, start_state=(0, 0), target_state=None,
14
+ render_mode="human", keyboard_control=False, max_steps=100000000,
15
15
  cell_size=135, circle_radius=35, font_size=16, max_arrow_length=30, color_alpha=0.0,
16
16
  show_cell_pos=False, show_step_num=False):
17
17
  """
@@ -19,9 +19,9 @@ class GridWorldEnv:
19
19
  grid_size: (rows, cols) 网格大小
20
20
  enable_keep: 是否允许停留在当前状态,True表示5状态动作,False表示4状态动作
21
21
  start_state: (x, y) 初始状态位置
22
+ target_state: 目标位置坐标 (x, y),如果为 None 则表示无目标位置
22
23
  render_mode: 渲染模式,"human"表示可视化
23
24
  keyboard_control: 是否启用键盘控制
24
- target_state: 目标位置坐标 (x, y),如果为 None 则表示无目标位置
25
25
  max_steps: 最大步数限制
26
26
  cell_size: 每个单元格的像素尺寸
27
27
  circle_radius: 显示奖励/价值的圆半径
@@ -620,7 +620,8 @@ class GridWorldEnv:
620
620
 
621
621
  if state_value is not None:
622
622
  # 显示两行:奖励和状态价值
623
- num_width = max(len(f"{reward_val:.2f}"), len(f"{state_value:.2f}"))
623
+
624
+ num_width = max(len(f"{0 if reward_val is None else reward_val:.2f}"), len(f"{state_value:.2f}"))
624
625
  reward_text = '' if reward_val is None else f"R: {reward_val:{num_width}.2f}"
625
626
  value_text = f"V: {state_value:{num_width}.2f}"
626
627
 
@@ -40,6 +40,10 @@ class QTable:
40
40
  return state, policy
41
41
  return [f(idx) for idx in range(len(self.table))]
42
42
 
43
+ def best_q_value(self, state):
44
+ idx = self.state2idx(state)
45
+ return np.max(self.table[idx])
46
+
43
47
  def _id2policy(self, action_idx):
44
48
  p = [0, 0, 0, 0, 0] if self.enable_keep else [0, 0, 0, 0]
45
49
  p[action_idx] = 1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gridworldpy
3
- Version: 0.1.2
3
+ Version: 0.2.0
4
4
  Summary: A flexible and interactive grid world environment for reinforcement learning
5
5
  Home-page: https://github.com/hitlic/gridworldpy
6
6
  Author: LIC
@@ -92,7 +92,8 @@ from gridworldpy import GridWorldEnv
92
92
  env = GridWorldEnv(
93
93
  grid_size=(4, 4),
94
94
  keyboard_control=True, # 启用键盘控制
95
- terminal_condition=(3, 3), # 目标位置为(3,3)
95
+ enable_keep= True, # 启用停留动作
96
+ target_state=(3, 3), # 目标位置为(3,3)
96
97
  cell_size=150, # 每个格子150像素
97
98
  circle_radius=40, # 奖励圆圈半径40像素
98
99
  )
@@ -113,7 +114,7 @@ policy = [
113
114
  env.set_policy(policy)
114
115
 
115
116
  # 禁用某些状态(创建障碍物)
116
- env.disable_states([(1, 2), (2, 1)])
117
+ env.set_disable_states([(1, 2), (2, 1)])
117
118
 
118
119
  # 渲染环境
119
120
  env.render()
@@ -167,34 +168,44 @@ for step in range(10):
167
168
  ```python
168
169
  GridWorldEnv(
169
170
  grid_size=(5, 5), # 网格大小 (行, 列)
171
+ enable_keep=False, # 是否允许停留当前状态
172
+ start_state=(0, 0), # 初始状态
170
173
  render_mode="human", # 渲染模式
171
174
  keyboard_control=True, # 是否启用键盘控制
172
- terminal_condition=None, # 终止条件
175
+ max_steps=100000, # 最大步数
173
176
  cell_size=130, # 每个格子的像素大小
174
177
  circle_radius=35, # 奖励圆圈半径
175
178
  font_size=16, # 字体大小
176
179
  max_arrow_length=50, # 策略箭头最大长度
180
+ color_alpha=0.0, # 取值[0, 1),显示颜色对数值的灵敏度
177
181
  show_cell_pos=True, # 在每个单元上显示位置坐标
178
- color_alpha=0.0 # 控制色彩敏感度
182
+ show_step_num=False # 是否显示步数
179
183
  )
180
184
  ```
181
185
 
182
186
  #### 主要方法
183
187
 
184
- - `step(action)`: 执行动作,返回(observation, reward, done, info)
188
+ - `step(action)`: 执行动作,返回 `(next_state, reward, done, valid_action, info)`
189
+ - `reset()`:重置环境,返回初始状态
185
190
  - `render(state_values=None, policy_config=None, reward_config=None)`: 渲染环境
186
191
  - `set_rewards(reward_config)`: 设置奖励配置
187
192
  - `set_policy(policy_config)`: 设置策略配置
188
- - `disable_states(disabled_poses)`: 禁用指定状态
193
+ - `set_disable_states(disabled_poses)`: 禁用指定状态
189
194
  - `close()`: 关闭环境
190
195
 
191
196
  #### 动作空间
192
197
 
193
- - 0: 停留在当前位置
194
- - 1: 向上移动
195
- - 2: 向下移动
196
- - 3: 向左移动
197
- - 4: 向右移动
198
+ - 当`enable_keep=True`时
199
+ - 0: 停留在当前位置
200
+ - 1: 向上移动
201
+ - 2: 向下移动
202
+ - 3: 向左移动
203
+ - 4: 向右移动
204
+ - 当`enable_keep=False`时
205
+ - 0: 向上移动
206
+ - 1: 向下移动
207
+ - 2: 向左移动
208
+ - 3: 向右移动
198
209
 
199
210
  ## 🎨 可视化说明
200
211
 
@@ -221,6 +232,8 @@ GridWorldEnv(
221
232
  - `basic_usage.py`:基本使用示例
222
233
  - `policy_evaluation.py`:蒙特卡罗法策略评估
223
234
  - `value_iteration.py`:价值迭代寻找最优策略
235
+ - `sarsa.py`:SARSA算法实现
236
+ - `q_learning.py`:Q-Learning算法实现
224
237
 
225
238
  ## 📄 许可
226
239
 
@@ -5,7 +5,9 @@ pyproject.toml
5
5
  setup.py
6
6
  examples/basic_usage.py
7
7
  examples/policy_evaluation.py
8
+ examples/q_learning.py
8
9
  examples/sarsa.py
10
+ examples/test.py
9
11
  examples/value_iteration.py
10
12
  examples/gridworldpy/__init__.py
11
13
  examples/gridworldpy/gridworld.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "gridworldpy"
7
- version = "0.1.2"
7
+ version = "0.2.0"
8
8
  description = "A flexible and interactive grid world environment for reinforcement learning"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
File without changes
File without changes
File without changes
File without changes