# Learn C++ Optimization With A Genetic Algorithms Example

Solving C++ optimization problems are one of the areas of all quantitative disciplines from social science, economics to engineering fields such as computer science. Genetic Algorithm (GA) is a kind of machine learning process that is used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover, and selection. In this post, we explain how you can achieve optimization using artificial intelligence techniques.

The Genetic Algorithm that we use here below was first mentioned by Željko Kovačević (Embarcadero MVP). Željko has amazing VCL Examples and blog posts about C++ Builder. He gave me this example below as a console app about GA and allowed me to release it free, but credits of this code may require contact with him. Then I improve and simplify (I can’t ofc) it for the C++ Builder and C++ Builder CE. Here, the field and codes below may be harder for beginners but I am sure this post may help how you can develop your scientific applications with C++ Builder CE.

## What is a Genetic Algorithm?

In computer science and research, a Genetic Algorithm (GA) is an algorithm that is used to solve optimization problems by evolving towards better solutions, just as sentient beings do in nature. Genetic Algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms. Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover, and selection. In a Genetic Algorithm, first, we create an initial population, then we iterate in a loop by calculating the fitness value, selection, crossover, and mutation steps as below,

Genetic Algorithms are one of the older AI/ML methods developed to solve some problems such as solving sudoku puzzles. Genetic Algorithms and Fuzzy Logic were very popular in the 1990s. A typical genetic algorithm requires:

1. genetic representation of the solution domain,
2. fitness function to evaluate the solution domain.

## How to develop a genetic algorithm with C++ Builder?

In our optimization example in C++, we develop an optimization algorithm such as Genetic Algorithm about our chosen field. Now let’s explain quickly what we mean by that. First, we have a global `Input value` that represents a value (number) for which Genetic Algorithm (GA) is trying to find its binary representation.

We have individuals to evaluate with genetic algorithms, so we can create this class below.

In this case, for input value 1234567890 GA is trying to find a correct solution which is 0100.1001.1001.0110.0000.0010.1101.0010. To do so, first, it will create a random (initial) population (Generation 1) and then will optimize the currently best solutions through generations using selection, crossover, and mutation operations. So, first, we need a Population class as shown below.

We need to display our value of generations in binary mode, so we need to convert our value (i.e. InputValue) to binary string as below,

## How to iterate a genetic algorithm in C++?

In genetic iteration loop, we need a tournament selection method `tournamentSelection()` as shown below.

We need to crossover gene pairs with `crossPair()` method like so:

and we can create a `Crossover()` class as below:

We can do some mutations (random gene changes) with this `Mutation()` method like so:

## How to run a genetic algorithm with one click in C++ Builder?

Now we can create our C++ Builder FMX app, with the following components:

• 2 Edit (TEdit),
• a Memo (TMemo),
• a TeeChart with y=f(x) Series (TChart)
• a Button (TButton)

Here, Edit1 is for `Input value`, Edit2 is to see `Seed value`, Memo1 is to display results, TChart1 is to display GA optimization in graphics and Button1 to run GA again and again. Double click to Button1. This Button will setup GA and run the GA Algorithm. It will create a random (initial) population (`Generation 1`), and then will optimize currently best solutions through generations using `selection`,` crossover` and `mutation` operations. Here we is how it will do,

This will optimize currently best solutions through generations using `selection``crossover` and `mutation` operations. Here is an example runtime:

This application can be developed by the C++ Builder CE free version or by the professional versions of C++ Builder. Here is the full project that you can download from my personal web page : https://www.yyoru.com/dl/GeneticAlgorithmsFMX.rar

To know how close it is to a correct solution (global optima) fitness function (value) is used. In this case, it is a minimization problem where the GA values the solutions (binary representations) that are closer to the correct solution. So, the best fitness value, in this case, is 0 (the best solution in the current generation is no different than the correct solution). As it can be seen, first it starts with a large fitness value, and then it gets smaller and smaller because the individuals (solutions) get better and better through generations. Finally, it reaches 0 in the last generation number when it found a correct solution.

Note that, GAs are non-deterministic and solutions may vary each time you run the app. That is why the Seed value is used to be able to repeat the process under the same conditions.

What are you waiting for? Download the free version of C++ Builder 11 CE Community Edition here: https://www.embarcadero.com/products/cbuilder/starter and start to develop your scientific app!