<p>Q-Learning and Genetic algorithm are both good algorithms to create an IA that plays Snake.</p>
<p>The one you use depends mostly on how you understand and model your IA environment.</p>
<ul>
<li>Q-Learning algorithm is an algorithm that needs a <strong>State</strong> (give by
the environment), <strong>Actions</strong> it can take, and <strong>Rewards</strong> to give
him according to how it performs.</li>
<li>Genetic algorithm needs to have <strong>intrinsic parameters</strong> (could be caracteristics of the network taking the decisions, or more simple things like leg size / muscle strenght if you want to make an IA that runs), how to <strong>merge 2 parents</strong> to create children, and a <strong>Metric</strong> to evaluate how the network performed.</li>
</ul>
<p>I assume your snake is make of a grid (like old snake where you only have 4 directions, you can't go in diagonal directions).</p>
<p>In snake example, here is how I would define it in both case (careful, this is how I would model the problem, there might be more efficient models) :</p>
<h2>Q-Learning</h2>
<ul>
<li>State : what is in each block of the grid (snake tail, snake head, food)</li>
<li>Actions : Up, Down, Right, Left</li>
<li>Rewards : Not dying : +0.01; dying : -10; eating piece of food: +1.</li>
</ul>
<h2>Genetic Algorithm</h2>
<ul>
<li>Intrinsic Parameters : Parameters of the network that takes decisions (inputs of the network could be whole state of the environment as described in Q-Table).</li>
<li>How to merge : ?</li>
<li>Metric : Lenght of the snake or time lived.</li>
</ul>
<p>I can't find exactly how to make a genetic algorithm, but it should be possible.</p>
<p>Hope it helps. Q-Learning is usually easier so this is what I would use, but feel free to use whatever you want.</p>

CC BY-SA 4.0
2021-05-08T12:46:50.937Z
2021-05-08T12:46:50.937Z
Ubikuity
<p>I am working on a DIY project in which I want to be able to train a neural network to play Snake.</p>
<ul>
<li><p>Is a genetic algorithm an efficient way of training a network for this application?</p>
</li>
<li><p>For a GA, what should the inputs of the network be? (distance to walls and fruit or the squares in the proximity of the snake head as a vector)</p>
</li>
<li><p>What would the difference in efficiency be depending on the algorithm and what limitation does each one have? Are there any other alternatives I should consider?</p>
</li>
</ul>

<neural-networks><q-learning><genetic-algorithms>
Is a genetic algorithm efficient for a snake game?
XDead
# Is a genetic algorithm efficient for a snake game?

(Asked by: username_0 on 2021-05-07)

I am working on a DIY project in which I want to be able to train a neural network to play Snake.

* Is a genetic algorithm an efficient way of training a network for this application?
* For a GA, what should the inputs of the network be? (distance to walls and fruit or the squares in the proximity of the snake head as a vector)
* What would the difference in efficiency be depending on the algorithm and what limitation does each one have? Are there any other alternatives I should consider?

**Tags:** neural-networks, q-learning, genetic-algorithms


---

## 1 answers

### Answer by username_1 on 2021-05-08. Score: 1

Q-Learning and Genetic algorithm are both good algorithms to create an IA that plays Snake.

The one you use depends mostly on how you understand and model your IA environment.

* Q-Learning algorithm is an algorithm that needs a **State** (give by
  the environment), **Actions** it can take, and **Rewards** to give
  him according to how it performs.
* Genetic algorithm needs to have **intrinsic parameters** (could be caracteristics of the network taking the decisions, or more simple things like leg size / muscle strenght if you want to make an IA that runs), how to **merge 2 parents** to create children, and a **Metric** to evaluate how the network performed.

I assume your snake is make of a grid (like old snake where you only have 4 directions, you can't go in diagonal directions).

In snake example, here is how I would define it in both case (careful, this is how I would model the problem, there might be more efficient models) :

## Q-Learning

* State : what is in each block of the grid (snake tail, snake head, food)
* Actions : Up, Down, Right, Left
* Rewards : Not dying : +0.01; dying : -10; eating piece of food: +1.

## Genetic Algorithm

* Intrinsic Parameters : Parameters of the network that takes decisions (inputs of the network could be whole state of the environment as described in Q-Table).
* How to merge : ?
* Metric : Lenght of the snake or time lived.

I can't find exactly how to make a genetic algorithm, but it should be possible.

Hope it helps. Q-Learning is usually easier so this is what I would use, but feel free to use whatever you want.


---

