Defining objective function using nested functions (Genetic Algorithm Solver)

Hello everyone,
I am trying to do a maximization problem using optimization toolbox genetic algorithm solver. My objective function has parameters so I need to write a nested function(Since i have extra parameters besides from my variables) But i am having a trouble writing it. Here is what my objective function looks like and i wrote so far;
function z = objectivefunc(x,r,aii)
z= sum(x.*r.*aii, 'all');
end
(I reshaped my matrixes to be a row vector.)
X is my decsision variable and its dimensions are 1*1296 and r and aii are my parameters with dimensions 1*1296. I have looked through the documentation matlab provides and found this one;
I am very new to MATLAB so i really do not know how to write it correctly. So, any ideas or suggestions on how to implement nested functions to my problem?

3 个评论

a must have dimension of I, r and x must have dimension of IxJ. So if I has 12 elements and J has 6 elements, a must have dimension 1x12, r and x must have dimension 1x72.
So 1x1296 for all three cannot be correct.
originally, matrixes had 36x36 dimensions but for some reason i reshaped them(x and r) to be 1x1296 and originally a is 1x36 matrix. When i used 1x1296 for x and r and 1x36 for a i get an error saying index in this position exceeds array bounds etc. So i figured that the reason is a does not have same dimensions with the other two so it may be the reason. So i repeated a (lets say a is [1 2 3 4....] i repeated this matrix for 36 times ) to be 1x1296. Now it says not enough input.
So, what you are telling me might cause a error again since i am using optimization toolbox and i understand that this toolbox is not very flexible with the dimensions.
As a solution, i was recommended to use nested functions etc. But failed to write the nested function.
This problem has nothing to do with nested functions. You pass r and a to objectivefunc - so the two matrices can be accessed there. Maybe in a former version of your code, you did not include r and a in the list of parameters passed to objectivefunc and so someone suggested that you should use nested functions (because r and a are then also visible in objectivefunc although you don't pass them).
If you define the array of initial values for the unknowns x0 for "ga" as a 36x36 matrix, you will find them in objectivefunc also as a 36x36 matrix and you can use Matt's answer to calculate z if r is 36x36 and a is 36x1.

请先登录,再进行评论。

 采纳的回答

I suspect that the r and x are both in fact 36x36 matrices while a is a 36x1 vector. If so, then, the correct implementation is,
function z = objectivefunc(x,r,a)
z= sum(x.*r.*a, 'all');
end

10 个评论

First of all, thank you for your answer. I actually reshaped all 3 of them to be 1x1296 matrixes. In the workspace they are 1x1296 matrixes and when i hoover my mouse onto them i can still see that they are 1x1296 matrixes.
But still since r and a are parameters i need to pass them to the function using some functions but could not figured out the right form to write a nested function.
The length of all three arrays cannot be the same unless the set J contains only one element.
I = I*J -> J = 1.
If they have equal length, you did something wrong when initializing them.
First of all, thank you for your answer. I actually reshaped all 3 of them to be 1x1296 matrixes.
Is that relevant? Restore them to their original 36x36 shape (36x1 in the case of a) and my proposal will work.
I restored them. x now 36x36 , r 36x36 and a 36x1.
Error is now saying 'Arrays have incompatible sizes for this operation'
Nope, the following simple example, which you can run yourself, proves that this will not happen if the variables have the sizes that you say.
r=rand(36); x=rand(36); a=rand(36,1);
z=sum(x.*r.*a,'all');
whos r x a z
Name Size Bytes Class Attributes a 36x1 288 double r 36x36 10368 double x 36x36 10368 double z 1x1 8 double
Here i will attach the file, if can have a look i will be very happy. Because I do not see any difference between what you wrote and i write for objective function. But i am sure there is something i did wrong.
Ah, yes. It's because it's ga().
function z = objectivefunc(xij,rij,ai)
xij=reshape(xij,size(rij));
z= sum(xij.*rij.*ai, 'all');
end
Yes thank you, it worked. But still gives not enough input argument. For this line;
xij=reshape(xij,size(rij));
Not for me. I ran your script with my proposed change and it worked fine.

请先登录,再进行评论。

更多回答(0 个)

类别

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by