我的代码得到了错误的答案
n 是变量的数量,公式是包含子句的列表
给定一个包含“n”个变量和在列表“公式”中编码的子句的 SAT 实例,如果该实例可满足,则返回“可满足”,否则返回“不可满足”。'formula' 的每个元素代表一个子句,是一个整数列表,其中整数 i 表示文字 Xi 存在于子句中,整数 -i 表示文字 ~Xi 存在于子句中。例如,子句“X1 v ~X11 v X7”用列表 [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)