fmincon pause after 2 iterations when using gradients
Show older comments
Without gradients my code runs 'fine'. Meaning that it is slow to converge, but at least it is continuously iterating towards a better solution.
Adding gradients results in a freeze/pauze, usually after the second iteration. After a while it picks up again and it's better/faster than without gradients. Consecutive runs of the same script don't have this freeze/pause, which leads me to believe it's some sort of caching related issue.
'checkGradients' tells me that the gradients are good. I get my gradients from a different script by taking the symbolic jacobian of ceq, and using matlabFunction to generate a sparse optimized matlab function file.
If anyone could explain why this happens and maybe how I could resolve the slowdown I would be very grateful!
(the slowdown is not because of the 'checkGradients' as it also happens without the check. I only generate the function once, not every time I try to optimise.)
CheckGradients Information
Objective function derivatives:
Maximum relative difference between supplied
and finite-difference derivatives = 2.0994e-07.
Nonlinear equality constraint derivatives:
Maximum relative difference between supplied
and finite-difference derivatives = 4.20079e-08.
CheckGradients successfully passed.
____________________________________________________________
First-order Norm of
Iter F-count f(x) Feasibility optimality step
0 1 1.960200e+01 9.900e-01 5.822e-01
1 2 2.011323e+01 9.888e-01 1.048e+00 7.881e-01
2 3 2.174207e+01 9.884e-01 1.435e+00 4.568e-01
3 Comments
Matt J
on 8 Mar 2022
We'll need to see code, preferably in a form that we can run ourselves.
Hendrik van Gils
on 8 Mar 2022
Matt J
on 8 Mar 2022
Have you tried running with "Pause on Nan or Inf"

Accepted Answer
More Answers (1)
Matt J
on 8 Mar 2022
0 votes
I get my gradients from a different script by taking the symbolic jacobian of ceq, and using matlabFunction to generate a sparse optimized matlab function file.
If you're repeating this process every time your constraint function is called, it's probably a bad idea. You should generate your matlabFunction once, before the optimization is launched.
1 Comment
Hendrik van Gils
on 8 Mar 2022
Categories
Find more on Choose a Solver 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!