Commits

Pierre Carbonnelle  committed 313364b

improve resolution algorithm in threadSafe

  • Participants
  • Parent commits 70b38e3

Comments (0)

Files changed (1)

File pyDatalog/examples/threadSafe.py

 import threading
 
 pyDatalog.create_atoms('N,X0,X1,X2,X3,X4,X5,X6,X7')
-pyDatalog.create_atoms('ok,queens0,queens1,queens2,queens3,queens4,queens5,queens6,queens7')
+pyDatalog.create_atoms('ok,queens, next_queen')
 
 @pyDatalog.predicate()
 def ok3(X1, N, X2):
     n = int(random.random() * 8) + 1 # 1 to 8
     pyDatalog.clear()
     
-    queens0(X0) <= X0._in(range(n))
-    queens1(X0,X1) <= queens0(X0) & queens0(X1) & ok(X1,1,X0)
-    queens2(X0,X1,X2) <= queens1(X0,X1) & queens1(X1,X2) & ok(X0,2,X2)
-    queens3(X0,X1,X2,X3) <= queens2(X0,X1,X2) & queens2(X1,X2,X3) & ok(X0,3,X3)
-    queens4(X0,X1,X2,X3,X4) <= queens3(X0,X1,X2,X3) & queens3(X1,X2,X3,X4) & ok(X0,4,X4)
-    queens5(X0,X1,X2,X3,X4,X5) <= queens4(X0,X1,X2,X3,X4) & queens4(X1,X2,X3,X4,X5) & ok(X0,5,X5)
-    queens6(X0,X1,X2,X3,X4,X5,X6) <= queens5(X0,X1,X2,X3,X4,X5) & queens5(X1,X2,X3,X4,X5,X6) & ok(X0,6,X6)
-    queens7(X0,X1,X2,X3,X4,X5,X6,X7) <= queens6(X0,X1,X2,X3,X4,X5,X6) & queens6(X1,X2,X3,X4,X5,X6,X7) & ok(X0,7,X7)
+    queens(X0)                      <= (X0._in(range(n)))
+    queens(X0,X1)                   <= queens(X0)                   & next_queen(X0,X1)
+    queens(X0,X1,X2)                <= queens(X0,X1)                & next_queen(X0,X1,X2)
+    queens(X0,X1,X2,X3)             <= queens(X0,X1,X2)             & next_queen(X0,X1,X2,X3)
+    queens(X0,X1,X2,X3,X4)          <= queens(X0,X1,X2,X3)          & next_queen(X0,X1,X2,X3,X4)
+    queens(X0,X1,X2,X3,X4,X5)       <= queens(X0,X1,X2,X3,X4)       & next_queen(X0,X1,X2,X3,X4,X5)
+    queens(X0,X1,X2,X3,X4,X5,X6)    <= queens(X0,X1,X2,X3,X4,X5)    & next_queen(X0,X1,X2,X3,X4,X5,X6)
+    queens(X0,X1,X2,X3,X4,X5,X6,X7) <= queens(X0,X1,X2,X3,X4,X5,X6) & next_queen(X0,X1,X2,X3,X4,X5,X6,X7)
     
-    queens = pyDatalog.ask("queens%s(%s)" % (n-1, ",".join("X%s" % i for i in range(n))))
-    answers = queens.answers if queens else [] 
+    next_queen(X0,X1)                   <= queens(X1)                       & ok(X0,1,X1)
+    next_queen(X0,X1,X2)                <= next_queen(X1,X2)                & ok(X0,2,X2)
+    next_queen(X0,X1,X2,X3)             <= next_queen(X1,X2,X3)             & ok(X0,3,X3)
+    next_queen(X0,X1,X2,X3,X4)          <= next_queen(X1,X2,X3,X4)          & ok(X0,4,X4)
+    next_queen(X0,X1,X2,X3,X4,X5)       <= next_queen(X1,X2,X3,X4,X5)       & ok(X0,5,X5)
+    next_queen(X0,X1,X2,X3,X4,X5,X6)    <= next_queen(X1,X2,X3,X4,X5,X6)    & ok(X0,6,X6)
+    next_queen(X0,X1,X2,X3,X4,X5,X6,X7) <= next_queen(X1,X2,X3,X4,X5,X6,X7) & ok(X0,7,X7)
+    
+    query = pyDatalog.ask("queens(%s)" % (",".join("X%s" % i for i in range(n))))
+    answers = query.answers if query else [] 
     result = "OK" if len(answers) == [1,0,0,2,10,4,40,92][n-1] else "* not OK ! *"
     print("%s : n = %d %s " % (thread_name, n, result))