How to code a Genetic Algorithm that has a function within the fitness function?

1 view (last 30 days)
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 ?

Answers (1)

Alan Weiss
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
Ryan Banks
Ryan Banks on 6 Nov 2018
Edited: Ryan Banks on 9 Nov 2018
Hello Alan,
Thanks for the reply and the suggestions you've provided. However, in an effort to not over complicate my question I've provided a more simple cost function than what I will eventually implement later on. I'm specifically interested in using the GA and learning the syntax/setup so that I can move onto a more advanced problem.
I've updated my question to help clarify some things. If more details are required to answer my question on how to implement a GA in the manner I specified let me know and I can provide further details. I apologize for any confusion.
Also here are some other simialr questions I found online question 1 and question 2 .
Alan Weiss
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

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!