Advertisement
Guest User

Arc Consistency in J

a guest
Jun 27th, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 5.74 KB | None | 0 0
  1. NB. ----------------------------------------------------------------------------
  2. NB. Arc consistency for constraint satisfaction problems (CSP).
  3. NB. ----------------------------------------------------------------------------
  4. NB. Michal Dobrogost 2012-11-03
  5. NB.
  6. NB. A CSP can model various satisfaction problems such as sudoku puzzles.
  7. NB. A CSP consists of a set of variables X, each with an associated domain D of
  8. NB. values. In sudoku, X = {1,2,...,81} and for each x in X, Dx = {1,2,...,9}.
  9. NB. There are also a set of constraints C between pairs of variables.
  10. NB. In sudoku, Cxy is simply the 'not equal' constraint and applies to all pairs
  11. NB. of variables in rows, columns, and 3x3 squares.
  12. NB.
  13. NB. Arc consistency (AC) is a filtering algorithm that removes values, from the
  14. NB. domain of a variable, that can not participate in any solution. In sudoku,
  15. NB. if a variable x1 has domain {1,2,6} and is on the same row as x2 with domain
  16. NB. {6} we can remove the value 6 from the domain of x1 because there can not
  17. NB. be a solution where Dx1 = Dx2 = {6}.
  18. NB.
  19. NB. Filtering algorithms are usually combined with backtracking search which
  20. NB. guesses a specific value for a variable with multiple available values.
  21. NB. Guesses are interleaved with filtering (commonly refered to as maintaining
  22. NB. arc consistency (MAC) search).
  23. NB.
  24. NB. Thanks to:
  25. NB.   Raul M., Mike D., programming@jsoftware.com for many suggestions.
  26.  
  27. load 'format/printf'
  28.  
  29. NB. ----------------------------------------------------------------------------
  30. NB. Generate a random CSP instance and filter it.
  31.  
  32. NB. Constants
  33. n=. 4 NB. # variables
  34. d=. 4 NB. domain size
  35.  
  36. NB.* 'D' is the domain of each variable, one row per variable.  We generate the
  37. NB.*     domains so that each one is half full.  An empty domain would signify
  38. NB.*     that the instance is not consistent to begin with so we avoid that.
  39. ] D=. (n%2) > (?]) n$d
  40.  
  41. NB. a single constraint (note: constraints may be asymmetric, for example x<y).
  42. c1=. ? (d,d)$2
  43. 2 2 $ '' ; 'Dx' ; 'Dy' ; c1
  44.  
  45. NB.* 'C' is all of our constraints with the 0'th entry being the empty.
  46. ] C=. a: , c1 ; (|:c1)
  47.  
  48. NB.* 'A' an adjacency matrix (which constraint is between a pair of variables).
  49. A=. ? (2$n)$2          NB. generate random matrix of [0,1]
  50. A=. A *. (i.n) </ i.n  NB. make it upper diagonal, zeros on diagonal
  51. ] A=. A + |: 2*A       NB. make it symmetrix referencing transpose in C.
  52. 2 2 $ '' ; 'y' ; 'x' ; A
  53.  
  54. NB. ----------------------------------------------------------------------------
  55. NB. Utility functions.
  56.  
  57. NB.* 'adj A' computes adjacency list from an adjacency matrix.
  58. adj=: < @ I. @: *
  59. NB.* 'a' is an adjacency list: which variables are adjacent to a given variable.
  60. ] a=. adj A
  61.  
  62. NB.* 'xs arcsX A' computes a list of all adjacent variables to those in xs.
  63. NB.* 'ys arcsY A' computes a list of all adjacent variables to those in ys.
  64. arcsX=: [  ((#~ #@>) ,. ;@]) {
  65. arcsY=: [  (;@] ,. (#~ #@>)) {
  66. ] (i.n) arcsX a
  67. ] (i.>.n%2) arcsY a
  68.  
  69. NB. Mike D.'s compute arcs directly from A
  70. NB. arcsA=: ((]#~ (e.~ {."1))   ($#: I.@,)) *
  71.  
  72. NB.* 'c revDom (Dx,Dy)' will return the domain of y supported by x.
  73. getx=: 0{ ]
  74. gety=: 1{ ]
  75. revDom=: getx *. +./ @ (gety # [)
  76.  
  77. NB.* 'isValid D' decides if all domains are non-empty.
  78. isValid=: <./ @: (>./"1)
  79.  
  80. NB.* 'allAssigned D' is true if all domains have exactly one value.
  81. allAssigned=: 1&= @ */ @: (+/"1)
  82.  
  83. NB. ----------------------------------------------------------------------------
  84. NB. The arc consistency algorithm itself.
  85.  
  86. NB.* '(A;a;<C) revise (ys;D)' filter domains of all variables (xs) adjacent to
  87. NB.*                          ys. Returns (newXs;newD) where newXs are those
  88. NB.*                          variables that are different across newD and D.
  89. revise=: 4 : 0
  90.     'A a C'=. x
  91.     'ys D'=. y
  92.        
  93.     if. 0 < # ys
  94.     do.
  95.         arcs=. ys arcsY a
  96.         ax=. 0{"1 arcs
  97.         xs=. ~. ax
  98.         if. 0 = # xs do.
  99.             (0#0);D
  100.         else.
  101.            
  102.  
  103.             NB. revLookup [x,y]  <=>  Cxy revDom (Dx;Dy)
  104.             revLookup=. (> @ {&C @ {::&A) revDom ({&D)
  105.  
  106.             NB. produce modified domains and the variables they correspond to.
  107.             newD=. ax *.//. revLookup"1 arcs
  108.  
  109.             ((newD ([: >./"1 ~:) xs{D) # xs) ; (newD xs} D)
  110.         end.
  111.     else.
  112.         (0$0) ; D
  113.     end.
  114. )
  115.    
  116. NB.* '(A;a;<C) ac (ys;D)' return D filtered into an arc consistent state given
  117. NB.*                      that the the variables in ys have been changed.
  118. NB.*                      Use (i.#D);D as right argument to filter from scratch.
  119. ac=: 4 : 0
  120.     > (1&{) ((x&revise)^:_) y
  121. )
  122.  
  123. NB. ----------------------------------------------------------------------------
  124. NB. Filter the random instance.
  125.  
  126. NB. Recall the random instance.
  127. (A;a;<C)
  128. D
  129.  
  130. NB. Filter the random instance.
  131. (A;a;<C) ac ((i.#D);D)
  132.  
  133. NB. Are all the filtered domains non-empty?
  134. isValid ((A;a;<C) ac ((i.#D);D))
  135.  
  136.  
  137. NB. ----------------------------------------------------------------------------
  138. NB. Search
  139.  
  140. search=: 4 : 0
  141.     if. allAssigned >1{y do.
  142.         > 1{y  
  143.     else.
  144.         D=. x ac y
  145.  
  146.         if. -. isValid D do.
  147.             D
  148.         else.
  149.             if. allAssigned D do.
  150.                 D
  151.             else.
  152.                 nonEmpties=. 1< +/"1
  153.                 var=. {. I. nonEmpties D       
  154.                 ret=. ($D) $ 0
  155.                 for_i. I. var{D do.
  156.                     D2=. (1 i} ((1{$D) # 0)) var} D
  157.                     D2=. x search ((1#var) ; D2)
  158.                     if. allAssigned D2 do.
  159.                         ret =. D2
  160.                         break.
  161.                     end.
  162.                 end.
  163.                 ret
  164.             end.
  165.         end.
  166.     end.
  167. )
  168.  
  169. (A;a;<C) search (i.#D);D
  170.  
  171.  
  172. NB. ----------------------------------------------------------------------------
  173. NB. Raul M.'s brute force sudoku instance. (doesn't work for me).
  174.  
  175. NB. C=: , |: 9#,:i.9 NB. identity within columns (C)
  176. NB. R=: ,9#,:i.9 NB. identity within rows (R)
  177. NB. B=: ,3#3#"1 i.3 3 NB. identity within boxes
  178. NB. A=: ((2*>/~i.81)+</~i.81) * (+. |:) (C =/ R) +. (C =/B ) +. (R =/ B)
  179. NB. D=. 81 9 $ 1
  180. NB.
  181. NB. (A;(adj A);<C) search (i.#D);D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement