How to call a function with multi variable in real coded GA in function handle and how to define upper and lower bound of that variable in matlab?
2 views (last 30 days)
Show older comments
Danishtah Quamar
on 19 Jan 2022
Commented: Star Strider
on 25 Jan 2022
I have defined a function with many variable
Example: function T= Torque(a,b,c)
where a,b,c is variable having upper and lower bound 1<a<3; 2<b<7; 6<c<7
T=a^2 + 3a + b*c +b^2 + a*b*c + c^2
In GA code I have used function handle for calling torque function and also defined the bounds of variables as
lb=[1 2 6] % lower bound of variable
ub=[3 7 7] % upper bound of variable
prob=@Torque
while calculating the fitness function error shows "Not enough input arguments"
Please suggest me how to resolve this issue.
0 Comments
Accepted Answer
Star Strider
on 19 Jan 2022
If ‘Torque’ is the fitness function, it should be coded as:
function T = Torque(p)
% % % MAPPING: a = p(1), b = p(2), c = p(3)
T = p(1).^2 + 3*p(1) + p(2).*p(3) +p(2).^2 + p(1).*p(2).*p(3) + p(3).^2;
end
That should work.
.
2 Comments
More Answers (1)
Steven Lord
on 19 Jan 2022
If you've defined Torque to accept 3 inputs as per the code at the end of this answer, you can still use that function in your ga call. You just need an adapter, like a plumbing fitting to connect different sized pipes. This is one of the approaches described in the documentation page "Passing Extra Parameters" linked to in the Tips section of the ga documentation page.
callFunctionNoAdapter = Torque(1, 2, 3)
adapter = @(x) Torque(x(1), x(2), x(3));
callFunctionAdapter = adapter([1 2 3])
The function handle adapter accepts 1 input as ga requires. It calls Torque with three inputs as Torque requires. It passes the output from Torque back to ga as ga requires. Both ga and Torque are happy.
function T= Torque(a,b,c)
T = a + 2*b + 3*c; % sample
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!