0

I want to find the point that a two variable function has a maximum using a genetic algorithm in MATLAB.

The following code implements my function. The initial function has no minus sign.

function f = coveragefn(x)
p(1) = x(1)^2 - 12*x(1)+70; 
p(2) = 10*cos(pi*x(1)/2); 
p(3) = 8*sin(5*pi*x(1)); 
p(4) = (exp(-(0.5*(x(2)-0.5)^2)))/sqrt(5);

f = -14.9*100/(p(1)+p(2)+p(3)-p(4));

end

The next code calls the solver ten times. The area of interest is [-30 30;-10 10].

lb = [-30 -10];
ub = [30 10];

options = optimoptions(@ga, 'PopulationSize',500,'CreationFcn','gacreationlinearfeasible','CrossoverFraction',0.5,'MutationFcn','mutationadaptfeasible','FunctionTolerance',0.001);

for i=1 :10 [x,fval,exitflag,output,population,scores] = ga(@coveragefn,2,[],[],[],[],lb,ub,[],options); disp(x); end

An I'm getting the following results.

  x1         x2       fval
5.9013    0.5000    95.0004
5.9013    0.4998    95.0004
5.9014    0.5001    95.0004
5.9013    0.5000    95.0004
5.9013    0.5000    95.0004
5.9013    0.5000    95.0004
5.9013    0.5001    95.0004
5.9013    0.5000    95.0004
5.9013    0.5000    95.0004
5.9013    0.4999    95.0004

For a genetic algorithm I have done something wrong. For each call I'm getting the same best point.

UPDATE This is the surface of the function. Solution of ga is correct. Verified also with fmincon with a proper initial point. But still shouldn't I have some more difference in the solution based on the nature of the algorithm.

enter image description here

  • Is there any other way to validate that at the domain of interest there are other solutions? Could it be that this solution is the “globally” best at the domain of interest and the algorithm does indeed “its job”? – ZaellixA Jan 06 '24 at 11:56
  • 1
    plot the function and see if the identified peak is the peak? – Greg Locock Jan 06 '24 at 19:16
  • Thanks for the plot. That's a very 'sharp' maximum to find, I imagine it is quite easy for an algorithm to ascend that gradient. – Greg Locock Jan 07 '24 at 00:28
  • If i drop your values for x1 and x2 into the function I get -95, why do you get 95? – Greg Locock Jan 07 '24 at 06:20
  • @Greg Locock The given function is without a minus. I added a minus so the algorithm is not searching for f but for -f. I followed the same logic as in fmincon, in order to find the maximum you look at -f. May it is not the case for the genetic algorithm. – Pavlos Papanikolaou Jan 07 '24 at 08:15

1 Answers1

0

It seems ga() function of Matlab iterates the genetic algorithm generations automatically, so your 10 iterations simply re-start searching the optimum point.

You may try to debug the progress of the GA. Here's some excerpt from Matlab's docs: https://www.mathworks.com/help/gads/ga.html#d126e50318

intcon = 1;
rng default % For reproducibility
fun = @ps_example;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
options = optimoptions('ga','PlotFcn', @gaplotbestf); %plot best
[x,fval,exitflag,output] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
Gürkan Çetin
  • 906
  • 5
  • 20