Optimization terminated: maximum number of generations exceeded.

37 views (last 30 days)
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
f2 = function_handle with value:
@(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);
Optimization terminated: maximum number of generations exceeded.
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 = 1206.044779 y = -884.937818 This solution has value -28272094798.760334
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
f2 = function_handle with value:
@(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);
Optimization terminated: maximum number of generations exceeded.
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 = -1128.320461 y = -849.805146 This solution has value -23761468456.310452
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

Accepted Answer

Alan Weiss
Alan Weiss on 10 Apr 2023
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!