I have a complicated function whose output contains noise. I'm interested in using Differential Evolution via NMaximize to solve it. However, NMaximize appears to compile or somehow 'freeze' the function outside its loop.
Below is a minimal example. Here, 'function 1' produces the expected behaviour in finding the argmax $\pi$ alongside a different random maximum each time.
That is, for three example runs:
{{0.368844, {\[Eta] -> 3.14159}}, {1.29786, {\[Eta] ->
3.14159}}, {0.128056, {\[Eta] -> 3.14159}}}
My actual problem is more like 'function 2'. But in this case it provides the same result three times. The result is also wrong and not $\pi$ - probably linked with the unexpected behaviour:
{{0.08951, {\[Eta] -> 3.89056}}, {0.08951, {\[Eta] ->
3.89056}}, {0.08951, {\[Eta] -> 3.89056}}}
Here is the example code:
ClearAll["Global`*"]
(* function 1 *)
f1 := NMaximize[{1.0 - (\[Pi] - \[Eta])^2 +
RandomVariate[NormalDistribution[]],
0. < \[Eta] < 4.}, \[Eta] \[Element] Reals,
Method -> "DifferentialEvolution" ];
Table[f1, {3}]
(* function 2 *)
w[\[Eta]_?NumericQ] :=
Block[{\[Epsilon]}, \[Epsilon] :=
RandomVariate[NormalDistribution[]];
1.0 - (\[Pi] - \[Eta])^2 + \[Epsilon]];
f2 := NMaximize[{w[\[Eta]], 0. < \[Eta] < 4.}, \[Eta] \[Element]
Reals, Method -> "DifferentialEvolution" ];
Table[f2, {3}]