Optimization terminated: maximum number of generations exceeded.
26 次查看(过去 30 天)
显示 更早的评论
Why I got the maximum number exceeding? The solution is fluctuated as it shows in first run and second run.
% GA without constraints
% Objective function
f1 = @(x) 11*x(1)+22*x(1).^2.*x(2)+x(2).^2+31.*x(1).^2;
f2 = @(x,y) 11*x+22*x.^2.*y + y.^2 +31*x.^2
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [];
% Alternatively:
% lb = []
ub = [];
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, [-1500 1500 -1000 1000]);
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
% GA without constraints
% Objective function
f1 = @(x) 11*x(1)+22*x(1).^2.*x(2)+x(2).^2+31.*x(1).^2;
f2 = @(x,y) 11*x+22*x.^2.*y + y.^2 +31*x.^2
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [];
% Alternatively:
% lb = []
ub = [];
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, [-1500 1500 -1000 1000]);
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
1 个评论
采纳的回答
Alan Weiss
2023-4-10
ga exceeded 200 generations. If you want more, increase the MaxGenerations option.
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf,...
'MaxGenerations',1e3);
Alan Weiss
MATLAB mathematical toolbox documentation
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Direct Search 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!