Suppose we have some function f[x,y]
that we want to optimize in way that we are only interested in values (x,y)
that guarantee our function value is below some value target
. See the following MWE:
f[x_, y_] := f[x, y] = x^2 - 4*x + y^2 - y - x*y;
findMin[target_, steps_] := Block[{nbr = -1},
solsOpt = NMinimize[
f[x, y],
{x, y},
Method -> "NelderMead",
(*EvaluationMonitor:>{nbr += 1; If[Mod[nbr, steps] == 0, Print["Step: ", nbr," ; Current value: ",f[x,y], " ; parameters: ",{x,y}],Print]}*)
EvaluationMonitor :> {nbr += 1; If[Mod[nbr, steps] == 0, Print["Step: ", nbr," ; Current value: ", f[x, y], " ; parameters: ", {x, y}], Print] || If[f[x, y] <= target, Abort[], Print]}
];
Print["Number of iterations: ", nbr];
Print["Final value: ", solsOpt[[1]]];
Return[solsOpt];
]
Of course findMin[-5,1]
stops after a few iterations and I can read the values (x,y)
that satisfy my criterion. However, I need to do that for a bunch of different functions f
inside a ParallelTable
structure, that in the end holds (function_index, final value, parameter values). By aborting no values are stored. What I want is something like "After reaching target, just assume optimization is finished and go on with the next one". Is that possible with the built-in function(s)?
CheckAbort
? Perhaps withReap
andSow
... $\endgroup$FindInstance[f[x, y] <= -5, {x, y}]
? $\endgroup$StepMonitor
, e.g. having itThrow
a result if it is sufficiently good. If you have in mind a particular threshold you can also make it into a constraint. $\endgroup$CheckAbort
- I did not know of this function. I will definitely give it a try although george2079 already posted a working answer. Always so many ways to reach the goal :) $\endgroup$FindMinimum
but it tended to find the same results asNMinimize
in my real problem. But now after I know how to stop, it might probably find results faster and I will give it a try again. - Also as an aside: In another comment to one of my questions you explained whyNelderMead
will probably not work that good. I tried different ones likeDifferentialEvolution
andSimulatedAnnealing
and also other algorithm parameters as explained in F. Gao, L. Han, Comp. Opt. and Appl., 51, 259-277. However,NelderMead
with standard parameters led to best results in most cases $\endgroup$