0
$\begingroup$

Is it possible to time limit NMaximize? It is taking so long to evaluate my minimum, that I would like to see if it is doing what I expect it to do. So I was thinking of time limiting it in order to have at least a result.

CLOSED

I should use TimeConstrained[] as JM suggested in this case.

| improve this question | | | | |
$\endgroup$
  • 1
    $\begingroup$ Have you seen TimeConstrained[]? $\endgroup$ – J. M.'s technical difficulties May 8 '16 at 0:40
  • $\begingroup$ No I haven't will do now! $\endgroup$ – Mirko Aveta May 8 '16 at 8:44
  • $\begingroup$ OK. J.M. Answered my question. Thank you once again sir!! $\endgroup$ – Mirko Aveta May 8 '16 at 8:46
  • 1
    $\begingroup$ Why not answer your own question (with a usage example) instead of putting it in as an edit? $\endgroup$ – J. M.'s technical difficulties May 8 '16 at 9:05
3
$\begingroup$

I'm not sure if you can specify a time limit, but you can use the StepMonitor option to watch the maximization as it progresses. Here is an example:

f[x_] = -x^4 - 3 x^2 + x;
NMaximize[f[x], x, StepMonitor :> (Print["step x = ", x, ", f[x]=", f[x]]; Pause[.1])]

I'm only using Pause[] in the StepMonitor to slow down the process, so we can watch it happen for this simple function.

If the goal is to "time out" when the value is good enough, you may be better off using AccuracyGoal and/or PrecisionGoal. You could also lower MaxIterations below the default value of 100, but I would try watching things first with StepMonitor.

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Thank you for your comment. I have tried all your options yet I still don't received even one evaluation. I used even accuracyGoals and PrecisionGoals set to 1, which I guess is the minimum. I really don't understand why. Unfortunately I can't post my function because it needs a package to run. $\endgroup$ – Mirko Aveta May 8 '16 at 8:44
1
$\begingroup$

I should use TimeConstrained[] as JM suggested in this case.

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ It is an answer! It was exactly what I was using for. As soon as I can I'll tick it and edit my question. $\endgroup$ – Mirko Aveta May 8 '16 at 9:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.