Implementing Mars Lander 1 with a kotlin Genetic Algorithm

I made a first try with genetic algorithm for Mars Lander. I implemented it in kotlin (even if I can’t yet run my code on CodinGame).

I’d be happy to receive any comment on my post and code.


I don’t understand your fitness function.
Will a flying state ever be possible in the end? Does the pod have enough fuel for 1200 rounds of commands?
In a crash, will mars1Ground.landingZone.first.y - position.y + if (speed.ySpeed >= -40) 0.0 else speed.ySpeed + 40 ever be different than just speed.ySpeed + 40 ? The code can not crash on a spot different from the landing zone, and if it’s speed were over -40 it wouldn’t have been a crash…

I don’t understand the create function either. I mean, I understand what it does, but I do not understand the reason why that’d be a good way to go about it. It seems too much like hard-coding. It can never create a lander that’s good for several different starting positions. Feels like you left out the actually good bits of your post as a homework to the reader.

1 Like

At the end no. But during the flight yes.

True for this first puzzle. In fact I was working in parallel on the Mars Lander 2 for which my fitness function is more elaborate (where the lander crashed, …). Thanks for the comment, I correct it.

It’s a simplistic version that works well for a vertical trajectory. My chimp can just add power.

With the current code, implementing a version which works for MarsLander 2 is just 40 lines of code (replace the create and fitness functions) and more generations. But yes the creation function is completely different.

I was not sure about giving my complete solution for mars lander 2. It’s a puzzle with rules so the code should be given through CodinGame and not github or external post, no?

Fair enough, I see your point about not wanting to reveal solutions to CG puzzles outside of CG’s own solution browser. Although it’s in a language that does not even run on CG… But ok, I guess it would still be too easy to steal and adapt to some other language. In that line of thought, why did you pick that language to begin with?

Does your level 2 solution still work with as few as 10 generations of 20 population, or did you need to increase those? Is the evaluation of the generations fast enough to be actually done in the puzzle’s time limit on CG if it were translated in an available language? Or does it need to be pre-computed and only the winning chimp makes it to the solution browser?

Very expressive and some features that will make it a great language for CG (operator overloading, functionnal + object, extension functions, …). There is a lot of traction on it.

I hope to see it on CG soon.

No. I need up to 300 generations of 20 individuals. It takes approximatively 700 ms so it could easily be done during the first loops.

1 Like

I’m wondering… your approach is to get one chimp that solves all tests for level 2 right? But if it’s fast enough to happen at runtime, it can be simplified back to the hard-coding-like solution fit just for 1 test-case…

No. One chimp for one test. The ground surface and the initial state are my runtime inputs.

It is my current implementation.

Thanks for sharing that!

Ported it to the language of my choice and think it’s a fantastic starting point to understand genetic algorithms.

Still trying to figure out the bits for MarsLander2 by randomly setting power and angle, but still coercing to power 4 and angle 0. Also made some modifications to the fitness function to determine the horizontal fitness, but I haven’t found the solution yet.

Any hint on the create and fitness function is much appreciated!

I gave you some on medium :wink:

1 Like

Thanks a bunch!