How can i sum my objective function with each iteration

Hello everyone,
I am trying to do an maximiztion with optimization toolbox using genetic algorithm solver. What i am trying to do is i have a function with 2 parameters and 1 variable. I have a for loop and in the loop i wanna do this.
for i = 1:1
for j = 1:1296
z = z + (-1)*(xij(36*i+j-36)*aii(i,j)*r(i,j));
end
end
I wanna multpily the x,r, and a for each iteration and sum every iteration.
like this;
for (1,1)
x(1,1)*r(1,1)*a(1,1) = 188
for (1,2)
x(1,2)*r(1,2)*a(1,2)=74 +188= 262 (you get the idea)
Any suggestions on how to do this?
Because when i write it this was i get an error.
z = z + (-1)*(xij(36*i+j-36)*aii(i,j)*r(i,j));
Thank you in advance,
Beyza.

 Akzeptierte Antwort

Matt J
Matt J am 22 Mär. 2022
sum(x.*r.*a,'all')

16 Kommentare

Should i write it outside the for loop or inside it?
You should not have any loops.
so here is the full version;
function [xij,fval] = objectivefunc(r,aii,xij)
[xij,fval] = fminunc(@nestedfun,xij);
function z = nestedfun(xij)
for i = 1:1
for j = 1:1296
z = (-1)*(xij(36*i+j-36)*aii(i,j)*r(i,j));
end
end
end
end
What i need to do is;
function [xij,fval] = objectivefunc(r,aii,xij)
[xij,fval] = fminunc(@nestedfun,xij);
function z = nestedfun(xij)
sum(xij.*r.*aii, 'all');
end
end
Is it the correct use?
Torsten
Torsten am 22 Mär. 2022
Bearbeitet: Torsten am 22 Mär. 2022
Do you only want to test "fminunc" ?
The result is obvious:
x(i,j) = +Inf if a(i,j)*r(i,j) >= 0,
x(i,j) = -Inf if a(i,j)*r(i,j) < 0.
Your code bears no resemblance to your description of the desired operation. This is what you said you wanted:
for (1,1)
x(1,1)*r(1,1)*a(1,1) = 188
for (1,2)
x(1,2)*r(1,2)*a(1,2)=74 +188= 262 (you get the idea)
I am saying that sum(x.*r.*a, 'all') implements that part of your post
sorry if i was not able to clearly define my problem.
I have these matrixes of x,r,a with 1x1296 dimensions and i wanna multpily the same cells of these matrixs and as a result i want the summation of all 1296 multiplication.
No i do not want to test it. Genetic algorithm solver does not allows me to use parameters therefore i ttried to write a nested function in order to pass parameters to my objective function. And since this is a maximization problem i thought i should multiply this with -1 and then it would be maximization instead of minimization(Cause the solver solves it to minimize). And I wanna found the maximum value, and with this format it only gives me the last ones value since there is not summation operation going on.
In my case the last value is 334 and as a solution it always converges to 334. But instead it should converge to 5k or 6k.
Torsten
Torsten am 22 Mär. 2022
Bearbeitet: Torsten am 22 Mär. 2022
Perhaps x is a column vector. Then you'll have to use
z = sum((x.').*r.*a)
But as I said: The optimal x is given by
x(i,j) = +Inf if a(i,j)*r(i,j) >= 0,
x(i,j) = -Inf if a(i,j)*r(i,j) < 0.
That's why I asked if this is only a test problem for "fminunc".
Three of the matrixes are row vector with dimensions 1x1296.
I could not understand what this supposed to mean to me
x(i,j) = +Inf if a(i,j)*r(i,j) >= 0,
x(i,j) = -Inf if a(i,j)*r(i,j) < 0.
x should be binary variable but could not make it binary right now it takes values between 1-0. r and a are parameters. r is also binary. a is demand.
I used fminunc because i looked it from this website:
I have these matrixes of x,r,a with 1x1296 dimensions and i wanna multpily the same cells of these matrixs and as a result i want the summation of all 1296 multiplication.
That is what I gave you originally.
For a binary problem, fminunc is not suited.
Use intlinprog or bintprog or - if your objective function or other constraints are nonlinear - ga instead.
I just wanna be more clear so the phyton equivalent of what i want to do is like this.
z += x*r*a
What is the matlab way to this?
No, the Python equivalent of what you describe is,
z=sum(x*r*a)
My original answer gives the Matlab equivalent of this.
@Torsten yes i am using ga solver. So ı wrote the objective function entirely wrong? Any suggestions on how to correct it? Like i told earlier i have parameters that i should pass to the objective function and using nested function was recommended to me.
@Matt J okey thank you!
You're welcome. If the problem is now resolved, though, please do Accept-click the answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2021b

Gefragt:

am 22 Mär. 2022

Kommentiert:

am 22 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by