Answers.Body: <p>A human analogy can help you here (a variance).</p>
  <p>Initialize all the agents with an initial value <span class="math-container">$x$</span>; we will call this <code>energyUnits</code>. I Will talk later more about this.</p>
  <p>Now, add some value, as an incentive, whenever the agent eats good food, to the <code>energyUnits</code>. You need to add a function that will keep decrementing the value of the agent's <code>energyUnits</code>, as humans degrade energy (calories) with time. We will call this function <code>normalDegrade</code>. This is the core part of the solution for your problem.</p>
  <p>Now, for the bad (or poisonous) food you can be more creative with. You can simply subtract a given value whenever an agent eats poisonous food. Or you can extend your <code>normalDegrade</code> function with a very high downward slope. In this case, the energy units (value) of the agent will fall very rapidly. This will force the agent to look for good food to survive.</p>
  <p>Since the ratio of food is 9:1 with poisonous, you need to initialize the value of <span class="math-container">$x$</span> (<code>energyUnits</code>) very high. You need to do some trial and error to find the right fit for you here.</p>
  <p>Also, I am assuming that the agent is being removed from the population whenever the value of <span class="math-container">$x$</span> is zero or some negative value (which depends). This is important, as it makes sure that the algorithm is not wasting time in processing bad agents.</p>
  <p>Because of this, another problem arises of the population coming to extinction. For this, you need to keep generating new agents for which any of the genetic algorithms will do. A new population with better parents of the already present generation will keep the population fit and efficient.</p>
  <p>A good fitness function is a core to solving any problem of this kind, and sometimes it is hard to find. You might need to do some trial and error with different values to look for the right fit.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-03-06T11:10:05.103Z
Answers.LastActivityDate: 2021-01-30T02:29:05.747Z
Answers.LastEditDate: 2021-01-30T02:29:05.747Z
Answers.UserDisplayName: Ugnes
Body: <p><strong>Summary:</strong></p>
  <p>I am teaching bots to pick food on a playing field. Some food is poisonous and some is good.</p>
  <p><strong>Food Details:</strong></p>
  <ul>
  <li>Poisonous food subtracts score points and good food adds.</li>
  <li>Food points vary based on its size.</li>
  <li>There is about 9:1 ratio of poisonous food to good food, so a lot more chances to end up in negative numbers.</li>
  <li>Food grows in points overtime.</li>
  <li>Food spoils after some predetermined size becoming poisonous.</li>
  </ul>
  <p><strong>Fitness Function:</strong></p>
  <p>The fitness function I use is simply counting points by the end of iterations. Bots might choose to eat it or skip it.</p>
  <p><strong>The Problem:</strong></p>
  <p>The problem I am having is that, in the first generation, most bots eat a lot of bad crap and the curious ones end up in negative numbers. So, mostly the ones that make it are the ones that are lazy and didn't eat or didn't head towards the food and most of the time the fittest for first few generations comes out with 0 points and 0 eats of any kind of food.</p>
  <p>When trained for a long time, they just end up waiting for the food instead of eating multiple times. Often, while they wait, food goes bad and they just end up going to another food. This way, at the end of the iteration, I have some winners, but they are nowhere near the potential they could have been at.</p>
  <p><strong>Question:</strong></p>
  <p>I somehow need to weigh the importance of eating food. I want them to eventually learn to eat.</p>
  <p>So I thought of this:</p>
  <pre><code>brain.score += foodValue * numTimesTheyAteSoFar
  </code></pre>
  <p>But this blows up the score too much and now the food quality is not respected and they just gulp on anything slightly above 0.</p>
  
Tags: <evolutionary-algorithms><neat><fitness-functions><fitness-design>
Title: How do I design a fitness function that weighs the importance of eating food?
UserDisplayName: Alexus
ThreadText: # How do I design a fitness function that weighs the importance of eating food?
  
  (Asked by: username_0 on 2018-03-05)
  
  Summary:
  I am teaching bots to pick food on a playing field. Some food is poisonous and some is good.
  Food Details:
  
  Poisonous food subtracts score points and good food adds.
  Food points vary based on its size.
  There is about 9:1 ratio of poisonous food to good food, so a lot more chances to end up in negative numbers.
  Food grows in points overtime.
  Food spoils after some predetermined size becoming poisonous.
  
  Fitness Function:
  The fitness function I use is simply counting points by the end of iterations. Bots might choose to eat it or skip it.
  The Problem:
  The problem I am having is that, in the first generation, most bots eat a lot of bad crap and the curious ones end up in negative numbers. So, mostly the ones that make it are the ones that are lazy and didn't eat or didn't head towards the food and most of the time the fittest for first few generations comes out with 0 points and 0 eats of any kind of food.
  When trained for a long time, they just end up waiting for the food instead of eating multiple times. Often, while they wait, food goes bad and they just end up going to another food. This way, at the end of the iteration, I have some winners, but they are nowhere near the potential they could have been at.
  Question:
  I somehow need to weigh the importance of eating food. I want them to eventually learn to eat.
  So I thought of this:
  brain.score += foodValue * numTimesTheyAteSoFar
  
  But this blows up the score too much and now the food quality is not respected and they just gulp on anything slightly above 0.
  
  **Tags:** evolutionary-algorithms, neat, fitness-functions, fitness-design
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-06. Score: 4 | Accepted answer
  
  A human analogy can help you here (a variance).
  
  Initialize all the agents with an initial value $x$; we will call this `energyUnits`. I Will talk later more about this.
  
  Now, add some value, as an incentive, whenever the agent eats good food, to the `energyUnits`. You need to add a function that will keep decrementing the value of the agent's `energyUnits`, as humans degrade energy (calories) with time. We will call this function `normalDegrade`. This is the core part of the solution for your problem.
  
  Now, for the bad (or poisonous) food you can be more creative with. You can simply subtract a given value whenever an agent eats poisonous food. Or you can extend your `normalDegrade` function with a very high downward slope. In this case, the energy units (value) of the agent will fall very rapidly. This will force the agent to look for good food to survive.
  
  Since the ratio of food is 9:1 with poisonous, you need to initialize the value of $x$ (`energyUnits`) very high. You need to do some trial and error to find the right fit for you here.
  
  Also, I am assuming that the agent is being removed from the population whenever the value of $x$ is zero or some negative value (which depends). This is important, as it makes sure that the algorithm is not wasting time in processing bad agents.
  
  Because of this, another problem arises of the population coming to extinction. For this, you need to keep generating new agents for which any of the genetic algorithms will do. A new population with better parents of the already present generation will keep the population fit and efficient.
  
  A good fitness function is a core to solving any problem of this kind, and sometimes it is hard to find. You might need to do some trial and error with different values to look for the right fit.
  
  
  ---
  
  