OpenAI Gym入门与实操(3)

接前一篇文章:OpenAI Gym入门与实操(2)

本文内容参考:

Getting Started With OpenAI Gym | Paperspace Blog

【强化学习】 OpenAI Gym入门:基础组件(Getting Started With OpenAI Gym: The Basic Building Blocks)_iioSnail的博客-CSDN博客

4. 与Environment进行交互

在本节中,我们将介绍Env类中帮助智能体与环境交互的函数。 与Environment进行交互换句话说就是打游戏。其主要通过调用env对象的函数来完成,其包含两个重要函数:

  • reset

此函数将环境重置为初始状态,并返回与初始状态相对应的环境观察结果。

重置环境,就是重置游戏。其会对环境进行状态初始化,并返回初始化后的observation。

注意:虽然是重置,但也是初始化,也就是env即使是第一次使用也要调用reset。

  • step

此函数将动作作为输入,并将其应用于环境,从而导致环境转换到新状态。

采取行动。就是让Agent采取一个动作,例如向前移动。step函数返回做完该动作后的信息,包括以下4个返回值:

  • observation:对环境状态的观察,即执行该动作后的游戏状态。
  • reward:执行作为step函数输入的动作后,从环境中获得的奖励,即执行该动作后得到的奖励。
  • done:(本次)游戏是否终止。如果为true,则需要结束模拟或重置环境以重新启动游戏。例如命都用完了,则游戏终止,done=True。
  • info:此项提供了取决于环境的附加信息,诸如剩余生命的数量或可能有助于调试的一般信息。。通俗地说就是一些额外的信息,例如还剩几条命等。不同的游戏info不同。

现在让我们看一个例子,说明上面讨论的概念。我们首先重置环境,之后检查观察结果。然后,我们应用一个操作并检查新的观察结果。 代码演示:

import matplotlib.pyplot as plt 

# reset the environment and see the initial observation
obs = env.reset()
print("The initial observation is {}".format(obs))

# Sample a random action from the entire action space
random_action = env.action_space.sample()

# # Take the action and get the new observation space
new_obs, reward, done, info = env.step(random_action)
print("The new observation is {}".format(new_obs))

(1)obs = env.reset()

重置环境,虽说是重置,第一次使用env前,也要调用reset,会返回与初始状态相对应的observation。

(2)random_action = env.action_space.sample()

从所有动作中随机选择一个,random_action。

(3)new_obs, reward, done, info = env.step(random_action)

调用step,执行上述动作,得到下一个状态。

输出结果如下:

The initial observation is [-0.48235664  0.]
The new observation is [-0.48366517 -0.00130853]

在本例中,我们的观察结果不是正在执行的任务的屏幕截图。在许多其它环境中(如我们将看到的雅达利),观察结果是游戏的屏幕截图。无论在这两种情况的哪一种中,如果要查看环境在当前状态下的外观,都可以使用渲染方法。

env.render(mode = "human")

这会在弹出窗口中显示当前状态下的环境。你可以使用close函数关闭窗口。

env.close()

如果想将游戏的屏幕截图显示为图像,而不是弹出窗口,则应将render函数的模式参数设置为rgb_array。

env_screen = env.render(mode = 'rgb_array')
env.close()

import matplotlib.pyplot as plt 
plt.imshow(env_screen)

 收集到目前为止我们已经涵盖的所有代码块,在MountainCar环境中运行代理的典型代码如下所示:

import time 

# Number of steps you run the agent for 
num_steps = 1500

obs = env.reset()

for step in range(num_steps):
    # take random action, but you can also do something more intelligent
    # action = my_intelligent_agent_fn(obs) 
    action = env.action_space.sample()
    
    # apply the action
    obs, reward, done, info = env.step(action)
    
    # Render the env
    env.render()

    # Wait a bit before the next frame unless you want to see a crazy fast video
    time.sleep(0.001)
    
    # If the epsiode is up, then start another one
    if done:
        env.reset()

# Close the env
env.close()

在本例中,我们只是采取随机行动,但你可以使智能体根据得到的观察结果做一些更智能的事情。

你也可以依据你的具体环境(Jupyter, Py等)来在上面的代码中使用env.render来增加实时动画,例如如果使用Google Colab,则你可以使用以下代码来显示动画:

apt install xvfb

pip install pyvirtualdisplay
from pyvirtualdisplay import Display
virtual_display = Display(visible=0, size=(1400, 900))
virtual_display.start()

%matplotlib inline
import matplotlib.pyplot as plt

from IPython import display

import time
import gym

env = gym.make('MountainCar-v0')

# 初始化环境
obs = env.reset()
img = plt.imshow(env.render(mode='rgb_array'))

# 让Agent进行1500次动作
for step in range(1500):
	# 选择一个随机的动作,这里可以替换成你训练好的智能算法
    action = env.action_space.sample()
    
	# 采取行动,然后得到下个状态
    obs, reward, done, info = env.step(action)
    
    img.set_data(env.render(mode='rgb_array'))
    display.display(plt.gcf())
    display.clear_output(wait=True)

    # 如果游戏结束,则重置环境
    if done:
        env.reset()

# 关闭env
env.close()

猜你喜欢

转载自blog.csdn.net/phmatthaus/article/details/131514657