Genetic Algorithm - Fitness function value differences...

2 views (last 30 days)
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
Sean de Wolski on 4 Nov 2013
Edited: Sean de Wolski on 4 Nov 2013
What is the boring stuff? The secret likely lies in there.
*Some specific things to look for:
  1. Anything random?
  2. 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
Vialla on 5 Nov 2013
Hello,
Thank you for your answer. I checked my code and there is nothing random in there. In fact each computation is repeatable when executed line by line (the boring stuff are just elementary operations). I could maybe try another optimization method but the problem isn't linked with the precision of the final solution: it's just that I can't find the same result at one given step of computation with the algorithm and when I compute the program line by line. Moreover, my coworker encounters the same issue whereas he is not using the same program at all (in fact, it's not even applied to the same physics field!). He just saves each computation step in a txt file like me.
I'm not sure, but I maybe found one possible explanation: the variable written in the txt file has a precision of 7 digits after the comma. There is maybe a loss of precision in the txt file compared with the variable precision in Matlab. So, when I load back the values in Matlab, there could be a slight difference in my input parameters. I'm just a little bit surprised that these small differences in my input paramters can change a lot my function score... I don't know if there is a way for me to check this possibility...
  1 Comment
Sean de Wolski
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.

Sign in to comment.


Ben Petschel
Ben Petschel on 6 Nov 2013
An alternative way to record the evaluated points is with the utility PEEK. E.g. set
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).

Products

Community Treasure Hunt

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

Start Hunting!