# Bound on number of function evalulations in FindMinimum

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. 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).

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])]]]][[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}]]] • +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! – Alexey Popkov Oct 30 '13 at 1:09
• @AlexeyPopkov It has the clear advantage of saving one evaluation per step :) – Dr. belisarius Oct 30 '13 at 1:15
• 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. – Alexey Popkov Oct 30 '13 at 1:20
• 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]]. – Alexey Popkov Oct 30 '13 at 1:27
• +1 for fval = fun[x]. It works – my account_ram Oct 30 '13 at 9:39