18
$\begingroup$

Is it possible to extract the Method which is used in functions like NMinimize, FindRoot, FindMinimium, and partners when one doesn't set this option explicitly? Basic example:

NMinimize[x^4 - 3 x^2 - x, x]

What is used as Method here?

I expected to find some setting in SystemOptions[] but was unable to. Obviously AbsoluteOptions[expr] cannot be used in such a case since unlike a e.g. graphic, the minimization result does not contain this option information anymore.

Here is what is stated in the documentation of Method:

With the default setting Method -> Automatic, Mathematica will automatically try to pick the best method for a particular computation.

This tells us the chosen Method depends on the input problem. Therefore, a fixed setting in SystemOptions wouldn't make sense.

Additionally, there exists a document describing some internal implementations. Basically, you can extract most information from there, but I was hoping for a way to extract the used Method during a run.

$\endgroup$
  • 1
    $\begingroup$ For NMinimize[] (from here): "For linear cases, NMinimize and NMaximize use the same methods as LinearProgramming. For nonlinear cases, they use Nelder-Mead methods, supplemented by differential evolution, especially when integer variables are present." $\endgroup$ – J. M. will be back soon Nov 15 '12 at 13:10
  • 2
    $\begingroup$ did you ask this question because its number 4000 :-) $\endgroup$ – chris Nov 15 '12 at 13:22
  • 1
    $\begingroup$ This doesn't work, but how come OptionValue doesn't get evaluated when/where the option exist? f[x_, y_] := (x^2 + y^2 - 16)^2; first = True; {sol, {{ mthd}}} = Reap[ NMinimize[f[x, y], {{x, -5, 5}, {y, -5, 5}}, Method -> "DifferentialEvolution", EvaluationMonitor :> If[first, Sow[OptionValue[Method]]; first = False;]]]; mthd $\endgroup$ – ssch Nov 15 '12 at 13:37
  • 2
    $\begingroup$ For NMinimize, yes, it is possible. I described how to get diagnostic output here. For your example it seems to be that differential evolution is used. $\endgroup$ – Oleksandr R. Nov 15 '12 at 14:12
  • 1
    $\begingroup$ Another possibility: Cases[Trace[NMinimize[x^4 - 3 x^2 - x, x], Optimization`NMinimizeDump`method, TraceInternal -> True], {HoldForm[Optimization`NMinimizeDump`method], m_ /; FreeQ[m, Automatic]} :> m, Infinity] $\endgroup$ – J. M. will be back soon Nov 15 '12 at 14:34
18
$\begingroup$

As I described here, we can obtain diagnostic output from NMinimize using an internal variable,

NMinimize; (* Auto-load package, in case we haven't called NMinimize already *)
Optimization`NMinimizeDump`$DiagnosticLevel = 3;

where the 3 means, as mentioned previously, that we would like to receive information at an intermediate level of detail. In fact, this also happens to be the lowest setting that states explicitly which method was used.

Trying your example minimization, we get output showing that differential evolution was used in this case:

NMinimize diagnostic output

Since this method only gives you a printout rather than something you can make use of programmatically, and because this internal option is of course completely undocumented anyway, in practice I'd probably tend to prefer J. M.'s suggestion of just grabbing the value of Optimization`NMinimizeDump`method directly.

$\endgroup$
  • $\begingroup$ @J.M. Hmm, it's really hard to decide who get the accept since both answers are valid and solve the problem (initially, I was only interested in the method used by NMinimize!). I give the accept to Olek, since (I think) he did all the digging inside Mathematica-code. $\endgroup$ – halirutan Nov 19 '12 at 1:08
  • $\begingroup$ @hal, no worries; he needs the rep more than me... ;) $\endgroup$ – J. M. will be back soon Nov 19 '12 at 15:00
  • 3
    $\begingroup$ FWIW, the variable Optimization`NMinimizeDump`$DiagnosticLevel seems to have been superseded by the function Optimization`NMinimizeDump`dbPrint. It seems to be called thus: Optimization`NMinimizeDump`dbPrint[level, stuff], where stuff seems to be a list of stuff. The level that shows the method is 2. $\endgroup$ – Michael E2 May 22 '15 at 17:43
7
$\begingroup$

At halirutan's behest: here's one way to reckon out the method used internally by NMinimize[] and cohorts:

Cases[Trace[NMinimize[x^4 - 3 x^2 - x, x], Optimization`NMinimizeDump`method,
            TraceInternal -> True],
      {HoldForm[Optimization`NMinimizeDump`method], m_ /; FreeQ[m, Automatic]} :> m,
      Infinity]

where in this case, the output is {HoldForm["DifferentialEvolution"]}. Trying the same tactic on NMinimize[x^2 + y^2, {x, y}] yields {HoldForm["NelderMead"]}.

SFAICT, this only works since the guts of NMinimize[]/NMaximize[] are mostly written in high-level Mathematica; I'm not sure if the internals used can be teased out of FindMinimum[] or FindRoot[] in this manner.

(Here is a related question.)

$\endgroup$
  • $\begingroup$ FindMinimum is quite obscure ... $\endgroup$ – Dr. belisarius Aug 27 '15 at 7:26
  • $\begingroup$ Well, that was not implemented in top-level Mathematica code, so yes, not transparent at all. $\endgroup$ – J. M. will be back soon Aug 27 '15 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.