why I got orange warning about contour plot?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Patis Thepsorn
el 7 de Abr. de 2023
Comentada: Patis Thepsorn
el 8 de Abr. de 2023
I run my code two times but the results are not the same. As you can see in x and y value after running the code. I go the different values but it is the same code. I don't understand what is happening and how should I do with this code to get the correct result. Moreover, when I run in the MATLAB software, the orange warning is coming up like below.

1st trial
% GA without constraints
% Objective function
f = @(x) (x(1).^4) + ((2.*x(1).^2).*x(2)) + x(2).^2 +3;
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [-Inf, -Inf];
% Alternatively:
% lb = []
ub = [Inf, Inf];
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf);
% Solve the problem
[x, fval] = ga(f, 2, A, B, Aeq, Beq, lb, ub, nonlcon, options);
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-10 10 -50 50],'LevelStep',200);
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
2nd trial
% GA without constraints
% Objective function
f = @(x) (x(1).^4) + ((2.*x(1).^2).*x(2)) + x(2).^2 +3;
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [-Inf, -Inf];
% Alternatively:
% lb = []
ub = [Inf, Inf];
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf);
% Solve the problem
[x, fval] = ga(f, 2, A, B, Aeq, Beq, lb, ub, nonlcon, options);
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-10 10 -50 50],'LevelStep',200);
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
0 comentarios
Respuesta aceptada
Cris LaPierre
el 7 de Abr. de 2023
"Specify a function of the form z = f(x,y). The function must accept two matrix input arguments and return a matrix output argument of the same size."
The simplest solution is to define 2 anonymous functions; one for ga and one for fcontour.
One other correction. You wrote your anonymous function to only use the first 2 values of the input rather than all values. I have adjusted it to treat the first column as x and the second column as y.
% GA without constraints
% Objective function
f1 = @(x) (x(:,1).^4) + ((2.*x(:,1).^2).*x(:,2)) + x(:,2).^2 +3;
f2 = @(x,y) x.^4 + 2*x.^2.*y + y.^2 +3;
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [-Inf, -Inf];
% Alternatively:
% lb = []
ub = [Inf, Inf];
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf);
% Solve the problem
[x, fval] = ga(f1, 2, A, B, Aeq, Beq, lb, ub, nonlcon, options);
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
figure
hold on
fcont = fcontour(f2, [-10 10 -50 50],'LevelStep',200);
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
3 comentarios
Cris LaPierre
el 8 de Abr. de 2023
The algorithm uses random sampling to find a minimum. Depending how it samples run to run can lead to different results depending on your function. One solution is to use multiple runs, another is to set the random number generator seed. See here:
Más respuestas (0)
Ver también
Categorías
Más información sobre Genetic Algorithm en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






