2014-04-04 58 views
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) 
+0

這是不可滿足的BTW爲變量的所有排列 – user3349106

+1

有什麼錯誤嗎?您是否在期望值或異常中遇到錯誤? –

+0

你試過用調試器來調試你的代碼嗎?這個例子的預期和實際產出是多少? – amit

回答

0

這裏是一個修正版本:

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) 

考慮的要點:

  1. 而不是引入一些複雜—也錯—邏輯找到合取和析,你可以使用anyall。這是更清潔,不易發生錯誤。

  2. 自然對象遍歷是itertools.product([False, True], repeat = n),也就是說,升高到n功率可能的布爾值的設定[False, True]。換句話說,的n副本[False, True]Here是itertools.product的文檔。

  3. 我引入了更多的輸出來看看事情進展如何。下面是輸出我得到與Python3(Python2加括號,但版畫基本上是相同的):


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