I encountered this problem while addressing an answer to this other question.
Here I do the following:
I ask NMaximize
to do its job, and I use Reap
and Sow
with EvaluationMonitor
to store the values that it tried in the list called reapSowPoints
. At the same time, the objective function also stores the variables that were called in a separate list called functionCalls
. These two lists should be the same, but they are actually very different. Why is that?
Here's the code:
myMaximization[vars_, objective_] :=
Reap[
Module[{customFunction},
customFunction[v_ /; VectorQ[v, NumericQ]] :=
Module[{val = objective @@ v},
AppendTo[functionCalls, v];
val];
NMaximize[
{customFunction[vars], Norm @ vars == 1},
vars,
EvaluationMonitor :> Sow[vars]]]]
When I evaluate it I obtain (e.g.):
functionCalls = {};
{result, reapSowPoints} = myMaximization[{x, y}, #1 + #2 &];
Length /@ {functionCalls, reapSowPoints[[1]]}
{1603, 222}
The function was called 1603 times, but Sow
appears to have caught only 222 of them.
Sow
versusSaw
. $\endgroup$Length /@ DeleteDuplicates /@ {functionCalls, First@reapSowPoints}
. It's more a problem withEvaluationMonitor
than withSow
. $\endgroup$MaxIterations -> 10
) than with more (MaxIterations -> 100
). $\endgroup$NMaximize[]
routine. TheEvaluationMonitor
is not used in this phase, I believe. $\endgroup$