Possible bug in NMaximize function?
This is an extended comment rather than an answer, but it would have been unwieldy in a comment box.
I was able to reproduce the problem you describe on Windows 10 / MMA 12. I also noticed that specifying any method option seems to eliminate the problem. Below is a list of the methods available to NMinimize
on my system; I assumed that the same are available to NMaximize
as well and tried them explicitly:
NMaximize[{Sqrt[1 - x^2], -1 <= x <= 1}, x, Method -> #]& /@ {"Automatic",
"DifferentialEvolution", "MeshSearch", "NelderMead", "SimulatedAnnealing",
"RandomSearch", "NonlinearInteriorPoint"}
All returned a reasonable answer with no delay. I am suspecting that some pre-processing or method selection routine within NMaximize
may be at fault, since a call with Method -> Automatic
fails, but specifying any method (weirdly enough, even "Automatic"
as a string), works fine.
In V12, WRI introduced new convex optimization solvers. NMiminize
was updated to automatically choose one when appropriate.
But in this case, it seems to enter an infinite loop. Please report it to WRI.
You can turn off convex minimization as follows:
Block[{Optimization`UseConvexMinimize = False &},
NMaximize[{Sqrt[1 - x^2], -1 <= x <= 1}, x]
]
(* {1., {x -> 1.01852*10^-9}} *)
The offending function is a preprocessor, Optimization`TransformProblem
. If we disable this to just return the first argument (which is an optimization problem object), the optimization is completed successfully:
Block[{Optimization`TransformProblem = # &},
NMaximize[{Sqrt[1 - x^2], -1 <= x <= 1}, x]
]
(* {1., {x -> 1.01852*10^-9}} *)
One might figure this out from the following trace (NMaximize[f,..]
calls NMinimize[-f,..]
in effect):
Trace[
TimeConstrained[
NMinimize[{-Sqrt[1 - x^2], -1 <= x <= 1}, x],
0.001],
TraceInternal -> True]
If you select the final evaluation chain, which ends with a long repeated sequence of {Sign[-1],-1},{1/(1/2),2},{Sign[-1],-1}
, it leads you back to Optimization`TransformProblem
.