Genetic Algorithm Back
Evolutionary Computation

Genetic Algorithm Optimizer

Select a benchmark function and configure population size, mutation rate, and crossover rate. Watch the population converge to the global optimum.

Parameters
Objective Function
Population N
Mutation Rate
Crossover Rate
Max Generations
Results
0
Generation
Best Fitness
Best x
Best y
Diversity (std. dev.)
0.05
Mutation Rate
0.80
Crossover Rate
200
Max Generation
Fitness Landscape + Population (white dots) + Best (red star)
Best Fitness Over Generations
Theory & Key Formulas
$$f(x,y)=20+x^2+y^2-10(\cos 2\pi x+\cos 2\pi y)$$

Domain $[-5,5]^2$, global minimum $f(0,0)=0$.

What is a Genetic Algorithm?

🙋
What exactly is a Genetic Algorithm? It sounds like biology, not engineering.
🎓
Basically, it's a search algorithm inspired by natural evolution. You start with a random "population" of candidate solutions. Then, you repeatedly select the best ones, "mate" them to create new solutions, and apply random "mutations." Over generations, the population evolves toward an optimal solution. Try running the simulator above with the default settings to see a population spread out and then converge.
🙋
Wait, really? So the sliders for "Population Size" and "Mutation Rate" control this artificial evolution? What happens if I set the mutation rate to zero?
🎓
Great question! If you set mutation to zero, you lose a key source of new genetic material. The algorithm might get stuck on a local optimum—a good but not the best solution—because it can't explore new areas of the "fitness landscape" you see in the plot. For instance, on the Rastrigin function, which has many local minima, a low mutation rate might cause premature convergence to a suboptimal point. Try it and watch the population stop improving.
🙋
That makes sense. So what's the "Crossover Rate" for? And how do I know which benchmark function to test on, like Sphere vs. Rosenbrock?
🎓
Crossover is the "mating" process, where you combine parts of two parent solutions to create a child. The rate controls how often this happens versus just copying parents. A common case is using a high crossover rate (e.g., 0.8) to aggressively combine good traits. As for the functions, each tests the GA differently. The Sphere function is smooth and simple—good for seeing basic convergence. The Rosenbrock function has a long, flat valley, testing the algorithm's ability to navigate tricky terrain. Switch between them and see how the search behavior changes!

Physical Model & Key Equations

The core of a Genetic Algorithm is the selection process, which determines which solutions survive to reproduce. A common method is "Fitness-Proportionate Selection," where the probability of selecting an individual is proportional to its fitness score relative to the whole population.

$$P(\text{select individual }i) = \frac{f_i}{\sum_{j=1}^{N} f_j}$$

Here, $f_i$ is the fitness of individual $i$, and $N$ is the population size. A higher fitness means a better solution (e.g., a lower function value for minimization). This drives the population toward better regions of the search space.

Another key operation is mutation, which introduces random variations to maintain diversity and explore new solutions. For a real-valued parameter $x$, a simple mutation adds Gaussian noise.

$$x' = x + \mathcal{N}(0, \sigma)$$

Here, $x'$ is the mutated parameter, and $\mathcal{N}(0, \sigma)$ is a random number from a normal distribution with mean 0 and standard deviation $\sigma$, which is often related to the mutation rate you control in the simulator. This small random jump can help escape local optima.

Frequently Asked Questions

Try adjusting the crossover rate and mutation rate. If the mutation rate is too low, it tends to get stuck in local optima, and if it is too high, convergence slows down. Additionally, increasing the population size can improve search diversity and may enhance convergence.
Both are multimodal benchmark functions, but the Rastrigin function has uniform periodic undulations with many local optima, whereas the Ackley function has a steep valley near the center and is nearly flat on the outside. The Ackley function is more difficult to converge to the global optimum, providing a stricter evaluation of GA search capability.
Fix the random seed before starting the simulation. Using the same seed value ensures that the randomness in crossover and mutation is identical, allowing you to reproduce the exact same search path. You can specify the seed value on the initial settings screen.
Yes, the default domain is [-5, 5], but you can change it to any range. However, expanding the range too much increases the search space and slows convergence. Also, ensure that the global optimum is not outside the range by checking the theoretical minimum position in advance.

Real-World Applications

Aerodynamic Design: Genetic Algorithms are used to optimize the shape of aircraft wings or turbine blades. Engineers define parameters describing the shape, and the GA searches for the configuration that minimizes drag or maximizes lift, evaluating thousands of designs computationally.

Antenna Design: Designing antennas for specific radiation patterns is highly complex. GAs can optimize the layout and dimensions of antenna elements to meet target frequency and gain specifications, a task difficult for traditional methods.

Robotics & Motion Planning: GAs help optimize the gait of walking robots or the trajectory of robotic arms. The algorithm searches for joint angle sequences that minimize energy consumption or complete a task fastest while avoiding obstacles.

Financial Portfolio Optimization: In finance, GAs can search for the optimal mix of assets (stocks, bonds) that maximizes expected return for a given level of risk, navigating a complex landscape of historical data and constraints.

Common Misconceptions and Points to Note

As you become familiar with genetic algorithms (GAs) using this simulator, you might be tempted to see them as a "universal optimization tool." First, GAs do not guarantee finding the "optimal solution." They are a method for finding a "very good solution" with high probability. In practical applications, especially, the computational cost can become enormous. For example, optimizing a vehicle's crash safety might require several hours for a single CAE analysis. With a population of 100 and 50 generations, a simple calculation suggests 5000 analyses would be needed, which is often impractical. In practice, GAs are frequently used for rough exploration to determine an initial design or for generating initial solutions for problems where other methods provide no leads.

Next, avoid relying on magical parameter settings. Discard superstitions like "a mutation rate of 0.01 is the golden rule." The optimal parameters vary greatly depending on the problem. What you should try with this tool is observing how an extremely high mutation rate (e.g., 0.5) causes the population to perform a random walk and fail to converge, while an excessively low rate (e.g., 0.001) causes a loss of initial diversity, trapping the search in the first local optimum it encounters. In practice, you'll need to adjust parameters, starting with default values, then increasing the mutation rate if diversity is lost too quickly, or strengthening the selection pressure if convergence is too slow.

How to Use

  1. Select a benchmark function (Rastrigin, Ackley, Sphere, or Rosenbrock) from the function dropdown.
  2. Set population size (10–500 individuals), mutation rate (0.01–0.5), crossover rate (0.5–0.99), and maximum generations (10–1000) using the sliders.
  3. Click Start to evolve the population; observe fitness convergence, diversity metrics, and parameter trace in real-time plots.

Worked Example

Optimizing the Rastrigin function (global minimum f(0,0)=0) with population=50, mutation=0.15, crossover=0.8, generations=200: initial best fitness ≈ 45.2 at generation 1; after 100 generations, fitness drops to 8.3 with best point near (0.4, −0.2); final generation achieves f=1.7 at (0.08, 0.06). Diversity (std. dev.) decays from 12.4 to 0.3, indicating convergence. Mutation rate 0.15 prevents premature stagnation while crossover 0.8 enables effective recombination.

Practical Notes

  1. For multimodal landscapes (Rastrigin, Ackley), use higher mutation rates (0.2–0.3) and lower crossover (0.6–0.7) to escape local optima; for unimodal Sphere function, reduce mutation to 0.05–0.1 for faster convergence.
  2. Monitor diversity plateau; if std. dev. drops below 0.1 before generation target, increase mutation or restart with larger population to explore unexplored regions.
  3. Crossover rates >0.9 exploit elite solutions aggressively; use 0.5–0.7 for balanced exploration–exploitation on ill-conditioned problems like Rosenbrock.