why I got orange warning about contour plot?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Cris LaPierre
2023년 4월 7일
"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
Cris LaPierre
2023년 4월 8일
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:
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!