Answers.Body: <p>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.</p>
  <p>I recommend reworking the fitness function to make the two components more equal, e.g. something like</p>
  <pre><code>fitness(angle, distance)= 
      w1 * |desired_angle - angle| / maximum_angle_error
    + w2 * distance / maximum_distance
  </code></pre>
  <p>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&gt;w2 if staying upright is the most important).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-19T18:22:29.287Z
Answers.Text: 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?
Answers.UserDisplayName: Rasmus
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-19T19:09:32.030Z
Answers.Text: set it to the starting distance? would that be possible?
Answers.UserDisplayName: Mike NZ
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-19T04:20:13.550Z
Answers.LastActivityDate: 2021-04-19T04:20:13.550Z
Answers.UserDisplayName: Mike NZ
Body: <p>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.</p>
  <pre><code>fitness += 1/distToTarget + cos(drone_angle)
  </code></pre>
  <p>My current inputs are:</p>
  <pre><code>difference_target_X
  difference_target_Y
  velocity_X
  velocity_Y
  angular_velocity (degree per second)
  drone_angle     | = 0;     |_ = 90     _| = -90
  </code></pre>
  <p>The output (I don't think it is important but)</p>
  <pre><code>left_thruster_angle
  left_thruster_boost
  right_thruster_angle
  right_thruster_boost
  </code></pre>
  <p>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.</p>
  <p>How do I get the drone to set the best weights to fulfill all tasks: fly stable, fly fast, fly to the target?</p>
  
Tags: <neural-networks><genetic-algorithms><fitness-functions>
Title: How to design fitness function for multiple objectives?
UserDisplayName: Rasmus
ThreadText: # How to design fitness function for multiple objectives?
  
  (Asked by: username_0 on 2021-04-18)
  
  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:
  difference_target_X
  difference_target_Y
  velocity_X
  velocity_Y
  angular_velocity (degree per second)
  drone_angle     | = 0;     |_ = 90     _| = -90
  
  The output (I don't think it is important but)
  left_thruster_angle
  left_thruster_boost
  right_thruster_angle
  right_thruster_boost
  
  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?
  
  **Tags:** neural-networks, genetic-algorithms, fitness-functions
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-19. Score: 1 | Accepted answer
  
  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).
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: set it to the starting distance? would that be possible?  
  
  ---
  
  