AI for continuous snake game (trying NEAT algorithm)

Started by
14 comments, last by rouncer 9 years, 9 months ago

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:

Added the exe

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)

Advertisement

Interesting, seems to work okay with what you have?

Some other options:

Some basic avoidance steering AI (it would be somewhat stupid, but it might work just fine)

Marco Pinter's method, which is A* + grid + dubin's car: http://www.gamasutra.com/view/feature/131505/toward_more_realistic_pathfinding.php?print=1

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:

1) Avoid walls. Your two suggestions address that.

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.

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:

1) Avoid walls. Your two suggestions address that.

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.

Yes, this sounds fiddly indeed smile.png But I appreciate the input and the interest.

I've tuned the AI some more and got something that starts to be a bit decent.

Here, I'm playing against it (I'm yellow, the AI is blue). It's actually pretty fun:) I was quite surprised.

As you can see, it's still severely lacking in number 4) above (topological analysis) but I do have some ideas for it now.

I'll try adding some inputs representing the shape of the topology toward some angles based on the results of some forward search on a grossly discretized state space. It's not yet clear in my mind. I know I need something somewhat continous for the AI to learn from it. Also again it evolves this spiral pattern which is actually not that bad.

Surprisingly, as you can see, the evolved neural network has only 2 of the inputs connected (or I don't understand the NEAT algorithm yet)

I can't wait to get more time on it. If someone wants to play it, I'll be happy to upload an exe (it's very simple to use)

I can't contribute sadly having never heard of NEAT before, but i'd be interested in reading about your progress smile.png

(also the exe would be nice)

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!

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.

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 cool.png

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!

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

This topic is closed to new replies.

Advertisement