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

조회 수: 12 (최근 30일)
James Choi
James Choi 2016년 2월 19일
답변: James Choi 2016년 2월 22일
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
John D'Errico
John D'Errico 2016년 2월 19일
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 2016년 2월 21일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2016년 2월 19일
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
James Choi
James Choi 2016년 2월 20일
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.

댓글을 달려면 로그인하십시오.


James Choi
James Choi 2016년 2월 22일
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.

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by