Gym TransformationObservation 包装器似乎没有改变观察空间

问题描述 投票:0回答:1

目前我正在尝试建立一个环境来使用 python 和gym 训练神经网络。我想使用一个单热编码观察空间,而是使用值 -1 和 1 而不是 0 和 1。所以我尝试使用函数“transform”来转换多离散观察空间,使这些空间保持不变并进行变换将零变为-1。但是,当我使用变换函数将包装器 TransformationObservation 应用到我的多离散观察时,它似乎对观察空间没有影响。有趣的是我没有收到任何错误。该函数只是让观察空间保持不变。我是新来设置环境并使用神经网络做一些事情,我确信解决方案非常简单,但目前我被困在这里。谷歌搜索尚未找到解决方案。

预先感谢您的帮助!

这是我尝试过的代码:

from gym import Env
from gym.spaces import MultiDiscrete
from gym.wrappers import TransformObservation, FlattenObservation
import numpy as np
import random as rd

class Test_Env(Env): 
    def __init__(self):
        self.action_space = MultiDiscrete(np.repeat(3, 118))
        self.observation_space = MultiDiscrete(np.repeat(2, 118))
        self.state = np.array(np.zeros(118))
        self.test_begin = 7
    def step(self, action):
        self.action = hot_encoding_action
        pass
    def render(self):
        pass
    def reset(self):
        self.state = np.array(np.zeros(2))
        self.test_begin = 7
        return self.state

env = Test_Env()
env.observation_space.sample()

array([0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1,
       1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0,
       1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0,
       0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1,
       1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1,
       1, 1, 0, 1, 0, 0, 1, 1], dtype=int64)

def transform(vec):
    new_vec =  vec*2-np.ones(118)
    return new_vec

wrapped_env = TransformObservation(env, transform)
wrapped_env.observation_space.sample()

array([1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0,
       0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0,
       1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0,
       0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1,
       1, 1, 0, 1, 0, 1, 0, 0], dtype=int64)

python transform wrapper environment
1个回答
0
投票

根据我的理解,包装器仅影响输出观察,而不影响观察空间。 (https://gymnasium.farama.org/_modules/gymnasium/core/#RewardWrapper:~:text=class-,ObservationWrapper,-(Wrapper%5B)

我希望以下流程有帮助:

原环境下: env -> 生成观察 -> 检查观察是否匹配observation_space -> 观察

在包装器环境中: env -> 生成观察 -> 检查观察是否匹配observation_space -> **f(**observation) -> 新观察

f 是包装函数。

© www.soinside.com 2019 - 2024. All rights reserved.