如何为强化学习环境显示 tkinter-canvas
How to display tkinter-canvas for Reinforcement Learning environment
我正在创建自定义 强化学习环境。到目前为止,环境只是一个 3 x 3 的网格。我想创建一个自定义环境,这就是我不使用 OpenAI Gym 的原因。最终 objective 是 DQN-agent 找到合适的路径以最大化可能的奖励并到达网格上的目的地(比方说,例如,目标是到达坐标为 [2|2] 的字段)。
我已经为环境 (class Env) 创建了一个示例 class。
网格的"architecture"在函数build_canvas(self)中描述。可见,我使用 tkinter.canvas 来定义网格系统。不幸的是,当我尝试实例化 Env 类型的对象时,网格没有显示。
class Env(tk.Tk):
def __init__(self):
super(Env, self).__init__()
print("This is the standard constructor of our
environment class.")
self.build_canvas()
def build_canvas(self):
canvas = tk.Canvas(self, bg='green', height=HEIGHT,
width=WIDTH)
## Create grid with 3x3 (3 rows with 3 columns)
for c in range(0, WIDTH, 60):
x1, y1, x2, y2 = c, 0, c, HEIGHT
canvas.create_line(x1, y1, x2, y2)
for r in range(0, HEIGHT, 60):
x1, y1, x2, y2 = 0, r, HEIGHT, r
canvas.create_line(x1, y1, x2, y2)
canvas.pack()
return canvas
def render(self):
print("This renders the environment to the screen.")
def reset(self):
print("This resets the environment.")
def step(self, action):
print("This takes an action and the environment.")
if __name__ == "__main__":
env = Env()
它只是将字符串打印到控制台,但是根本没有加载网格。有人有什么建议吗?
1- 您需要在您的根目录上调用 mainloop()
(此处为 env
)。
2- 您必须保留 canvas (self.canvas
)
的引用
3- 在 python 3 中,您可以像这样调用 super:super().__init__()
,不带参数。
4- 在绘制线条的循环中 WIDTH
和 HEIGHT
之间存在一些混淆。
import tkinter as tk
HEIGHT, WIDTH = 500, 500
class Env(tk.Tk):
def __init__(self):
super().__init__()
print("This is the standard constructor of ourenvironment class.")
self.canvas = self.build_canvas()
self.canvas.pack()
def build_canvas(self):
canvas = tk.Canvas(self, bg='green', height=HEIGHT, width=WIDTH)
## Create grid with 3x3 (3 rows with 3 columns)
for c in range(0, WIDTH, 60):
x1, y1, x2, y2 = c, 0, c, HEIGHT
canvas.create_line(x1, y1, x2, y2)
for r in range(0, HEIGHT, 60):
x1, y1, x2, y2 = 0, r, WIDTH, r
canvas.create_line(x1, y1, x2, y2)
return canvas
def render(self):
print("This renders the environment to the screen.")
def reset(self):
print("This resets the environment.")
def step(self, action):
print("This takes an action and the environment.")
if __name__ == "__main__":
env = Env()
env.mainloop()
我正在创建自定义 强化学习环境。到目前为止,环境只是一个 3 x 3 的网格。我想创建一个自定义环境,这就是我不使用 OpenAI Gym 的原因。最终 objective 是 DQN-agent 找到合适的路径以最大化可能的奖励并到达网格上的目的地(比方说,例如,目标是到达坐标为 [2|2] 的字段)。
我已经为环境 (class Env) 创建了一个示例 class。 网格的"architecture"在函数build_canvas(self)中描述。可见,我使用 tkinter.canvas 来定义网格系统。不幸的是,当我尝试实例化 Env 类型的对象时,网格没有显示。
class Env(tk.Tk):
def __init__(self):
super(Env, self).__init__()
print("This is the standard constructor of our
environment class.")
self.build_canvas()
def build_canvas(self):
canvas = tk.Canvas(self, bg='green', height=HEIGHT,
width=WIDTH)
## Create grid with 3x3 (3 rows with 3 columns)
for c in range(0, WIDTH, 60):
x1, y1, x2, y2 = c, 0, c, HEIGHT
canvas.create_line(x1, y1, x2, y2)
for r in range(0, HEIGHT, 60):
x1, y1, x2, y2 = 0, r, HEIGHT, r
canvas.create_line(x1, y1, x2, y2)
canvas.pack()
return canvas
def render(self):
print("This renders the environment to the screen.")
def reset(self):
print("This resets the environment.")
def step(self, action):
print("This takes an action and the environment.")
if __name__ == "__main__":
env = Env()
它只是将字符串打印到控制台,但是根本没有加载网格。有人有什么建议吗?
1- 您需要在您的根目录上调用 mainloop()
(此处为 env
)。
2- 您必须保留 canvas (self.canvas
)
的引用
3- 在 python 3 中,您可以像这样调用 super:super().__init__()
,不带参数。
4- 在绘制线条的循环中 WIDTH
和 HEIGHT
之间存在一些混淆。
import tkinter as tk
HEIGHT, WIDTH = 500, 500
class Env(tk.Tk):
def __init__(self):
super().__init__()
print("This is the standard constructor of ourenvironment class.")
self.canvas = self.build_canvas()
self.canvas.pack()
def build_canvas(self):
canvas = tk.Canvas(self, bg='green', height=HEIGHT, width=WIDTH)
## Create grid with 3x3 (3 rows with 3 columns)
for c in range(0, WIDTH, 60):
x1, y1, x2, y2 = c, 0, c, HEIGHT
canvas.create_line(x1, y1, x2, y2)
for r in range(0, HEIGHT, 60):
x1, y1, x2, y2 = 0, r, WIDTH, r
canvas.create_line(x1, y1, x2, y2)
return canvas
def render(self):
print("This renders the environment to the screen.")
def reset(self):
print("This resets the environment.")
def step(self, action):
print("This takes an action and the environment.")
if __name__ == "__main__":
env = Env()
env.mainloop()