I noticed the following behavior while using NMinimize
in Mathematica. The first invocation of the objective function is with variable names, rather than with points from the space, as one would expect.
So for example if my objective function is a module, this module is called only once, evaluated symbolically and then in further iterations, this symbolic expression is evaluated with points from the variable space.
This behavior could slow down the computation significantly for a large expression.
Is there any way to get around this? Has anyone else experienced this? Is there any way to speed up NMinimize
then?
Example:
dummy[x_] := Module[
{},
Print["x=", x ];
4 x^4 - 4 x^2 + 1
]
In: NMinimize[dummy[x], x]
Out:x=x
{0., {x -> 0.707107}}