-3

I have the following Boolean expression x > 5 AND y > 10

C:\>python Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36)Type "help", "copyright", "credits" or "license" for more information.
>>> x = 3
>>> y = 11
>>> eval("x>5 and y > 10") False
>>> x = 6
>>> eval("x>5 and y > 10") True
>>>

When x > 5 and y > 10 the evaluation is formula is evaluated to "true".

When x == 6 and y == 5 the formula is evaluated to "false" because y < 10.

I would like to know if there is a library/software (python is used as an example, the language is not a problem) that can answer to the caller which values satisfy the formula.

cateof
  • 6,608
  • 25
  • 79
  • 153
  • 4
    There are an infinite number of values that satisfy both of those formulas. Also, why are you using `eval` when you could just run that code. – user3483203 Jun 13 '18 at 15:23
  • 1
    I would have thought that `x > 5 and y > 10` is the clearest possible description of which values are satisfactory. – khelwood Jun 13 '18 at 15:23
  • Look into `sympy` if you come up with an inequality that is actually solveable. In the meantime, this is off topic. – FHTMitchell Jun 13 '18 at 15:23
  • I want to know if there is at least one and the "smaller" value. – cateof Jun 13 '18 at 15:24
  • Are you looking for some sort of equation solver? Maybe sympy then as mentioned. – Anton vBR Jun 13 '18 at 15:24
  • are you looking for a equation solver/linear programming? https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.linprog.html – Yikang Luo Jun 13 '18 at 15:26
  • @FHTMitchell there is a shell http://live.sympy.org/, can you provide an example? – cateof Jun 13 '18 at 15:26
  • do you want to be able to feed data and do math on your inputs or just evaluate variables which are defined in your code? for the former have a look at https://newville.github.io/asteval/ – wiesion Jun 13 '18 at 15:28
  • @cateof err, no. Theres an entire page dedicated to it http://docs.sympy.org/latest/modules/solvers/inequalities.html . How do you have 230 questions on SO and still not understand what is and isn't on topic? – FHTMitchell Jun 13 '18 at 15:29

1 Answers1

1

SMT solvers would fit the bill nicely. Here's your problem coded using the Python bindings to z3:

from z3 import *

def getResult (s):
  r = s.check()
  if r == sat:
     print True
     print s.model()
  elif r == unsat:
     print False
  elif r == unknown:
     print "Solver said unknown!"
  else:
     print "Unexpected result!"
     print r

x, y = Ints('x y')

s1 = Solver()
s1.add(x == 3)
s1.add(y == 11)
s1.add (x > 5)
s1.add (y > 10)
getResult(s1)

s2 = Solver()
s2.add (x == 6)
s2.add (y == 11)
s2.add (x > 5)
s2.add (y > 10)
getResult (s2)

When run, this prints:

False
True
[y = 11, x = 6]

Of course this is rather a silly example; you don't have to specify the values of x and y at all; if you don't z3 will give you some value that satisfies constraints.

Read more about z3 here: https://rise4fun.com/z3/tutorial

Read more about SMT solving in general here: http://smtlib.cs.uiowa.edu/

z3 is open source, download from: https://github.com/Z3Prover/z3

z3 can be scripted in many languages; SMTLib/C/C++/Java/Python/Scala and even Haskell. The higher-level the language, the easier it is to use the interface, in general.

alias
  • 28,120
  • 2
  • 23
  • 40