Hello,
I do not understand why fmincon does not respect the maximum number of iteration I specify and does whatever it
l;ikes to do. My cod is very long to show you but I show the main part which I believe is enough:
Max_iter=solver{2};
par0=lb+(ub-lb).*rand(1,dim);
options=optimoptions('fmincon','MaxFunEvals',realmax,'MaxIterations',Max_iter,'OutputFcn',@(x,A,state)myoutput_fmincon_Spline(x,A,state));
problem = createOptimProblem('fmincon','objective',cost,'x0',par0,'lb',lb,'ub',ub,'options',options);
ms=MultiStart('UseParallel',UseParallel);
myCluster=parcluster('local');NumWorkers=myCluster.NumWorkers;
[EstimatedNoise_par, f_best]=run(ms,problem,NumWorkers);
I try Max_tier = 1 and I expect the code to stop fast but fminbcon ignore it.
Any idea?
Thanks in advance,
Babak

8 commentaires

Walter Roberson
Walter Roberson le 18 Nov 2023
Note that the initial iteration requires evaluating the objective function (dim+1) times.
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 18 Nov 2023
Hi Walter,
Thanks.
My dim is 8. So, it means that even if I set MaxIter=1 fmincon still considers 9 iterations. But, still I expect my code to end rather fast. I tested my code with MaxIter=1. And, I set Useparalel = false. I noticed that the solutions alternate between Iteration =0 and Iteration =1 quite a lot. But, I expect an increasing pattern. Below you will see part of the solutions I get in the command window (I believe you do not need to know the details of my code):
Iteration : 0
Estimated parameters of diffusion function : 0.340694 24.7095 11.5061 10.303 12.1326 25.8397 20.2418 28.2327
Objective function value (negative sum of log-likelihoods) : 22775715.025
Iteration : 1
Estimated parameters of diffusion function : 1.21214 24.3313 11.6899 10.4399 12.2596 25.8293 20.0435 28.0639
Objective function value (negative sum of log-likelihoods) : 21561496.9938
Iteration : 1
Estimated parameters of diffusion function : 1.21214 24.3313 11.6899 10.4399 12.2596 25.8293 20.0435 28.0639
Objective function value (negative sum of log-likelihoods) : 21561496.9938
Iteration : 0
Estimated parameters of diffusion function : 13.4131 29.2669 12.1729 20.2488 5.63615 13.5646 27.3545 28.6846
Objective function value (negative sum of log-likelihoods) : 25489598.6602
Iteration : 0
Estimated parameters of diffusion function : 13.4131 29.2669 12.1729 20.2488 5.63615 13.5646 27.3545 28.6846
Objective function value (negative sum of log-likelihoods) : 25489598.6602
Iteration : 1
Estimated parameters of diffusion function : 14.4645 14.7116 13.6338 18.3257 9.41868 14.7275 21.5547 22.3799
Objective function value (negative sum of log-likelihoods) : 19816903.2732
Iteration : 1
Estimated parameters of diffusion function : 14.4645 14.7116 13.6338 18.3257 9.41868 14.7275 21.5547 22.3799
Objective function value (negative sum of log-likelihoods) : 19816903.2732
Do you know why?
Walter Roberson
Walter Roberson le 18 Nov 2023
My dim is 8. So, it means that even if I set MaxIter=1 fmincon still considers 9 iterations.
Not exactly. For iteration 0, fmincon would evaluate the function 8+1 = 9 times, in order to establish initial values to estimate the jacobian.
The iteration counts you are observing, are for each time fmincon() starts. You are using MultiStart, which runs several independent fmincon() from different starting points.
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 18 Nov 2023
Déplacé(e) : Torsten le 18 Nov 2023
Thanks Walter,
I wish I can ask a short question. Aparently, it is really difficult to stop fmincon (I really tried har). I used the global optimization aspect of fmincon as bellow:
par0=lb+(ub-lb).*rand(1,dim);
gs=GlobalSearch; options=optimoptions('fmincon','MaxIterations',40,'OutputFcn',@(x,A,state)myoutput_fmincon_Spline(x,A,state)); problem=createOptimProblem('fmincon','x0',par0,'objective',cost,'lb',lb,'ub',ub,'options',options);
[EstimatedNoise_par, f_best]=run(gs,problem);
As you see below I set the MaxIter to 40. However, I get a very nice answer at iteration 26:
Iteration : 25
Estimated parameters of diffusion function : 9.3973 12.3577 17.1114 20.2589 21.4486 19.3357 11.637 6.14941
Objective function value (negative sum of log-likelihoods) : 16701379.7463
Iteration : 26
Estimated parameters of diffusion function : 9.39729 12.3577 17.1114 20.2588 21.4486 19.3357 11.6371 6.14942
Objective function value (negative sum of log-likelihoods) : 16701379.7463
Iteration : 27
Estimated parameters of diffusion function : 9.39729 12.3577 17.1114 20.2588 21.4486 19.3357 11.6371 6.14942
Objective function value (negative sum of log-likelihoods) : 16701379.7463
Iteration : 28
Estimated parameters of diffusion function : 9.39729 12.3577 17.1114 20.2588 21.4486 19.3357 11.6371 6.14942
Objective function value (negative sum of log-likelihoods) : 16701379.7463
Is there a brute force way to just stop it? I also tried TolX=0.01, but again it does not stop. I do not know what to do with this.
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 18 Nov 2023
Déplacé(e) : Torsten le 18 Nov 2023
PS:) I forgot to add that, even if I set Maxiteration=10 it also does not stop. Now you see that from iteration 26 the solution is really good with 4-5 digits of accuracy, so whay TolX=0.01 fails?
Torsten
Torsten le 18 Nov 2023
Déplacé(e) : Torsten le 18 Nov 2023
After each completed parameter update, fmincon calls OutputFcn. So why don't you stop the solver on your own if you are satisfied with the solution ?
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 18 Nov 2023
Hi Torsten,
You know, I ma making a MATLAB package now. If it was for my personal use then I would not bother you. I need to publish this package. The package also has a tutorial. It is not really nice to write in the tutorial "stop it whenever you see that the solution does not improve". Furthermore, in my package a user can choose not to see the results in the command window. So, I AM SEEKING A WAY TO STOP THE CODE WITHOUT PRESSING THE Ctrl+C
Walter Roberson
Walter Roberson le 18 Nov 2023
outputfcn returns a value. That value can directly signal stop (that is, it is a fundamental part of the design, easily done, not something obscure)
So your outputfcn could evaluate the solution at hand and somehow decide that it is good enough.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 18 Nov 2023
Modifié(e) : Matt J le 18 Nov 2023
Now you see that from iteration 26 the solution is really good with 4-5 digits of accuracy, so whay TolX=0.01 fails?
Remember that MultiStart is an outer function deploying many instances of fmincon. Each instance of fmincon has its own inner iteration sequence. The values you've set using optimoptions (e.g.TolX, MaxIter) are used by each instance of fmincon to decide when its inner iteration sequence will stop. They do not control the outer level deployments of MultiStart.
If you want to define your own stopping criterion for the outer sequence of deployments, you must define a separate OuputFcn for that and pass it to MultiStart directly, e.g.,
ms=MultiStart('UseParallel',UseParallel,'OutputFcn', @stopMultiStart);
See here, for an example:

3 commentaires

Mohammad Shojaei Arani
Mohammad Shojaei Arani le 19 Nov 2023
Hi Matt,
I am not using MultiStart. I found global optimization with fmincon better:
par0=lb+(ub-lb).*rand(1,dim);
gs=GlobalSearch; options=optimoptions('fmincon','MaxIterations',40,'OutputFcn',@(x,A,state)myoutput_fmincon_Spline(x,A,state)); problem=createOptimProblem('fmincon','x0',par0,'objective',cost,'lb',lb,'ub',ub,'options',options);
[EstimatedNoise_par, f_best]=run(gs,problem);
But, it still does not respect any of my stopping criteria (here, MaxIteration).
Matt J
Matt J le 19 Nov 2023
The same remarks apply. You are passing in stopping criteria that govern the inner iterations of fmincon, but not the outer iterations of GlobalSearch.
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 19 Nov 2023
OK. Thanks Matt!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by