I have to minimize a function where the evaluation of one parameter set takes very long (around 5sec) and discovered alongside, that NMinimize seems to call this function multiple times with the exact same values. As an example, consider the code

f[x_?NumericQ] := (Print[N[x, 10]]; x^2);
NMinimize[{f[x]}, x, Method -> "RandomSearch"]

The first couple of lines of the output are:


Am I missing something? The same output is also produced by setting PrecisionGoal -> 3 and AccuracyGoal -> 3, thus we can rule out that the numbers in the output are just identical representations of different numbers.

  • 1
    $\begingroup$ You can do this with g[x_] = x^2, {sol, data} = Reap[NMinimize[{g[x]}, x, Method -> "RandomSearch", EvaluationMonitor :> Sow[{x, g[x]}]]]; and look at data. $\endgroup$ Commented Feb 13, 2014 at 10:18
  • 3
    $\begingroup$ I've observed this as well. I haven't investigated it thoroughly (you could use the diagnostic output if you want to see in detail what's going on) but the problem seems to be that in most cases Mathematica doesn't save the objective function value anywhere, so wherever it's required, it just gets reevaluated. NMinimize is great because of its very general constraint handling, but it isn't the most efficient choice in most cases. Perhaps try FindMinimum or, if you don't need constraints, my own Nelder-Mead optimizer. $\endgroup$ Commented Feb 13, 2014 at 11:12
  • $\begingroup$ Hmm, I wonder why manual memonization does not help. ClearAll@f; f[x_?NumericQ] := (f[x] = (Print[N[x, 10]]; x^2)); NMinimize[f[x], x, Method -> "RandomSearch"]. So I guess f gets converted/compiled under the hood... $\endgroup$
    – Ajasja
    Commented Feb 13, 2014 at 14:42
  • 2
    $\begingroup$ In addition to the useful suggestions from @Oleksandr R., you could define your function so that it memoizes, that is f[x_?NumericQ]:= f[x]=.... This will not prevent all reevaluations but I find that it can help. Caveat: you might need to sporadically clear out definitions so as not to use too much memory. $\endgroup$ Commented Feb 13, 2014 at 14:50
  • $\begingroup$ I find the memoization works: ClearAll[f, g]; g = {}; f[x_?NumericQ] := f[x] = (g = {g, x}; x^2); NMinimize[f[x], x, Method -> "RandomSearch"]; DuplicateFreeQ[Flatten[g]] returns True. $\endgroup$
    – ecoxlinux
    Commented Feb 13, 2014 at 15:45


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.