test-chrest-2.lisp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. ;;; Tests for mini Chrest architecture version 2
  2. ;;; written by Peter Lane, November 2007 - February 2008
  3. (load "chrest-2")
  4. (load "../Utilities/test-framework")
  5. ;; tests to check internal functions in the code
  6. (def-unit-tests presequence-tests ()
  7. (assert-true (presequence-p () ()))
  8. (assert-true (presequence-p () '(1 2 3)))
  9. (assert-false (presequence-p '(1 2 3) ()))
  10. (assert-true (presequence-p '(1) '(1)))
  11. (assert-true (presequence-p '(1 2) '(1 2)))
  12. (assert-true (presequence-p '(1) '(1 2 3)))
  13. (assert-true (presequence-p '(1 2) '(1 2 3)))
  14. (assert-false (presequence-p '(1 2 3) '(1 2))))
  15. (def-unit-tests remove-presequence-tests ()
  16. (assert-equalp () (remove-matching-presequence () '(1 2 3)))
  17. (assert-equalp '(1 2 3) (remove-matching-presequence '(1 2 3) ()))
  18. (assert-equalp '(2 3) (remove-matching-presequence '(1 2 3) '(1)))
  19. (assert-equalp '(2 3) (remove-matching-presequence '(1 2 3) '(1 3))))
  20. ;; tests to check verbal-learning pattern
  21. (def-unit-tests verbal-equal-pattern-tests ()
  22. (let ((a (make-instance 'vl-pattern :data ()))
  23. (b (make-instance 'vl-pattern :data '(1)))
  24. (c (make-instance 'vl-pattern :data '(1 2)))
  25. (d (make-instance 'vl-pattern :data '(1 2 3))))
  26. (assert-true (equal-patterns-p a a))
  27. (assert-false (equal-patterns-p a d))
  28. (assert-false (equal-patterns-p d a))
  29. (assert-true (equal-patterns-p b b))
  30. (assert-true (equal-patterns-p c c))
  31. (assert-false (equal-patterns-p b d))
  32. (assert-false (equal-patterns-p c d))
  33. (assert-false (equal-patterns-p d c))))
  34. (def-unit-tests verbal-matching-pattern-tests ()
  35. (let ((a (make-instance 'vl-pattern :data ()))
  36. (b (make-instance 'vl-pattern :data '(a)))
  37. (c (make-instance 'vl-pattern :data '(a b)))
  38. (d (make-instance 'vl-pattern :data '(a b c))))
  39. (assert-true (matching-patterns-p a a))
  40. (assert-true (matching-patterns-p a d))
  41. (assert-false (matching-patterns-p d a))
  42. (assert-true (matching-patterns-p b b))
  43. (assert-true (matching-patterns-p c c))
  44. (assert-true (matching-patterns-p b d))
  45. (assert-true (matching-patterns-p c d))
  46. (assert-false (matching-patterns-p d c))))
  47. (def-unit-tests verbal-get-next-item-tests ()
  48. (let ((a (make-instance 'vl-pattern :data ()))
  49. (b (make-instance 'vl-pattern :data '(a)))
  50. (c (make-instance 'vl-pattern :data '(b)))
  51. (d (make-instance 'vl-pattern :data '(a b)))
  52. (e (make-instance 'vl-pattern :data '(a c))))
  53. (assert-true (equal-patterns-p a (get-next-item a a)))
  54. (assert-true (equal-patterns-p a (get-next-item a b)))
  55. (assert-true (equal-patterns-p c (get-next-item d b)))
  56. (assert-true (equal-patterns-p c (get-next-item d e)))))
  57. (def-unit-tests verbal-combine-pattern-tests ()
  58. (assert-true (equal-patterns-p (make-instance 'vl-pattern :data '(1 2 3))
  59. (combine-patterns (make-instance 'vl-pattern :data '(1))
  60. (make-instance 'vl-pattern :data '(2 3))))))
  61. ;; tests confirming the core processes within the architecture
  62. (def-process-tests recognise-tests ()
  63. (let ((bif (make-instance 'vl-pattern :data '(B I F)))
  64. (bef (make-instance 'vl-pattern :data '(B E F)))
  65. (vif (make-instance 'vl-pattern :data '(I F)))
  66. (bi (make-instance 'vl-pattern :data '(B I)))
  67. (b (make-instance 'vl-pattern :data '(B)))
  68. (i (make-instance 'vl-pattern :data '(I)))
  69. (g (make-instance 'vl-pattern :data '(G))))
  70. (let ((empty-model (create-chrest)))
  71. (assert-eq (chrest-ltm empty-model)
  72. (recognise-pattern empty-model bif)))
  73. (let* ((node-1 (make-node :contents bi :image bi :children nil))
  74. (link-1 (make-link :test i :child node-1))
  75. (node-2 (make-node :contents b :image bif :children (list link-1)))
  76. (link-2 (make-link :test b :child node-2))
  77. (node-3 (make-node :contents i :image vif :children nil))
  78. (link-3 (make-link :test i :child node-3))
  79. (root-node (make-node :contents nil :image nil :children (list link-2 link-3)))
  80. (model (create-chrest)))
  81. (setf (chrest-ltm model) root-node) ; point at test root-node
  82. (assert-eq node-1 (recognise-pattern model bif))
  83. (assert-eq node-2 (recognise-pattern model b))
  84. (assert-eq node-2 (recognise-pattern model bef))
  85. (assert-eq node-3 (recognise-pattern model i))
  86. (assert-eq root-node (recognise-pattern model g)))))
  87. (def-process-tests familiarise-tests ()
  88. "Use verbal patterns to test familiarisation"
  89. (let ((model (create-chrest))
  90. (node (make-node :contents (make-instance 'vl-pattern)
  91. :image (make-instance 'vl-pattern)
  92. :children nil))
  93. (pattern (make-instance 'vl-pattern :data '(B I F))))
  94. (assert-true (empty-pattern-p (node-image node)))
  95. (familiarise model node pattern)
  96. (assert-true (equal-patterns-p (make-instance 'vl-pattern :data '(B))
  97. (node-image node)))
  98. (familiarise model node pattern)
  99. (assert-true (equal-patterns-p (make-instance 'vl-pattern :data '(B I))
  100. (node-image node)))
  101. (familiarise model node pattern)
  102. (assert-true (equal-patterns-p pattern (node-image node)))
  103. (familiarise model node pattern)
  104. (assert-true (equal-patterns-p pattern (node-image node)))))
  105. (def-process-tests discriminate-tests ()
  106. (let ((node (make-node :contents (make-instance 'vl-pattern :data '(B I))
  107. :image (make-instance 'vl-pattern :data '(B I F))
  108. :children nil))
  109. (pattern (make-instance 'vl-pattern :data '(B I G))))
  110. (discriminate (create-chrest) node pattern)
  111. (assert-false (null (node-children node)))
  112. (assert-false (eq node (recognise-pattern (create-chrest) pattern node)))
  113. (assert-true (equal-patterns-p (make-instance 'vl-pattern :data '(G))
  114. (link-test (car (node-children node)))))
  115. (assert-true (equal-patterns-p pattern (node-contents (recognise-pattern (create-chrest) pattern node))))))
  116. (def-process-tests recognise-and-learn-tests ()
  117. (let ((model (create-chrest))
  118. (pattern-a (make-instance 'vl-pattern :data '(B I F)))
  119. (pattern-b (make-instance 'vl-pattern :data '(X A Q))))
  120. (dotimes (n 4)
  121. (recognise-and-learn-pattern model pattern-a)
  122. (recognise-and-learn-pattern model pattern-b))
  123. (assert-true (equal-patterns-p pattern-a (recall-pattern model pattern-a)))
  124. (assert-true (equal-patterns-p pattern-b (recall-pattern model pattern-b)))))
  125. (def-process-tests timing-test ()
  126. (let ((model (create-chrest))
  127. (pattern-a (make-instance 'vl-pattern :data '(B I F)))
  128. (pattern-b (make-instance 'vl-pattern :data '(X A Q))))
  129. (setf (chrest-familiarisation-time model) 2000)
  130. (setf (chrest-discrimination-time model) 10000)
  131. (assert= 0 (chrest-clock model))
  132. ;; check changed on one learning operation
  133. (recognise-and-learn-pattern model pattern-a)
  134. (assert= 10000 (chrest-clock model))
  135. ;; check changed on other learning operation
  136. (recognise-and-learn-pattern model pattern-a)
  137. (assert= 12000 (chrest-clock model))
  138. ;; check a busy model is not changed
  139. (recognise-and-learn-pattern model pattern-b 10000)
  140. (assert= 12000 (chrest-clock model))
  141. (assert-eq (chrest-ltm model) (recognise-pattern model pattern-b))
  142. ;; check model updates to time of current input pattern
  143. (recognise-and-learn-pattern model pattern-a 20000)
  144. (assert= 22000 (chrest-clock model))))
  145. ;; -- for short-term memory
  146. (def-unit-tests take-tests ()
  147. (assert-null (take 0 '(1 2 3)))
  148. (assert-null (take 10 ()))
  149. (assert-equalp '(1 2 3) (take 3 '(1 2 3 4)))
  150. (assert-equalp '(1 2 3) (take 3 '(1 2 3)))
  151. (assert-equalp '(1 2) (take 3 '(1 2))))
  152. (def-process-tests stm-tests ()
  153. (let ((model (create-chrest))
  154. (pattern-a (make-instance 'vl-pattern :data '(B I F))))
  155. (assert-true (visual-pattern-p pattern-a))
  156. (recognise-pattern model pattern-a)
  157. (assert-eq (chrest-ltm model) (car (fixed-queue-items (stm-visual (chrest-stm model)))))
  158. (recognise-and-learn-pattern model pattern-a)
  159. (assert-true (and (= 2 (length (fixed-queue-items (stm-visual (chrest-stm model)))))
  160. (eq (chrest-ltm model) (cadr (fixed-queue-items (stm-visual (chrest-stm model)))))))
  161. (recognise-and-learn-pattern model (make-instance 'vl-pattern :data '(C I F)))
  162. (assert-true (= 3 (length (fixed-queue-items (stm-visual (chrest-stm model))))))
  163. (recognise-and-learn-pattern model (make-instance 'vl-pattern :data '(D I F)))
  164. (assert-true (= 4 (length (fixed-queue-items (stm-visual (chrest-stm model))))))
  165. (recognise-and-learn-pattern model (make-instance 'vl-pattern :data '(E I F)))
  166. (assert-true (= 4 (length (fixed-queue-items (stm-visual (chrest-stm model))))))
  167. (recognise-and-learn-pattern model pattern-a)
  168. (assert-true (and (= 4 (length (fixed-queue-items (stm-visual (chrest-stm model)))))
  169. (equal-patterns-p (make-instance 'vl-pattern :data '(B))
  170. (node-contents (car (fixed-queue-items (stm-visual (chrest-stm model))))))))))
  171. ;; tests providing empirical support for the theory
  172. ;; -- experiments can be asssessed based on correlation coefficients
  173. (defun compute-pearson-correlation-coefficient (paired-list)
  174. "Given a list of pairs of numbers, return the Pearson correlation coefficient"
  175. (labels ((expected-value (lst)
  176. (if (null lst)
  177. 0.0
  178. (/ (apply #'+ lst)
  179. (length lst))))
  180. (sigma-lst (lst) (sqrt (- (expected-value (mapcar #'(lambda (n) (* n n)) lst))
  181. (* (expected-value lst)
  182. (expected-value lst))))))
  183. (let ((sigma-x (sigma-lst (mapcar #'first paired-list)))
  184. (sigma-y (sigma-lst (mapcar #'second paired-list))))
  185. (if (or (zerop sigma-x)
  186. (zerop sigma-y))
  187. 0.0
  188. (/ (- (expected-value (mapcar #'(lambda (pair) (* (first pair) (second pair)))
  189. paired-list))
  190. (* (expected-value (mapcar #'first paired-list))
  191. (expected-value (mapcar #'second paired-list))))
  192. (* sigma-x sigma-y))))))
  193. (def-unit-tests pearson-coefficient-tests ()
  194. (assert= 1.0 (compute-pearson-correlation-coefficient '((1 1) (2 2))))
  195. (assert= -1.0 (compute-pearson-correlation-coefficient '((1 0) (0 1))))
  196. (assert= 0.0 (compute-pearson-correlation-coefficient '((0 1) (0 0) (1 0) (1 1))))
  197. )
  198. ;; -- Verbal learning experiment
  199. ;; -- aim is to replicate Bugelski's 1962 result that number of cycles through a
  200. ;; list until it is completely learnt is inversely proportional to the
  201. ;; presentation time
  202. (defstruct experiment
  203. items ; the list of items to present to the model
  204. current ; the current indexed item
  205. cycles ; the number of times the list has been presented
  206. clock ; the current experiment time, measured in milliseconds
  207. presentation-time ; the time to allow each item to be presented, in milliseconds
  208. )
  209. (defun create-experiment (items presentation-time)
  210. "Define an experiment by providing the initial list of items and the item-presentation time"
  211. (make-experiment :items items
  212. :current 0
  213. :cycles 0
  214. :clock 0
  215. :presentation-time presentation-time))
  216. (defun get-current-item (experiment)
  217. "Return the current item in the experiment"
  218. (assert (< (experiment-current experiment) (length (experiment-items experiment))))
  219. (nth (experiment-current experiment) (experiment-items experiment)))
  220. (defun starting-new-cycle-p (experiment)
  221. "Experiment is beginning a new cycle if the current item is the first one"
  222. (= 0 (experiment-current experiment)))
  223. (defun next-item (experiment)
  224. "Advance experiment to next item, incrementing clock"
  225. (incf (experiment-current experiment))
  226. (incf (experiment-clock experiment) (experiment-presentation-time experiment))
  227. ;; check for wrapping around end of list
  228. (when (>= (experiment-current experiment) (length (experiment-items experiment)))
  229. (setf (experiment-current experiment) 0)
  230. (incf (experiment-cycles experiment))))
  231. (def-unit-tests experiment-tests ()
  232. "Some tests of the experiment setup and format"
  233. (let ((expt-1 (create-experiment '((A B C) (D E F) (G H I)) 200)))
  234. (assert-true (starting-new-cycle-p expt-1))
  235. (assert-equalp '(A B C) (get-current-item expt-1))
  236. (next-item expt-1)
  237. (assert-false (starting-new-cycle-p expt-1))
  238. (assert-equalp '(D E F) (get-current-item expt-1))
  239. (assert= 200 (experiment-clock expt-1))
  240. (next-item expt-1)
  241. (assert-false (starting-new-cycle-p expt-1))
  242. (assert-equalp '(G H I) (get-current-item expt-1))
  243. (assert= 400 (experiment-clock expt-1))
  244. (assert= 0 (experiment-cycles expt-1))
  245. (next-item expt-1)
  246. (assert-true (starting-new-cycle-p expt-1))
  247. (assert-equalp '(A B C) (get-current-item expt-1))
  248. (assert= 600 (experiment-clock expt-1))
  249. (assert= 1 (experiment-cycles expt-1))))
  250. (defun presentation-cycle (model experiment)
  251. "Present each stimulus to the model for learning in turn, until starting a new cycle"
  252. (recognise-and-learn-pattern model
  253. (get-current-item experiment)
  254. (experiment-clock experiment))
  255. (next-item experiment)
  256. (unless (starting-new-cycle-p experiment)
  257. (presentation-cycle model experiment)))
  258. (defun reached-success-p (model experiment)
  259. "Model has succeeded when it recalls the stimulus exactly for all items in list"
  260. (every #'(lambda (pattern) (equal-patterns-p pattern (recall-pattern model pattern)))
  261. (experiment-items experiment)))
  262. (defun train-to-success (model experiment &optional (safety-net 100))
  263. "Repeat the presentation of a cycle repeatedly, until the successfully learnt.
  264. A safety-net is provided to stop recursion after a number of cycles, if failed to learn."
  265. (unless (or (zerop safety-net)
  266. (reached-success-p model experiment))
  267. (presentation-cycle model experiment)
  268. (train-to-success model experiment (1- safety-net))))
  269. (defun do-bugelski (initial-time time-step num-runs items)
  270. "Perform the experiment, repeated for gradually increasing presentation times.
  271. Return a list of pairs, time vs num-cycles."
  272. (let ((results ()))
  273. (do ((current-time initial-time (+ current-time time-step))
  274. (cycle 0 (1+ cycle)))
  275. ((= cycle num-runs) (reverse results))
  276. (let ((ex (create-experiment items current-time))
  277. (m (create-chrest)))
  278. (train-to-success m ex)
  279. (push (list current-time (experiment-cycles ex))
  280. results)))))
  281. (defun list-to-visual-pattern (lst) (make-instance 'vl-pattern :data lst))
  282. (def-canonical-result-tests constant-learning-rate ()
  283. "This result performs the experiment, and checks there is at least a -0.9 correlation
  284. between the presentation time and the number of training cycles."
  285. (assert-true (< (compute-pearson-correlation-coefficient
  286. (do-bugelski 500 100 20
  287. (mapcar #'list-to-visual-pattern
  288. '((D A G) (B I F) (G I H) (J A L) (M I Q) (P E L) (S U J)))))
  289. -0.9)))