0

I am getting a wrong answer for my code

n is the number of variables and formula is a list containing clauses

Given a SAT instance with 'n' variables and clauses encoded in list 'formula', returns 'satisfiable' if the instance is satisfiable, and 'unsatisfiable' otherwise. Each element of 'formula' represents a clause and is a list of integers where an integer i indicates that the literal Xi is present in the clause and an integer -i indicates that the literal ~Xi is present in the clause. For example, a clause "X1 v ~X11 v X7" is represented with the list [1, -11, 7].

import itertools
n = 4 
formula = [[1, -2, 3], [-1, 3], [-3], [2, 3]]


booleanValues = [True,False] * n

allorderings = set(itertools.permutations(booleanValues, n)) #create possible combinations of variables that can check if formula is satisfiable or not

print(allorderings)

for potential in allorderings:
    l = [] #boolean value for each variable / different combination for each iteration
    for i in potential:
        l.append(i)
    #possible = [False]*n
    aclause = []
    for clause in formula:
        something = []
        #clause is [1,2,3]
        for item in clause:
            if item > 0:
                something.append(l[item-1]) 
            else:
                item = item * -1
                x = l[item-1]
                if x == True:
                    x = False
                else:
                    x = True
                something.append(x)
        counter = 0
        cal = False
        for thingsinclause in something:
            if counter == 0:
                cal = thingsinclause
                counter = counter + 1
            else:
                cal = cal and thingsinclause
                counter = counter + 1

        aclause.append(cal)

    counter2 = 0
    formcheck = False
    for checkformula in aclause:
        if counter2 == 0:
            formcheck = checkformula
            counter2 = counter2 + 1
        else:
            formcheck = formcheck or checkformula
    print("this combination works", checkformula)
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3349106
  • 77
  • 1
  • 7
  • this is unsatisfiable btw for all permutations of variables – user3349106 Apr 04 '14 at 07:49
  • 1
    What's the bug? Are you getting an error in expected value or an exception? – machine yearning Apr 04 '14 at 07:51
  • Did you try debugging your code with a debugger? What is the expected and actual output for the example? – amit Apr 04 '14 at 07:52
  • *edited sorry i meant i was getting the wrong answer – user3349106 Apr 04 '14 at 07:54
  • Also, for sanity checks, how many permutations are returned to `allorderings` in the set(itertols...) expression? What is the size of each? – amit Apr 04 '14 at 07:55
  • allordering returns all possible permutations... so 2^n i think.. cant remember losing my mind been working on this question for 2 days without sleep – user3349106 Apr 04 '14 at 08:00
  • @user3349106 Yes, it should return 2^n, checked and it indeed does, just throwing hints on how to debug a program - step by step... Your code also seems to be overcomplicating the problem, checking if an assignment satisfies a formula can be done much simpler IMO. – amit Apr 04 '14 at 08:03
  • @user3349106 Also, your identifiers (variable names) do not help you nor other follow the code. `something` is a terrible variable name/ – amit Apr 04 '14 at 08:04
  • For one, `booleanValues = [True,False] * n` is `[True,False,True,False,True,False,True,False]` for `n = 4` which is probably not what you want. What do you want `booleanValues` to be? – Gassa Apr 04 '14 at 08:04
  • true and false... you have to do all permutations.. ive re-edited the code... ive imported itertools... if you print out the values of booleanvalues and allorderings everything will make sense... – user3349106 Apr 04 '14 at 08:07
  • Oh I see now. The straightforward, less obscure, and more efficient way to do that is `allorderings = itertools.product([False,True], repeat = n)`. The relevant documentation is [here](https://docs.python.org/2.7/library/itertools.html#itertools.product). This one does not need a `set (...)` since every possible outcome is produced exactly once. – Gassa Apr 04 '14 at 08:27

1 Answers1

0

Here is a corrected version:

import itertools
n = 4 
formula = [[1, -2, 3], [-1, 3], [-3], [2, 3]]

allorderings = itertools.product ([False, True], repeat = n)

for potential in allorderings:
    print ("Initial values:", potential)
    allclauses = []
    for clause in formula:
        curclause = []
        for item in clause:
            x = potential[abs (item) - 1]
            curclause.append (x if item > 0 else not x)
        cal = any (curclause)
        allclauses.append (cal)
    print ("Clauses:", allclauses)
    formcheck = all (allclauses)
    print ("This combination works:", formcheck)

Points to consider:

  1. Instead of introducing some complex — and also wrong — logic to find the conjunction and disjunction, you can use any and all. That's cleaner and less prone to bugs.

  2. The natural object to loop over is itertools.product([False, True], repeat = n), that is, the set [False, True] of possible boolean values raised to the power of n. In other words, the Cartesian product of n copies of [False, True]. Here is the documentation for itertools.product.

  3. I introduced a bit more output to see how things are going. Here is the output I get with Python3 (Python2 adds parentheses but prints essentially the same):


Initial values: (False, False, False, False)
Clauses: [True, True, True, False]
This combination works: False
Initial values: (False, False, False, True)
Clauses: [True, True, True, False]
This combination works: False
Initial values: (False, False, True, False)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (False, False, True, True)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (False, True, False, False)
Clauses: [False, True, True, True]
This combination works: False
Initial values: (False, True, False, True)
Clauses: [False, True, True, True]
This combination works: False
Initial values: (False, True, True, False)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (False, True, True, True)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (True, False, False, False)
Clauses: [True, False, True, False]
This combination works: False
Initial values: (True, False, False, True)
Clauses: [True, False, True, False]
This combination works: False
Initial values: (True, False, True, False)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (True, False, True, True)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (True, True, False, False)
Clauses: [True, False, True, True]
This combination works: False
Initial values: (True, True, False, True)
Clauses: [True, False, True, True]
This combination works: False
Initial values: (True, True, True, False)
Clauses: [True, True, False, True]
This combination works: False
Initial values: (True, True, True, True)
Clauses: [True, True, False, True]
This combination works: False
Gassa
  • 8,546
  • 3
  • 29
  • 49