1
$\begingroup$

I am minimizing a nonlinear constrained problem using FindMinimum. The problem involves about 200 variables and the function is a black-box which cannot be compiled/optimized (I have set the Method as Automatic).

During evaluations, I collect the value of the objective function at intermediate evaluations using EvaluationMonitor option. I have a Monitor statement enclosing the FindMinimum which plots the objectiveFunction against the number of function evaluations. This was the picture.

objective_fn_against_num_of_fn_evals

Is there an option to (gracefully) terminate the FindMinimum after nMax number of function evaluations, returning the best solution of those so far evaluated (like position 25 in the image)? ( MaxIterations does not achieve it, since it is a bound on the number of steps taken and not the function evaluations).

$\endgroup$
2
$\begingroup$
f[fun_, x0_, iters_] := 
 Reverse@First@Sort[
   Reap[Block[{n = 0, fval}, Catch[FindMinimum[fval = fun[x], {x, x0}, 
                                         EvaluationMonitor :> 
                                           (Sow[{fval, x}]; If[++n >= iters, Throw[0]])]]]][[2, 1]]]

f[Sin, 1, 8]
(*
 {-1.57247, -0.999999}
*)

.

Show[Plot[Sin@x, {x, -  2, 1}, AspectRatio -> 3/2], 
     Graphics[{Red, PointSize[Medium], Point@#, Line@#} &@ Table[f[Sin, 1, i], {i, 1, 10}]]]

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ +1. Did not know that the form fval = fun[x] will update fval on each evaluation of the objective function if EvaluationMonitor is specified. Such a very basic usage case should be in the Documentation! $\endgroup$ – Alexey Popkov Oct 30 '13 at 1:09
  • $\begingroup$ @AlexeyPopkov It has the clear advantage of saving one evaluation per step :) $\endgroup$ – Dr. belisarius Oct 30 '13 at 1:15
  • $\begingroup$ Wait... What happens is not updating the fval but just assignment fval = fun[x] is made globally and then at each evaluation fun[x] is evaluated twice: once as objective function and once from EvaluationMonitor. This means that fval is not needed at all: you can put fun[x] in the monitor with the same effect. I was too fast. $\endgroup$ – Alexey Popkov Oct 30 '13 at 1:20
  • 1
    $\begingroup$ You can see this with n=0; f[_?NumberQ] := (n++; Sin[x]); FindMinimum[fval = f[x], {x, 1}, EvaluationMonitor :> Print[{n, fval}]] and also with FindMinimum[fval = Sin[x], {x, 1}, EvaluationMonitor :> Print[OwnValues@fval]]. $\endgroup$ – Alexey Popkov Oct 30 '13 at 1:27
  • $\begingroup$ +1 for fval = fun[x]. It works $\endgroup$ – my account_ram Oct 30 '13 at 9:39

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.