# AI for continuous snake game (trying NEAT algorithm)

This topic is 1671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi!

I'm trying to evolve an AI for a game of 'continuous' snake.

Check this vid to understand the game:

Basically, the snake drives a bit like a car and the first one of the 2 players to crash into his trail or the other's trail loses the game.

What ideas would you have guys to design an AI for this ?

The state space is huge: ideally there'd be 60fps per second and every frame, the snake can steer from -1 (left) to +1 (right) including all the values in between.

So A* wouldn't cut it.

My current try is to use the NEAT algorithm (http://www.cs.ucf.edu/~kstanley/neat.html).

I have the framework working and the AI is starting to show some traces of intelligence.

The fitness value doesn't go up much though and it tends to converge toward the spiral pattern. It might be a good strategy though

The inputs I've chosen so far are:

- the opponent angle relative to us

- the opponent distance

- the opponent heading relative to us

- the eyes: inputs representing how far a certain number of ray casts can go before hitting a wall (represented on the vid)

I've attached the code to this post if anyone wants to try it.

You just need to install the SharpDX packages using nuget and hit F5.

Basically I'm interested in knowing what you think, and what other ideas you could give me for coding an AI for this game.

Thanks!

edit:

Numpad 4 and 6 : turn left and right

Space : Restart a game against the new champion

E : Pause/Unpause evolution. Useful to get a responsive UI. Pauses after the current iteration (8s on my machine)

##### Share on other sites

Ah, very interesting article thanks, I'll read it carefully tomorrow for insights.

Basic avoidance steering would work to avoid obstacles, but it'd be pretty stupid indeed.

A good player needs to:

2) Notice occasions when it can 'cut short' the other one (when next to him a bit ahead).

3) Avoid getting 'cut short'.

4) Also ideally a great player would need an idea of the topology of the current 2d space to try to enclose the other one in a smaller space / safeguard himself a bigger space.

My approach seems to have potential indeed as it starts to exhibit 1), 2) and 3). I wonder what other kind of inputs I could give the neural network though...

Also for some reason, my networks do not evolve any hidden neurons so far. I probably need to get more familiar with the NEAT algorithm.

##### Share on other sites

Ah, very interesting article thanks, I'll read it carefully tomorrow for insights.

Basic avoidance steering would work to avoid obstacles, but it'd be pretty stupid indeed.

A good player needs to:

2) Notice occasions when it can 'cut short' the other one (when next to him a bit ahead).

3) Avoid getting 'cut short'.

4) Also ideally a great player would need an idea of the topology of the current 2d space to try to enclose the other one in a smaller space / safeguard himself a bigger space.

My approach seems to have potential indeed as it starts to exhibit 1), 2) and 3). I wonder what other kind of inputs I could give the neural network though...

Also for some reason, my networks do not evolve any hidden neurons so far. I probably need to get more familiar with the NEAT algorithm.

You might be able to simulate some of the 2nd and 3rd points with basic steering.  If the AI is ahead of the opponent, instead of avoidance, it could have an attractive parallel force, while if it is behind someone, it could have a repulsive force to the player.  Though there would be no way to do 4.

Or it might just end up a fiddly mess =)

I look forward to seeing how the NEAT algorithm does though, and it looks promising.

Edited by ferrous

##### Share on other sites

(also the exe would be nice)

Edited by SerialKicked

##### Share on other sites

Sure, I've added the .exe in the first post. Let me know how it works for you !

I've not made huge progress really, I've been mostly playing with the parameters.

I got some ideas for new inputs, I'll post again when I implement them!

##### Share on other sites

Thanks, It worked perfectly (Win7 4core AMD CPU -> used 100% with evo on)

A few things i noticed, no idea if it's of any use to you:

I've not played extensively and in general it tries to get the same spiral pattern you described earlier.

However, on my first go, its behavior was pretty weird, maybe because I died a few times stupidly at the beginning of the round (getting used to the lag, a sleep somewhere would help a bit). Maybe it reached an evolutionary dead end, I dunno. Anyway, even after 150+ generations its go-to behavior was to draw a simple circle and kill itself. After a few rounds it tried to go for a spiral again and / or follow me, failed and back to the circle for a few more rounds (rinse and repeat).

The graph was still getting fairly complicated, with all top nodes connected to the bottom ones and several additional nodes created in the middle. I probably should have taken a screenshot, sorry i forgot.

When it gets a good generation, it can be a fun opponent indeed, much less predictable than what you'd usually expect in this kind of game.

Edited by SerialKicked

##### Share on other sites

Cool! thanks for trying.

Yes, you're right in your descriptions:

- you need to be a bit lucky to make it break from its spiral pattern

- it does now evolve hidden neurons and cyclic connections (I got the parameters right)

- you can remove the lag by pressing 'E'. And at the end of the current generation, it will pause the evolution algorithm so you can play without lag. Press 'E' again to resume evolution.

Also, it does not learn from you playing against it. It just learns by playing against different versions of itself in the background.

I've implemented my first idea of the new inputs. I call them smart rays, it's a bit hard to explain in detail, but basically they do a depth first search on certain paths and it does improve the AI a lot. Here's a vid of me (yellow) playing against it:

Notice I've evolved it to about the 30th generation (took a while) before taking the vid. And it even beat me a few times fair and square. These smart rays do give it a bit of an indication about the topology of the playfield. When it gets good at making sense of the smartrays, it seems it forgets a bit how to cut me short when it has the chance though.

I've added an .exe for it (_V2) in the first post.

A few notes about it in no particular order:

- When you start it, you'll need to wait about 2min for the 1st gen to finish before you can play (quad core intel i5-3570k 3.40GHz). Then press space to play against the AI.

- I've disabled multithreading because it causes exceptions I can't explain. So you should have a mostly lag free experience but the evolving is core times slower.

- It's again doing the silly spiral pattern at the beginning, but did evolve out of it for me.

- The .exe is exactly the same as the one I used for producing the video this time (uses Randoms() based on time seeds, so might be different every time)

- It does lag a lot when the smart rays all get blocked, but that doesn't happen too often. Might be a bug on my part.

- I had lots of fun playing it

I now think a good AI for it will come from switching between different AIs with a state machine (got some more ideas).

Again, if you can try it, I'd love your feedback!

##### Share on other sites

If there is only 1 AI snake on the gamefield and no players, what the longest survival time it can reach?

Edited by tom_mai78101

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 11
• ### Forum Statistics

• Total Topics
634113
• Total Posts
3015592
×