<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Learning to Play Pong Video Game via Deep Reinforcement Learning</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Ilya Makarov</string-name>
          <email>iamakarov@hse.ru</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Andrej Kashin</string-name>
          <email>kashin.andrej@gmail.com</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Alisa Korinevskaya</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>National Research University Higher School of Economics, School of Data Analysis and Arti cial Intelligence</institution>
          ,
          <addr-line>3 Kochnovskiy Proezd, 125319 Moscow</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>We consider deep reinforcement learning algorithms for playing a game based on the video input. We discuss choosing proper hyperparameters in the deep Q-network model and compare with the modelfree episodic control focused on reusing of successful strategies. The evaluation was made based on the Pong video game implemented in Unreal Engine 4.</p>
      </abstract>
      <kwd-group>
        <kwd>Deep Reinforcement Learning</kwd>
        <kwd>Deep Q-Networks</kwd>
        <kwd>Q-Learning</kwd>
        <kwd>Episodic Control</kwd>
        <kwd>Pong Video Game</kwd>
        <kwd>Unreal Engine 4</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>
        Reinforcement learning (RL) is a eld of machine learning, that is dedicated to
agents acting in the environment in order to maximize some cumulative reward.
Actions of an agent are rewarded by the environment to reinforce correct
behaviour of the agent. In this case the agent is able to automatically learn the
optimal strategy. Methods of reinforcement learning appeared to be useful in
many areas where AI is involved: robotics [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ], industrial manufacturing [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], and
video games [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. RL usually solves sequential decision making problems [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. We
follow success of [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] experiment in order to test applicability of episodic control
RL algorithm in Pong video game.
1.1
      </p>
      <sec id="sec-1-1">
        <title>Reinforcement Learning Basics</title>
        <p>
          An RL agent interacts with an environment over time. At each time step t the
agent is situated in a state st . The agent selects an action at from some action
space A , following a policy (atjst) . This policy is a probability distribution
describing the agent behavior, i.e., a mapping from state st to actions at . Then
agent receives a scalar reward rt from the environment, and transitions to the
next state st+1 according to the environment dynamics or model: reward
function R(s; a) and state transition probability P (st+1jst; at) respectively. In an
episodic RL problem [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ], this process continues until the agent reaches a terminal
state and then restarts. The return Rt = Pk1=0 krt+k is the discounted,
accumulated reward with the discount factor 2 (0; 1] . The agent aims to maximize
the expectation of such long term return from each state.
        </p>
        <p>
          To determine agent preference over states, a value function is introduced as
a prediction of the expected accumulated and discounted future reward. The
action-value function Q (s; a) = E[Rtjst = s; at = a] is the expected return
for selecting action a in state s and following policy afterwards. An optimal
action value function Q (s; a) is the maximum action value achievable by any
policy for state s and action a . We could de ne a state value V (s) function
and the optimal state value V (s) in the same way [
          <xref ref-type="bibr" rid="ref4">4</xref>
          ].
        </p>
        <p>Temporal di erence (TD) learning is the key idea in RL. It learns a value
function V (s) online directly from the experience with TD error, bootstrapping
in a model-free and fully incremental way.</p>
        <p>In TD learning the update rule has the following form:</p>
        <p>V (st)</p>
        <p>V (st) +
[rt + V (st+1)</p>
        <p>V (st)]
where is a learning rate, and rt + V (st+1) V (st) is called TD error.</p>
        <p>Similarly, a Q-learning agent learns action-value function with the update
rule</p>
        <p>Q(st; at)</p>
        <p>Q(st; at) + [r +
max Q(st+1; at+1)
at+1</p>
        <p>Q(st; at)]</p>
        <p>In contrast to Q-learning which is o -policy approach, SARSA is an on-policy
control method, with the update rule</p>
        <p>QEC (st; at)
(</p>
        <p>Rt
max QEC (st; at); Rt
if (st; at) 2= QEC
otherwise
(1)
(2)
(3)
(4)
1.2</p>
      </sec>
      <sec id="sec-1-2">
        <title>Episodic Control</title>
        <p>
          While Q-networks improves performance after gradient-based optimization
procedures, model-free episodic control (EC) [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ] is focused on reusing of successful
strategies. Although the model-free episodic control algorithm is seemingly closer
to the human learning, in the real world we rarely encounter exactly the same
situation over and over again. But in games with the nite number of states,
episodic control could run correctly.
        </p>
        <p>In order to remember most successful strategy, EC agent makes use of
additional structure { QEC (s; a) table where the best action-values for each state
are kept. At each time-step in the particular state of the environment the agent
peeks the action with the maximal value of QEC (s; a) . At the end of each episode
the value is updated in the following way:</p>
        <p>Q(st; at)</p>
        <p>Q(st; at) + [r +</p>
        <p>Q(st+1; at+1)</p>
        <p>Q(st; at)]</p>
        <p>SARSA re nes the policy greedily with respect to action values. TD-learning,
Q-learning and SARSA converge under certain conditions. From optimal
actionvalue function one can derive an optimal policy for RL agent.</p>
        <p>To carry out maximization's of QEC (s; a) over all states, it is regarded as
a nearest neighborhood model. Then of particular interest is the mapping of
observations onto states as observations ot tend to have high dimensionality
than drastically slows down KNN search.
2</p>
      </sec>
    </sec>
    <sec id="sec-2">
      <title>Pong</title>
      <p>
        To illustrate application of RL methods, we implemented them in the Pong
Game environment [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] designed in Unreal Engine 4. Unreal Engine 4 (UE4, [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ])
is a powerful suite of integrated tools for game developers to design and build
games, simulations, and visualizations. Since its release in 1998 Unreal Engine
has became a staple among the community.
      </p>
      <p>
        The main goal of this work is to prove that it is feasible to apply methods
of RL inside UE4. We show that it is indeed possible to e ciently train and
apply model built with the modern machine learning framework (in our case
TensorFlow [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]) in UE4.
      </p>
      <p>We achieve this by patching a plugin to support Python scripting inside
UE4, implementing C++ module for capturing game screenshots, and creating
TensorFlow-based Python controller for the player paddle.
2.1</p>
      <sec id="sec-2-1">
        <title>Environment</title>
        <p>We use physics based UE4 implementation of classical Pong environment.</p>
        <p>This screenshot from the environment shows main elements of the game:
1. Paddles. Every player controls a solid rectangle called paddle that can
reect the ball.
2. Ball. The ball is a rigid body that moves with a constant speed and re ects
from the walls and paddles.
3. Walls. The walls are present on the top and on the bottom of the screen.
4. Goals. The left and right sides of the screen represent goals. In order to
score a point, the player needs to hit the opposite goal with the ball.
5. Scores. The top part of the screen shows two numbers that are equal to the
number of points each player have scored.</p>
        <p>The purpose of the game is to maximize the di erence between your score
and opponent score, while making it positive during time limit.</p>
        <p>The human player uses raw pixels (screenshots, 5 frames per seconds) as an
input during play, and outputs three types of actions using the keyboard:
{ Key up: move paddle up
{ Key down: move paddle down
{ Idle: paddle stays at the same place</p>
        <p>The game comes with the built-in AI controller that gets high-level features
such as position and speed of the ball, position of his and opponent paddle as
an input, and uses rule-based approach to control the paddle.
2.2</p>
      </sec>
      <sec id="sec-2-2">
        <title>Python Scripting</title>
        <p>In Unreal Engine 4, the standard way to implement game logic is through writing
C++ modules or using internal visual scripting language called Blueprints. The
C++ approach is more powerful and is used to implement core game logic and
reusable modules, though in many cases it is overly verbose, and does not allow
to quickly iterate on the solution due to slow compilation speeds. On the other
hand, blueprints are simple to create and understand, provide faster development
cycles, but yet not expressible enough in many cases.</p>
        <p>When it comes to the modern machine learning engines, they are usually
written in C++, while the provided public API comes in form of Python
bindings. Although it is possible to use TensorFlow C++ API, it limits the reuse
of openly available RL algorithms implemented in Python, and slows down the
research process due to the nature of the C++ language.</p>
        <p>
          Because of all these reasons we looked into alternative languages support
for UE4 and identi ed two candidates: Python and Lua. Both languages are
supported through third-party plugins available on GitHub, UETorch [
          <xref ref-type="bibr" rid="ref9">9</xref>
          ] for
Lua and UnrealEnginePython [
          <xref ref-type="bibr" rid="ref10">10</xref>
          ] for Python.
        </p>
        <p>As the authors were more familiar with Python and TensorFlow, it was chosen
as the primary language.</p>
        <p>The UnrealEnginePython plugin allows users to write Python scripts that
interact with UE4 by being able to access and mutate the internal state of the
game. This can be used to obtain current position of the ball and paddle, and
giving the commands to move the paddle.During this work the original plugin
was extended to support Python-based controllers in UE4.
2.3</p>
      </sec>
      <sec id="sec-2-3">
        <title>RL Model Implementation</title>
        <p>
          We implement AI controller using TensorFlow machine learning framework [
          <xref ref-type="bibr" rid="ref11">11</xref>
          ]
to utilize GPU and multi-core CPU resources. The reward of +1 is given to the
agent when it scores a point, and -1 when the opponent scores a point. The
agent is trained with xed 32 FPS. The screenshots are binarized and rescaled
to resolution 80x80.
        </p>
        <p>
          We use Deep Q-Network (DQN) [
          <xref ref-type="bibr" rid="ref12">12</xref>
          ] learning algorithm to train and control
AI agent. Episodic Control was implemented with an embedding function
represented as a random projection. It the simplest way to reduce dimensionality
while preserving Euclidean distances.
3
        </p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Results</title>
      <p>We have built a Deep RL based controller that outperforms the standard
rulebased AI, while using raw pixels as an input. The training takes 6 hours on GPU
and it takes 10 million iterations to beat the built-in scripted AI.</p>
      <p>
        However, with regard to Episodic Control it was noticed that Euclidean
distance is impractical way to measure frames' similarity. In case of binary images,
Euclidean distance scores only sum of di erent pixels but not their relative
proximity. In other words, using Euclidean distance it is impossible to choose frames
where a ball is closer to its current position. Later, the authors found a result
on too long applicability of episodic control in similar tasks [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ]. This problem
can be dealt with Variational Auto Encoder (VAE) [
        <xref ref-type="bibr" rid="ref14">14</xref>
        ] in the future work for
more complex 3D game [
        <xref ref-type="bibr" rid="ref15">15</xref>
        ].
      </p>
      <p>
        The code implementation is available on GitHub ([
        <xref ref-type="bibr" rid="ref16">16</xref>
        ], [
        <xref ref-type="bibr" rid="ref17">17</xref>
        ]).
The work was supported by the Russian Science Foundation under grant
1711-01294 and performed at National Research University Higher School of
Economics, Russia.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Smart</surname>
          </string-name>
          , W.D.,
          <string-name>
            <surname>Kaelbling</surname>
            ,
            <given-names>L.P.:</given-names>
          </string-name>
          <article-title>E ective reinforcement learning for mobile robots</article-title>
          .
          <source>In: Robotics and Automation</source>
          ,
          <source>2002. Proceedings. ICRA'02. IEEE International Conference on. Volume</source>
          <volume>4</volume>
          ., IEEE (
          <year>2002</year>
          )
          <volume>3404</volume>
          {
          <fpage>3410</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Mahadevan</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Theocharous</surname>
          </string-name>
          , G.:
          <article-title>Optimizing production manufacturing using reinforcement learning</article-title>
          .
          <source>In: FLAIRS Conference</source>
          .
          <article-title>(</article-title>
          <year>1998</year>
          )
          <volume>372</volume>
          {
          <fpage>377</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Cole</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Louis</surname>
            ,
            <given-names>S.J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Miles</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          :
          <article-title>Using a genetic algorithm to tune rst-person shooter bots</article-title>
          .
          <source>In: Proceedings of the 2004 Congress on Evolutionary Computation (IEEE Cat. No.04TH8753)</source>
          . Volume
          <volume>1</volume>
          . (
          <year>June 2004</year>
          )
          <volume>139</volume>
          {145 Vol.1
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Mnih</surname>
          </string-name>
          et al.:
          <article-title>Playing atari with deep reinforcement learning</article-title>
          .
          <source>Review of Scienti c Instruments</source>
          <volume>72</volume>
          (
          <issue>12</issue>
          ) (
          <year>December 2013</year>
          )
          <volume>4477</volume>
          {
          <fpage>4479</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Blundell</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Uria</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Pritzel</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Li</surname>
            ,
            <given-names>Y.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ruderman</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Leibo</surname>
            ,
            <given-names>J.Z.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rae</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wierstra</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Hassabis</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          :
          <article-title>Model-free episodic control</article-title>
          .
          <source>Review of Scienti c Instruments</source>
          (
          <volume>14</volume>
          ) (
          <year>June 2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6. Atari: Atari arcade: Pong game [Online: Accessed:
          <fpage>2017</fpage>
          -05-14].
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Games</surname>
          </string-name>
          , E.:
          <article-title>Unreal engine technology</article-title>
          . https://www.unrealengine.com/ [Online: Accessed:
          <fpage>2017</fpage>
          -05-14].
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>Google</given-names>
            <surname>Inc</surname>
          </string-name>
          .:
          <article-title>Tensor ow</article-title>
          . https://www.tensor ow.org/ [Online: Accessed:
          <fpage>2017</fpage>
          -05- 14].
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Lerer</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          : Uetorch. https://github.com/facebook/UETorch [Online: Accessed:
          <fpage>2017</fpage>
          -05-14].
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Ioris</surname>
          </string-name>
          , R.D.: UnrealEnginePython. https://github.com/20tab/UnrealEnginePython [Online: Accessed:
          <fpage>2017</fpage>
          -05-14].
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <surname>Abadi</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          , et al.:
          <article-title>TensorFlow: Large-scale machine learning on heterogeneous systems (2015) Software available from tensor ow</article-title>
          .
          <source>org.</source>
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <surname>Mnih</surname>
            ,
            <given-names>V.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kavukcuoglu</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Silver</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rusu</surname>
            ,
            <given-names>A.A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Veness</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bellemare</surname>
            ,
            <given-names>M.G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Graves</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Riedmiller</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Fidjeland</surname>
            ,
            <given-names>A.K.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ostrovski</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Petersen</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Beattie</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sadik</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Antonoglou</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>King</surname>
            ,
            <given-names>H.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kumaran</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wierstra</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Legg</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Hassabis</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          :
          <article-title>Human-level control through deep reinforcement learning</article-title>
          .
          <source>Nature</source>
          <volume>518</volume>
          (
          <issue>7540</issue>
          ) (
          <year>02 2015</year>
          )
          <volume>529</volume>
          {
          <fpage>533</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          13.
          <string-name>
            <surname>Pritzel</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Uria</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Srinivasan</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Badia</surname>
            ,
            <given-names>A.P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Vinyals</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Hassabis</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wierstra</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Blundell</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          :
          <article-title>Neural episodic control</article-title>
          .
          <source>CoRR abs/1703</source>
          .
          <year>01988</year>
          (
          <year>2017</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          14.
          <string-name>
            <surname>Kingma</surname>
            ,
            <given-names>D.P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Welling</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          :
          <article-title>Auto-encoding variational bayes</article-title>
          .
          <source>Review of Scienti c Instruments</source>
          (
          <volume>1</volume>
          ) (May
          <year>2014</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          15.
          <string-name>
            <surname>Makarov</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          , et al.:
          <article-title>First-person shooter game for virtual reality headset with advanced multi-agent intelligent system</article-title>
          .
          <source>In: Proceedings of the 2016 ACM on Multimedia Conference. MM '16</source>
          , New York, NY, USA, ACM (
          <year>2016</year>
          )
          <volume>735</volume>
          {
          <fpage>736</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          16.
          <string-name>
            <surname>Kashin</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Tensor ow + unrealengine + python lab</article-title>
          . https://github.com/akashin/HSE AI Labs/tree/master/Lab 4 [Online: Accessed:
          <fpage>2017</fpage>
          -05-14].
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          17.
          <string-name>
            <surname>Korinevskaya</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Episodic control for pong game</article-title>
          . https://github.com/koral/episodic-control
          <string-name>
            <surname>-</surname>
          </string-name>
          pong-game [Online: Accessed:
          <fpage>2017</fpage>
          -05-14].
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>