Optimization speed using fmincon by function expression (bsxfun, reshape)
12 views (last 30 days)
Show older comments
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 Comments
John D'Errico
on 19 Feb 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.
Answers (2)
Walter Roberson
on 19 Feb 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.
See Also
Categories
Find more on Problem-Based Optimization Setup in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!