why I got orange warning about contour plot?

1 回表示 (過去 30 日間)
Patis Thepsorn
Patis Thepsorn 2023 年 4 月 7 日
コメント済み: Patis Thepsorn 2023 年 4 月 8 日
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);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
The optimal decision is: x = 1.231051 y = -1.511658 This solution has value 3.000015
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-10 10 -50 50],'LevelStep',200);
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
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);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
The optimal decision is: x = 2.313426 y = -5.490086 This solution has value 3.019085
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-10 10 -50 50],'LevelStep',200);
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off

採用された回答

Cris LaPierre
Cris LaPierre 2023 年 4 月 7 日
See the description of the function handle input on the fcontour documentation page
"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);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
The optimal decision is: x = 2.205098 y = -4.881599 This solution has value 3.000366
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
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:
Patis Thepsorn
Patis Thepsorn 2023 年 4 月 8 日
Thank you so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by