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?

4 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2022-1-19
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.
.

更多回答(1 个)

Steven Lord
Steven Lord 2022-1-19
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

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by