Genetic Algorithm for Solving Constrained Optimization Problems in C++
The following article refers to my previous one where besides I solve simple quadratic equations I focused mainly on robot path planners in 2D and 3D.
In this article, I will display shorty how you can solve an optimization problem with constraints (s.t — subjected to).
The theoretical background you can find in the article I referred to.
The source code for this particular problem you will find on my GitHub.
In order to find the solution for this particular problem (minimize the F function), the objective function which is constrained has to include penalties (one for each constraint) — for each constraint in the discussed problem I used different values of penalty. The panties are added by running the optimization, however in this iteration where a certain condition is not fulfilled
For the purposes of this article, we will solve the following optimization problem using a genetic algorithm.
As expected the algorithm solved the problem with the error 0.00001. In order to achieve the required accuracy the number of “chromosomes” has to be increased. I use 10000 chromosomes (see source code).
Compile your code with optimization (see my previous article):
g++ genetic_algorithm_constrained.cpp -O3 -o genetic
After running you should receive the solution for this particular problem,
For your particular objective function, you have to adapt the parameters of the genetic algorithm (mostly the number of chromosomes and the size of (percent) of each group: elitism, mutation, and crossover). The other thing is connected with the modification of the conditions where the objective function is satisfied. Please enjoy.
Thank you for reading!