from Hacker News

Show HN: I built an autopilot for the lunar lander game

by szhu on 3/10/23, 3:30 AM with 69 comments

I got pretty good at (and very addicted to) the lunar lander game from a few days ago...

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

Writing the autopilot code was a lot more fun than I expected! It felt a bit like programming a robot.

Source code: https://github.com/szhu/lunar-lander-autopilot

Original lander HN post: https://news.ycombinator.com/item?id=35032506

  • by simonh on 3/10/23, 9:32 AM

    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.

    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.

  • by timmaxw on 3/10/23, 7:08 AM

    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.

    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.

  • by paradite on 3/10/23, 12:00 PM

    Lunar lander is a one of the problems in Open AI Gym, where you test AI against standard set of problems: https://www.gymlibrary.dev/environments/box2d/lunar_lander/

    Then you have stable baselines which implements popular reinforcement learning algorithms to solve these gym problems: https://stable-baselines3.readthedocs.io/en/master/

    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.

    You can check it out here: https://ai-simulator.com/

  • by Nodraak on 3/10/23, 11:37 AM

    Nice auto pilot! Very basic, but works well enough.

    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: https://blog.nodraak.fr/2020/12/aerospace-sim-2-guidance-law... (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).

    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).

  • by marketdev on 3/10/23, 7:26 AM

    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.
  • by ricardobeat on 3/10/23, 7:32 AM

    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.

    Adding some realism to the engine physics (firing delays, minimum firing time, power ramp up, heat limits etc) would likely make it 10x harder.

  • by monkellipse on 3/10/23, 5:56 AM

    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!
  • by mg on 3/10/23, 6:18 AM

    It's interesting to think about how the optimal strategy to land the lander would look like.

    If the distance to the moon was large, I would expect 4 phases:

    1: Turn the lander towards the moon

    2: Constant thrust towards the moon

    3: Turn the lander away from the moon

    4: Constant thrust away from the moon

    But if the initial distance is small enough, turning it around might not be worth it or even possible.

    So the optimal strategy is probably a somewhat complex function of the initial angle and distance to the moon.

  • by mckirk on 3/10/23, 11:39 PM

    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.
  • by jkonline on 3/10/23, 3:10 PM

    "Oh SNAP! I'm amazing at this!" 101.1 point landing on my second try!

    At least, that's what I thought, until I realized I had previously activated the auto-pilot. Best #footgun today (so far!).

    So I guess I should say: > Oh SNAP! This auto-pilot is amazing at this!

    Thanks!

  • by dropbox_miner on 3/10/23, 7:20 AM

    This is great but I'd like to see an autopilot that can do the hardest crash
  • by huevosabio on 3/10/23, 6:09 AM

    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.
  • by szhu on 3/10/23, 6:38 PM

    Update: I improved the algorithm.

    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!

    I got my first 103+ point landing! (It does this about half the time now)

    https://user-images.githubusercontent.com/1570168/224399420-...

  • by Aperocky on 3/10/23, 2:07 PM

    A career in KSP is calling for you (Landing on the mun automatically):

    https://www.youtube.com/watch?v=TY63i8V1-DA

    The most I've used linear algebra since college:

    https://github.com/Aperocky/ksp_eng/blob/master/lib/space_li...

  • by khalidx on 3/10/23, 8:36 PM

    This is awesome! The closest I got without the autopilot was 94 or so. The autopilot consistently scores 100+
  • by ehmorris on 3/10/23, 11:24 AM

    Woah this is awesome! Someone on Twitter made an autopilot with ML but this seems a lot better.

    https://twitter.com/_s_w_a_y_a_m_/status/1633468475611004928...

  • by bjd2385 on 3/10/23, 2:02 PM

    Really cool! The fact that astronauts handled the landing in the Apollo missions is nerve wracking
  • by 000ooo000 on 3/10/23, 6:09 AM

    Cool, neat code too :) Now do https://race-condition.reaktor.com/! I wanted to pour a few hours into race condition but I got it to complete a lap and never went back.
  • by curiousgal on 3/10/23, 8:20 AM

    Best I could score was 102.0 with a combination of manual and autopilot. Neat!
  • by josephcsible on 3/10/23, 7:21 AM

    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.
  • by tired_and_awake on 3/10/23, 3:35 PM

    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.
  • by iandanforth on 3/10/23, 2:07 PM

    I'm impressed by the simplicity of the solution, well done.
  • by handonam on 3/10/23, 8:34 AM

    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.
  • by m00dy on 3/10/23, 10:16 AM

  • by hawski on 3/10/23, 7:59 AM

    Would it be possible to put the autopilot in a bookmarklet for the original game?
  • by tiffanyh on 3/10/23, 6:14 AM

    Mobile?

    How to you trigger autopilot on mobile?

  • by tommica on 3/10/23, 9:04 AM

    What about adding a limited fuel supply?
  • by kevinwang on 3/10/23, 12:57 PM

    And now we've gone full circle
  • by _ZeD_ on 3/10/23, 10:53 AM

    uhm... it doesn't seem to work, at least on my workstation (firefox on a debian)
  • by antegamisou on 3/10/23, 1:43 PM

    Now do it with LQR :D