How to code a Genetic Algorithm that has a function within the fitness function?
1 view (last 30 days)
Show older comments
I'm attempting to use the Genetic Algorithm (GA) to find variables in functions g1(x) and g2(x) so that g1(x) and g2(x) will match functions f1(x) and f2(x), respectively. The functions f1(x) and f2(x) are predetermined from a set of measured data. The function g1(x) and g2(x) have four unkown variables that are the same across the two functions. I'm trying to find the four unknown variables that will be used to equate g1(x) to f1(x) and g2(x) to f2(x). The cost function that I am trying to minimize is of the following format:
weight = 0.5
Cost = weight*(f1(x) - g1(x)).^2 + weight*(f2(x) - g2(x)).^2
The cost function is what I'm using as my simplefitness function following the procedure of this documentation. How do I go about setting up the fitness function that has a functions, g1(x) and g2(x), within it whos variables I'm trying to find ?
0 Comments
Answers (1)
Alan Weiss
on 5 Nov 2018
Instead of minimizing the sum of absolute deviations, I suggest that you minimize the sum of squared deviations. Then do not use ga, but use lsqnonlin or lsqcurvefit as shown in this example.
Of course, you are free to minimize the sum of absolute deviations instead. Just use that as your objective function as shown in the fminunc portion of the example I just referenced.
Alan Weiss
MATLAB mathematical toolbox documentation
2 Comments
Alan Weiss
on 9 Nov 2018
The example I linked also has a section using fminunc, which basically has the same sort of syntax as ga. So I don't understand what your difficulty is. Is it calling ga? There are a lot of examples in the documentation. Is it setting up an objective function? The example I linked shows that. So I don't know how to help.
Alan Weiss
MATLAB mathematical toolbox documentation
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!