Genetic algorithm calculated values compared to measured

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

Replace
for i=1:15
S(i) =sum((Rexp(i)-R*(1-exp(-t(i)./40)))^2);
end
with
S = sum((Rexp-R*(1-exp(-t./40))).^2);

7 Comments

I replace it, but when I do the simulation in the optimization tool (ga) the running can not stop. I am a beginner, so can you explain to me how can I do exactly please.
options = optimoptions('ga', 'PlotFcn',...
{@gaplotbestf, @gaplotbestindiv, @gaplotexpectation, @gaplotstopping});
[x,fval,exitflag,output] = ga(@ga_Newton, 1, [], [], [], [], [], [], [], options)
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];
S = sum((Rexp-R*(1-exp(-t./40))).^2);
sorry Walter, I do as you ask me but there is a problem; this is the text of error:
Error using optimoptions (line 114)
Invalid solver specified. Provide a solver name or handle (such as 'fmincon' or @fminunc).
Type DOC OPTIMOPTIONS for a list of solvers.
options = struct('PlotFcn',...
{@gaplotbestf, @gaplotbestindiv, @gaplotexpectation, @gaplotstopping});
You could also use options = [] as that option is just adding on plotting while the ga optimizes for a few seconds.
thank you once again, please be patient with me, I remind you that informatique is not my domain and I am a beginner in matlab, I try all the advice that you give to me but the program run without stopping. maybe the problem is in the declaration of my fitness function, if you have understood the idea of my problem can you give me a proposal of a fitness function to fit a nonlinear equation to data with the genetic algorithm and to minimize it. I'm stuck in this task for a long time, i would be very grateful if you could help me.
I have attached the exact files I used. Execution requires less than 3 seconds.
More generally, the approach I would use would be to use the Symbolic Toolbox to prepare the function to be minimized:
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];
syms R
prediction = R*(1-exp(-t./40));
actual = Rexp;
residue = sum((actual - prediction).^2);
F = matlabFunction(residue);
nvars = 1;
A = []; b = [];
Aeq = []; beq = [];
lb = []; ub = [];
nonlcon = [];
options = gaoptimset('PlotFcn', {@gaplotbestf @gaplotbestindiv @gaplotexpectation @gaplotstopping}, ...
'TolFun', 1e-13, 'Generations', 1000);
[R, fval, exitflag, output] = ga(F, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);
fprintf('best R: %g\n', R);
fprintf('best residue: %g\n', fval);
Note: you will not get especially close to the true minima. The true minima is at approximately 0.000128381339932429 which can be quickly determined:
[R, fval] = fminunc(F, 0)
thank you very much :) :)

Sign in to comment.

More Answers (2)

Hello;
My problem is to make a fit of a nonlinear equation to data with the genetic algorithm and to minimize ‘R’, and ‘rate’ at the same time, 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), first of all I will test my code by a single value of R and rate I declared my function (fitnes function) as follows:
function residue =fitness_XY(x)
t=[0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120];
Rexp= 0.000157*(1-exp(-t/70));
residue=sum((Rexp-x(1)*(1-exp(-t./x(2)))).^2) ;
%genetic code
F=@fitness_XY
nvars = 2;
A= []; b = [];
Aeq = []; beq = [];
lb = []; ub = [];
nonlcon = [];
%Options
options=gaoptimset('PlotFcn',{@gaplotbestf}, 'CrossoverFraction',0.01,'TolFun',1e-13,'Generations',300) ;
[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);
at the end the code must return the exact value of R=0.000157 and rate=70 but it still running more than 30mn and return a false value, I don’t know where is the problem exactly

6 Comments

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.)
Sorry Mr.Walter I try the solution you have said more than once but I have not found the same solution as you, the result always different for exemple i got: (0.0466984 to 165.189)and(-0.0114416 to 9.04008) and more false results.
I want to know if there are any changes in the option to find always the exact result
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
i know that fminsearch will give me a precise result, but in my search i must minimize my function using genetic algorithm and levenberg marquardt methods and compare the two methods and identify their advantages and disadvantages
"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.
i already use the ga option and i used a lower and upper bound on the values and i got reasonable value.
thank you very much :)

Sign in to comment.

hello
I have a problem in tracing curves( plot) with the results obtained by genetic algorithme(i can't extract the result obtained by GA in each generation, i obtain just the result of final generation), my fitness function has been already declared:
function residue =fitness_XY(x)
t=[0;2;4;6;8;10;12;14;16;18;20;22;24;26;28;30;32;34;36;38;40;42;44;46;48;50;52;54;56;58;60;62;64;66;68;70;72;74;76;78;80;82;84;86;88;90;92;94;96;98;100;102];
Rexp=[-2.52708010767266e-14;5.32064895666630e-06;5.42159073721499e-06;7.75678296093904e-06;1.67256311050823e-05;1.23031850182854e-05;1.09794849995505e-05;1.20652240252192e-05;1.24698346918211e-05;2.33361919229394e-05;1.82336903162199e-05;2.33101153576165e-05;2.45945311647171e-05;1.69556354173215e-05;1.75494764515605e-05;2.36697072906705e-05;2.01220931647325e-05;2.94548337297039e-05;1.93821061390601e-05;2.03102518112670e-05;2.60712020709046e-05;2.20680494493528e-05;2.37415365853562e-05;1.95054548844394e-05;2.67173634397363e-05;3.21106195109085e-05;2.63163846059201e-05;2.80977713622924e-05;3.08339673608630e-05;4.27002816931244e-05;3.96175206240119e-05;2.90039717124409e-05;4.73335574049721e-05;3.45245585232448e-05;3.57778734384076e-05;4.21295733024959e-05;3.75669338617277e-05;4.87572144151742e-05;3.77461296632742e-05;3.58914963798983e-05;4.30056378514206e-05;3.84680097772286e-05;3.82897377331970e-05;5.62266325833374e-05;3.80177616775395e-05;4.43695039815036e-05;4.00476713092609e-05;3.88913016586448e-05;4.49264984228608e-05;4.49264984228608e-05;4.49264984228608e-05;4.49264984228608e-05];
Rth=x(1)*(1-exp(-t./x(2)));
residue=sum((Rexp-Rth).^2) ;
how i can plot:
-my fitness function(or any function) in each generation, to see the evolution of my code.
-Rexp and Rth in each generation.
-how my function converge to the best result in different generation.

3 Comments

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?
sorry you don't understand me, i don't like to see the default plot function in matlab(like @gaplotbest, @gaplotbestindiv...).
i want to get a custom function, in the xlabel(generation) and in the ylabel(an ather parameter in differents figure)
or [xlabel(x(1)) and ylabel(x(2)) at different generation] to show how the populatios of individus converg to the best individu from generation to an ather.
thank you very much.
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!