Error:Too many input arguments in GA solver

Dear programmers
I need to minimize a single-objective function in ga having two(2) input variables. But the solver is showing an error:Too many input arguments. Please help.
function y = simple_fitness(x)
%SIMPLE_FITNESS fitness function for GA
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
rng default % For reproducibility
FitnessFunction = @simple_fitness;
numberOfVariables = 2;
lb = [-3,-3];
ub = [3,3];
[x,fval] = ga(FitnessFunction,numberOfVariables,[],[],[],[],lb,ub);

 Respuesta aceptada

Mario Malic
Mario Malic el 15 de Ag. de 2020
Editada: Mario Malic el 15 de Ag. de 2020
function y = simple_fitness(x)
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
end
rng default % For reproducibility
numberOfVariables = 2;
lb = [-3,-3];
ub = [3,3];
[x,fval] = ga(@(x)simple_fitness(x),numberOfVariables,[],[],[],[],lb,ub);
Now should be working.

12 comentarios

Rajdeep Ghosh
Rajdeep Ghosh el 15 de Ag. de 2020
Dear Mario
Thank you for your reply. I have tried what you have suggested but it is showing same error. But the same error doesnot arise when I run the same code in multi-objective GA solver.
Mario Malic
Mario Malic el 15 de Ag. de 2020
Edited the main answer
Rajdeep Ghosh
Rajdeep Ghosh el 15 de Ag. de 2020
Dear Mario
Still not working.
Mario Malic
Mario Malic el 15 de Ag. de 2020
What do you have in simple_fitness.m
Only this much piece of script. But I am astonished that the algorithm is running now when I have copied the files to Desktop and running from there rather than a different folder that I was running before. Thanks for the help Mario. But may I know what might be causing this problem?
function y = simple_fitness(x)
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
end
Sara Boznik
Sara Boznik el 15 de Ag. de 2020
With the same code I get this error: Optimization terminated: maximum number of generations exceeded.
Mario Malic
Mario Malic el 15 de Ag. de 2020
I can't point out at something. Maybe your workspace contained variables from runs before, or interference with files/functions having the same name.
Rajdeep Ghosh
Rajdeep Ghosh el 15 de Ag. de 2020
Dear Sara
What is the maximum number of generations you have provided as a stopping criteria ? If the algorithm reach your defined stopping criteria, it will terminate. Like in my case it has terminated as "Optimization terminated: average change in the fitness value less than options.FunctionTolerance." as the algorithm has already reached provided Function Tolerance.
Rajdeep Ghosh
Rajdeep Ghosh el 15 de Ag. de 2020
Yes Mario...It might be a case though it is not rightly detectable since the code was okay. Thanks again.
Sara Boznik
Sara Boznik el 15 de Ag. de 2020
Sara Boznik
Sara Boznik el 15 de Ag. de 2020
This is what is saved in x and in fval is saved 1 constant ... 1.2344
Mario Malic
Mario Malic el 15 de Ag. de 2020
Editada: Mario Malic el 15 de Ag. de 2020
[x,fval,exitflag,output,population,scores] = ga(___)
This is a code if one should further investigate about the solution, and maybe try to check the available ga options if solution is not the optimal one.
Also, since this function is quite straight forward, one could just generate meshgrid within the bounds, evaluate objective function and then find a minimum of it.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2017a

Etiquetas

Preguntada:

el 15 de Ag. de 2020

Editada:

el 15 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by