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

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
Dear Mario
Still not working.
Mario Malic
Mario Malic 2020년 8월 15일
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 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.
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.
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