I got pretty good at (and very addicted to) the lunar lander game from a few days ago...<p>so I decided to make an autopilot for the lander based on what I felt like was the best strategy! Now I can have perfect landings every time without lifting a finger :D<p>Writing the autopilot code was a lot more fun than I expected! It felt a bit like programming a robot.<p>Source code: <a href="https://github.com/szhu/lunar-lander-autopilot">https://github.com/szhu/lunar-lander-autopilot</a><p>Original lander HN post: <a href="https://news.ycombinator.com/item?id=35032506" rel="nofollow">https://news.ycombinator.com/item?id=35032506</a>
That's cool, but it has a very conservative descent profile and would use a ton of fuel. Having played Kerbal I'm used to attempting fuel efficient landings, which means more of a suicide burn approach, but of course this game doesn't keep track of that.<p>There's something timelessly appealing about lunar lander games. The very first game I ever played on a computer, written in BASIC, was a 'turn based' one dimensional lunar lander game where you input how much thrust you used for each second of the descent, and then it recalculated your altitude, velocity, etc. I learned programming by rewriting it to be a real-time game where you pressed a key to fire the engine.
I wanted to go for a record of as many flips as possible. So I boosted up to 25,000 feet; started spinning until it was doing about one flip per second; waited a few minutes; then turned on the autopilot to come into a landing.<p>The autopilot did a perfect job of stopping the rotation and lateral motion, so the lander came down straight as an arrow. Unfortunately, the autopilot didn't even try to decelerate! I crashed into the moon at 0.9 degrees and 770 MPH.
Lunar lander is a one of the problems in Open AI Gym, where you test AI against standard set of problems: <a href="https://www.gymlibrary.dev/environments/box2d/lunar_lander/" rel="nofollow">https://www.gymlibrary.dev/environments/box2d/lunar_lander/</a><p>Then you have stable baselines which implements popular reinforcement learning algorithms to solve these gym problems: <a href="https://stable-baselines3.readthedocs.io/en/master/" rel="nofollow">https://stable-baselines3.readthedocs.io/en/master/</a><p>Shamless plug: I've built a series of games where you solve puzzles (2048) / toy problems (MDP) like the lunar lander using various AI and ML algorithms.<p>You can check it out here: <a href="https://ai-simulator.com/" rel="nofollow">https://ai-simulator.com/</a>
Nice auto pilot! Very basic, but works well enough.<p>In the real world, you would derive physics equations (acceleration -> velocity -> position), add constraints and then solve everything to obtain an optimal trajectory (mostly in term of fuel, but you can add other constraints too, for ex due to radar-ground or Antenna-Earth visibility). I wrote a blog post about Apollo's algorithm: <a href="https://blog.nodraak.fr/2020/12/aerospace-sim-2-guidance-law/" rel="nofollow">https://blog.nodraak.fr/2020/12/aerospace-sim-2-guidance-law...</a> (Described in the second section ; the first section is about a naive algorithm similar to yours that in the end did not work as well as I wanted).<p>Also, thanks for the code, I wanted to do the same, but lost motivation when I could not really expose in a satisfying way the internal state out of these JS modules (it's not complicated in the end, but I'm simply not a frontend dev ; and I wanted to avoid forking and monkey patching everything and simply adding some JS code throught the console or something).
While I was playing the game, the idea of traveling in a SpaceX rocket ship didn't seem appealing to me. Now that I have experienced your autopilot feature, I feel MUCH more confident about it.
It’s amazing how a simple algorithm performs so well, the autopilot is able to recover from some pretty extreme situations, and does it gracefully.<p>Adding some realism to the engine physics (firing delays, minimum firing time, power ramp up, heat limits etc) would likely make it 10x harder.
Nice! I also got hooked on the lander thing, really quick to pick up and good replay value! Your autopilot handles a gentle landing well. If I get the lander going sideways at any appreciable speed, then flip autopilot on, a crash is inevitable :) As a human my solution to that is simply slowing down with lots of thrust, though I still wind up turning the lander into scrap most of the time hah!
It's interesting to think about how the optimal strategy to land the lander would look like.<p>If the distance to the moon was large, I would expect 4 phases:<p>1: Turn the lander towards the moon<p>2: Constant thrust towards the moon<p>3: Turn the lander away from the moon<p>4: Constant thrust away from the moon<p>But if the initial distance is small enough, turning it around might not be worth it or even possible.<p>So the optimal strategy is probably a somewhat complex function of the initial angle and distance to the moon.
I can confirm that with the improved auto-pilot it is possible to get a 103.6 point landing after performing 1,151 flips (and reaching a max speed of 504 mph and a height of about 38k feet), if you can spare your phone for 38 minutes.
"Oh SNAP! I'm amazing at this!" 101.1 point landing on my second try!<p>At least, that's what I thought, until I realized I had previously activated the auto-pilot. Best #footgun today (so far!).<p>So I guess I should say:
> Oh SNAP! This auto-pilot is amazing at this!<p>Thanks!
Love it! I started playing by disabling autopilot, putting the ship in complicated positions (e.g. spinning uncontrollably) and then turning the autopilot on, it's fun to which conditions it can recover.
Update: I improved the algorithm.<p>The autopilot now scores a "perfect landing" almost every single time, it lands very noticeably faster, and the code no longer contains a complicated, trial-by-error formula!<p>I got my first 103+ point landing! (It does this about half the time now)<p><a href="https://user-images.githubusercontent.com/1570168/224399420-5a8d688a-9306-4499-aea0-5e9e212b438e.png" rel="nofollow">https://user-images.githubusercontent.com/1570168/224399420-...</a>
A career in KSP is calling for you (Landing on the mun automatically):<p><a href="https://www.youtube.com/watch?v=TY63i8V1-DA">https://www.youtube.com/watch?v=TY63i8V1-DA</a><p>The most I've used linear algebra since college:<p><a href="https://github.com/Aperocky/ksp_eng/blob/master/lib/space_lib.ks#L301">https://github.com/Aperocky/ksp_eng/blob/master/lib/space_li...</a>
Woah this is awesome! Someone on Twitter made an autopilot with ML but this seems a lot better.<p><a href="https://twitter.com/_s_w_a_y_a_m_/status/1633468475611004928?s=20" rel="nofollow">https://twitter.com/_s_w_a_y_a_m_/status/1633468475611004928...</a>
Cool, neat code too :) Now do <a href="https://race-condition.reaktor.com/" rel="nofollow">https://race-condition.reaktor.com/</a>! I wanted to pour a few hours into race condition but I got it to complete a lap and never went back.
One suggestion: if the lander is within about 10 feet of the ground and is still either swaying back and forth or going really fast laterally, it should thrust upwards to buy more time to correct those things.
Simple and fun, thanks for sharing! Have you thought of adding a wordle style variation - just a daily puzzle that's a different start orientation + velocity? Just a thought.
this is great! The one thing i think to optimize it is to predict how late to start the engine to minimize its usage. The last I got was 730 times used, at 53seconds.
very similar to <a href="https://www.gymlibrary.dev/environments/box2d/lunar_lander/" rel="nofollow">https://www.gymlibrary.dev/environments/box2d/lunar_lander/</a>