You have 2 options I can think of:
- opt for constrained optimization
- modify your objective function to diverge whenever your numerical simulation does not converge. Basically this means returning a large value, large compared to a 'normal' value, which depends on your problem at hand.
minimize
will then try to optimize going in another direction
I am however a bit surprised that minimize
does not understand inf
as a large value, and does not try to look for a solution in another direction. Could it be that it returns with 0 iterations only when your objective function returns nan
? You could try debugging the issue by printing the value just before the return
statement in your objective function.