问题:根据官方的定制gym环境,构建了gym运行环境后,代码运行正常,但是没有Agent与环境交互的效果图。
gym环境的定制过程参见本人前面的发布
原因:是因为官方的代码中有bug,实际就没有执行render函数
解决方案:
1. 在环境make中,增加render_mode,如图1所示。

import gymnasium
import gym_examples
env = gymnasium.make('gym_examples/GridWorld-v0', render_mode="human")
observation, info = env.reset(seed=42)
for _ in range(100000):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
env.render()
if terminated or truncated:
observation, info = env.reset()
env.close()
2. 将自己创建的环境的文件,grid_world.py中,render函数的几行代码注释掉,如图2所示。

import numpy as np
import pygame
import gymnasium as gym
from gymnasium import spaces
class GridWorldEnv(gym.Env):
metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 4}
def __init__(self, render_mode=None, size=5):
self.size = size # The size of the square grid
self.window_size = 512 # The size of the PyGame window
# Observations are dictionaries with the agent's and the target's location.
# Each location is encoded as an element of {0, ..., `size`}^2, i.e. MultiDiscrete([size, size]).
self.observation_space = spaces.Dict(
{
"agent": spaces.Box(0, size - 1, shape=(2,), dtype=int),
"target": spaces.Box(0, size - 1, shape=(2,), dtype=int),
}
)
# We have 4 actions, corresponding to "right", "up", "left", "down"
self.action_space = spaces.Discrete(4)
"""
The following dictionary maps abstract actions from `self.action_space` to
the direction we will walk in if that action is taken.
I.e. 0 corresponds to "right", 1 to "up" etc.
"""
self._action_to_direction = {
0: np.array([1, 0]),
1: np.array([0, 1]),

本文介绍了解决自定义Gym环境中无法显示Agent与环境交互画面的问题。通过调整环境构造参数并修正自定义环境中的渲染函数,成功实现了交互过程的可视化。

3501

被折叠的 条评论
为什么被折叠?



