21
$\begingroup$

In 12.0.0 for Microsoft Windows (64-bit) (April 6, 2019) writing:

Maximize[{Sqrt[1 - x^2], -1 <= x <= 1}, x]
Minimize[{Sqrt[1 - x^2], -1 <= x <= 1}, x]
NMinimize[{Sqrt[1 - x^2], -1 <= x <= 1}, x]

the following outputs are obtained immediately:

{1, {x -> 0}}

{0, {x -> -1}}

{0., {x -> -1.}}

while writing:

NMaximize[{Sqrt[1 - x^2], -1 <= x <= 1}, x]

the software enters an infinite loop without giving any warning.

Am I missing something or is something wrong?


EDIT: through the email address support@wolfram.com I contacted Wolfram Technical Support who in less than eight hours have confirmed that it is a bug and have already proceeded to report to their developers.

$\endgroup$
  • 2
    $\begingroup$ Very weird. This also happens to me. MacOS 10.15.1 and MMA V12.0. Interestingly, if you add StepMonitor :> Print[x], it never prints anything. It seems like it's unable to even take a single step. However, if you specify any Method (other than Automatic I suppose) it seems to start working. I wonder if it can't decide which Method to use on this problem? Using NMinimize[{-Sqrt[1 - x^2],...] also fails. Specifying a search interval, MaxIterations, or PrecisionGoal don't seem to help. Definitely seems like a bug to me. $\endgroup$ – MassDefect Dec 2 at 23:12
  • $\begingroup$ @MassDefect Even weirder: Automatic works fine too! Your further evidence with Print also seems to indicate that the process gets stuck early, and never gets to any calculations when no method is included. $\endgroup$ – MarcoB Dec 2 at 23:14
  • 1
    $\begingroup$ One workaround is to use Surd instead of Sqrt, i.e., NMaximize[{Surd[1 - x^2, 2], -1 <= x <= 1}, x] $\endgroup$ – Bob Hanlon Dec 2 at 23:14
  • $\begingroup$ @Bob Hanlon: yes, I had noticed this afternoon, but tonight I forgot to mention it, thanks for the observation. $\endgroup$ – TeM Dec 2 at 23:31
  • 1
    $\begingroup$ Just to mention that in Mma 11.2 NMaximize seems to work ok. $\endgroup$ – Rebel-Scum Dec 3 at 9:08
16
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ On my system Method -> Automatic does not work, but Method -> "Automatic" does work. $\endgroup$ – MassDefect Dec 2 at 23:22
  • $\begingroup$ @MassDefect Good observation! Most clues point to a flaw in the method selection routine then. Added your point in the answer. $\endgroup$ – MarcoB Dec 2 at 23:25
11
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ In the evening I collect your observations and I attach everything by email to WRI, thank you! Just a curiosity: how do you keep yourself up to date on the subject? In other terms, where could I have read the news introduced about the NMinimize function? Thanks! $\endgroup$ – TeM Dec 3 at 13:11
  • 2
    $\begingroup$ @TeM I went to the Wolfram Tech Conference and attended the presentation on convex optimization. In other words, I was lucky. One could view all the WTC presentations online. I'm not sure that would keep one completely up-to-date, but they would probably present the more important advances over the past year. It's a lot of work to do. So I'd rather ask a question on SE and hope someone here had the relevant info. Relying on others to help you is not a bad way to keep up with something as large and complicated and continually evolving like Mathematica. $\endgroup$ – Michael E2 Dec 3 at 13:16
  • 3
    $\begingroup$ The bug was reported in house several months back. As best I can tell, it is fixed for the next release. $\endgroup$ – Daniel Lichtblau Dec 3 at 16:06
  • 2
    $\begingroup$ Another way to avoid the convex solver: Method -> Except["Convex"] $\endgroup$ – ilian Dec 3 at 23:06

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.