Neural Network + Genetic Algorithm + Game = ❤

栏目: IT技术 · 发布时间: 4年前

内容简介:First of all, let me describe the game. As you have probably guessed from the gif, the goal of this game is to avoid the red bars and move the green creature forward. The further you go the more your score will be. To make the game realistic, I’ve implemen

This is how I created an A.I. that beat this game completely!

Apr 18 ·5min read

Neural Network + Genetic Algorithm + Game = ❤

A.I. plays the game

The Game: Rules and Moves

First of all, let me describe the game. As you have probably guessed from the gif, the goal of this game is to avoid the red bars and move the green creature forward. The further you go the more your score will be. To make the game realistic, I’ve implemented gravity which means if the creature moves up it must come down with the given acceleration. But how/when does it jump? To be able to jump it must be on the ground. At this position, the creature can apply an action force on the ground and the ground will apply a reaction force on it. This reaction will help it to jump (just like how we jump). The x-component of this force will produce an acceleration in the x-direction and the same is true for y-direction.

Note: For the sake of simplicity I’ve not implemented the action-reaction mechanism instead I’m working with the accelerations only. When the creature is on the ground it can decide it’s x and y accelerations!

Neural Network + Genetic Algorithm + Game = ❤

Physics of jumping

The challenge is to find the values of these accelerations such that it can safely jump over the next red bar! It is to be noted that the creature can’t change it’s velocity when it’s in the air (why? left for the readers). The collisions with the ground are inelastic hence it doesn’t bounce automatically.

Building the AI

There are many ways to approach these kinds of problems. Here, I am applying something called Neuroevolution, which is a combination of Neural Network and Genetic Algorithm .

I. Neural Network (NN)

To be able to ‘think’ ( when and how to jump) the creature needs some kind of decision-making model. In my approach, I’m using a neural network as the brain of our little friend. I’ve used a small nn containing 6 nodes in the input layer, 8 in the hidden layer and 2 in the output layer. But what goes in those 6 input nodes and what comes out of the 2 output nodes? Let’s first see the output nodes. 2 nodes decide the values of acceleration in two directions. As I’ve stated earlier, the creature just needs to decide it’s x-acceleration and y-acceleration.

Neural Network + Genetic Algorithm + Game = ❤

The architecture of our NN showing input features and outputs

For the input nodes, there can be many sets of possible features. The aim is to provide as much information to the brain about the creature’s current state and its surroundings as possible. I am using this 6 information (can you come up with a better set of features?)

II. Genetic Algorithm (GA)

As the name suggests, it has something to do with genetics. It is one kind of Evolutionary Algorithm where we try to mimic biological evolution to find an optimal solution for a given problem. We start with a set of solutions and choose the best ones out of them and let them evolve. Loosely speaking, every genetic algorithm follows 5 steps.

  • Initial Population: The initial set of solutions. Each solution has certain genes (set of parameters) that determine its behavior. The genes vary among the individuals and this is known as variation .
Note: If there is no variation in the initial population then each solution will evolve exactly in the same way and we will fail to explore orher solutions!
  • Fitness Function: A mathematical function to determine how fit (optimal) a solution is by assigning a fitness score to every solution. This will be helpful in the next step.
  • Selection: In this step, we select the fittest individuals and allow them to pass their genetic information to the next generation.
  • Crossover: Combining the genes of two (or more) parents to produce one individual of the next generation.
  • Mutation: While transferring genetic information from the current generation to the next one a very small percentage of the information is changed randomly.
Note: Because of mutation, some new traits (properties) will be introduced that will ‘hopefully’ make our solution more optimal. ‘Hopefully’ because mutaion is random and can affect negatively too!

III. Combining NN with GA

Now is the time for the big reveal. How would you combine these two completely different algorithms? The main idea is to consider the weights of the nn (brain) as genes. Initially, we start with a population of 250 creatures each with its separate brain. To have variation in the population we initialize the weights for these neural networks randomly. We will pass the best weights to the next generation. How would you determine which balls are better? We will use the fitness score. In my case, the fitness score is a linear function of distance covered by the creature. So, the creatures who have covered the largest distances will have a greater probability of creating offsprings. Thus their nn’s weights will be transferred to the next generation. Now, let me mention a couple of details here…

i) I’ve not used the concept of crossover so one creature can create its offspring without mating with another one.

ii) Creating offsprings (in this case) means copying the weights of one creature’s nn and making a new one with the previous creature’s weights after adding some mutation.

iii) The mutation is nothing but changing a small percentage (generally less than 2% ) of weights randomly by a small amount.

Doing this process over and over again mimics the biological evolution and after many generations, we hope to obtain some creatures with optimal weights. In other words, after many generations, we will have some little guys who can decide the values of x and y accelerations such that it can jump over the next red bar. In terms of Machine Learning, we have successfully trained our model :D

But we never used backpropagation!

In traditional Deep Learning we use a gradient based optimization technique (aka backpropagation) to train neural networks. In that case we take the last layer’s error and propagate it backwords and in each layer we calculate the gradient and based on this we change the corresponding layer’s weights. But in this case (GA+NN) we can’t perform backprop. Why? Because we can’t calculate error from the last layer (as we don’t know the ground truths). We are solely dependent on the selection and mutation for learning.

Want to watch the A.I. learn? Here’s the video…

Visit the web app…

https://suji04.github.io/jumpingameAI/

Code for the game and AI…

https://github.com/Suji04/jumpingameAI


以上所述就是小编给大家介绍的《Neural Network + Genetic Algorithm + Game = ❤》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

掘金大数据

掘金大数据

程新洲、朱常波、晁昆 / 机械工业出版社 / 2019-1 / 59.00元

在数据横向融合的时代,充分挖掘数据金矿及盘活数据资产,是企业发展和转型的关键所在。电信运营商以其数据特殊性,必将成为大数据领域的领航者、生力军。各行业的大数据从业者要如何从电信业的大数据中挖掘价值呢? 本书彻底揭开电信运营商数据的神秘面纱,系统介绍了大数据的发展历程,主要的数据挖掘方法,电信运营商在网络运行及业务运营方面的数据资源特征,基于用户、业务、网络、终端及内在联系的电信运营商大数据分......一起来看看 《掘金大数据》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码