Genetic Algorithm - Fitness function value differences...
Show older comments
Hello everybody,
I am currently using a genetic algorithm thanks to the "ga" function in MatLab 2012a, and I am encountering an issue whom I have no idea how to solve...
I have my fitness function that i'm trying to minimize. Let's call her myfun :
function merit = myfun(x,...,...,...)
A=x(1);
B=x(2);
C=x(3);
... some boring stuff ...
merit=...;
% I save the current parameters values and the figure of merit in a txt file
param=[A B C merit];
save parameters.txt param -append -ascii
end
Until that point, everything is fine. Each individual is written in my txt file...
BUT: When I'm looking at my current best individual in my txt file, and I recompute the figure of merit of my function, I don't get the same value than the one in the txt file.
Example:
My best individual in my txt file:
A = 9.9488952e-01
B = 7.7381392e+01
C = 3.2928992e+07
merit = 3.9479605e+00
If I recompute the performance manually (with the same code, just evaluating the code line by line) and I write the result in my txt file, I get:
A = 9.9488952e-01
B = 7.7381392e+01
C = 3.2928992e+07
merit = 3.5089634e+00
The same problem occurs in the totally different program of one of my colleague... And I don't understand why... Hope that I was clear enough and I apologize for my weak english skills...
Thank you in advance
Answers (3)
Sean de Wolski
on 4 Nov 2013
Edited: Sean de Wolski
on 4 Nov 2013
0 votes
What is the boring stuff? The secret likely lies in there.
*Some specific things to look for:
- Anything random?
- Anything that relies on persistent variables, global variables or nested functions with persistent states?
- What options are you passing into ga?
- Have you tried using ga with a 'HybridFcn' so that the genetic algorithm may find the basin of the global minimum and then a gradient based solver can finish it?
- Have you tried using patternsearch, when integer constraints aren't necessary, this is generally recommended over ga.
Vialla
on 5 Nov 2013
0 votes
1 Comment
Sean de Wolski
on 5 Nov 2013
Edited: Sean de Wolski
on 5 Nov 2013
This could be. MATLAB uses double precision so about 16 digits. It seems reasonable to me that for some problems a loss of nine digits of precision could cause serious errors and for errors to propagate.
Ben Petschel
on 6 Nov 2013
fpeek = @(x)peek(myfun(x,...),x);
and then run the genetic algorithm with fpeek as the objective function, then extract the points and function values with
[fval,xval] = peek()
If the function is deterministic then you should have fval{i} equal to myfun(xval{i}).
If myfun just involves simple arithmetic operations then you should check whether catastrophic cancellation is occurring by subtracting two nearly equal values. Try to algebraically simplify the expressions where possible to avoid subtraction, or replace expressions such as exp(x)-1 with expm1(x).
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!