by Harini Janakiraman

通过哈里尼·贾纳基拉曼

第22天:如何使用OpenAI Gym和Universe构建AI游戏机器人 (Day 22: How to build an AI Game Bot using OpenAI Gym and Universe)

Let’s face it, AI is everywhere. A face-off battle is unfolding between Elon Musk and Mark Zuckerberg on the future of AI. There are some that demonize it. And some whose utopian views claim that AI could almost be God-like in helping humanity. Whichever side your views tilt, AI is here to stay.

面对现实,人工智能无处不在。 埃隆·马斯克(Elon Musk)和马克·扎克伯格(Mark Zuckerberg)之间就AI的未来展开的对抗之战正在展开。 有一些妖魔化了它。 还有一些人以乌托邦的观点声称,人工智能在帮助人类方面几乎可以像上帝一样。 无论您的观点偏向哪一侧,人工智能都将继续存在。

“With artificial intelligence, we are summoning the demon.” — Elon Musk

“借助人工智能,我们正在召唤恶魔。” —伊隆·马斯克(Elon Musk)

“Fearing a rise of killer robots is like worrying about overpopulation on Mars.” — Andrew Ng

“害怕杀手机器人的崛起就像担心火星上的人口过多。” -吴彦祖

If you’re excited to dive right in and tinker with AI, then games are a great place to start. They have been the go-to testbed for AI. But before jumping in, here’s a little bit of history on how game programming has evolved through time.

如果您很高兴直接潜入并尝试AI,那么游戏就是一个不错的起点。 它们已成为AI的首选测试平台。 但是在进入之前,这里有一些关于游戏编程如何随着时间演变的历史。

游戏编程的历史 (The History of Game Programming)

Game programmers used to use heuristic if-then-else type decisions to make educated guesses. We saw this in the earliest arcade videos games such as Pong and PacMan. This trend was the norm for a very long time. But game developers can only predict so many scenarios and edge cases so your bot doesn’t run in circles!

游戏程序员过去常常使用启发式的if-then-else类型决策来进行有根据的猜测。 我们在最早的街机视频游戏(例如Pong和PacMan)中看到了这一点。 长期以来,这种趋势一直是常态。 但是游戏开发人员只能预测这么多的场景和极端情况,因此您的机器人不会运转!

Game developers then tried to mimic how humans would play a game, and modeled human intelligence in a game bot.

然后,游戏开发人员试图模仿人类如何玩游戏,并在游戏机器人中模拟人类智能。

The team at DeepMind did this by generalizing and modeling intelligence to solve any Atari game thrown at it. The game bot used deep learning neural networks that would have no game-specific knowledge. They beat the game based on the pixels they saw on screen and their knowledge of the game controls. However, parts of DeepMind are still not open-sourced as Google uses it to beat competition.

DeepMind团队通过对情报进行归纳和建模来解决投掷给它的任何Atari游戏来做到这一点。 该游戏机器人使用了深度学习神经网络,而该神经网络没有特定于游戏的知识。 他们根据在屏幕上看到的像素和对游戏控件的了解来打败游戏。 但是,由于Google使用DeepMind击败竞争对手,因此DeepMind的某些部分仍未开源。

人工智能的民主化 (The Democratization of AI)

To avoid concentrating the incredible power of AI in the hands of a few, Elon Musk founded OpenAI. It seeks to democratize AI by making it accessible to all. Today we shall explore OpenAI Gym and the recently released Universe, which is built on top of Gym.

为了避免将AI的强大功能集中在少数人的手中,Elon Musk创立了OpenAI 。 它试图通过使所有人都能使用来使人工智能民主化。 今天,我们将探索OpenAI Gym和最近发布的基于Gym的Universe。

OpenAI Gym provides a simple interface for interacting with and managing any arbitrary dynamic environment. OpenAI Universe is a platform that lets you build a bot and test it out.

OpenAI Gym提供了一个简单的界面,用于与任意动态环境进行交互和管理。 OpenAI Universe是一个平台,可让您构建一个机器人并对其进行测试。

There are thousands of environments. They range from classic Atari games, Minecraft, and Grand Theft Auto, to protein fold simulations that can cure cancer. You can create a bot and run it in any environment using only a few lines of Python code. This is too awesome not to try!

有成千上万的环境。 它们的范围从经典的Atari游戏,Minecraft和Grand Theft Auto到可以治愈癌症的蛋白质折叠模拟 。 您可以创建bot并仅使用几行Python代码在任何环境中运行它。 这太棒了,不要尝试!

专案(1小时) (Project (1 Hour))

We are going to build an AI Game Bot that uses the “Reinforcement Learning” technique. I’ll explain that later. It will autonomously play against and beat the Atari game Neon Race Car (you can select any game you want). We will build this game bot using OpenAI’s Gym and Universe libraries.

我们将构建一个使用“强化学习”技术的AI游戏机器人。 稍后再解释。 它将自动与Atari游戏Neon Race Car对抗并击败Atari游戏Neon Race Car(您可以选择任何游戏)。 我们将使用OpenAI的Gym和Universe库构建此游戏机器人。

步骤1:安装 (Step 1: Installation)

Ensure you have Python installed, or install it using Homebrew. You can download a dedicated Python IDE like PyCharm or iPython notebook. I like to keep it simple and use Sublime. Finally, install Gym, Universe and other required libraries using pip.

确保已安装Python,或使用Homebrew安装它。 您可以下载专用的Python IDE,例如PyCharm或iPython Notebook。 我喜欢保持简单并使用Sublime。 最后,使用pip安装Gym,Universe和其他必需的库。

// Install python using brewbrew install python3// Install the required OpenAI librariespip3 install gympip3 install numpy incrementalbrew install golang libjpeg-turbo pip install universe

Everything in Universe (the environments) runs as containers inside Docker. In case you don’t have it already, install and run Docker from here.

Universe(环境)中的所有内容都作为Docker内部的容器运行。 如果您还没有它,请从这里安装并运行Docker。

第2步:编写游戏机器人代码 (Step 2: Code the Game Bot)

The Game Bot is coded in Python, so we start by importing the only two dependencies needed: Gym and Universe.

Game Bot是用Python编码的,因此我们首先导入所需的两个依赖项:Gym和Universe。

import gymimport universe

For this Game Bot, let’s use my favorite childhood game, Neon Race Cars, as the test environment. You can find a complete list of other environment/games you can choose from here.

对于这个游戏机器人,让我们使用我最喜欢的童年游戏Neon Race Cars作为测试环境。 您可以在此处找到其他环境/游戏的完整列表。

Universe lets you run as many environments as you want in parallel. But for this project, we will use only one.

Universe使您可以并行运行任意多个环境。 但是对于这个项目,我们将只使用一个。

env = gym.make(‘flashgames.NeonRace-v0’)env.configure(remotes=1) # creates a local docker container

强化学习 (Reinforcement Learning)

Now we add the game bot logic that uses the reinforcement learning technique. This technique observes the game’s previous state and reward (such as the pixels seen on the screen or the game score). It then comes up with an action to perform on the environment.

现在,我们添加了使用强化学习技术的游戏机器人逻辑。 此技术观察游戏的先前状态和奖励(例如,屏幕上看到的像素或游戏得分)。 然后提出要在环境上执行的操作。

The goal is to make its next observation better (in our case — to maximize the game score). This action is chosen and performed by an agent (Game Bot) with the intention of maximizing the score. It’s then applied on the environment. The environment records the resulting state and reward based on whether the action was beneficial or not (did it win the game?).

我们的目标是使下一次观察更好(在我们的案例中-最大化游戏得分)。 此动作是由代理商(游戏机器人)选择并执行的,目的是使得分最大化。 然后将其应用于环境。 环境根据操作是否有益(它是否赢得了游戏?)记录结果状态和奖励。

Now we can retrieve the list of observations for each environment initialized using the env.reset() method.

现在,我们可以检索使用env.reset()方法初始化的每个环境的观察结果列表。

observation_n = env.reset()

The observation here is an environment-specific object. It represents what was observed, such as the raw pixel data on the screen or the game status/score.

这里的观察是特定于环境的对象。 它代表观察到的内容,例如屏幕上的原始像素数据或游戏状态/得分。

The next step is to create a game agent using an infinite loop, which continuously performs some action based on the observation. In our bot, let’s define a single action of repeatedly pressing the up arrow (Silly bot! Feel free to evolve it to a complex one…). Action here is defined by the event type (KeyEvent), the control key (Up Arrow), and setting it to true for all observation that the agent sees.

下一步是使用无限循环创建游戏代理,该循环根据观察结果连续执行某些动作。 在我们的机器人中,让我们定义一个反复按下向上箭头的动作(Silly机器人!随意将其演变为一个复杂的……)。 此处的操作由事件类型(KeyEvent),控制键(向上箭头)定义,并针对代理看到的所有观察值将其设置为true。

while True:action_n = [[('KeyEvent', 'ArrowUp', True)] for ob in observation_n]

We then use the env.step() method to use the action to move forward one time step. This is a very basic implementation of reinforced learning.

然后,我们使用env.step()方法来使用该动作向前移动一个时间步。 这是强化学习的非常基本的实现。

observation_n, reward_n, done_n, info = env.step(action_n)

The step method here returns four variables:

这里的step方法返回四个变量:

  1. observation_n: Observations of the environment

    observation_n :对环境的观察

  2. reward_n: If your action was beneficial or not: +1/-1

    reward_n :如果您的举动是否有益:+ 1 / -1

  3. done_n: Indicates if the game is over or not: Yes/No

    done_n :指示游戏是否结束:是/否

  4. info: Additional info such as performance and latency for debugging purposes

    info :用于调试目的的其他信息,例如性能和延迟

You can run this action simultaneously for all the environments in which you’re training your bot. Use the env.render() method to start the bot.

您可以在训练机器人的所有环境中同时运行此操作。 使用env.render()方法启动机器人。

env.render()

Now you have the Game Bot ready to compete with the environment. The complete code for this basic bot as well as an advanced version is available in my Github repo here.

现在您已经准备好与环境竞争。 这个基本的机器人,以及一个高级版本的完整代码,请在我的GitHub库在这里 。

步骤3:运行游戏机器人 (Step 3: Run the Game Bot)

Now for the fun part: ensure Docker is running and run the bot. See it in action beating other cars or failing to do so. If it fails, keep tweaking your bot to make it beat intelligence!

现在开始有趣的部分:确保Docker正在运行并运行该机器人。 实际观察它击败其他汽车还是不这样做。 如果失败,请不断调整您的机器人使其胜过智能!

python gamebot.py

Keep tinkering with AI and eventually you can unlock God Mode! #100DaysOfCode

继续修补AI,最终您可以解锁上帝模式! #100DaysOfCode

If you enjoyed this, please clap ? so others can see it as well! Follow me on Twitter @HariniLabs or Medium to get the latest updates on other stories or just to say Hi :)

如果喜欢这个,请鼓掌吗? S 0其他人可以看到它的! 在Twitter上关注我@ H ariniLabsMedium,以获取其他故事的最新更新,或者只是打个招呼:)

PS: Sign up for my newsletter here to be the first to get fresh new content and it’s filled with a dose of inspiration from the world of #WomenInTech and yes men can signup too!

PS: 在这里注册我的新闻通讯是第一个获得新鲜内容的新闻,它充满了#WomenInTech世界的启发 ,是的,男性也可以注册!

翻译自: https://www.freecodecamp.org/news/how-to-build-an-ai-game-bot-using-openai-gym-and-universe-f2eb9bfbb40a/

第22天:如何使用OpenAI Gym和Universe构建AI游戏机器人相关推荐

  1. OpenAI 成近期顶流团队?如何使用 OpenAI 和 Node.js 构建 AI 图像生成器?

    摘要: 12月7号,知名人工智能研究机构 Open AI 在Youtub上发布视频介绍使用OpenAI 和 DALL-E 模型创建一个网络应用程序,该应用程序将根据输入的文本从头开始生成图像.http ...

  2. Ubuntu下常用强化学习实验环境搭建(MuJoCo, OpenAI Gym, rllab, DeepMind Lab, TORCS, PySC2)

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/77144590 和其它的机器学习方向一样,强化学习(Reinforcement Learni ...

  3. OpenAI Gym介绍

    上篇博客介绍了OpenAI Gym.OpenAI Gym与强化学习以及OpenAI Gym的安装,接下来运行一个demo体验一下OpenAI Gym这个平台,以CartPole(倒立摆)为例,在工作目 ...

  4. 独家 | 使用Python的OpenAI Gym对Deep Q-Learning的实操介绍(附学习资源)

    作者:ANKIT CHOUDHARY 翻译:张睿毅 校对:吴金笛 本文4300字,建议阅读10+分钟. 本文作者通过实战介绍了Deep Q-Learning的概念. 导言 我一直对游戏着迷.在紧凑的时 ...

  5. 强化学习(一)——专业术语及OpenAI Gym介绍

    强化学习(一)--专业术语及OpenAI Gym介绍 1. 专业术语 1.1 Agent(智能体) 1.2 Environment(环境) 1.3 State *s*(状态) 1.4 Action * ...

  6. [环境] OpenAI gym经典控制环境CartPole-v0 介绍

    [环境]CartPole-v0 聊一聊我对强化学习的理解 对应的代码请访问我的GitHub:fxyang-bupt(可能你进去之后发现什么都没有,那是因为我注册了新的账号还在整理,这并不影响你先fol ...

  7. dqn在训练过程中loss越来越大_强化学习笔记:OpenAI Gym+DQN+Tensorflow2实现

    参考了一些文章,针对OpenAI gym环境,使用tf2.x实现了DQN算法:加上了一些没有太大必要(?)的小功能,比如:自动保存视频,保存训练日志从而利用TensorBoard实现数据可视化,保存和 ...

  8. qlearning算法_通过OpenAI Gym编写第一个强化学习算法

    腾讯互娱Turing Lab从创建开始,每周在内部进行分享读书会,对业界的技术研究和应用进行讨论.在此通过公众号形式把相关有趣内容也推送给对新技术和业界趋势感兴趣的朋友. 和大量的所谓技术公众号不同, ...

  9. OpenAI Gym 是一个优秀开发和比较强化学习算法的工具

    OpenAI Gym 是一个优秀开发和比较强化学习算法的工具. gym的核心接口是Env方法: reset(self):重置环境的状态,返回观察.     step(self, action):推进一 ...

最新文章

  1. 菜鸟裹裹电脑版_干货|利用菜鸟裹裹商家版低价寄快递
  2. java ui设计用什么_UI设计是什么?UI怎么设计?
  3. Neutron Router 工作原理 - 每天5分钟玩转 OpenStack(142)
  4. 2017-2018-20172309 《程序设计与数据结构》第八周学习总结
  5. mysql第七章课后答案_mysql核心内幕第七章-查询解析与优化器
  6. Redis学习总结(23)——Redis集群化方案对比:Codis、Twemproxy、Redis Cluster
  7. php递归实现冒泡排序,排序算法之PHP版快速排序、冒泡排序
  8. Swift类属性 static
  9. IDA install keystone
  10. Kettle 数据迁移
  11. 央行数字货币在技术上是如何实现的
  12. plupload插件上传总结(分片上传,php后端处理)
  13. python视频字幕处理_用Python处理字幕文件
  14. js前端缓存的几种方法
  15. 大数据时代,你应该知道的生活真相(下)
  16. 【论文笔记】CycleGAN(基于PyTorch框架)
  17. 新出台的治理iMessage垃圾短信的规则
  18. win,linux双系统开机引导修复
  19. 上计算机课怎么备课,如何备课写教案
  20. 开发项目中,360浏览器遇到的一个坑

热门文章

  1. static、volatile、synchronize
  2. Educational Codeforces Round 25 C. Multi-judge Solving
  3. / ./ ../ 的区别
  4. mysql复制主从集群搭建
  5. POJ3264 【RMQ基础题—ST-线段树】
  6. VS2015 Cordova Ionic移动开发(五)
  7. Android版:验证手机号码的正则表达式 (转)
  8. MySQL 添加列,修改列,删除列 的SQL写法
  9. php实现当前用户在线人数
  10. VS2005中ReportViewer 本地模式下报表呈现 入门示例