Hello , i’ wan’t to solve this puzzle with a genetic algorithm but how i can generate generation ? because when the land crash the game it’s over

# Mars Lander - Puzzle discussion

**AuCalmeCaptain13**#229

I don’t understand why but topo (table of ints here) is full of zeros except for the first position which is correct… Can someone help me understand what I did wrong please ?

int [] topo = new int[7000];

for (int i = 0; i < surfaceN; i++) {

int landX = in.nextInt();

int landY = in.nextInt();

topo[i] = landY;

}

**vidaye**#230

salut quelqu’un pourrait m’aider je suis bloqué au premier niveau est je ne vois parce qu’il faut faire. de l’aide ?

**vidaye**#231

hello someone could help me I’m stuck at the first level is I do not see because we have to do it. help ?

**PCXpert**#234

Ok. I still can’t understand what I have to do:neutral_face:… Can someone please give me full code about this…? in C++…?

**L_one**#237

Hi there, is there a single algorithm that is successful in all levels, or is each level coded differently?

**OptiPanda**#238

You have to find a generic solution to adapt every situation! You have variables that give you information, you compile them and with them you determine which action you have to do!

**L_one**#239

Grand, I think I kind of cheated by using trial and error method…

I know I want to go left/right a bit then down a bit etc and then use some additional thrust to ensure a soft landing.

I used a counter with some event triggers by using multiple if-else-if statements

if count < x seconds apply ? thrust and ? rotate

else if count < x+10 seconds apply…etc etc

Tweaking the times and outputs allows for a nice soft landing (eventually)!

My next steps are to look at a more automated approach potentially using some equations of motion.

Still hardcoding the co-ordinates of the landing site for simplicity for now.

i think i will subdivide controlling horizontal movement in one phase and then vertical movement in the next and alternate (perhaps ~4 timesteps each noting the game does not just go from power 0->4 in one step but 0,1,2,3,4.) using small rotations.

Testing my recollection of GCSE/A level maths/physics.

Very interesting puzzle and is more addictive than sugar!

Can the game inputs be modified to have a user defined level?

**EliasHasle**#240

My current state step code looks like this:

```
thr = radians(thetan)
co = cos(thr)
si = -sin(thr)
vxn = vx+powern*si
vyn = vy+powern*co+g #g is negative
xn = x+vxn#vx
yn = y+vyn#vy
fueln = fuel-powern
```

In the game loop, I compare the projected state to the one I get from CodinGame, like this:

```
errors = tuple(int(round(root[i]))-int(env_root[i]) for i in range(len(root)))
```

and (only) the positions deviate over time.

Rounding position at the end of the step has little to no impact. Rounding velocity before or after the position update worsens the error.

What am I not getting?

**Di_Masta**#241

Finally I’ve solved it, all 3 episodes. Man, what a relieve. It only took me about 6 months with an year break in the middle. Using C++. I had to go through almost all physics learned in school from this site:

https://www.physicsclassroom.com/class

I was determined to solve the task using Genetic Algorithm. I had to go through many tutorials, such as:

But I still couldn’t get my algorithm to converge. I enrolled in online course for the Genetic Algorithm:

Which took me about a month to complete to get deeper knowledge in the field.

One of the hardest thing was to debug my algorithm, so I wrote my own tool for debugging the algorithm visually, the tool is made with SVG and javascript, which involved fair amount of learning also:

I cannot imagine writing such an algorithm without visual debugging.

I had very difficult time coming up with good evaluation function also, used above 10 of those.

After above 100 iterations of my code my algorithm started to converge, this was AWESOME. But it was very slow…

I’ve started to analyze my solution using Visual Studio native profiler. And I’ve found out that I’m using very much memory and started optimizing it. I couldn’t get very good results until it hit me, I could use only 2 populations the current and the next after each crossover next becomes current and current is overwritten with next. And for this I had to rewrite again the whole solution.

The thoughessest puzzle of all 3 episodes was “Initial speed, wrong side” There I have some convergence to a local maximum and my algorithm needed above 400 populations to escape that maximum.

The puzzle for which biggest optimizations were needed, to get in the time frame, was “Cave, wrong side”. It passed for around 150 populations, but was slow

And also the biggest optimization was to reduce the use of STL to minimum.

After this optimizations I still didn’t get in time for all solutions until I found that I use too few genes in each chromosome. And BOOOOOM the tests and the submit passed

After that I took it further to pass the “Stalagtite” test form the tech.io tutorial. There 200 chromosomes were needed in each population and above 1000 populations.

I feel very proud of myself, to see all efforts paid off is one of the best feeling ever. The main lesson I learned from this: “Keep it simple” no need to overengineere something, no need of all C++ fancy syntax, at the end I solved the task using plain old C++. Never stop beliving

**_CG_Thibaud**#242

Congrats! Awesome achievement. I’m impressed by your perseverance. Want to write about it in the blog?

**_CG_Thibaud**#244

it’s simple, you write a draft and I help you publish it.

more details here: https://www.codingame.com/playgrounds/40701/contribute---help/articles

**WilliamSmith11**#245

Can someone help me figure out how to determine the coordinates for the landing pad…?

I got it! Thanks!!