This is an example of Object Oriented programming. This program has been written using VC++ 6.0 as a Win32 Console application. This program uses Genetic Algorithm to fit a function to as many points as you desire. it's objective is to minimize the some of square errors in all the points.
We choose the function as follows:
it has 12 coefficients, so we choose the chromosome as an array of 12 Real numeric quantities and we generate each generation (population) of 20 chromosomes as follows:
1.Ten best chromosomes of the previous generation are included in this generation automatically.
2.Next ten chromosomes are generated using Genetic laws of inheritance ( Cross Over, Mutation ) Using the best 10 results of the previous generation.
The first population is generated using Random Real numbers and then they are sorted from the one resulting in minimum Sum of Square Errors to the maximum. And the top ten on the list are used to generate the next population and so on. The formula used to calculate Sum of Square Error is as follows:
There are many different rules to be used for Cross Over and Mutation, as an example you can see the results for a couple of them after 10,000 iteration.
As an example after 10,000 iterations for fitting the function to 6 points using the rules seen in the Fit_Function program we have the following results:
desired and current point and values :
points | -10 | -5 | 0 | 5 | 10 | 15 |
desired values | 15 | 8 | 7 | 11 | 20 | 25 |
current values | 15.2596 | 8.50325 | 6.99848 | 12.1297 | 20.9653 | 23.16 |
iteration#0(best result):
A0..A5 | 0.75 | 76.93 | 143.05 | 2.29 | -47.26 | 85.24 |
B0..B5 | 41.11 | -4.13 | -123.96 | -28.46 | 1.01 | 6.12 |
sqr_error= 338.754
iteration#10001(best result):
A0..A5 | 260.164 | -219.125 | -159.13 | -158.922 | 114.02 | 141.365 |
B0..B5 | 37.1744 | 167.5 | 167.1 | 167.52 | -7.77039 | 6.12 |
sqr_error= 5.91435
Here are two examples of other laws applied to our Cross Over function and one example for Mutation. You can replace the functions in the program with these examples and see the change in results. In some cases it can speed up the process or in other cases slow it down.