第22天:如何使用OpenAI Gym和Universe構建AI游戲機器人

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/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395416.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395416.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395416.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

軟件測試基礎理論(總結)

1. 軟件的三個要素:程序(實行特定功能的代碼) 文檔(支持代碼運行) 數據(支持程序運行一切有關) 2. 軟件的產品質量 指的是? 1)質量是指實體特性…

android studio 7200u,#本站首曬# 多圖殺貓 華為MateBook X上手體驗

#本站首曬# 多圖殺貓 華為MateBook X上手體驗2017-06-09 18:45:4437點贊33收藏78評論前幾天華為開了個發布會,帶來了三款筆記本電腦,有幸在第一時間借到了MateBook X,現在就來來做一個簡單的上手,稍晚一些再跟大家詳細聊聊使用起來…

svn強制解鎖的幾種做法

標簽: svn強制解鎖2013-12-16 17:40 12953人閱讀 評論(0) 收藏 舉報分類:SoftwareProject(23) 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 作者:朱金燦 來源:http://blog.…

數據結構和算法練習網站_視頻和練習介紹了10種常見數據結構

數據結構和算法練習網站“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds, creator of Linux“糟糕的程序員擔心代碼。 好的程序員擔心數據結構及其關系。” — Linux的創建者Linus Torva…

突然討厭做前端,討厭代碼_有關互聯網用戶最討厭的廣告類型的新數據

突然討厭做前端,討厭代碼You know that feeling when you’re scrolling through a blog post and then — BAM! — one of those “Sign up for our newsletter” modals pops up?當您滾動瀏覽博客文章,然后-BAM時,您就會知道這種感覺。 -彈出“注冊我…

iOS設計模式-生成器

定義&#xff1a;將一個產品的內部表象與產品的生成過程分割開來&#xff0c;從而可以使一個建造過程生成具有不同的內部表象的產品對象。 類型&#xff1a;對象創建 類圖&#xff1a; #import <Foundation/Foundation.h> interface Character : NSObject property(nonat…

《Android 應用案例開發大全(第二版)》——導讀

本節書摘來自異步社區《Android 應用案例開發大全&#xff08;第二版&#xff09;》一書中的目錄 &#xff0c;作者 吳亞峰 , 于復興 , 杜化美&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看 目 錄 第1章 初識廬山真面目——Android簡介 1.1 Android的誕生 1…

模塊--sys模塊

sys模塊是與python解釋器交互的一個接口 import sys sys.path #python解釋器找模塊的環境變量import sys print(sys.path)結果:[H:\\王文靜\\python\\4練習\\課堂練習, H:\\王文靜\\python, C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\pyth…

匿名方法

與前面的可空類型是一樣的&#xff0c;匿名方法也是C# 2.0里面提出來的。 1 匿名方法 1.1 什么是匿名方法&#xff1f; 顧名思義&#xff0c;就是沒有名稱的方法&#xff0c;因為沒有名稱&#xff0c;匿名方法只能在函數定義&#xff08;匿名方法是把方法的實現和定義嵌套在了一…

使用React,Redux和Router進行真正的集成測試

by Marcelo Lotif通過馬塞洛洛蒂夫(Marcelo Lotif) 使用React&#xff0c;Redux和Router進行真正的集成測試 (Real integration tests with React, Redux and Router) After being bitten a couple of times by bad refactoring and a broken app?—?even with all my tests…

Go語言從入門到精通 - 數據類型轉換

本節核心內容 介紹 Go語言數據類型轉換的格式介紹 數據轉換代碼示例介紹 數據轉換過程中的注意事項 本小節視頻教程和代碼&#xff1a;百度網盤 可先下載視頻和源碼到本地&#xff0c;邊看視頻邊結合源碼理解后續內容&#xff0c;邊學邊練。 Go語言數據類型轉換 Go 語言使用類型…

JNI通過線程c回調java層的函數

1、參看博客&#xff1a;http://www.jianshu.com/p/e576c7e1c403 Android JNI 篇 - JNI回調的三種方法&#xff08;精華篇&#xff09; 2、參看博客&#xff1a; JNI層線程回調Java函數關鍵點及示例 http://blog.csdn.net/fu_shuwu/article/details/41121741 3 http://blog.cs…

signature=f7a4b29b93ef2b36608792fdef7f454a,Embedding of image authentication signatures

摘要&#xff1a;A method (), an apparatus, a computer readable medium and use of said method for authenticating an audio-visual signal (), such as a digital image or video, are disclosed. A signature is derived from all image regions, including areas with …

glob

主要是用來在匹配文件&#xff0c;相當shell中用通配符匹配. 用法: glob.glob(pathname) # 返回匹配的文件作為一個列表返回 glob.iglob(pathname) # 匹配到的文件名&#xff0c;返回一個迭代器 ps: pathname是路徑, 可以是絕對和相對路徑 匹配當前目錄下有一個數字開頭…

構建微服務:Spring boot 入門篇

Spring官方網站本身使用Spring框架開發&#xff0c;隨著功能以及業務邏輯的日益復雜&#xff0c;應用伴隨著大量的XML配置文件以及復雜的Bean依賴關系。隨著Spring 3.0的發布&#xff0c;Spring IO團隊逐漸開始擺脫XML配置文件&#xff0c;并且在開發過程中大量使用“約定優先配…

img 加載 svg占位符_如何使用SVG作為占位符以及其他圖像加載技術

img 加載 svg占位符by Jos M. Prez由JosM.Prez 如何使用SVG作為占位符以及其他圖像加載技術 (How to use SVG as a Placeholder, and Other Image Loading Techniques) I’m passionate about image performance optimisation and making images load fast on the web. One of…

hibernate 注解

參考鏈接地址&#xff1a;https://blog.csdn.net/wx5040257/article/details/78697119 主鍵生成策略:https://www.cnblogs.com/ph123/p/5692194.html 注解轉載于:https://www.cnblogs.com/wangxuekui/p/10287647.html

iOS - UIScrollView

前言 NS_CLASS_AVAILABLE_IOS(2_0) interface UIScrollView : UIView <NSCoding>available(iOS 2.0, *) public class UIScrollView : UIView, NSCoding 移動設備的屏幕大小是極其有限的&#xff0c;因此直接展示在用戶眼前的內容也相當有限。當展示的內容較多&…

機器學習的展望

現階段越來越多的投入到機器學習的熱潮中來&#xff0c;有的人很是興奮&#xff0c;認為這是一場新和革命&#xff0c;一場終極人工智能來臨的前夜。也有人表示悲觀&#xff0c;認為不僅機器學習不代表終極人工智能&#xff0c; 也還非常不成熟。 大量的新生代投入到這個領域&a…

BZOJ3453 XLkxc(拉格朗日插值)

顯然f(i)是一個k2項式&#xff0c;g(x)是f(i)的前綴和&#xff0c;則顯然其是k3項式&#xff0c;插值即可。最后要求的東西大膽猜想是個k4項式繼續插值就做完了。注意2p>maxint…… #include<iostream> #include<cstdio> #include<cmath> #include<cs…