首页 > 其他分享 >gym.ObservationWrapper使用时的注意点——reset和step函数可以覆盖observation函数

gym.ObservationWrapper使用时的注意点——reset和step函数可以覆盖observation函数

时间:2022-09-25 22:25:51浏览次数:43  
标签:reset 函数 gym obs step env self observation

记录一个刚学习到的gym使用的点,就是gym.ObservationWrapper使用时的注意点——reset和step函数可以覆盖observation函数。

 

 

给出代码:

import gym


class Wrapper(gym.ObservationWrapper):
    def __init__(self, env):
        super(Wrapper, self).__init__(env)


    def reset(self):
        obs = self.env.reset()
        print(obs)
        return obs
        # return self.observation(obs)

    def step(self, action):
        obs, reward, is_done, info = self.env.step(action)
        print(obs)
        return obs, reward, is_done, info
        # return self.observation(obs), reward, is_done, info

    def observation(self, observation):
        observation += 100
        return observation

env=gym.make("CartPole-v0")
env = Wrapper(env)
print("reset:")
print(env.reset())
print("step:")
print(env.step(0)[0])

运行:

 

 

可以看到,继承gym.ObservationWrapper类后,如果重写reset函数或step函数,那么对应的返回的observation也不会被该类的observation函数所处理。

如果我们把reset函数和step函数注释掉,再次运行可以看到运行结果:

 

 

 

 

 

=======================================

 

那么在继承gym.ObservationWrapper类后,如果重写reset函数或step函数,同时又希望对应的返回的observation被该类的observation函数所处理,那么我们可以做如下的修改:

 

import gym


class Wrapper(gym.ObservationWrapper):
    def __init__(self, env):
        super(Wrapper, self).__init__(env)


    def reset(self):
        obs = self.env.reset()
        print(obs)
        # return obs
        return self.observation(obs)

    def step(self, action):
        obs, reward, is_done, info = self.env.step(action)
        print(obs)
        # return obs, reward, is_done, info
        return self.observation(obs), reward, is_done, info

    def observation(self, observation):
        observation += 100
        return observation

env=gym.make("CartPole-v0")
env = Wrapper(env)
print("reset:")
print(env.reset())
print("step:")
print(env.step(0)[0])

 

运行结果:

 

 

 

---------------------------------------------------------------------------

 

 

 

继承gym.ObservationWrapper类后reset和step函数的使用可以具体看下ObservationWrapper类的实现:

 

 

标签:reset,函数,gym,obs,step,env,self,observation
From: https://www.cnblogs.com/devilmaycry812839668/p/16729162.html

相关文章

  • 怎么写出数组扁平化?如何手写flat函数
    手写一下数组扁平化flat(),但是发现居然没有一个能够完成写出来,所以打算总结一下如果遇到了数组扁平化的题目(也可以叫做手动封装flat()方法),到底应该怎么写,怎么写可......
  • mysql函数
    日期函数返回当前日期,只包含年月日selectcurdate()返回当前时间,只包含时分秒selectcurtime()返回当前的日期和时间,年月日时分秒全都包含selectnow()提取......
  • lambda函数和map函数的理解和使用
    lambda函数在说lambda函数前,先来想一下平时的在Python中怎么定义和使用函数的,简单的如下:defsum(x):x=x+5returnxprint(sum(8))输出结果都晓得:13上......
  • React 函数式组件怎样进行优化
    前言目的本文只介绍函数式组件特有的性能优化方式,类组件和函数式组件都有的不介绍,比如key的使用。另外本文不详细的介绍API的使用,后面也许会写,其实想用好hooks还是......
  • C/C++ 关于默认构造函数
    前言:在C++中,对于一个类,C++的编译器都会为这个类提供四个默认函数,分别是:A()//默认构造函数~A()//默认析构函数A(constA&)//默认拷贝构造函数A&operator=(const......
  • 函数式接口作为方法的参数案例和函数式接口作为方法的返回值类型案例
    函数式接口作为方法的参数案例:/*假如java.lang.Runnable接口就是一个函数式接口假设有一个startThread方法使用该接口作为参数,那么参数就可以使用Lambda进行传......
  • js 内置函数Date
    从深入潜JavaScript日期对象方法描述*空执行Date()返回当日的日期和时间(字符串)。getDate()orsetDate()get返回月中的第几天(1-31),set设置对象当月天。......
  • C语言第18天,字符串处理函数
    字符串处理函数与printf不同,这些函数不在之前熟悉的头文件stdio.h中。而是在字符串专用的头文件string.h中。1.获取字符串长度strlenstrlen函数可以获取字符数组中的字......
  • 封装axios函数记录
    基于axios进行二次封装统一配置,便于管理下载axiosyarnaddaxios创建utils/request.js//基于axios封装的请求模块importajaxfrom'axios'//新建一......
  • Const修饰类成员函数
    主要说明const类成员函数调用方式以及this指针对应变化SimpleConst.h#pragmaonceclassA{public: inta; intb; constintc; A(inti,intj,intt); ......