# Choosing optimal values from the genetic algorithm

4 views (last 30 days)
Vivek on 3 Feb 2023
Commented: Vivek on 3 Feb 2023
As GA are probabilistic and indetriministic everytime I run the code I am getting different optimas with the same value of objective function.
How to choose the proper and best optima when the function value remains same for different optimias.
Is there any way to choose the best optima.

Sam Chak on 3 Feb 2023
Edited: Sam Chak on 3 Feb 2023
You can try setting the rng to 'default' for the reproducibility of the result.
If the function has multiple extrema, I'd probably set the lower and upper bounds on the design variables, so that the solution is searched and found in the range of interest.
xx = linspace(-pi, pi, 51);
yy = linspace(-pi, pi, 51);
[X, Y] = meshgrid(xx, yy);
Z = (sin(X)).^2 + (cos(Y)).^2;
% contour(X, Y, Z)
surfc(X, Y, Z)
xlabel('x_1'), ylabel('x_2'), zlabel('f(x_1, x_2)')
rng default % For reproducibility
fun = @(x) (sin(x(1))).^2 + (cos(x(2))).^2;
lb = [-pi -pi]; % lower bound
ub = [pi pi]; % upper bound
x = ga(fun, 2, [], [], [], [], lb, ub)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x = 1×2
-0.0000 1.5708
Vivek on 3 Feb 2023
Below shown is my objective function and related m and b function files are external function files.
function P1=f(x0)
M=5;
%x0=[5,8];
% x0=[12,13];
M2=m(M,x0(1));
M3=m(M2,x0(2));
M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)));
beta2=b(M2,(x0(2)));
beta3=b(M3,((x0(1)+x0(2))));
s1=sin(beta1);
s2=sin(beta2);
s3=sin(beta3);
t1n=(13.824)*((M2*M3*M)^2)*((s1*s2*s3)^2);
t1d=(((0.4)*(M3^2)*(s3^2))+2)*(((0.4)*(M2^2)*(s2^2))+2)*(((0.4)*(M^2)*(s1^2))+2);
t1=(t1n/t1d)^(3.5);
t2n=13.824;
t2d=((2.8*(M3^2)*(s3^2))-0.4)*((2.8*(M2^2)*(s2^2))-0.4)*((2.8*(M^2)*(s1^2))-0.4);
t2=(t2n/t2d)^(2.5);
P1=(t1*t2);
P= P1*(-1);
% surf(
% fplot(x0,f)
end
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
%This is the separate script file
x0=[x1,x2];
% Lower bounds
lb=[1,5];
% Upper Bounds
ub=[40,38];
nonlcon=@area
%
rng default
x1 = linspace(1,100,55);
x2 = linspace(1,100,55);
[X, Y] = meshgrid(x1, x2);
Z = f(x0);
%contour(X, Y, Z)
surfc(X, Y, Z)
xlabel('x_1'), ylabel('x_2'), zlabel('f(x_1, x_2)')
% opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter');
% opts1 = optimoptions(opts,'MaxIterations',100); % Recommended
% [x,fval,exitflag,output]= fmincon(@f,x0,[],[],[],[],lb,ub,[],opts1);
options=optimoptions('ga','ConstraintTolerance',1e-8,'Display','iter');
[x,fval,exitflag,output,population,scores]=ga(@f,2,[],[],[],[],lb,ub,nonlcon,[],options)
After using the above code it is showing the error as shown below.
Ig it is taking single value of x0
Error using surfc
The surface Z must contain more than one row or column.
Error in z (line 18)
surfc(X, Y, Z)

### Categories

Find more on Genetic Algorithm in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!