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:
-0.829053
-0.829053
-0.829053
-0.829052
-0.329053
-0.329053
-0.329052
-8.85837*10^-9
-8.85837*10^-9
6.0428*10^-9
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.
g[x_] = x^2,{sol, data} = Reap[NMinimize[{g[x]}, x, Method -> "RandomSearch", EvaluationMonitor :> Sow[{x, g[x]}]]];and look atdata. – b.gates.you.know.what Feb 13 '14 at 10:18NMinimizeis great because of its very general constraint handling, but it isn't the most efficient choice in most cases. Perhaps tryFindMinimumor, if you don't need constraints, my own Nelder-Mead optimizer. – Oleksandr R. Feb 13 '14 at 11:12ClearAll@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... – Ajasja Feb 13 '14 at 14:42f[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. – Daniel Lichtblau Feb 13 '14 at 14:50ClearAll[f, g]; g = {}; f[x_?NumericQ] := f[x] = (g = {g, x}; x^2); NMinimize[f[x], x, Method -> "RandomSearch"]; DuplicateFreeQ[Flatten[g]]returnsTrue. – ecoxlinux Feb 13 '14 at 15:45ClearAll[f, g]; g = {}; f[x_?NumericQ] := (g = {g, x}; x^2); NMinimize[f[x], x, Method -> "RandomSearch"]; DuplicateFreeQ[Flatten[g]]returnsFalse. In your example, iff[x]is requested the second time for the samex-value,g={g,x}is not executed. – Frederik Ziebell Feb 14 '14 at 08:00NMinimize. – Oleksandr R. Feb 15 '14 at 17:18