26

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 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.

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
πρόσεχε
  • 4,452
  • 1
  • 12
  • 28
  • 2
    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. – MassDefect Dec 02 '19 at 23:12
  • @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. – MarcoB Dec 02 '19 at 23:14
  • 1
    One workaround is to use Surd instead of Sqrt, i.e., NMaximize[{Surd[1 - x^2, 2], -1 <= x <= 1}, x] – Bob Hanlon Dec 02 '19 at 23:14
  • It should be noticed that the notation -1 <= x <= 1 is not mathematically correct because <= is a binary relation. I don't understand the economy of two symbols in comparison with -1 <= x &&x<=1. – user64494 Dec 03 '19 at 06:14
  • 1
    Just to mention that in Mma 11.2 NMaximize seems to work ok. – Hans Olo Dec 03 '19 at 09:08
  • 1
    @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. – Andreas Rejbrand Dec 03 '19 at 22:57
  • 1
    Seems to have been fixed in MMA 12.1 – user58955 Mar 23 '20 at 02:03

2 Answers2

20

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.

kirma
  • 19,056
  • 1
  • 51
  • 93
MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • On my system Method -> Automatic does not work, but Method -> "Automatic" does work. – MassDefect Dec 02 '19 at 23:22
  • @MassDefect Good observation! Most clues point to a flaw in the method selection routine then. Added your point in the answer. – MarcoB Dec 02 '19 at 23:25
19

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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 4
    @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. – Michael E2 Dec 03 '19 at 13:16
  • 5
    The bug was reported in house several months back. As best I can tell, it is fixed for the next release. – Daniel Lichtblau Dec 03 '19 at 16:06
  • 6
    Another way to avoid the convex solver: Method -> Except["Convex"] – ilian Dec 03 '19 at 23:06