fork download
  1. (defglobal ?*start-position* = (create$ 5 2))
  2. (defglobal ?*winning-distance* = 13)
  3.  
  4. (deftemplate node
  5. (slot position)
  6. (slot g)
  7. (slot h)
  8. (slot f))
  9.  
  10. (deffunction distance (?x ?y)
  11. (return (sqrt (+ (* ?x ?x) (* ?y ?y)))))
  12.  
  13. (deffunction heuristic (?x ?y)
  14. (return (- ?*winning-distance* (distance ?x ?y))))
  15.  
  16. (deffunction generate-children (?node)
  17. (bind ?pos (fact-slot-value ?node position))
  18. (bind ?x (nth 1 ?pos))
  19. (bind ?y (nth 2 ?pos))
  20. (return (create$ (create$ (+ ?x 3) ?y)
  21. (create$ ?x (+ ?y 3))
  22. (create$ ?x (+ ?y 4)))))
  23.  
  24. (defrule init
  25. (initial-fact)
  26. =>
  27. (assert (node (position ?*start-position*)
  28. (g 0)
  29. (h (heuristic (nth 1 ?*start-position*) (nth 2 ?*start-position*)))
  30. (f (heuristic (nth 1 ?*start-position*) (nth 2 ?*start-position*))))))
  31.  
  32. (defrule expand-node
  33. ?node <- (node (position ?pos)
  34. (g ?g)
  35. (h ?h)
  36. (f ?f))
  37. =>
  38. (bind ?children (generate-children ?node))
  39. (foreach ?child ?children
  40. (bind ?x (nth 1 ?child))
  41. (bind ?y (nth 2 ?child))
  42. (bind ?dist (distance ?x ?y))
  43. (if (>= ?dist ?*winning-distance*)
  44. then
  45. (printout t "Winning position: " ?child crlf)
  46. (halt))
  47. (assert (node (position ?child)
  48. (g (+ ?g 1))
  49. (h (heuristic ?x ?y))
  50. (f (+ (+ ?g 1) (heuristic ?x ?y))))))))
  51.  
  52. (defrule display-tree
  53. ?node <- (node (position ?pos) (g ?g) (h ?h) (f ?f))
  54. =>
  55. (printout t "Node: " ?pos ", g: " ?g ", h: " ?h ", f: " ?f crlf))
  56. (reset)
  57. (run)
  58. ; your code goes here
  59.  
  60. (exit)
  61. ; empty line at the end
Success #stdin #stdout 0.01s 5500KB
stdin
Standard input is empty
stdout
[TMPLTFUN2] Attempted to assert a multifield value 
into the single field slot position of deftemplate node.
[PRCCODE4] Execution halted during the actions of defrule init.