Help with fmincon optimization

6 ビュー (過去 30 日間)
Shahab
Shahab 2012 年 8 月 30 日
I'm using these lines of code as part of an m-file:
**************************************************************************
options = optimset('Algorithm','sqp','MaxIter',3,'MaxFunEvals',3,'Display','iter');
delta = fmincon(@RI,delta0,A,b,[],[],-35*pi/180,35*pi/180,[],options);
**************************************************************************
Although I've set 'MaxIter' and 'MaxFunEvals' to 3, I get much more iterations of the function 'RI'; actually seems it's never going to end (I deliberately set max iterations such low to see when it stops). Furthermore, it doesn't display results at each iteration.
Can someone please tell me why?
  2 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 8 月 30 日
How are you measuring this? The more info (i.e. reproduction steps) you can give us the better.
Shahab
Shahab 2012 年 8 月 30 日
編集済み: Shahab 2012 年 9 月 1 日
I don't think it'll be helpful, but in case you need the full code, here it is:
function J = RI(del)
global delta
delta = del;
[~,~,yOut] = sim('OpenLoopWC');
J = 1000/max(yOut);
timestep = .002;
t = 0:timestep:5;
n = size(t); n = n(2);
delta0 = 25*pi/180*sin(2*pi/5*t); % initial guess
global delta
delta = delta0;
A = zeros(n-1,n);
for m = 1:n-1
A(m,m) = 1;
A(m,m+1) = -1;
end
b = 45*pi/180*timestep*ones(n-1,1);
options = optimset('Algorithm','sqp','MaxIter',3,'MaxFunEvals',3,'Display','iter');
delta = fmincon(@RI,delta0,A,b,[],[],-35*pi/180,35*pi/180,[],options);

サインインしてコメントする。

回答 (1 件)

Paul Kerr-Delworth
Paul Kerr-Delworth 2012 年 8 月 31 日
Hi,
With the options you have passed, fmincon should stop once your objective function, RI, has been evaluated more than 3 times.
Given that you see no iterative display in the command window, I wonder whether your simulink model is taking a long time to complete the simulation. To check this out, you could try running
[~,~,yOut] = sim('OpenLoopWC')
at the MATLAB command prompt and see how long it takes to return.
Hope this helps.
Best regards,
Paul
  2 件のコメント
Shahab
Shahab 2012 年 9 月 1 日
Hi dear Paul,
The simulation takes around 10 seconds to complete, but the function is evaluated (simulation is run) much more than 3 times, it runs until I have to interrupt it by ctrl+break.
Star Strider
Star Strider 2012 年 9 月 1 日
One way you might be able to track the number of times the function calls RI and how long it's taking is to insert persistent variables. This is kludgy, but sometimes kludgy is the only way to solve problems like this.
I suggest for a start:
function J = RI(del)
persistent RIcount
tic
global delta
delta = del;
[~,~,yOut] = sim('OpenLoopWC');
J = 1000/max(yOut);
RIcount = RIcount+1;
RItimer = toc;
sprintf('\n\tRI iteration %d = %f sec\n', RIcount, RItimer)
You can remove (or comment out) the persistent statement, RIcount, RItimer and the rest when you no longer need them, but in the interim they may give you some information about RI.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by