19
$\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$
8
  • 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$ Nov 15, 2012 at 13:10
  • 2
    $\begingroup$ did you ask this question because its number 4000 :-) $\endgroup$
    – chris
    Nov 15, 2012 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, 2012 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$ Nov 15, 2012 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$ Nov 15, 2012 at 14:34

2 Answers 2

20
$\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$
3
  • $\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, 2012 at 1:08
  • $\begingroup$ @hal, no worries; he needs the rep more than me... ;) $\endgroup$ Nov 19, 2012 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, 2015 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$
2
  • $\begingroup$ FindMinimum is quite obscure ... $\endgroup$ Aug 27, 2015 at 7:26
  • $\begingroup$ Well, that was not implemented in top-level Mathematica code, so yes, not transparent at all. $\endgroup$ Aug 27, 2015 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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