Answers.Body: <p>A <a href="https://stats.stackexchange.com/questions/344667/how-is-deep-learning-integrated-into-the-reinforcement-learning/344693#344693">closely related question</a> and a <a href="https://github.com/bruceweir/gameaiexperiments/blob/master/reinforcementLearning/reinforcementLearningNeuralNetworkDodge.py" rel="noreferrer">minimal implementation written in Python</a>.</p>
  
  <p>That program implements the reinforcement learning technique 'Q-Learning'.</p>
  
  <p>The idea is for the program to take in an observation of the environment (which could be a screenshot if learning a computer game, or sensor data for a robot) and output a decision in the form of a vector of values. Each cell in that output vector corresponds to a possible action (left, right, shoot, etc) and the highest valued cell shows the action that the agent/player should take. The values in that output vector are called the Q-Values, and it is the mapping from the input data to the vector of Q-Values that we are trying to learn.
  In your case, the function that takes in an observation of the environment and spits out a vector of decision choices is your SVM.</p>
  
  <p>The question linked above contains a description of the training algorithm. It boils down to playing the game multiple times while storing the input vector, output vector and output decision for each step until you reach a termination condition (ie hitting an asteroid) and receiving a score (in this case a negative score since you want to <em>avoid</em> the asteroids). Then, going backwards through the the output vectors, assigning a slowly reducing amount of the output score to the particular cell in each output vector corresponding to the decision that was made at that step of the game.
  The array of input vectors from the game, and the array of output decision vectors that you have just created become the training data for your machine learning system. Once you have completed a training run, play the game again with your trained system and keep repeating until it is a good as you need it to be, or it stops getting any better.</p>
  
  <p>To address your question directly, I don't think that you are going to be able to train your system <em>without</em> giving it information about the position of the asteroids (assuming that they are positioned at random). However, you could try just having a very simple input vector such as 3 integer values where each value corresponds to the presence (1) or absence (0) of an asteroid in the 3 squares to the above left, above and above right of the player. That might be enough to encourage it to dodge away from falling rocks... </p>
  
  <p>There are some Support Vector Machines implemented in JavaScript, although I have never used them, but since SVMs are basically two-class classifiers you would have to check that the library can easily support multi-class SVMs. In your case the SVM would be trying to classify each input into a particular player choice (left, right, shoot, etc).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-18T22:04:47.130Z
Answers.LastActivityDate: 2018-09-18T22:04:47.130Z
Answers.UserDisplayName: DrMcCleod
Body: <h3>Introduction</h3>
  <p>An attractive asteroid game was described in the paper <a href="http://www.aaai.org/Papers/IJCAI/2007/IJCAI07-203.pdf" rel="nofollow noreferrer">Learning Policies for Embodied Virtual Agents through Demonstration</a> (2017, Jonathan Dinerstein et al.):</p>
  <blockquote>
  <p>In our first experiment, the virtual agent is a spaceship pilot, The pilot's task is to maneuver the spaceship through random asteroid fields</p>
  </blockquote>
  <p>In theory, this game can be solved with reinforcement learning or, more specifically, with a support vector machine (SVM) and epsilon-regression scheme with a Gaussian kernel. But it seems that this task is harder than it looks like, as the authors of the same paper write</p>
  <blockquote>
  <p>Although many powerful AI and machine learning techniques exist, it remains difficult to quickly create AI for embodied virtual agents.</p>
  </blockquote>
  <blockquote>
  <p>it is quite challenging to achieve natural-looking behavior since these aesthetic goals must be integrated into the fitness function</p>
  </blockquote>
  <h3>Questions</h3>
  <p>I really want to understand how reinforcement learning works. I built a simple game to test this. There are squares falling from the sky and you have the arrow keys to escape.</p>
  <p><a href="https://i.sstatic.net/uugSX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uugSX.png" alt="enter image description here" /></a></p>
  <p>How could I code the RL algorithm to solve this game? Can I do this manually in Javascript according to what I think should happen? How can I do this without having to map the positions of the rectangles and mine, just giving the agent the keyboard arrows to interact and three information:</p>
  <ul>
  <li>Player life</li>
  <li>Survival time</li>
  <li>Maximum survival time</li>
  </ul>
  
Tags: <machine-learning><reinforcement-learning><game-ai><javascript>
Title: How can I apply reinforcement learning to solve this asteroid game?
UserDisplayName: GIA
ThreadText: # How can I apply reinforcement learning to solve this asteroid game?
  
  (Asked by: username_0 on 2018-07-26)
  
  ### Introduction
  
  An attractive asteroid game was described in the paper [Learning Policies for Embodied Virtual Agents through Demonstration](http://www.aaai.org/Papers/IJCAI/2007/IJCAI07-203.pdf) (2017, Jonathan Dinerstein et al.):
  
  > In our first experiment, the virtual agent is a spaceship pilot, The pilot's task is to maneuver the spaceship through random asteroid fields
  
  In theory, this game can be solved with reinforcement learning or, more specifically, with a support vector machine (SVM) and epsilon-regression scheme with a Gaussian kernel. But it seems that this task is harder than it looks like, as the authors of the same paper write
  
  > Although many powerful AI and machine learning techniques exist, it remains difficult to quickly create AI for embodied virtual agents.
  
  > it is quite challenging to achieve natural-looking behavior since these aesthetic goals must be integrated into the fitness function
  
  ### Questions
  
  I really want to understand how reinforcement learning works. I built a simple game to test this. There are squares falling from the sky and you have the arrow keys to escape.
  
  [![enter image description here](https://i.sstatic.net/uugSX.png)](https://i.sstatic.net/uugSX.png)
  
  How could I code the RL algorithm to solve this game? Can I do this manually in Javascript according to what I think should happen? How can I do this without having to map the positions of the rectangles and mine, just giving the agent the keyboard arrows to interact and three information:
  
  * Player life
  * Survival time
  * Maximum survival time
  
  **Tags:** machine-learning, reinforcement-learning, game-ai, javascript
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-18. Score: 6 | Accepted answer
  
  A [closely related question](https://stats.stackexchange.com/questions/344667/how-is-deep-learning-integrated-into-the-reinforcement-learning/344693#344693) and a [minimal implementation written in Python](https://github.com/bruceweir/gameaiexperiments/blob/master/reinforcementLearning/reinforcementLearningNeuralNetworkDodge.py).
  
  That program implements the reinforcement learning technique 'Q-Learning'.
  
  The idea is for the program to take in an observation of the environment (which could be a screenshot if learning a computer game, or sensor data for a robot) and output a decision in the form of a vector of values. Each cell in that output vector corresponds to a possible action (left, right, shoot, etc) and the highest valued cell shows the action that the agent/player should take. The values in that output vector are called the Q-Values, and it is the mapping from the input data to the vector of Q-Values that we are trying to learn.
  In your case, the function that takes in an observation of the environment and spits out a vector of decision choices is your SVM.
  
  The question linked above contains a description of the training algorithm. It boils down to playing the game multiple times while storing the input vector, output vector and output decision for each step until you reach a termination condition (ie hitting an asteroid) and receiving a score (in this case a negative score since you want to *avoid* the asteroids). Then, going backwards through the the output vectors, assigning a slowly reducing amount of the output score to the particular cell in each output vector corresponding to the decision that was made at that step of the game.
  The array of input vectors from the game, and the array of output decision vectors that you have just created become the training data for your machine learning system. Once you have completed a training run, play the game again with your trained system and keep repeating until it is a good as you need it to be, or it stops getting any better.
  
  To address your question directly, I don't think that you are going to be able to train your system *without* giving it information about the position of the asteroids (assuming that they are positioned at random). However, you could try just having a very simple input vector such as 3 integer values where each value corresponds to the presence (1) or absence (0) of an asteroid in the 3 squares to the above left, above and above right of the player. That might be enough to encourage it to dodge away from falling rocks...
  
  There are some Support Vector Machines implemented in JavaScript, although I have never used them, but since SVMs are basically two-class classifiers you would have to check that the library can easily support multi-class SVMs. In your case the SVM would be trying to classify each input into a particular player choice (left, right, shoot, etc).
  
  
  ---
  
  