8
$\begingroup$

This seems like it should be a simple question -- but I am looking to use a "home made" stopping condition with FindMaximum, while evaluating a very complex function.

Printing out the successive changes in the results for my problem (which cannot be boiled down to a quick code sample) I see that the relative differences in about 100 params are on the order of 10^-3 between steps (and often much smaller) which is more than enough for this application -- but it just continues to dance around the "right" answer until I hit MaxIterations, and adjusting Precision and Accuracy does not seem to help.

Q: Is there a way to implement a "stopping condition" option for these functions?

$\endgroup$

1 Answer 1

9
$\begingroup$

You could use StepMonitor to check if we reached our goal and then Throw / Catch to stop.

Here is test example, where we print x,y, and the change of the function.

fun[x_, y_] = -x^4 - y^4 + x y + x^2 y;
last = {0, 0};
FindMaximum[fun[x, y], {x, y}, 
 StepMonitor :> Print[{x, y, Norm[last - {x, y}]}]]

enter image description here

We now add Throwand Catchto stopp when the difference is smaller than eps:

eps = 0.1;
Catch@FindMaximum[fun[x, y], {x, y}, 
  StepMonitor :> 
   If[Print[{x, y, Norm[last - {x, y}]}]; Norm[last - {x, y}] < eps, 
    Throw[{x, y}], last = {x, y}]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Very elegant solution; have it implemented and working. $\endgroup$
    – Richard E
    Apr 9, 2021 at 12:02

Your Answer

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

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