I am currently building a neural network with genetic algorithms that learns to fly a 2D drone to a target. My goal is that it achieves all tasks as fast as possible, but I want the drone to also fly stable and upright. The way I tried to calculate the fitness was to create a function that has the greatest value when the drone does everything I want right.

fitness += 1/distToTarget + cos(drone_angle)

My current inputs are:

angular_velocity (degree per second)
drone_angle     | = 0;     |_ = 90     _| = -90

The output (I don't think it is important but)


The NN is programmed in unity and the drone uses a 2D rigid body and the NN adds a force to the thruster at the right angle.

How do I get the drone to set the best weights to fulfill all tasks: fly stable, fly fast, fly to the target?


1 Answer 1


Your fitness function has two objectives that are added together, but they are not necessarily on the same scale. The component cos(drone_angle) must have a value from 0..1. The component 1/distToTarget will have a range that depends on how you measure distToTarget; e.g. if distToTarget has a range 0..1000, then this part of the fitness function will always be small far from the target (e.g. distance of 500) and massive when it gets very close (e.g 0.1 distance from the target). So the contribution of both components may not always be equal. Another potential complication is that the cosine function is nonlinear and make a very rapid transition between 0 and 1, as opposed to a smooth transition.

I recommend reworking the fitness function to make the two components more equal, e.g. something like

fitness(angle, distance)= 
    w1 * |desired_angle - angle| / maximum_angle_error
  + w2 * distance / maximum_distance

In this function that should be minimised to zero, both components contribute linearly to the final fitness, both components have a range of 0..1, and you can tune w1 and w2 to give different importance to different components depending on your preference (e.g. you may prefer w1>w2 if staying upright is the most important).

  • $\begingroup$ Thanks a lot, u helped quite well. But I not sure what to do about the max distance because if the target is just far away how am I supposed to calculate that that or how to set a max distance? any idea? $\endgroup$
    – Rasmus
    Apr 19, 2021 at 18:22
  • $\begingroup$ set it to the starting distance? would that be possible? $\endgroup$
    – Mike NZ
    Apr 19, 2021 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .