Genetic Algorithm OPTIMTOOL Error
3 views (last 30 days)
Show older comments
Hi All,
I am running GA problem via OPTIMTOOL. This is my defined parameters at OPTIMTOOL
Fitness Function:@myff
Number of variables :4
Lower Bound: [1 14 30 12]
Upper Bond : [2 22 60 16]
And @myff code is per below :
function TotalDelay=myff(C,g,x,c)
a=(1-(g/C))^2;
p=1-((g/C)*x);
d1i=(0.38*C*a)/p;
a2=173*(x^2);
ri1=sqrt( (x-1) + (x-1)^2 + ((16*x)/c) );
d2i=a2*ri1;
TotalDelay=(d1i+d2i);

end;
When start the solver the below error pops out : "Input argument "g" is undefined."
Can anyone enlighten me, where did i made a mistakes.
Thanking in advance. Dawood
0 Comments
Answers (2)
Steven Lord
on 17 Aug 2017
"The fitness function should accept a row vector of length nvars and return a scalar value."
Your function, as defined, requires a row vector of length nvars and other information. For that scenario, see this suggestion in the Tips section on that documentation page:
"To write a function with additional parameters to the independent variables that can be called by ga, see Passing Extra Parameters (Optimization Toolbox)."
You will need to use one of the techniques on the Passing Extra Parameters page to pass additional inputs to your fitness function. The easiest will probably be the anonymous function approach.
1 Comment
Alan Weiss
on 17 Aug 2017
It is possible that your variables C, g, x, and c are all scalars that you want to optimize. In that case, instead of following Steve's suggestion, you would write your objective (fitness) function like this:
function TotalDelay = myff(r)
C = r(1);
g = r(2);
x = r(3);
c = r(4);
a=(1-(g/C))^2; % etc., put the rest of your code here
P.S. For such a smooth objective function, I suggest that you NOT use ga, but instead use a more appropriate solver such as fminunc or fmincon.
Alan Weiss
MATLAB mathematical toolbox documentation
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!