I’ve been working on neural networks for quite some time now (5-6 months full time, ~55hrs. a week)
Tonight I’ve finally reached a stage where I’m satisfied with their performance. I’m going to show you a quick demo of the finished product then go over how I got there.
Video Demo (viewing from cops perspective): Neural Network Cop Car's Perspective (Vehicle Simulator Roblox Demo) - YouTube
Here is a list my progress here to show how I went about doing this. Skip to the end for some finishing remarks & questions.
6 months ago I started my progress with neural networks & genetic algorithms: https://twitter.com/ScriptOnRoblox/status/1007459441073983488
I started with some more simple stuff so I could understand what I was doing before moving on to the meatier stuff: https://twitter.com/ScriptOnRoblox/status/1007464071145218049
Soon™ I learned about tweaking inputs: https://twitter.com/ScriptOnRoblox/status/1008928955083051008
There’s a lot that happened between here and moving on to more realistic cars, but here’s a progression:
Stage 1: Basic line following, I manually try to trigger breaks and some other things. AI handles driving along the route.
Stage 2: Neural net seems to start understanding that it’s velocity is important… drifting begins!
Ok so there’s actually quite a long time period that’s happened between stage 1-2-3. I spent a LOT of time on different mutation algorithms & networks. I probably spend another month or two of working everyday before I had the cars doing some of this more advanced stuff, and even at that I’m still triggering e-brake manually.
Stage 3: Combined with a pathfinding GPS on the VehicleSim map, I was able to record a demo!
Stage 4: Cars can now control handbrakes! Very fun to watch them learn how to nail corners:
Stage 5: Realized I could train a much more effective AI if I used ReLu (only positive outputs, negative outputs are treated as nil).
Things quickly began to ramp up at this point. Learning was faster and I could now train for some more complicated tasks, like flight! The airplane shown uses airfoil & flies fairly realistically.
Stage 6: Realized I needed a more effective mutation algorithm. Made some of my own momentum-based gene mutation modifiers that essentially made training more consistent (and at a much quicker rate)
Decided to go back and see if I could get more impressive results with the new algorithm:
I was right. Here’s the new algorithm learning to fly it’s very first plane!
And here it is mastering the plane + a cool visualizer to see what the AI sees
Stage 7: Realized once again that I was doing it all wrong. Changed a bunch of stuff within the algorithm. Results were excellent so I added some more variables (like rays to avoid hitting obvious objects).
Stage 8: Implementation! Combined everything I’ve learned, training data + new GPS algorithms and I started to have cop cars!
Stage 8.5: After some gameplay hours I realized I had a lot of areas to improve and works on things like collisions, AI cars getting stuck on things, and awkward driving situations (sometimes they just didn’t wanna throttle lmao)
Stage 9: Realized cops aren’t very effective if they just chase you. If I’m going to make this fun I need a challenge! I spent a few days working on different prediction algorithms for where a potential collision could occur between the AI car & chased car. Fed that into the GPS router & things started heating up fast
Cool car chases are now possible!
Remapped the entire node system on Vehicle Sim because I realized the current one didn’t support real-time chases very well (would awkwardly send the car to the nearest curb, THEN pursue the player…)
Yay more consistent chases!
At this point the cops could find you on the map, route to you, drive/drift their way as fast as they can to your location, & attack you.
It still didn’t feel challenging. Cops at close quarters would work pretty well but I needed them to find you faster and collide with you more accurately. After spending a week reworking some of the GPS algorithm internals I finally came up with something that felt challenging, which is where we are now !
Can I play it? Not yet. I still have to add a system to trigger the cops. So far the current plan is to have them casually patrol around town & only get angry when players interact with them. Ideally you’ll be chased down by November!
What is a neural network? To put it simply, a computer that randomly guesses what the solution to a problem could be (or searches for specific data, patterns, similarities, etc…). It guesses thousands (sometimes even billions!) of times and after each set of guesses (say, 100 guesses) it’ll try to see why it’s correct answers were right & why it’s wrong answers were wrong by essentially moving around if statements & thresholds to trigger them internally. This is done through forward propagation in my case with inputs (say there’s 10 inputs, they represent things like how fast the car is going, how far it is from the center line it should be following, where the next turn is, etc…) & outputs. Outputs represent things like SteerLeft, SteerRight, Throttle, etc… Internally it has a list of “nodes” in multiple layers that have different numbers attached to them called bias and weights. I don’t wanna go too into detail with bias & weights but think of bias as adding a number to an input, and weights as a gradient between true → false. This gradient behavior is what makes neural networks so powerful. They can recognize a set of similar inputs & patterns. To do this by hand would be impossible in many cases.
How did you learn neural networks? This video helped me establish the basics: But what is a neural network? | Chapter 1, Deep learning - YouTube This second video alone pretty much explained how to do it for me. Went back multiple times for reference while coding it (probably seen the entire video 6 times total now) Gradient descent, how neural networks learn | Chapter 2, Deep learning - YouTube
It’s been a long journey learning something I knew nothing about, but now I’m ready to move on to my next neural network project. Feel free to ask me anything else. Thanks for reading!