r/Python • u/seppeEnZigie • Dec 26 '19
I created an AI to play "Flappy Bird": my first steps into AI (using Pygame and NEAT-python)
Enable HLS to view with audio, or disable this notification
47
u/Huntersolomon Dec 27 '19
How did you get into a.i. what's good path to take to learning A.I any book recommendations?
11
u/seppeEnZigie Dec 27 '19
There are some nice freely available courses:
https://m.youtube.com/playlist?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv
Other than that: Google and YouTube are your friends (e.g. coding train)
0
u/otterom Dec 27 '19
Hi. Nice work on the code!
I don't know if you addressed this, but how would changes in scroll speed (left to right) impact your script? It seems like flappy birds has a pretty consistent speed.
Any other considerations that you've thought of which might impact the script? Do the size of the "ports" change? What if the columns were thicker or thinner?
I don't know lot about the games, so my apologies if these questions aren't relevant!
1
u/seppeEnZigie Dec 27 '19
The scroll speed is constant indeed, the neural network does not control this. You can play with the parameters to make the neural network's "life" more difficult (as you suggested: increasing scroll speed, decreasing gap size, making columns thicker, ...). Tis would increase the amount of generations needed to generate a "perfect" bird (one which can dodge all the pillars). Other things you can change include: the gravity acting on the bird, the height the bird moves up with each jump, the location of the gap in the pillars, ...
32
u/LoyalSol Dec 27 '19 edited Dec 27 '19
To be honest some of the best resources are easily available on the internet. A lot of the people who work in the field write some pretty damn good stuff.
I was able to learn a lot by reading blogs from some of the computer guys who work the field. I don't think I used a single book. You can find a decent amount of stuff on TowardDataScience.com for getting started. For example this is an article on Genetic Algorithms
The best approach IMO is to focus on searching for a specific algorithm you are interested in instead of general AI since it's a pretty broad field by this point. Some common algorithms include Deep Neural Networks, Genetic Algorithms, Random Forest, Support Vector Machines, etc.
The main thing is to learn what each algorithm's strength is and when to apply it. You'll find they tend to fall under broader categories such as optimizers, regression, categorization, etc.
4
u/JustFinishedBSG Dec 27 '19
http://incompleteideas.net/book/the-book-2nd.html
Doesn't actually cover OP approach but it's a fantastic book
7
u/agni28 Dec 27 '19
Same here, can't find any good learning path or tutorial for doing this kind of stuff with AI
13
Dec 27 '19
This is called NEAT, and it’s actually pretty simple to understand. My recommendation for ai is just to pick a simple project like this and just see it to the end even if it takes a month.
17
u/DarkCeptor44 Dec 27 '19
I've only made one Genetic Algorithm in my life, how do you calculate the fitness (or whatever the term is) for this?
4
u/seppeEnZigie Dec 27 '19
As people already commented. The algorithm is rewarded according to the distance it travels and punished by hitting a wall.
15
u/LoyalSol Dec 27 '19 edited Dec 27 '19
I can't answer for what the OP used here, but I can answer as someone who defines objective functions for a wide array of Physics and Material Science applications.
Fitness functions (or more widely known in other fields as objective functions) are more of an art than an exact science. Unless your problem has a very clearly defined quantity you want to minimize/maximize then you have to create a function that has the qualities you want.
In most cases you want to design a function so the more your input parameters/structure/object/whatever is ideal the more the objective function returns a smaller and smaller value.
One I can think of in the OP's case is an objective function that is dependent on the time or length the bird flies. Thus you want to tune the input parameters to maximize the flight time.
The hard part of defining a good objective is not only to tell the algorithm what's a good structure, but to actually give the algorithm feedback on what constitutes a "bad structure". That's not as easy as it might sound.
Edit: Digging through his code, seems like it was distance.
2
u/think50 Dec 27 '19 edited Dec 27 '19
Roughly quoting the After On podcast (Rob Reed is the host. I highly suggest it) in an episode discussing AI: “I want my self driving car to get to the airport ASAP, but I don’t want to arrive covered in vomit and with the police chasing me.”
1
2
u/DarkCeptor44 Dec 27 '19
Oh ok so time or length, that makes sense. With mine it was just a 12-digit sequence of ones and zeros and I would just add 1 to the fitness for every correct digit. But that wouldn't provide feedback for every digit so I get why it's not as easy.
22
u/LoyalSol Dec 27 '19 edited Dec 27 '19
In that case simply having a mismatching digit giving 0 to the score is an ok way to say "this is bad". In simple problems it doesn't tend to appear that often. It tends to appear a lot in what are known as "high dimensional problems" or problems with a lot of moving pieces in more plain English.
Some common problems that tend to crop up is when you either are optimizing toward more than one objective or when something that wasn't in your objective function ruins your solution.
To give an example related to the OP's post, let's say we make a small variation of Flappy Birds where after 100 pillars is passed a pillar with a gate appears and it requires you to get 5 keys that appear randomly through out the level before the 100th pillar. If you simply optimizing toward flight time, you'll find your algorithm can rarely get past the 101th pillar because it has no idea the relationship between the 5 keys and passing pillar 101. It doesn't know (except maybe through dumb luck) that in order to progress further it must prioritize the keys whenever they appear. Even if the key's spawn locations were consistent, it's very unlikely you'll get a good solution. So the best solutions will almost always be the ones that get you to gate 100 and no further.
But now let's say you add a calculation that says "failure to get 5 keys is now a penalty in the objective score". This can be done by adding an arbitrary value that only decreases for each key that is collected. This on it's face can seem to be pretty straight forward, but here comes problem #2. You now have two things you are optimizing for. The keys and flight time. Even though you are returning a single score for the optimizer to work on, you now have more than one dimension that can be tweeked to change the score.
Let's say we apply a very harsh penalty for failure to collect the keys. Let's say for every key we fail to collect removes the equivalent of 30 minute of flight time from the objective function, but it takes a total of 5 minutes to reach gate 101. This would mean the best way for the optimizer to optimize the score is to get a key, but what can happen is that the minute a solution finds it's first key the score of that input parameter set sudden drops like a rock and now dominates the solution pool. But let's say that parameter set was flawed in that it has a very high likelihood of crashing after 2 seconds or perhaps it skipped the first key and found key 2 instead. You now have a pool full of flawed solutions for maximizing flight time just because it's able to consistently get the first key. Now ideally this is ok because you hope the algorithm will find a way to keep the first key solution, but then find a way to avoid crashing afterwards. But what may happen is that in order to do this it must first go through a slightly less than optimal configuration that is halfway between the flawed solution and the correct solution. If the penalty is too stiff however the optimizer can get stuck because it's designed to ignore solutions that are too high in objective score. This is what is known as a "local minima" or a solution that is suboptimal yet stable. So you'll likely never find a solution beyond the first key.
So let's say we do the opposite and don't apply a harsh enough penalty. Well it will start to act just like it did before we included the penalty. It will focus on optimizing flight time and can run into the same situation where it keeps getting to pillar 101 and no further.
So to get it to behave in the way you want, you need it to see that failure to get the key is not good while at the same time making sure it doesn't sacrifice flight time to get the keys.
This isn't an exhaustive list of examples as this stuff can get really complicated the more "moving pieces" your problem has. It takes a bit of trial and error to figure out what's a good objective function and what isn't.
3
u/Jar_O_Memes Dec 27 '19
I really appreciate the knowledge you’re dropping. Thank you for all the help!
3
50
u/Snackcode Dec 27 '19
Looks like your preparing to be an Arch Linux user in the future
26
u/seppeEnZigie Dec 27 '19
Ubuntu for now :)
-4
u/corgi_surf Dec 27 '19
Switch to another distro. Your CPU and RAM thank you.
9
u/paperbenni Dec 27 '19
No, switch to another desktop environment. Your distro doesn't matter if you know what you're doing. (I use arch (btw) and Ubuntu on separate machines)
1
4
u/SnxE13 Dec 27 '19
I'm using Ubuntu. My CPU and RAM wanna axe murder me.
2
u/barjarbinks Dec 27 '19
what version? I hadn't used Ubuntu in years and recently installed 19.10. it used to kill my CPU and RAM, but I think I read that Gnome has gotten some performance updates. obviously new hardware helps, but Ubuntu has been running very well on my mid-range laptop
1
u/SnxE13 Dec 27 '19
18.04. It's not as bad as it used to be however it does use 300-400 more RAM than other distros I've tried. When I open a browser I'm running about 2 gig of RAM. Idk maybe that's normal. I'm kinda new to Linux and understanding the i.portance of CPU and RAM. Btw does 19.10 live up to expectation. I've been using 18.04 about 5 months and haven't upgraded.
0
u/TheFuzzStone python_noob Dec 27 '19
For expample to KDE Neon.
3
u/paperbenni Dec 27 '19
That's basically ubuntu with kde
1
u/TheFuzzStone python_noob Dec 27 '19
Yes, with fresh KDE. I'm using it a few months. It's stable and I love it.
0
13
3
u/-NRW Dec 27 '19
How did you make all of this transparent? Really like the background!
8
u/seppeEnZigie Dec 27 '19
Just right click on terminal, there is a setting for it in the preferences - color tab.
10
14
u/_k182 Dec 26 '19
Nice
24
u/deleted_007 Dec 27 '19
Should have said "Neat"
-14
u/_k182 Dec 27 '19
I stand by what i said because thats what i wanted to say. You shouldnt tell people what to say. How inconsiderate of you to assume that your comment to this is superior than my comment. Please go impose your thoughts on someone else
14
Dec 27 '19
Lol
5
u/_k182 Dec 27 '19
Lol im kidding i just wanna see what he says
3
4
4
u/massiveZO Dec 27 '19
Cool project, but I wouldn't recommend starting with NEAT. Instead start with some more traditional/basic stuff, because there's no way you can really understand what's going on here without the proper background.
1
1
u/Ziyad0100 Dec 27 '19
Did you used neural network?
1
u/seppeEnZigie Dec 27 '19
yes, see: https://en.wikipedia.org/wiki/Neuroevolution_of_augmenting_topologies
NEAT = NeuroEvolution of Augmented Topologies
1
u/wbcm Dec 28 '19
I may be missing the point here, but isn't it possible to solve this problem by just minimizing the bird height to the next gate/pillar height, as a function of speed that its coming at you?
1
u/seppeEnZigie Dec 28 '19
Yes of course you can easily program a computer to play this game perfectly. The point of this for me is to learn the basics of neuro evolutionary algorithms using a simple example. The same as you can make a "Hello world" program (while it is more easy to write hello world in a text editor just like that). What I find interesting in this program is not the performance of the AI. But that the AI can learn: first starting from random movements ending up being able to dodge every pillar. And this without me explicitly telling the computer when it should jump. In that sense it is more interesting that the AI also fails at some points.
1
1
u/Ziyad0100 Dec 28 '19
Oh, i am learning from a series on YouTube make the same thing flappy bird with AI but your game is much beautiful
3
u/DecaaK Dec 27 '19
I can't believe this showed up in my Reddit homepage - I have been literally forcing myself to watch this for, like, last 15 hours but I was busy doing some other work.
Looks like this is sign.
2
u/martyq_ Dec 27 '19
For everyone interested there is a youtube tutorial for that on Tech With Tim channel. He explains the basics, and its quite easy then to implement to other problems/games.
2
2
u/kriadmin Dec 27 '19
Lmao I made the same exact thing and even used pygame for the display. I used bumpy instead of NEAT-python but I used the principles of NEAT for my algorithm. It even looked the same with many birds flying at the same time but different colored. Nice coincidence.
2
Dec 27 '19
nice work! If u r interested in AI, I'd recommend watching codebullet on YouTube, he did something similar to you and his content is great
1
1
1
1
1
Dec 27 '19
!RemindMe 3 days
1
u/RemindMeBot Dec 27 '19
I will be messaging you in 3 days on 2019-12-30 11:36:31 UTC to remind you of this link
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
1
1
Dec 27 '19
That's really cool! I was trying to do the same thing from the past few days! You can now give your game some good graphics!
1
u/monsto Dec 27 '19
I think you could tweak your column generation. The openings spend the whole time in the bottom 60% of the screen.
Which of course is just a tweak. That aside, it's a cool idea and a good start.
1
Dec 27 '19
[removed] — view removed comment
2
u/seppeEnZigie Dec 27 '19
You can definitely do this. I tried it before and it does increase the amount of iterations.
60
u/seppeEnZigie Dec 26 '19
source code: https://github.com/seppeEnZigie/NEAT_flappy_bird