Setting lower and upper bounds in 'ga' algorithm.

20 次查看(过去 30 天)
I am trying to find the global minimum of the Ackley function using a genetic algorithm 'ga'. My code works fine until i try to add lower and upper bounds then I get errors. Please help me add the bounds to my code.
function y = myFitness(x)
y = -20.*exp(-0.2.*sqrt(0.5.*(x(1).^2+ x(2).^2))) - exp(0.5.*(cos(2.*pi.*x(1)))+(cos(2.*pi.*x(2))))+exp(1)+20;
end
clc
FitFcn = @myFitness;
nvars = 2;
lb = [-5 5];
ub = [5 5];
[x, fval] = ga(FitFcn, nvars)

采纳的回答

Ameer Hamza
Ameer Hamza 2020-10-10
See the lb and ub arguments: https://www.mathworks.com/help/gads/ga.html. For example
FitFcn = @myFitness;
nvars = 2;
lb = [-5 5];
ub = [5 5];
[x, fval] = ga(FitFcn, nvars, [], [], [], [], lb, ub)
  3 个评论
Learner123
Learner123 2021-4-27
Hi,
I have a similar issue. When I set lb and ub
I am getting an error.
Error using ga
Too many input arguments.
Any help is greatly appreciated. Thanks in advance

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by