Answers.Body: <p>Yours is a really nice and easy question: you've seen how to use GAs in a complex problem but you're missing how to apply them to the most basic of all. I'll show you:</p>
  
  <p>In a real-world implementation we have to structure our problem to be solved with GAs; we need to modify it, finding an equivalent representation that accepts individuals, each one of these is built from a genome and must be evaluated using a fitness function.</p>
  
  <p>If you see this graphically you'll discover that the fitness function is just describing an area (or line or volume, depending on the grade) on our space and we are randomly dropping individuals on it, awarding the ones that fell higher than the others. We then try to modify the genomes of these guys to move them towards peaks of this fitness function.
  <a href="https://i.sstatic.net/kaXXd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kaXXd.png" alt="Crossover vs mutation, I did this image, feel free to use it."></a></p>
  
  <p>In practice the fitness function is our world, our ground truth and we are exploring it.</p>
  
  <p>Now to the function optimization part: well, we do not need any abstraction here, any strange individual representation or transformation of the problem; we just want to find a maximum, and this is exactly what GAs are for!</p>
  
  <p>So, let's have a look at the elements you need to solve your problem with GAs:</p>
  
  <ul>
  <li>Fitness function => you have it already! It's the function you want to find the max of.</li>
  <li>Individual => as the maximum is enough, so the individual will be just a point in space.</li>
  <li>Genome => like every respective point in space, the genome will just be a collection of real numbers (one for each dimension).</li>
  </ul>
  
  <p>Now, like in any GA, you will instantiate randomly an initial population (random points in the domain). The evaluation of course is the easy part, just put them into the function and you'll se the value of the individuals.<br>
  What about crossover and mutation though? You'll have to use techniques that work with real-number genes, like BLXalpha, BLXalphabeta.
  These are just defining ranges between values and picking random values inside these ranges, I wrote a pretty detailed answer about this, you can check it out at: <a href="https://ai.stackexchange.com/a/6323/15530">https://ai.stackexchange.com/a/6323/15530</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-05T12:02:59.623Z
Answers.Text: In my opinion, this answer provides little insight into how to really solve this problem. For instance, you say that the fitness function is the function you're trying to find the maximum of. However, this is not really true, although the fitness function is a function of the original function. See [my answer](https://ai.stackexchange.com/a/24420/2444).
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-19T10:12:24.067Z
Answers.LastActivityDate: 2019-07-01T02:59:57.767Z
Answers.LastEditDate: 2019-07-01T02:59:57.767Z
Answers.UserDisplayName: Alvin Sartor
Answers.Body: <p>People usually say that genetic algorithms are used to solve optimization problems, but when it comes to optimizing a <em>specific</em> function given in an analytic form (i.e. when it comes to finding a maximum or minimum of such a function), it may not be clear how to proceed. I have created a complete but simple implementation and explanation of how to solve this problem <a href="https://github.com/nbro/function_max_with_ga" rel="nofollow noreferrer">here</a>, but let me also describe here the main idea behind the approach. Before that, let's briefly review genetic algorithms (GAs).</p>
  <h3>Genetic algorithms</h3>
  <p>Genetic algorithms are composed of</p>
  <ul>
  <li><p>a <strong>population</strong> (i.e. a set) of individuals (also known as chromosomes or genotypes), which represent the solutions to some problem</p>
  </li>
  <li><p>a <strong>fitness function</strong> that evaluates each individual (i.e. how &quot;good&quot; it is, maybe compared to other individuals, where &quot;good&quot; depends on the problem)</p>
  </li>
  <li><p><strong>genetic operations</strong> to stochastically change the individuals in the population: typically, these operations are the <strong>mutation</strong> and <strong>cross-over</strong></p>
  </li>
  <li><p>a method to <strong>select individuals</strong> for the cross-over (where you combine 2 or more individuals to produce other individuals); the selection is also a <em>genetic operation</em></p>
  </li>
  </ul>
  <h3>How to solve your problem?</h3>
  <p>To solve <em>any</em> problem with genetic algorithms, you first need to address all the four points above, i.e. define what your individuals (i.e. solutions) are, how to compute the fitness of a solution (i.e. how good it is), and define the specific evolutionary operations (specifically, mutation, cross-over and selection).</p>
  <p>In your specific problem, the solutions are <span class="math-container">$\hat{x} \in \mathbb{R}$</span>, such that</p>
  <p><span class="math-container">$$f(x)=\frac{-x^{2}}{10}+3 x$$</span></p>
  <p>is a (local or global) maximum, i.e. <span class="math-container">$f(\hat{x}) \geq f(x)$</span>, for all <span class="math-container">$x \in  \mathbb{R}$</span> in a neighbourhood of <span class="math-container">$\hat{x}$</span>.</p>
  <p>(Note that this is just the definition of the problem of <em>function maximization</em>: if you are not familiar with it, you should probably get familiar with it before trying to understand this answer or even trying to solve this problem with GAs).</p>
  <p>Therefore, in this case, the <strong>individuals</strong> are real numbers (which are the inputs to <span class="math-container">$f$</span>).</p>
  <p>The <strong>fitness function</strong> can be a function that computes <span class="math-container">$f(x_i)$</span>, for all <span class="math-container">$x_i$</span> in your population, then compares <span class="math-container">$f(x_i)$</span> to <span class="math-container">$f(x_j)$</span> for all <span class="math-container">$i \neq j$</span>. The higher the <span class="math-container">$f(x_i)$</span>, the closer it is to a maximum.</p>
  <p>The <strong>genetic operations</strong> can be implemented in different ways. You should think about it. If you are familiar with GAs and you know now that solutions are real numbers, at least one way of implementing these genetic operations should come to your mind at this point. Keep in mind that your solutions should be in the range <span class="math-container">$[0, 32]$</span>, i.e. this is a <em>constrained</em> optimization problem. If you do not have any idea on how to implement them, take a look at <a href="https://github.com/nbro/function_max_with_ga" rel="nofollow noreferrer">my implementation/explanation</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-19T11:45:12.617Z
Answers.Text: I have a .csv file with 5 inputs (columns) with different parameters. And one output (column). How can I change the input values to optimize my output? Although I have understood the general idea of Genetic Algorithms, I cannot understand how to use them in my case... (I don't feel there is need to open a new thread, because my problem  is quite related to the current question/thread)
Answers.UserDisplayName: jim
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-19T19:55:15.443Z
Answers.Text: Genetic algorithms are quite simple, although they may not always be effective. So, first, you need to identify what a solution is. What is a solution in your case, i.e. what do you want to find? A number? A vector? By convention, we call a solution an individual. The idea of genetic algorithms is to maintain a set (or population) of individuals, randomly change and combine them, in such a way that better solutions are more likely to be kept in this set (population) than worse solutions. Now, you see I used the word "better". That's another thing you need to define and it's very important:
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-19T19:57:27.923Z
Answers.Text: basically, it's the objective function, but in genetic algorithms it's called the fitness function (just another name for the same concept). So, in your case, you need to understand how you can or need to evaluate an individual. If you provide more info about how the 5 numbers are related to the output value and what exactly you want to find, then I could try to be more specific.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-05T12:27:37.873Z
Answers.LastActivityDate: 2020-11-05T14:58:09.107Z
Answers.LastEditDate: 2020-11-05T14:58:09.107Z
Answers.UserDisplayName: nbro
Body: <p>I recently learned about genetic algorithms and I solved the 8 queens problem using a genetic algorithm, but I don't know how to optimize any functions using a genetic algorithm.</p>
  <p><span class="math-container">$$
  \begin{array}{r}
  \text { maximize } f(x)=\frac{-x^{2}}{10}+3 x \\
  0 \leq x \leq 32
  \end{array}
  $$</span></p>
  <p>I want a guide on how to find chromosomes and fitness functions for such a function? And I don't want code.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-05T12:37:26.620Z
Comments.Text: [Here](https://datascience.stackexchange.com/q/30026/10640) you can find another similar question.
Comments.UserDisplayName: nbro
Tags: <genetic-algorithms><optimization><evolutionary-algorithms>
Title: How do I optimize a specific function using a genetic algorithm?
UserDisplayName: Evan Pk
ThreadText: # How do I optimize a specific function using a genetic algorithm?
  
  (Asked by: username_0 on 2018-09-29)
  
  I recently learned about genetic algorithms and I solved the 8 queens problem using a genetic algorithm, but I don't know how to optimize any functions using a genetic algorithm.
  
  $$
  \begin{array}{r}
  \text { maximize } f(x)=\frac{-x^{2}}{10}+3 x \\
  0 \leq x \leq 32
  \end{array}
  $$
  
  I want a guide on how to find chromosomes and fitness functions for such a function? And I don't want code.
  
  **Tags:** genetic-algorithms, optimization, evolutionary-algorithms
  
  > **Comments:**
  > **username_1**: [Here](https://datascience.stackexchange.com/q/30026/10640) you can find another similar question.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-10-19. Score: 2
  
  Yours is a really nice and easy question: you've seen how to use GAs in a complex problem but you're missing how to apply them to the most basic of all. I'll show you:
  
  In a real-world implementation we have to structure our problem to be solved with GAs; we need to modify it, finding an equivalent representation that accepts individuals, each one of these is built from a genome and must be evaluated using a fitness function.
  
  If you see this graphically you'll discover that the fitness function is just describing an area (or line or volume, depending on the grade) on our space and we are randomly dropping individuals on it, awarding the ones that fell higher than the others. We then try to modify the genomes of these guys to move them towards peaks of this fitness function.
  [![Crossover vs mutation, I did this image, feel free to use it.](https://i.sstatic.net/kaXXd.png)](https://i.sstatic.net/kaXXd.png)
  
  In practice the fitness function is our world, our ground truth and we are exploring it.
  
  Now to the function optimization part: well, we do not need any abstraction here, any strange individual representation or transformation of the problem; we just want to find a maximum, and this is exactly what GAs are for!
  
  So, let's have a look at the elements you need to solve your problem with GAs:
  
  * Fitness function => you have it already! It's the function you want to find the max of.
  * Individual => as the maximum is enough, so the individual will be just a point in space.
  * Genome => like every respective point in space, the genome will just be a collection of real numbers (one for each dimension).
  
  Now, like in any GA, you will instantiate randomly an initial population (random points in the domain). The evaluation of course is the easy part, just put them into the function and you'll se the value of the individuals.  
  What about crossover and mutation though? You'll have to use techniques that work with real-number genes, like BLXalpha, BLXalphabeta.
  These are just defining ranges between values and picking random values inside these ranges, I wrote a pretty detailed answer about this, you can check it out at: <https://ai.stackexchange.com/a/6323/15530>
  
  > **Comments:**
  > **username_1**: In my opinion, this answer provides little insight into how to really solve this problem. For instance, you say that the fitness function is the function you're trying to find the maximum of. However, this is not really true, although the fitness function is a function of the original function. See [my answer](https://ai.stackexchange.com/a/24420/2444).  
  
  ---
  
  ### Answer by username_1 on 2020-11-05. Score: 1
  
  People usually say that genetic algorithms are used to solve optimization problems, but when it comes to optimizing a *specific* function given in an analytic form (i.e. when it comes to finding a maximum or minimum of such a function), it may not be clear how to proceed. I have created a complete but simple implementation and explanation of how to solve this problem [here](https://github.com/username_1/function_max_with_ga), but let me also describe here the main idea behind the approach. Before that, let's briefly review genetic algorithms (GAs).
  
  ### Genetic algorithms
  
  Genetic algorithms are composed of
  
  * a **population** (i.e. a set) of individuals (also known as chromosomes or genotypes), which represent the solutions to some problem
  * a **fitness function** that evaluates each individual (i.e. how "good" it is, maybe compared to other individuals, where "good" depends on the problem)
  * **genetic operations** to stochastically change the individuals in the population: typically, these operations are the **mutation** and **cross-over**
  * a method to **select individuals** for the cross-over (where you combine 2 or more individuals to produce other individuals); the selection is also a *genetic operation*
  
  ### How to solve your problem?
  
  To solve *any* problem with genetic algorithms, you first need to address all the four points above, i.e. define what your individuals (i.e. solutions) are, how to compute the fitness of a solution (i.e. how good it is), and define the specific evolutionary operations (specifically, mutation, cross-over and selection).
  
  In your specific problem, the solutions are $\hat{x} \in \mathbb{R}$, such that
  
  $$f(x)=\frac{-x^{2}}{10}+3 x$$
  
  is a (local or global) maximum, i.e. $f(\hat{x}) \geq f(x)$, for all $x \in \mathbb{R}$ in a neighbourhood of $\hat{x}$.
  
  (Note that this is just the definition of the problem of *function maximization*: if you are not familiar with it, you should probably get familiar with it before trying to understand this answer or even trying to solve this problem with GAs).
  
  Therefore, in this case, the **individuals** are real numbers (which are the inputs to $f$).
  
  The **fitness function** can be a function that computes $f(x\_i)$, for all $x\_i$ in your population, then compares $f(x\_i)$ to $f(x\_j)$ for all $i \neq j$. The higher the $f(x\_i)$, the closer it is to a maximum.
  
  The **genetic operations** can be implemented in different ways. You should think about it. If you are familiar with GAs and you know now that solutions are real numbers, at least one way of implementing these genetic operations should come to your mind at this point. Keep in mind that your solutions should be in the range $[0, 32]$, i.e. this is a *constrained* optimization problem. If you do not have any idea on how to implement them, take a look at [my implementation/explanation](https://github.com/username_1/function_max_with_ga).
  
  > **Comments:**
  > **username_3**: I have a .csv file with 5 inputs (columns) with different parameters. And one output (column). How can I change the input values to optimize my output? Although I have understood the general idea of Genetic Algorithms, I cannot understand how to use them in my case... (I don't feel there is need to open a new thread, because my problem  is quite related to the current question/thread)  
  > **username_1**: Genetic algorithms are quite simple, although they may not always be effective. So, first, you need to identify what a solution is. What is a solution in your case, i.e. what do you want to find? A number? A vector? By convention, we call a solution an individual. The idea of genetic algorithms is to maintain a set (or population) of individuals, randomly change and combine them, in such a way that better solutions are more likely to be kept in this set (population) than worse solutions. Now, you see I used the word "better". That's another thing you need to define and it's very important:  
  > **username_1**: basically, it's the objective function, but in genetic algorithms it's called the fitness function (just another name for the same concept). So, in your case, you need to understand how you can or need to evaluate an individual. If you provide more info about how the 5 numbers are related to the output value and what exactly you want to find, then I could try to be more specific.  
  
  ---
  
  