Advertisement
Guest User

arc consistency in J

a guest
Oct 31st, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 2.39 KB | None | 0 0
  1. NB. Michal Dobrogost 2012-10-30
  2.  
  3. NB. ----------------------------------------------------------------------------
  4. NB. Generate a random CSP instance.
  5.  
  6. NB. Constants
  7. n =: 4 NB. # variables
  8. d =: 4 NB. domain size
  9.  
  10. NB. all variable domains (each one half full).
  11. ] D =: (n%2)> (]?]) n$d
  12.  
  13. NB. Our single constraint (note: constraints are not symmetric, for example x<y).
  14. c1 =: 0= ? (2$d)$(>. d%2)
  15. 2 2 $ a: , 'x' ; 'y' ; c1
  16.  
  17. NB. all constraints.
  18. ] C =: a: , c1 ; (|:c1)
  19.  
  20. NB. adjacency matrix (pairs of variables are constrained by which constraint)
  21. X   =: 0= ? (2$n)$2       NB. generate random matrix of [0,1]
  22. X   =: X *. (i.n) </ i.n  NB. make it upper diagonal, zeros on diagonal
  23. ] X =: X + |: 2*X         NB. make it symmetrix referencing transpose in C.
  24.  
  25. NB. ----------------------------------------------------------------------------
  26. NB. Generally useful functions.
  27.  
  28. NB. Compute adjacency list from an adjacency matrix.
  29. adj =: ((<@#)&(i.n)) @ (0&<)
  30. ] A =: adj X
  31.  
  32. NB.'xs arcsX A' computes a list of all adjacent variables to those in xs.
  33. NB.`ys arcsY A' computes a list of all adjacent variables to those in ys.
  34. arcsX =: [: (> @ (,&.>/)) ([ (,.&.>)"0 {)
  35. arcsY =: ({~ /:) @ (0 1&|.) @ arcsX
  36. ] (i.n) arcsX A
  37. ] (i.>.n%2) arcsY A
  38.  
  39. NB.'c revDom (Dx,Dy)' will return the domain of y supported by x.
  40. getx   =: 0{ ]
  41. gety   =: 1{ ]
  42. revDom =: getx *. +./ @ (gety # [)
  43.  
  44. NB.'isValid D' Decides if all variables have at least one value in their domain.
  45. isValid =. <./ @: (>./"1)
  46.  
  47. NB. ----------------------------------------------------------------------------
  48. NB.'revise (xs;D)' will revise all domains of variables adjacent to those in xs
  49. NB.                and return (newXs;newD) where newXs are those variables that
  50. NB.                have been modified between D and newD.
  51. revise =: 3 : 0
  52.     ys =: (> 0{y)
  53.     D  =. > 1{y
  54.     if. 0 < # ys
  55.     do.
  56.         a  =. ys arcsY A
  57.         ax =. 0{"1 a
  58.         xs =. ~. ax
  59.  
  60.         NB. revLookup [x,y]  <=>  Cxy revDom (Dx;Dy)
  61.         revLookup =. (> @ {&C @ {::&X) revDom ({&D)
  62.  
  63.         NB. produce modified domains and the variables they correspond to.
  64.         newD =. ax *.//. revLookup"1 a
  65.  
  66.         ((newD ([: >./"1 -.@=) xs{D) # xs) ; (newD xs} D)
  67.     else.
  68.         (0$0) ; D
  69.     end.
  70. )
  71.  
  72. ac =: > @ (1&{) @ (revise^:_) @ ((i.n)&;)
  73.  
  74. NB. ----------------------------------------------------------------------------
  75. NB. Let's run arc consistency on our generated data.
  76. D
  77. isValid D
  78. ] D =. ac D
  79. isValid D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement