Kyrexar

[SIN] sokoban.CLP

Oct 24th, 2018 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. (defglobal ?*nod-gen* = 0)
  2.  
  3. ;; (robot 4 1 cajas c 2 2 c 2 6 c 4 3 almacenes a 1 7 0 a 4 5 0 a 5 5 0 nivel 0)
  4. ;; (obs 1 4)(obs 3 1)(obs 3 4)(obs 3 5)(obs 3 8)(obs 4 4)(obs 4 5)
  5. ;; TABLERO
  6. ;; 1 2 3 4 5 6 7 8
  7. ;; 1 X A
  8. ;; 2 C C
  9. ;; 3 X X X X
  10. ;; 4 R C X A
  11. ;; 5 X A
  12.  
  13. ;; REGLAS DE MOVIMIENTO DEL ROBOT
  14.  
  15. (defrule derecha
  16. (robot ?y ?x cajas $?c1 almacenes $?a1 nivel ?nivel)
  17. (grid ?gy ?gx)
  18. (test(< ?x ?gx)) ;; no esta en el borde derecho
  19. (not (obs ?y =(+ ?x 1))) ;; no hay obstaculo a la derecha
  20. (test (not(member$ (create$ c ?y (+ ?x 1)) $?c1))) ;; no hay caja a la derecha
  21. (test (not(member$ (create$ a ?y (+ ?x 1)) $?a1))) ;; no hay almacen a la derecha
  22. (profundidad-maxima ?prof)
  23. (test (< ?nivel ?prof))
  24. =>
  25. (assert (robot ?y (+ ?x 1) cajas $?c1 almacenes $?a1 nivel (+ ?nivel 1)))
  26. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  27. )
  28.  
  29. (defrule izquierda
  30. (robot ?y ?x cajas $?c1 almacenes $?a1 nivel ?nivel)
  31. (grid ?gy ?gx)
  32. (test(> ?x 1)) ;; no esta en el borde izquierdo
  33. (not (obs ?y =(- ?x 1))) ;; no hay obstaculo a la izquierda
  34. (test (not(member$ (create$ c ?y (- ?x 1)) $?c1))) ;; no hay caja a la izquierda
  35. (test (not(member$ (create$ a ?y (- ?x 1)) $?a1))) ;; no hay almacen a la izquierda
  36. (profundidad-maxima ?prof)
  37. (test (< ?nivel ?prof))
  38. =>
  39. (assert (robot ?y (- ?x 1) cajas $?c1 almacenes $?a1 nivel (+ ?nivel 1)))
  40. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  41. )
  42.  
  43. (defrule arriba
  44. (robot ?y ?x cajas $?c1 almacenes $?a1 nivel ?nivel)
  45. (grid ?gy ?gx)
  46. (test (> ?y 1)) ;; no esta en el borde superior
  47. (not (obs =(- ?y 1) ?x)) ;; no hay obstaculo arriba
  48. (test (not(member$ (create$ c (- ?y 1) ?x) $?c1))) ;; no hay caja arriba
  49. (test (not(member$ (create$ a (- ?y 1) ?x) $?a1))) ;; no hay almacen arriba
  50. (profundidad-maxima ?prof)
  51. (test (< ?nivel ?prof))
  52. =>
  53. (assert (robot (- ?y 1) ?x cajas $?c1 almacenes $?a1 nivel (+ ?nivel 1)))
  54. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  55. )
  56.  
  57. (defrule abajo
  58. (robot ?y ?x cajas $?c1 almacenes $?a1 nivel ?nivel)
  59. (grid ?gy ?gx)
  60. (test (< ?y ?gy)) ;; no esta en el borde inferior
  61. (not (obs =(+ ?y 1) ?x)) ;; no hay obstaculo abajo
  62. (test (not(member$ (create$ c (+ ?y 1) ?x) $?c1))) ;; no hay caja abajo
  63. (test (not(member$ (create$ a (+ ?y 1) ?x) $?a1))) ;; no hay almacen abajo
  64. (profundidad-maxima ?prof)
  65. (test (< ?nivel ?prof))
  66. =>
  67. (assert (robot (+ ?y 1) ?x cajas $?c1 almacenes $?a1 nivel (+ ?nivel 1)))
  68. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  69. )
  70.  
  71. ;; REGLAS PARA EMPUJAR LA CAJA
  72.  
  73. (defrule empujarDerecha
  74. (robot ?y ?x cajas $?c1 c ?y =(+ ?x 1) $?c2 almacenes $?a1 nivel ?nivel)
  75. (grid ?gy ?gx)
  76. (test (< ?x (- ?gx 1)))
  77. ;;Comprobar si choca con algo
  78. (not (obs ?y =(+ ?x 2)))
  79. (test (not(or (member$ (create$ c ?y (+ ?x 2)) $?c1) (member$ (create$ c ?y (+ ?x 2)) $?c2))))
  80. (test (not(member$ (create$ a ?y (+ ?x 2)) $?a1)))
  81. ;;Comprobar nivel
  82. (profundidad-maxima ?prof)
  83. (test (< ?nivel ?prof))
  84. =>
  85. (assert (robot ?y (+ ?x 1) cajas $?c1 c ?y (+ ?x 2) $?c2 almacenes $?a1 nivel (+ ?nivel 1)))
  86. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  87. )
  88.  
  89. (defrule empujarIzquierda
  90. (robot ?y ?x cajas $?c1 c ?y =(- ?x 1) $?c2 almacenes $?a1 nivel ?nivel)
  91. (grid ?gy ?gx)
  92. (test (> ?x 2))
  93. ;;Comprobar si choca con algo
  94. (not (obs ?y =(- ?x 2)))
  95. (test (not(or (member$ (create$ c ?y (- ?x 2)) $?c1) (member$ (create$ c ?y (- ?x 2)) $?c2))))
  96. (test (not(member$ (create$ a ?y (- ?x 2)) $?a1)))
  97. ;;Comprobar nivel
  98. (profundidad-maxima ?prof)
  99. (test (< ?nivel ?prof))
  100. =>
  101. (assert (robot ?y (- ?x 1) cajas $?c1 c ?y (- ?x 2) $?c2 almacenes $?a1 nivel (+ ?nivel 1)))
  102. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  103. )
  104.  
  105. (defrule empujarArriba
  106. (robot ?y ?x cajas $?c1 c =(- ?y 1) ?x $?c2 almacenes $?a1 nivel ?nivel)
  107. (grid ?gy ?gx)
  108. (test (> ?y 2))
  109. ;;Comprobar si choca con algo
  110. (not (obs =(- ?y 2) ?x))
  111. (test (not(or (member$ (create$ c (- ?y 2) ?x) $?c1) (member$ (create$ c (- ?y 2) ?x) $?c2))))
  112. (test (not(member$ (create$ a (- ?y 2) ?x) $?a1)))
  113. ;;Comprobar nivel
  114. (profundidad-maxima ?prof)
  115. (test (< ?nivel ?prof))
  116. =>
  117. (assert (robot (- ?y 1) ?x cajas $?c1 c (- ?y 2) ?x $?c2 almacenes $?a1 nivel (+ ?nivel 1)))
  118. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  119. )
  120.  
  121. (defrule empujarAbajo
  122. (robot ?y ?x cajas $?c1 c =(+ ?y 1) ?x $?c2 almacenes $?a1 nivel ?nivel)
  123. (grid ?gy ?gx)
  124. (test (< ?y (- ?gy 1)))
  125. ;;Comprobar si choca con algo
  126. (not (obs =(+ ?y 2) ?x))
  127. (test (not(or (member$ (create$ c (+ ?y 2) ?x) $?c1) (member$ (create$ c (+ ?y 2) ?x) $?c2))))
  128. (test (not(member$ (create$ a (+ ?y 2) ?x) $?a1)))
  129. ;;Comprobar nivel
  130. (profundidad-maxima ?prof)
  131. (test (< ?nivel ?prof))
  132. =>
  133. (assert (robot (+ ?y 1) ?x cajas $?c1 c (+ ?y 2) ?x $?c2 almacenes $?a1 nivel (+ ?nivel 1)))
  134. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  135. )
  136.  
  137. ;; REGLAS PARA METER CAJAS EN ALMACEN
  138.  
  139. (defrule MeterDerecha
  140. (robot ?y ?x cajas $?c1 c ?y =(+ ?x 1) $?c2 almacenes $?a1 a ?y =(+ ?x 2) 0 $?a2 nivel ?nivel)
  141. (profundidad-maxima ?prof)
  142. (test (< ?nivel ?prof))
  143. =>
  144. (assert (robot ?y (+ ?x 1) cajas $?c1 $?c2 almacenes $?a1 a ?y (+ ?x 2) 1 $?a2 nivel (+ ?nivel 1)))
  145. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  146. )
  147.  
  148. (defrule MeterIzquierda
  149. (robot ?y ?x cajas $?c1 c ?y =(- ?x 1) $?c2 almacenes $?a1 a ?y =(- ?x 2) 0 $?a2 nivel ?nivel)
  150. (profundidad-maxima ?prof)
  151. (test (< ?nivel ?prof))
  152. =>
  153. (assert (robot ?y (- ?x 1) cajas $?c1 $?c2 almacenes $?a1 a ?y (- ?x 2) 1 $?a2 nivel (+ ?nivel 1)))
  154. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  155. )
  156.  
  157. (defrule MeterAbajo
  158. (robot ?y ?x cajas $?c1 c =(+ ?y 1) ?x $?c2 almacenes $?a1 a =(+ ?y 2) ?x 0 $?a2 nivel ?nivel)
  159. (profundidad-maxima ?prof)
  160. (test (< ?nivel ?prof))
  161. =>
  162. (assert (robot (+ ?y 1) ?x cajas $?c1 $?c2 almacenes $?a1 a (+ ?y 2) ?x 1 $?a2 nivel (+ ?nivel 1)))
  163. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  164. )
  165.  
  166. (defrule MeterArriba
  167. (robot ?y ?x cajas $?c1 c =(- ?y 1) ?x $?c2 almacenes $?a1 a =(- ?y 2) ?x 0 $?a2 nivel ?nivel)
  168. (profundidad-maxima ?prof)
  169. (test (< ?nivel ?prof))
  170. =>
  171. (assert (robot (- ?y 1) ?x cajas $?c1 $?c2 almacenes $?a1 a (- ?y 2) ?x 1 $?a2 nivel (+ ?nivel 1)))
  172. (bind ?*nod-gen* (+ ?*nod-gen* 1))
  173. )
  174.  
  175. ;; REGLAS FINALES
  176.  
  177. (defrule objetivo
  178. (declare (salience 100))
  179. ?f<- (robot ?y ?x cajas almacenes $?a1 nivel ?nivel)
  180.  
  181. =>
  182. (printout t "SOLUCION ENCONTRADA EN EL NIVEL " ?nivel crlf)
  183. (printout t "NUMERO DE NODOS EXPANDIDOS O REGLAS DISPARADAS " ?*nod-gen* crlf)
  184. (printout t "HECHO OBJETIVO " ?f crlf)
  185.  
  186. (halt)
  187. )
  188.  
  189. (defrule no_solucion
  190. (declare (salience -99))
  191. (puzzle $? nivel ?n $?)
  192.  
  193. =>
  194. (printout t "SOLUCION NO ENCONTRADA" crlf)
  195. (printout t "NUMERO DE NODOS EXPANDIDOS O REGLAS DISPARADAS " ?*nod-gen* crlf)
  196.  
  197. (halt)
  198. )
  199.  
  200. (deffunction inicio ()
  201. (reset)
  202. (printout t "Profundidad Maxima:= " )
  203. (bind ?prof (read))
  204. (printout t "Tipo de Busqueda " crlf " 1.- Anchura" crlf " 2.- Profundidad" crlf )
  205. (bind ?a (read))
  206. (if (= ?a 1)
  207. then (set-strategy breadth)
  208. else (set-strategy depth))
  209. (printout t " Ejecuta run para poner en marcha el programa " crlf)
  210. (assert (profundidad-maxima ?prof))
  211. (assert (grid 5 8))
  212. (assert (obs 3 1))
  213. (assert (obs 1 4))
  214. (assert (obs 3 4))
  215. (assert (obs 4 4))
  216. (assert (obs 5 4))
  217. (assert (obs 3 5))
  218. (assert (obs 3 8))
  219. (assert (robot 4 1 cajas c 2 2 c 2 6 c 4 3 almacenes a 1 7 0 a 4 5 0 a 5 5 0 nivel 0))
  220. )
Add Comment
Please, Sign In to add comment