Optimization speed using fmincon by function expression (bsxfun, reshape)

12 visualizaciones (últimos 30 días)
James Choi
James Choi el 19 de Feb. de 2016
Respondida: James Choi el 22 de Feb. de 2016
Hello. I'm newbie for Matlab and trying to run some optimization problem using fmincon.
My objective is to minimize function value "out" which is difference between maximum value and minimum value.
There are three types of function I wrote and their results are quite different.
.
version 1 is simpler than other two functions but its minimum value is far from zero. Simply it is not converged.
%%version 1
[k,l,i,j] = size(Y);
z = bsxfun(@times,Y,x);
mysum = squeeze(sum(reshape(z,k*l,1,i,j)));
out = max(mysum(:)) - min(mysum(:));
version 2 is too slow because of multiple for-loop but its fmincon result is better than version 1.
%%version 2
[kmax,lmax,imax,jmax] = size(Y);
mysum = zeros(imax,jmax);
for k=1:kmax
for l=1:lmax
for i=1:imax
for j=1:jmax
mysum(i,j) = mysum(i,j) + x(k,l) * Y(k,l,i,j);
end
end
end
end
out = max(mysum(:)) - min(mysum(:));
version 3 is the fastest and its minimum value is close to zero. It means it reached converged solution.
%%version 3
[kmax,lmax,imax,jmax] = size(Y);
mysum = zeros(imax,jmax);
for i=1:imax
for j=1:jmax
X2 = Y(:,:,i,j);
X3 = x.*Y2;
mysum = mysum+ Y3;
end
end
out = max(max(mysum)) - min(min(mysum));
Each code run from exactly same initial guess.
Y = randi(100,k,l,i,j)
x0 = randi(10,k,l)
But I found out that the code's performance and fmincon solution is different.
.
Please let me know how these three types of code actually work while running fmincon optimization solver.
Thanks in advance for your kind explanation.
James.
  2 comentarios
John D'Errico
John D'Errico el 19 de Feb. de 2016
There simply is not sufficient information here.
If they are computing the same thing as an objective, then fmincon will not see a difference. So you need to explain more clearly what you are doing differently. Have you verified that these codes are indeed the same in their results? If they are not, then this is all just a waste of time to ask.
Are you using different starting values. How are you calling fmincon.
For example, I see that in the second case, you allow the optimizer to go 5 times as long as the others. Essentially, you are comparing apples and oranges here.
James Choi
James Choi el 21 de Feb. de 2016
I modified and added to my posting. It start from exactly same initiaul guess. And script for calling fmincon is not changed during test.
So only different thing is like as above-mentioned simple calculation loop.
Thank you.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 19 de Feb. de 2016
Look more closely at your third code. It has
for j=1:jmax
X2 = Y(:,:,i,j);
X3 = x.*Y2;
mysum = mysum+ Y3;
end
You compute X2, and then in the next line you use Y2 rather than X2. You compute X3 and then in the next row you use Y3 not X3. Neither Y2 nor Y3 are defined in the code, so we do not know what you are computing.
  1 comentario
James Choi
James Choi el 20 de Feb. de 2016
Actually, I didn't use "X" and I made a mistake when posting this website.
My last code, there are just Y, Y2 and Y3.
Thanks.

Iniciar sesión para comentar.


James Choi
James Choi el 22 de Feb. de 2016
After I checked my question, I realized I make a mistake for version 3 because its has wrong formulation.
So, this problem is about version 1 and version 2 only.
It is not surprising that version 1 is much faster. But I don't understand why their results are different so.

Categorías

Más información sobre Problem-Based Optimization Setup en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by