0
$\begingroup$

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}] 
$\endgroup$
3
  • $\begingroup$ It's not completely clear what you mean by a function whose output contains noise. What happens in your 1st example is that the function is evaluated only once (!), and the resulting function (for example 1.18444 - (π - η)^2) gets maximized. Obviously the result is $\pi$, always. The 2nd example is completely different, because the function is reevaluated every time. Now, the issue I believe is in the way random generator is used inside NMaximize, because it seems to be always initiated with the same seed, and this also affects ... $\endgroup$ Commented Sep 28, 2024 at 9:05
  • $\begingroup$ ... your RandomVariate. If I use SeedRandom inside w, I do get expected results. $\endgroup$ Commented Sep 28, 2024 at 9:05
  • $\begingroup$ The following seems to work better. That is adding RandomSeed as an option in Method, appears to help NMaximize converge better: NMaximize[{w1[\[Eta]], 0. < \[Eta] < 4.}, \[Eta] \[Element] Reals, Method -> {"DifferentialEvolution", "RandomSeed" -> RandomInteger[2^63]}] $\endgroup$ Commented Sep 28, 2024 at 12:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.