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);

 採用された回答

Mario Malic
Mario Malic 2020 年 8 月 15 日
編集済み: Mario Malic 2020 年 8 月 15 日

0 投票

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 件のコメント

Rajdeep Ghosh
Rajdeep Ghosh 2020 年 8 月 15 日
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 2020 年 8 月 15 日
Edited the main answer
Rajdeep Ghosh
Rajdeep Ghosh 2020 年 8 月 15 日
Dear Mario
Still not working.
Mario Malic
Mario Malic 2020 年 8 月 15 日
What do you have in simple_fitness.m
Rajdeep Ghosh
Rajdeep Ghosh 2020 年 8 月 15 日
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 2020 年 8 月 15 日
With the same code I get this error: Optimization terminated: maximum number of generations exceeded.
Mario Malic
Mario Malic 2020 年 8 月 15 日
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 2020 年 8 月 15 日
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 2020 年 8 月 15 日
Yes Mario...It might be a case though it is not rightly detectable since the code was okay. Thanks again.
Sara Boznik
Sara Boznik 2020 年 8 月 15 日
Sara Boznik
Sara Boznik 2020 年 8 月 15 日
This is what is saved in x and in fval is saved 1 constant ... 1.2344
Mario Malic
Mario Malic 2020 年 8 月 15 日
編集済み: Mario Malic 2020 年 8 月 15 日
[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.

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2017a

質問済み:

2020 年 8 月 15 日

編集済み:

2020 年 8 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by