0
這個問題關閉。在python中完成np算法[解決]
有關這個問題的所有信息將在一週後內上載。我仍在試圖弄清楚全部細節。仍然將其作爲模塊進行測試。
n是元件的數量 和式是一個條款
這是一個NP完全算法。
該代碼試圖找出任何n的排列可以滿足,如果公式是正確的。
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)
這是不可滿足的BTW爲變量的所有排列 – user3349106
有什麼錯誤嗎?您是否在期望值或異常中遇到錯誤? –
你試過用調試器來調試你的代碼嗎?這個例子的預期和實際產出是多少? – amit