For the past one week, I had been breaking my head over a piece of Mathematica code. I wanted to use NMinimize in Mathematica to minimize a function which was written as a module. In this module, I was solving a linear system of equations (sparse and overdetermined, but that’s not the point). The problem was that even with moderate dimensions, say 20*17, Mathematica ran out of memory and crashed on me.
I realized that the first invocation to my objective function from NMinimize was being done symbolically rather than numerically. Why the hell was it doing that? I read and re-read the documentation (whatever little of it there is) until I knew it by heart. I was just about to punch my computer, when the good guys over at Stack Overflow told me what the problem was.
It turns out that NMinimize does not hold its arguments. This means that as the list of arguments is read from left to right, each argument is evaluated and replaced by the result of the evaluation. So for example, something like the following:
NMinimize[dummy[x],x]
would cause the evaluation of dummy with “x” as an input, which is exactly what was happening in this case.
I changed the call to
NMinimize[Hold[dummy[x]],x]
and lo and behold! everything was well in my world again.
So, what are the lessons for me in this totally forgettable experience?
#1. If a piece of software (which is not free) is driving you insane, the fault is most probably yours.
#2. You may have worked through all the advanced notebooks your prof gave you while learning Mathematica, but it won’t hit you until it has hit you.
#3. The www makes the world a better place to live in. Strive to contribute sense to this sea of sense and nonsense.
I have been banging my head against the wall all morning with the same exact problem. Im trying to use NMinimize on a module which is essentially a black box which spits out a single value. I was having the same problem as you when it tried to evaluate my module before substituting the parameter I am optimizing. I tried holding the argument but it seems to never evaluate it. I get “The function value – Hold[dummy[5.3]] is not a number at x=5.3″. Is that all you did? If you can help I would greatly appreciate it.
Well,I did an additional thing: I imposed a constraint that my module should only accept numeric values, and that seemed to work for me, even if I did not hold the first argument.something like dummy[arg/;VerctorQ[arg,NumericQ] in my case since the argument to my module was a vector.
hope this helps!
Thank you very much. The second solution, imposing constraints on the parameters, is indeed the solution.
Denis
Whats going seriously.I have been trying this since week.Can you help me how to generate simulated data under given condition
NMinimize[] filling the memory is still stupid, and having to “Hold[]” doesn’t justify this. I can understand that a single call could fill up memory, but I can’t understand why that filled memory can’t be released after the minimization is done…