Answers.Body: <p>If I understand correctly your problem, you always want to maximize some function <span class="math-container">$h(x)$</span>, which is defined as follows <span class="math-container">$h: \Gamma \rightarrow \mathbb{R}$</span>, where <span class="math-container">$\Gamma$</span> is the space of genotypes. However, at every generation <span class="math-container">$g$</span>, you don't know exactly <span class="math-container">$h(i)$</span> of each individual <span class="math-container">$i$</span>, i.e., maybe in one generation <span class="math-container">$g$</span>, all <span class="math-container">$h(i)$</span>, for <span class="math-container">$i=1, \dots, N$</span> (where <span class="math-container">$N$</span> is the size of the population), are negative, but in the next generation <span class="math-container">$g+1$</span>, due to the mutations and crossovers, that may not be the case anymore, so you don't know how to shift <span class="math-container">$h(i), \forall i$</span>, so that they are all positive and you can compute the probability of being selected, assuming you are using the <a href="https://ai.stackexchange.com/a/9083/2444"><em>fitness proportionate selection</em></a>.</p>
  <p>If my intepretation is correct, then, at every generation <span class="math-container">$g$</span>, you just need to find <span class="math-container">$w = \operatorname{min}_i h(i)$</span>, then shift all <span class="math-container">$h(i)$</span> by adding <span class="math-container">$|w| + \epsilon$</span> (where <span class="math-container">$\epsilon$</span> can be zero or a very small number) to all <span class="math-container">$h(i)$</span>, so you would compute the fitness as follows <span class="math-container">$f(i) = h(i) + |w| + \epsilon$</span>, for all <span class="math-container">$i$</span>. Here are all the steps to compute the probability of individual <span class="math-container">$i$</span> being selected <span class="math-container">$p(i)$</span></p>
  <ol>
  <li><span class="math-container">$w = \operatorname{min}_i h(i)$</span></li>
  <li><span class="math-container">$f(i) \leftarrow h(i) + |w| + \epsilon, \forall i$</span></li>
  <li><span class="math-container">$p(i) \leftarrow \frac{f(i)}{\sum_{j=1}^{N} f(j)}, \forall i$</span></li>
  </ol>
  <p>This technique is known as <strong>windowing</strong>, which I have used in the past to solve the exact same problem that you seem to be trying to solve (check it <a href="https://github.com/nbro/function_max_with_ga" rel="nofollow noreferrer">here</a>).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-09T06:00:31.757Z
Answers.Text: In one generation there can be chromosomes with both positive and negative values, not as you assumed all must have one sign inside one generation. But it looks like the solution of adding absolute value of the generation's minimum would actually work.
Answers.UserDisplayName: GKozinski
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-09T11:14:24.813Z
Answers.Text: @Makintosz I assumed that there can be both positive and negative values of $h(i)$ in one generation. If you shift by the absolute value of the smallest $h(i)$, you make all positives, and that allows you to convert the fitness into a probability of being selected. That's the idea.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-09T12:27:03.343Z
Answers.Text: ahhh ok, I was misslead because you wrote: "ALL h(i), for i=1,…,N (where N is the size of the population), are negative" and "so that they are ALL positive and you can compute the probability". But it doesnt matter, I will try the solution later.
Answers.UserDisplayName: GKozinski
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-08T23:10:46.930Z
Answers.LastActivityDate: 2021-02-08T23:10:46.930Z
Answers.UserDisplayName: nbro
Body: <p>I am optimising function that can have both positive and negative values in pretty much unknown ranges, might be -100, 30, 0.001, or 4000, or -0.4 and I wonder how I can transform these results so I can use it as a fitness function in evolutionary algorithms and it can be optimised in a way that, for example, it can go from negative to positive along the optimisation process (first generation best chromosome can have -4.3 and best at 1000 generation would have 5.9). Although the main goal would always be to maximise the function.</p>
  <p>Adding a constant value like 100 and then treating it simply as positive is not possible because like I said, the function might optimise different ranges of results in different runs for example (-10000 to +400 and in another run from -0.002 to -0.5).</p>
  <p>Is there a way to solve this?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-08T15:55:48.380Z
Comments.Text: What make you think that you can't use negative number in your fitness function ?
  The objective of the fitness function is to tell how much your solutions are close to the optimum solution so that you can rank them. From what you are describing it look like your function is doing exactly this or am I missing something ?
Comments.UserDisplayName: kirua
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-08T18:49:44.687Z
Comments.Text: @kirua in evolutionary algorithms, choice of chromosomes to the new population is based on their scores from the fitness function, to achieve we take fitness function score of each chromosome and divide it by sum of all fitness function results and if we add negative values it will simply not make sense, because sum will be lower than it really should be.
Comments.UserDisplayName: GKozinski
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-10T00:00:16.623Z
Comments.Text: You can always try and see if taking the logarithm of the objective function improves your situation. This makes them positive and their distribution uniform (if it was exponential before). Might make sense if you have such a huge range of values. Also maybe you can somehow normalize (e.g. divide by the range between highest and lowest value) if the range changes that much from iteration to iteration. After all, you only need to compare the members of each generation to each other.
Comments.UserDisplayName: Mafu
Tags: <genetic-algorithms><optimization><evolutionary-algorithms><fitness-functions>
Title: How to deal with evolutionary/genetic fitness function that can have both negative and positive values?
UserDisplayName: GKozinski
ThreadText: # How to deal with evolutionary/genetic fitness function that can have both negative and positive values?
  
  (Asked by: username_0 on 2021-02-08)
  
  I am optimising function that can have both positive and negative values in pretty much unknown ranges, might be -100, 30, 0.001, or 4000, or -0.4 and I wonder how I can transform these results so I can use it as a fitness function in evolutionary algorithms and it can be optimised in a way that, for example, it can go from negative to positive along the optimisation process (first generation best chromosome can have -4.3 and best at 1000 generation would have 5.9). Although the main goal would always be to maximise the function.
  
  Adding a constant value like 100 and then treating it simply as positive is not possible because like I said, the function might optimise different ranges of results in different runs for example (-10000 to +400 and in another run from -0.002 to -0.5).
  
  Is there a way to solve this?
  
  **Tags:** genetic-algorithms, optimization, evolutionary-algorithms, fitness-functions
  
  > **Comments:**
  > **username_1**: What make you think that you can't use negative number in your fitness function ? The objective of the fitness function is to tell how much your solutions are close to the optimum solution so that you can rank them. From what you are describing it look like your function is doing exactly this or am I missing something ?  
  > **username_0**: @username_1 in evolutionary algorithms, choice of chromosomes to the new population is based on their scores from the fitness function, to achieve we take fitness function score of each chromosome and divide it by sum of all fitness function results and if we add negative values it will simply not make sense, because sum will be lower than it really should be.  
  > **username_2**: You can always try and see if taking the logarithm of the objective function improves your situation. This makes them positive and their distribution uniform (if it was exponential before). Might make sense if you have such a huge range of values. Also maybe you can somehow normalize (e.g. divide by the range between highest and lowest value) if the range changes that much from iteration to iteration. After all, you only need to compare the members of each generation to each other.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2021-02-08. Score: 0 | Accepted answer
  
  If I understand correctly your problem, you always want to maximize some function $h(x)$, which is defined as follows $h: \Gamma \rightarrow \mathbb{R}$, where $\Gamma$ is the space of genotypes. However, at every generation $g$, you don't know exactly $h(i)$ of each individual $i$, i.e., maybe in one generation $g$, all $h(i)$, for $i=1, \dots, N$ (where $N$ is the size of the population), are negative, but in the next generation $g+1$, due to the mutations and crossovers, that may not be the case anymore, so you don't know how to shift $h(i), \forall i$, so that they are all positive and you can compute the probability of being selected, assuming you are using the [*fitness proportionate selection*](https://ai.stackexchange.com/a/9083/2444).
  
  If my intepretation is correct, then, at every generation $g$, you just need to find $w = \operatorname{min}\_i h(i)$, then shift all $h(i)$ by adding $|w| + \epsilon$ (where $\epsilon$ can be zero or a very small number) to all $h(i)$, so you would compute the fitness as follows $f(i) = h(i) + |w| + \epsilon$, for all $i$. Here are all the steps to compute the probability of individual $i$ being selected $p(i)$
  
  1. $w = \operatorname{min}\_i h(i)$
  2. $f(i) \leftarrow h(i) + |w| + \epsilon, \forall i$
  3. $p(i) \leftarrow \frac{f(i)}{\sum\_{j=1}^{N} f(j)}, \forall i$
  
  This technique is known as **windowing**, which I have used in the past to solve the exact same problem that you seem to be trying to solve (check it [here](https://github.com/username_3/function_max_with_ga)).
  
  > **Comments:**
  > **username_0**: In one generation there can be chromosomes with both positive and negative values, not as you assumed all must have one sign inside one generation. But it looks like the solution of adding absolute value of the generation's minimum would actually work.  
  > **username_3**: @Makintosz I assumed that there can be both positive and negative values of $h(i)$ in one generation. If you shift by the absolute value of the smallest $h(i)$, you make all positives, and that allows you to convert the fitness into a probability of being selected. That's the idea.  
  > **username_0**: ahhh ok, I was misslead because you wrote: "ALL h(i), for i=1,…,N (where N is the size of the population), are negative" and "so that they are ALL positive and you can compute the probability". But it doesnt matter, I will try the solution later.  
  
  ---
  
  