Guest User

arc consistency in J #2

a guest
Nov 3rd, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 4.37 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. for better arcsX, arcsY.
  26. NB.   [email protected] for misc suggestions.
  27.  
  28. NB. ----------------------------------------------------------------------------
  29. NB. Generate a random CSP instance and filter it.
  30.  
  31. NB. Constants
  32. n=. 4 NB. # variables
  33. d=. 4 NB. domain size
  34.  
  35. NB.* 'D' is the domain of each variable, one row per variable.
  36. ] D=. (n%2) > (?]) n$d
  37.  
  38. NB. a single constraint (note: constraints are not symmetric, for example x<y).
  39. c1=. 0= ? (2$d)$(>. d%2)
  40. 2 2 $ a: , 'Dx' ; 'Dy' ; c1
  41.  
  42. NB.* 'C' is all of our constraints with the 0'th entry being the empty.
  43. ] C=. a: , c1 ; (|:c1)
  44.  
  45. NB.* 'A' an adjacency matrix (which constraint is between a pair of variables).
  46. A=. 0= ? (2$n)$2       NB. generate random matrix of [0,1]
  47. A=. A *. (i.n) </ i.n  NB. make it upper diagonal, zeros on diagonal
  48. ] A=. A + |: 2*A       NB. make it symmetrix referencing transpose in C.
  49. 2 2 $ a: , 'y' ; 'x' ; A
  50.  
  51. NB. ----------------------------------------------------------------------------
  52. NB. Utility functions.
  53.  
  54. NB.* 'adj A' computes adjacency list from an adjacency matrix.
  55. adj=: ((<@#)&(i.n)) @ (0&<)
  56. NB.* 'a' is an adjacency list: which variables are adjacent to a given variable.
  57. ] a=. adj A
  58.  
  59. NB.* 'xs arcsX A' computes a list of all adjacent variables to those in xs.
  60. NB.* 'ys arcsY A' computes a list of all adjacent variables to those in ys.
  61. arcsX=: [  ((#~ #@>) ,. ;@]) {
  62. arcsY=: [  (;@] ,. (#~ #@>)) {
  63. ] (i.n) arcsX a
  64. ] (i.>.n%2) arcsY a
  65.  
  66. NB.* 'c revDom (Dx,Dy)' will return the domain of y supported by x.
  67. getx=: 0{ ]
  68. gety=: 1{ ]
  69. revDom=: getx *. +./ @ (gety # [)
  70.  
  71. NB.* 'isValid D' decides if all domains are non-empty.
  72. isValid=: <./ @: (>./"1)
  73.  
  74. NB. ----------------------------------------------------------------------------
  75. NB. The arc consistency algorithm itself.
  76.  
  77. NB.* '(A;a;<C) revise (xs;D)' filter domains of all variables (ys) adjacent to
  78. NB.                           xs. Returns (newYs;newD) where newYs are those
  79. NB.*                          variables that are different across newD and D.
  80. revise=: 4 : 0
  81.     A=. > 0{x
  82.     a=. > 1{x
  83.     C=. > 2{x
  84.    
  85.     ys=. > 0{y
  86.     D=.  > 1{y
  87.        
  88.     if. 0 < # ys
  89.     do.
  90.         arcs=. ({~ /:) ys arcsY a
  91.         ax=. 0{"1 arcs
  92.         xs=. ~. ax
  93.  
  94.         NB. revLookup [x,y]  <=>  Cxy revDom (Dx;Dy)
  95.         revLookup=. (> @ {&C @ {::&A) revDom ({&D)
  96.  
  97.         NB. produce modified domains and the variables they correspond to.
  98.         newD=. ax *.//. revLookup"1 arcs
  99.  
  100.         ((newD ([: >./"1 ~:) xs{D) # xs) ; (newD xs} D)
  101.     else.
  102.         (0$0) ; D
  103.     end.
  104. )
  105.    
  106. NB.* '(A;a;<C) ac (xs;D)' return D filtered into an arc consistent state given
  107. NB.*                      that the the variables in xs have been changed.
  108. NB.*                      Use (i.#D);D as right argument to filter from scratch.
  109. ac=: 4 : 0
  110.     > (1&{) ((x&revise)^:_) y
  111. )
  112.  
  113. NB. ----------------------------------------------------------------------------
  114. NB. Filter the random instance.
  115.  
  116. NB. Recall the random instance.
  117. (A;a;<C)
  118. D
  119.  
  120. NB. Filter the random instance.
  121. ] D=. (A;a;<C) ac ((i.#D);D)
  122.  
  123. NB. Are all the filtered domains non-empty?
  124. isValid D
Advertisement
Add Comment
Please, Sign In to add comment