Genetic algorithm calculated values compared to measured
Show older comments
Hi
I am a student enrolled in the 2nd year physics Master's degree, i am a beginner in Matlab and i have the same problem like Mattias, can you help me and is it was possible to get your e-mail.
My problem is to make a fit of a nonlinear equation to data with the genetic algorithm and to minimize R, my model function is
R*(1-exp(-t/rate))
The industrial data Rexp and t are known: these are two vectors that give the variation of Rexp as a function of time (t)
rate also is known and equal to 40
I declared my function (fitnes function) as follows:
function S=ga_Newton(R)
t=[0 2 4 6 8 10 12 14 16 18 20 22 24 26 28];
Rexp=[1 .7987e-05 3.07996e-05 3.90531e-05 1.89562e-05 2.82794e-05 5.55377e-05
4.75723e-05 6.2447e-05 5.35282e-05 5.16775e-05 4.67144e-05 4.63215e-05 6.62753e-05
3.72551e-05 3.54374e-05];
for i=1:15
S(i) =sum((Rexp(i)-R*(1-exp(-t(i)./40)))^2);
end
The function
S (i) = sum ((Rexp (i) -R * (1-exp (-t (i) ./ 40))) ^ 2)
I use it in the methods of Gauss Newton and Levenberg marquardt and I got a good result of R which is in the order of 0.000059 but with the genetic algorithm I found no result.
Accepted Answer
More Answers (2)
Saber_phy
on 10 Sep 2018
6 Comments
Walter Roberson
on 11 Sep 2018
I got to 0.000155851 with 69.036 using the below; it took 2207 generations.
%genetic code
F=@fitness_XY
nvars = 2;
A= []; b = [];
Aeq = []; beq = [];
lb = [0 0]; ub = [];
nonlcon = [];
%Options
options=gaoptimset('PlotFcn',{@gaplotbestf}, 'CrossoverFraction',0.01,'TolFun',1e-15,'Generations',3000) ;
[x,fval,exitflag,output]=ga(F, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options) ;
fprintf('best R and rate: %g\n', x) ;
fprintf('The number of generations was : %d\n', output.generations);
fprintf('The number of function evaluations was : %d\n', output.funccount);
fprintf('The best function value found was(best residue) : %g\n', fval);
Remember that ga() is not necessarily efficient about finding global minima, especially when the gradients are relatively small (though it does not compute gradients.)
Saber_phy
on 12 Sep 2018
Walter Roberson
on 12 Sep 2018
No, if you need the exact results then you should not use ga().
You can get close by quite quickly using fminsearch()
0.00157 and 70 is not an exact solution: if you differentiate the function and substitute in those values, you will get a slightly negative X derivative. The exact solution is closer to 0.00015699999999999999478923798244855 69.999999999999999999999999999511 or maybe 0.00015699999999999999437776121435917 70.0 depending how you calculate
Saber_phy
on 12 Sep 2018
Walter Roberson
on 13 Sep 2018
"ga does not give an acceptable result within an acceptable time" is a valid conclusion.
You can use ga options to pass in a starting point: look at the initial population matrix parameter.
If it is reasonable to use a lower bound or upper bound on the values, you should do so. Even if it is just lower bound 0.
Saber_phy
on 13 Sep 2018
3 Comments
Walter Roberson
on 4 Oct 2018
For plotting the fitness value at each generation (not each iteration) then modify
options = gaoptimset('PlotFcn', {@gaplotbestf @gaplotbestindiv @gaplotexpectation @gaplotstopping}, ...
'TolFun', 1e-13, 'Generations', 1000);
to suit your needs.
Rth and Rexp are vectors in each evaluation of the fitness_XY() function. Are you sure it makes sense to plot them?
Saber_phy
on 4 Oct 2018
Walter Roberson
on 4 Oct 2018
Use nested functions with shared variables to take a record of all the values you want plotted. Provide your own plot function that you name in the 'PlotFcn' options.
ga does not appear to track individuals as to whether they are converging to the best of the generation. A work around might be for you to use an extra variable in your vector that encoded a unique identity, and code custom mutation and cross-over functions that somehow encoded identity as well, and then provide your own custom plot function that analyzed the Population state to show whatever it is you are trying to plot.
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!