26
$\begingroup$

Bug introduced in 12.0. Fixed in 13.2 or earlier


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 [email protected] 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$
7
  • 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
    Commented Dec 2, 2019 at 23:12
  • 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
    Commented Dec 2, 2019 at 23:14
  • 1
    $\begingroup$ Just to mention that in Mma 11.2 NMaximize seems to work ok. $\endgroup$
    – Hans Olo
    Commented Dec 3, 2019 at 9:08
  • 1
    $\begingroup$ @user64494: Well, that notation is used everywhere in the mathematical community. It is certainly considered "correct" by the vast majority of mathematical students, teachers, and researchers. Out of curiosity, would you also object against something like f(x, y) = x^2 + y^2 + 6x + 9 = (x+3)^2 + y^2 >= 0 for all x and y? This kind of syntax is somewhat uncommon in programming languages (because there these symbols are typically binary operators returning booleans), but that's another story. $\endgroup$ Commented Dec 3, 2019 at 22:57
  • 1
    $\begingroup$ Seems to have been fixed in MMA 12.1 $\endgroup$
    – user58955
    Commented Mar 23, 2020 at 2:03

2 Answers 2

20
$\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$
2
  • $\begingroup$ On my system Method -> Automatic does not work, but Method -> "Automatic" does work. $\endgroup$
    – MassDefect
    Commented Dec 2, 2019 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
    Commented Dec 2, 2019 at 23:25
19
$\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$
3
  • 5
    $\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
    Commented Dec 3, 2019 at 13:16
  • 5
    $\begingroup$ The bug was reported in house several months back. As best I can tell, it is fixed for the next release. $\endgroup$ Commented Dec 3, 2019 at 16:06
  • 7
    $\begingroup$ Another way to avoid the convex solver: Method -> Except["Convex"] $\endgroup$
    – ilian
    Commented Dec 3, 2019 at 23:06

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.