by szhu on 3/10/23, 3:30 AM with 69 comments
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
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
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
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
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
by ricardobeat on 3/10/23, 7:32 AM
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
by mg on 3/10/23, 6:18 AM
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
by jkonline on 3/10/23, 3:10 PM
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
by huevosabio on 3/10/23, 6:09 AM
by szhu on 3/10/23, 6:38 PM
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
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
by ehmorris on 3/10/23, 11:24 AM
https://twitter.com/_s_w_a_y_a_m_/status/1633468475611004928...
by bjd2385 on 3/10/23, 2:02 PM
by 000ooo000 on 3/10/23, 6:09 AM
by curiousgal on 3/10/23, 8:20 AM
by josephcsible on 3/10/23, 7:21 AM
by tired_and_awake on 3/10/23, 3:35 PM
by iandanforth on 3/10/23, 2:07 PM
by handonam on 3/10/23, 8:34 AM
by m00dy on 3/10/23, 10:16 AM
by hawski on 3/10/23, 7:59 AM
by tiffanyh on 3/10/23, 6:14 AM
How to you trigger autopilot on mobile?
by tommica on 3/10/23, 9:04 AM
by kevinwang on 3/10/23, 12:57 PM
by _ZeD_ on 3/10/23, 10:53 AM
by antegamisou on 3/10/23, 1:43 PM