1、本篇文章主要介绍在gym环境下env环境编写(未待完续,空闲更新)
经典的迷宫宝藏例子
import numpy as np
import time
import tkinter as tk # 窗口界面库
UNIT = 40 # 像素
MAZE_H = 5 # 高宽格子数
MAZE_W = 3
class Maze(tk.Tk): # 新类继承父类tkinter.Tk
def __init__(self):
super(Maze, self).__init__() # super类继承方法,初始化
self.action_space = ['u', 'd', 'l', 'r']
self.n_actions = len(self.action_space)
self.title('maze')
self.geometry('{0}x{1}'.format(MAZE_H * UNIT, MAZE_H * UNIT)) # format格式化函数 geometry分辨率函数
self._build_maze()
def _build_maze(self):
self.canvas = tk.Canvas(self, bg='white', # tk画图组件
height=MAZE_H * UNIT,
width=MAZE_W * UNIT)
for c in range(0, MAZE_W * UNIT, UNIT): # 网格
x0, y0, x1, y1 = c, 0, c, MAZE_H * UNIT
self.canvas.create_line(x0, y0, x1, y1)
for r in range(0, MAZE_H * UNIT, UNIT):
x0, y0, x1, y1 = 0, r, MAZE_W * UNIT, r
self.canvas.create_line(x0, y0, x1, y1)
origin = np.array([20, 20]) # 原点
hell2_center = origin + np.array([UNIT * 0, UNIT * 1])
self.hell2 = self.canvas.create_rectangle(
hell2_center[0] - 15, hell2_center[1] - 15,
hell2_center[0] + 15, hell2_center[1] + 15,
fill='black')
hell3_center = origin + np.array([UNIT * 1, UNIT * 1])
self.hell3 = self.canvas.create_rectangle(
hell3_center[0] - 15, hell3_center[1] - 15,
hell3_center[0] + 15, hell3_center[1] + 15,
fill='black')
hell6_center = origin + np.array([UNIT * 1, UNIT * 3])
self.hell6 = self.canvas.create_rectangle(
hell6_center[0] - 15, hell6_center[1] - 15,
hell6_center[0] + 15, hell6_center[1] + 15,
fill='black')
hell7_center = origin + np.array([UNIT * 2, UNIT * 3])
self.hell7 = self.canvas.create_rectangle(
hell7_center[0] - 15, hell7_center[1] - 15,
hell7_center[0] + 15, hell7_center[1] + 15,
fill='black')
oval_center = origin + np.array([UNIT * 2, UNIT * 4]) # 宝藏
self.oval = self.canvas.create_oval(
oval_center[0] - 15, oval_center[1] - 15,
oval_center[0] + 15, oval_center[1] + 15,
fill='yellow')
self.rect = self.canvas.create_rectangle( # 主体
origin[0] - 15, origin[1] - 15,
origin[0] + 15, origin[1] + 15,
fill='red')
self.canvas.pack() # 打包
def reset(self):
self.update()
time.sleep(0.1)
self.canvas.delete(self.rect) # 配置Python Tkinter 画布(Canvas),删除变化的矩形,然后重新创建
origin = np.array([20, 20])
self.rect = self.canvas.create_rectangle(
origin[0] - 15, origin[1] - 15,
origin[0] + 15, origin[1] + 15,
fill='red') # 创建中心位置红色方块代表当前位置
# return observation
return self.canvas.coords(self.rect)
def step(self, action):
s = self.canvas.coords(self.rect)
base_action = np.array([0, 0])
if action == 0: # up
if s[1] > UNIT:
base_action[1] -= UNIT
elif action == 1: # down
if s[1] < (MAZE_H - 1) * UNIT:
base_action[1] += UNIT
elif action == 2: # right
if s[0] < (MAZE_W - 1) * UNIT:
base_action[0] += UNIT
elif action == 3: # left
if s[0] > UNIT:
base_action[0] -= UNIT
self.canvas.move(self.rect, base_action[0], base_action[1]) # move agent
s_ = self.canvas.coords(self.rect) # next state
# reward function
if s_ == self.canvas.coords(self.oval):
reward = 1
done = True
s_ = 'terminal'
elif s_ in [self.canvas.coords(self.hell2), self.canvas.coords(self.hell3),
self.canvas.coords(self.hell6), self.canvas.coords(self.hell7)]:
reward = -1
done = True
s_ = 'terminal'
else:
reward = 0
done = False
return s_, reward, done
def render(self):
time.sleep(0.1)
self.update()
def update():
for t in range(10):
s = env.reset()
while True:
env.render()
a = 1
s, r, done = env.step(a)
if done:
break
if __name__ == '__main__':
env = Maze()
env.after(100, update)
env.mainloop()
经典的CartPole例子
reset函数:
def reset(self):
self.state = self.np_random.uniform(low=-0.05, high=0.05, size=(4,))
# 利⽤均匀随机分布初试化环境的状态
self.steps_beyond_done = None
# 设置当前步数为None
return np.array(self.state)
numpy的random库, 用于生成随机数矩阵,uniform函数用于产生均匀分布随机数(其他函数可以产生高斯分布随机数等。)
2、补充一点DQN算法模块的函数阅读
函数:寻宝算法模块choose_action
def choose_action(self, observation):
self.check_state_exist(observation)
if np.random.uniform() < self.epsilon: # epsilon等于0.9,90%概率Q函数决定行动
state_action = self.q_table.loc[observation, :] # 从Q表中提取观测值那一行的Q值
# 最大Q值对应多个动作时,随机选择动作
action = np.random.choice(state_action[state_action == np.max(state_action)].index)
else: # 10%概率随机行动,探索其他可能
# choose random action
action = np.random.choice(self.actions)
return action
注:这里的第一个observation是[5,5,35,35]。这个数组矩阵来自于canvas.create_rectangle函数,根据左上和右下坐标绘制矩形,定义一个像素40*40,则起点位置的状态是[5,5,35,35]。
参考文献:Pandas中loc和iloc函数用法详解(源码+实例) https://blog.csdn.net/w_weiying/article/details/81411257
参考文献:莫烦老师,DQN代码学习笔记 https://blog.csdn.net/yyyxxxsss/article/details/80467058