1

I have a ANTLR expression parser which can evaluate expressions of the form ( A & ( B | C ) ) using the generated visitor. A , B and C can take any of the 2 values true or false. However I am faced with a challenge of finding all combinations of A,B and C for which the expression is true. I tried to solve this by the following method.

  1. Evaluate the expression for the 3 variables taking true and false each
  2. This comes to 8 combinations since 2 ^ 3 is 8
  3. I evaluate giving values like 000, 001, 010 ....... 111 to the variables and evaluate using the visitor

Though this works this method becomes compute intensive as the number of variables increases. Hence for an expression with 20 variables 1048576 computations are required. How can I optimise this complexity so that I get all the true expressions ? I hope this falls under Boolean satisfiabilty problem

ssdimmanuel
  • 448
  • 10
  • 29
  • 1
    This was a close answer which still needs research from my side. I was waiting to see if there would be more answers – ssdimmanuel Dec 04 '16 at 03:26

1 Answers1

3

It does. If you are liimted to 20-30 variables, you can simply brute force a trial of all the combinations. If it takes 100ns per try (that's 500 machine instructions), it will run in about 100 seconds. That's faster than you.

If you want to solve much bigger equations than that, you need to build a real constraint solver.

EDIT due to OP remark about trying to go parallel to speed up a Java program that brute forces the answer:

I don't know how you represent your boolean formula. For brute force, you don't want to interpret a formula tree or do something else which is slow.

A key trick is to make evaluation of the boolean formula fast. For most programming languages, you should be able to code the formula to test as an native expression in that language by hand, wrap it N nested loops and compile the whole thing, e.g.,

A=false;
do {
     B=false;
     do {
          C= false;
          do { 
               if (A & (B | C) ) { printf (" %b %b %b\n",A,B,C);
               C=~C;
             } until C==false;
          B=~B;
        } until B==false;
     A=~A;
   } until A==false;

Compiled (or even JITted by Java), I'd expect the innner loop to take 1-2 machine instructions per boolean operation, touching only registers or a single cachec line, plus 2 for the loop. For 20 variables thats around 42 machine instructions, even better than my rough estimate in the first paragraph.

If one insists, one could convert the outermost loops (3 or 4) into parallel threads, but if all you want are the print statements I don't see how that will actually matter in terms of utility.

If you have many of these formulas, it is easy to write a code generator to produce this from whatever representation you have of the formula (e.g., ANTLR parse tree).

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • thanks for the answer. Even for 20 variables I had multithreaded my Java program to get results faster. Are there any open source constraint solving libraries available? – ssdimmanuel Dec 03 '16 at 07:31
  • Open source constraint solver: this looks pretty promising: http://www.gecode.org/ – Ira Baxter Dec 03 '16 at 11:08
  • Great thanks !! gecode seems to be a C++ library. One of the java libraries that I came across is [SAT4j] (http://www.sat4j.org/). However I am new to these and have to see how I can adapt them to my Java application. The documentation says that the boolean expression has to be converted to CNF form before using the SAT solvers. Hope it would take sometime for me to get used to the conventions used – ssdimmanuel Dec 03 '16 at 18:50
  • A search yielded [Choco solver]( http://www.choco-solver.org) which seemed like a promising one for me. It makes use of integer and Boolean constraints. But my business rules check for strings as well.. I am stuck on how to convert my business problem to a constraint problem. Any help would be much appreciated – ssdimmanuel Dec 16 '16 at 06:22
  • A question about how to use a constraint solver with strings is not the same question as this. You should start a new question and make sure you provided details more than "check for strings". If you leave a link here to that question, I'll be sure look at it. – Ira Baxter Dec 16 '16 at 08:11
  • I started a new question http://stackoverflow.com/questions/41198639/represent-a-business-rule-as-a-constraint-model-to-find-the-solution-set – ssdimmanuel Dec 17 '16 at 13:19