EmpowerOperations / babel

Simple math expression parser and evaluator
Apache License 2.0
1 stars 1 forks source link

babel, matlab, cpp have different interpretation of same function #10

Open Groostav opened 4 years ago

Groostav commented 4 years ago

TBD

From Cenk:

inputs:
  x1: {lower: -1.0, upper: 1.0}
  x2: {lower: -1.0, upper: 1.0}
objectives:
  f1: "-2.0*2.718281828459045^(15.0*(-(x1-0.1)^2-x2^2.0))-2.718281828459045^(20.0*(-(x1-0.6)^2.0-(x2-0.6)^2.0))+2.718281828459045^(20.0*(-(x1+0.6)^2.0-(x2-0.6)^2.0))+2.718281828459045^(20*(-(x1-0.6)^2.0-(x2+0.6)^2))+2.718281828459045^(20*(-(x1+0.6)^2.0-(x2+0.6)^2.0))"
  f2: "2.0*2.718281828459045^(20.0*(-x1^2.0-x2^2.0))+2.718281828459045^(20.0*(-(x1-0.4)^2.0-(x2-0.6)^2.0))-2.718281828459045^(20.0*(-(x1+0.5)^2.0-(x2-0.7)^2.0))-2.718281828459045^(20.0*(-(x1-0.5)^2.0-(x2+0.7)^2.0))+2.718281828459045^(20.0*(-(x1+0.4)^2.0-(x2+0.8)^2.0))"

from george:

I may have discovered a bug in Babel. I persistently get crashing when running OASIS with the Behzad problem but don't get the problem in my C++ implementation of the problem. I noticed a huge difference in the objective values I was getting between the two implementations, but can't find anything wrong with my implementation. I copied the functions into MATLAB and plugged in x1 = x2 = 1.0, and I got the following values:

f1 = -0.0017 f2 = 3.0432e-05 however in OASIS I get huge values I get 7E20 and -5.77E18 for the two functions now, regardless, this has revealed a problem in my algorithm code, however I do think there's a problem with how Babel is interpreting the functions

Groostav commented 4 years ago

my suspicion:

Groostav commented 4 years ago

Ok so,

what is

-3^2

wolfram alpha, and MATLAB, both give -9 excel, and babel, both give +9

the question is about negation and exponentiation prescedence, and they relate to the complex and real planes.

question: will your language mostly deal with integer exponents or rational exponents?

if the former, you're with excel. If the latter, you're with matlab.

Groostav commented 4 years ago

In other words

language prescedence -3^2 -3^2.718 (ie -3^e)
Matlab/Mathmatica FIRST: exponent THEN negate -9 -19.812
Excel FIRST: negate THEN exponent 9 #NUM (java returns NaN)

babel has chosen to act like the latter.

Still, its error handling system should flag this with a good error, so I'll leave this open

Groostav commented 4 years ago

In this particular case, its really unfortunate that we write (complex-expr)^2.0. What happens there is sign-confusion.

I think we can make this a compilation error: we we should look for an AST where:

Then we raise an error:

cannot raise a negative number negated-value to a non-integer power. Please rewrite as -(base-expr^expo-expr) or (-base-expr)^expo-expr.