python - OpenAI-gym如何为step()中的某个 Action 实现计时器

标签 python pygame openai-gym

我希望代理执行的操作之一需要在每个操作之间有延迟。对于上下文,在 pygame 中我有以下用于射击子弹的代码:

if keys[pygame.K_SPACE]:
    current_time = pygame.time.get_ticks()
    # ready to fire when 600 ms have passed.
    if current_time - previous_time > 600:
        previous_time = current_time
        bullets.append([x + 25, y + 24])

我已经设置了一个计时器来防止垃圾邮件,我将如何构造它以与 step() 方法一起使用?我的其他 Action 是向上、向下、向左、向右移动。

这是我第一次使用 OpenAI-gym 创建项目,所以我不确定该工具包的功能是什么,任何帮助将不胜感激。

最佳答案

您可以使用任何您喜欢的跟踪时间的方法(我想除了 pygame.time.get_ticks() ),并使用与 pygame 中类似的方法代码。您希望将 previous_time 存储为环境成员,而不仅仅是局部变量,因为您希望它在函数调用之间保持不变。

实际上阻止你的强化学习代理(假设你正在使用gym进行强化学习)完全选择开火 Action 并不容易,但你可以简单地在这样的情况下实现step()函数这样,如果特工太快选择开火 Action ,他们根本不会做任何事情。

至于测量时间,你可以测量挂钟时间,但是你的CPU的能力将影响你的代理被允许射击的频率(它可能能够在非常旧的硬件上每一步发射一颗新子弹,但在强大的硬件上只允许每 100 步发射一颗子弹),这可能是一个坏主意。相反,我建议仅通过计算 step() 调用来测量“时间”。例如,仅使用上面问题中的代码,step() 函数可能如下所示:

def step(action):
    self.step_counter += 1

    # other step() code here

    if action == FIRE:
        if self.step_counter - self.previous_time > 10:    # or any other number
            self.previous_time = self.step_counter
            bullets.append([x + 25, y + 24])

    # other step() code here

不要忘记,您还需要在 reset() 中重置新添加的成员变量:

def reset():
    self.step_counter = 0
    self.previous_time = -100   # some negative number such that your agent can fire at start
    # other reset() code here

关于python - OpenAI-gym如何为step()中的某个 Action 实现计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49737552/

相关文章:

python - 如何使用 safe_load 使用 PyYAML 反序列化对象?

python - 如何在 Pygame 中打印按下控制台的特定键?

python - 无法为python安装pygame

python-3.x - 稳定基线 3 的字典观察空间不起作用

Python mysql.connector.pooling 导入错误 : No module named pooling

Python Eratosthenes 筛算法优化

python - pygame连续下雨

python - 如何为我的自定义 openai 环境定义 Observation_space?

python - FrozenLake-v1 环境中的渲染问题

python - Tweepy "page parameter is invalid"错误