;;;;;;;;;;;;;;;;;;;;;;;;

;;;
;;;  Word Problem Solving Model
;;;  for "Atomic Components of Thought"
;;;  Dario Salvucci & John Anderson
;;;

;;; ACT-R 4.0 model
;;; to run the model call:
;;; (physp-initialize-variables)
;;; then
;;; (physp-run)

;;;=========================================================================================
;;;
;;;  Model Interface Code (bypasses visual interface)
;;;

(defvar *physp-action-trace* nil)
(defvar *physp-action-traces* nil)
(defvar *physp-stimuli* nil)
(defvar *physp-mouse-loc* nil)
(defvar *physp-short-move-latency* 0.3)
(defvar *physp-long-move-latency* 0.8)
(defvar *physp-long-move-threshold* 50)
(defvar *physp-short-attend-latency* 0)
(defvar *physp-long-attend-latency* 1.0)
(defvar *physp-type-latency* 1.0)

(defparameter *physp-pos2str-mappings*
  #((20 20) (0 0) (130 20) (20 50) (0 0) (130 50) (20 80) (0 0) (130 80)
    (20 120) (130 120) (150 120) (170 120) (190 120) (210 120) (230 120)
    (130 150) (150 150) (170 150) (190 150) (210 150) (230 150)
    (270 20) (0 0) (380 20) (270 50) (0 0) (380 50) (270 80) (0 0) (380 80)
    (270 120)))

(defparameter *physp-loc-names*
  #("SP Quantity 1" nil "SP Value 1"
    "SP Quantity 2" nil "SP Value 2"
    "SP Quantity 3" nil "SP Value 3"
    "Problem Type"
    "S1 Item 1" "S1 Item 2" "S1 Item 3" "S1 Item 4" "S1 Item 5" "S1 Item 6"
    "S2 Item 1" "S2 Item 2" "S2 Item 3" "S2 Item 4" "S2 Item 5" "S2 Item 6"
    "TP Quantity 1" nil "TP Value 1"
    "TP Quantity 2" nil "TP Value 2"
    "TP Quantity 3" nil "TP Value 3"
    "Problem Type"))


(defparameter *physp-pct-stimuli*
      '(("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "n" "*" "f" "/" "c" ""
         "9" "*" "4" "/" "2" ""
         "current" "=" "3" "number" "=" "2" "flux" "=" "5"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "n" "*" "f" "/" "c" ""
         "9" "*" "4" "/" "2" ""
         "number" "=" "7" "current" "=" "2" "flux" "=" "3"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "n" "*" "f" "/" "c" ""
         "9" "*" "4" "/" "2" ""
         "current" "=" "3" "flux" "=" "8" "number" "=" "1"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "n" "*" "f" "/" "c" ""
         "9" "*" "4" "/" "2" ""
         "flux" "=" "9" "current" "=" "7" "number" "=" "4"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "n" "*" "f" "/" "c" ""
         "9" "*" "4" "/" "2" ""
         "current" "=" "5" "number" "=" "1" "flux" "=" "3"
         "inductance")
        ))

(defparameter *physp-nct-stimuli*
      '(("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "c" "*" "n" "/" "f" ""
         "9" "*" "4" "/" "2" ""
         "current" "=" "3" "number" "=" "2" "flux" "=" "5"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "c" "*" "n" "/" "f" ""
         "9" "*" "4" "/" "2" ""
         "number" "=" "7" "current" "=" "2" "flux" "=" "3"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "c" "*" "n" "/" "f" ""
         "9" "*" "4" "/" "2" ""
         "current" "=" "3" "flux" "=" "8" "number" "=" "1"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "c" "*" "n" "/" "f" ""
         "9" "*" "4" "/" "2" ""
         "flux" "=" "9" "current" "=" "7" "number" "=" "4"
         "inductance")
        ("flux" "=" "4" "number" "=" "9" "current" "=" "2"
         "inductance" "c" "*" "n" "/" "f" ""
         "9" "*" "4" "/" "2" ""
         "current" "=" "5" "number" "=" "1" "flux" "=" "3"
         "inductance")
        ))

;;;
;;;  Utility Routines
;;;

(defun physp-loc-dist (loc1 loc2)
  (let ((dx (- (first loc1) (first loc2)))
        (dy (- (second loc1) (second loc2))))
    (isqrt (+ (* dx dx) (* dy dy)))))

(defun physp-get-location-value (location)
  (when *physp-stimuli*
    (let* ((value (elt (car *physp-stimuli*)
                       (position location *physp-pos2str-mappings* :test #'equalp))))
      (unless (equal value "") value))))

(defun physp-get-location-name (location)
  (let* ((pos (position location *physp-pos2str-mappings* :test #'equalp)))
    (elt *physp-loc-names* pos)))

(defun physp-prefix-p (prefix string)
  (if (<= (length prefix) (length string))
      (string= prefix string :end1 (length prefix) :end2 (length prefix))
      nil))

(defun physp-average-lists (lsts)
  (when lsts
    (apply #'mapcar (cons #'(lambda (&rest args)
                              (if (null args) 0 (/ (apply #'+ args) (length args))))
                          lsts))))

;;;
;;;  Model Routines
;;;

(defun physp-find-location (&key attended nearest value isa screen-x screen-y feat)
  (declare (ignore attended nearest value isa feat))
  (let ((location (list screen-x screen-y)))
    (if (physp-get-location-value location) location)))

(defun physp-move-attention (&key location scale)
  (declare (ignore scale))
  (if (member location '((20 20) (20 50) (20 80)) :test #'equalp)
      (actr-time-fct *physp-long-attend-latency*)
      (actr-time-fct *physp-short-attend-latency*))
  (let ((value (physp-get-location-value location)))
    (if value
        (let ((gen (read-from-string (string (gensym "word")))))
          (eval `(add-dm (,gen isa visual-object value ,value screen-pos ,location)))
          (get-wme gen))
        (get-wme 'nothing))))

(defun physp-move-mouse (location)
  (if (>= (physp-loc-dist *physp-mouse-loc* location) *physp-long-move-threshold*)
      (actr-time-fct *physp-long-move-latency*)
      (actr-time-fct *physp-short-move-latency*))
  (setf *physp-mouse-loc* location))

(defun physp-click-mouse (&optional location)
  (let* ((loc (if location location *physp-mouse-loc*)))
    (if (equalp loc '(400 150))
        (progn
          (push (list 'done (actr-time-fct))
                *physp-action-trace*)
          (setf *physp-stimuli* (cdr *physp-stimuli*)))
        (push (list 'click (actr-time-fct) (physp-get-location-name loc)
                    (physp-get-location-value loc))
              *physp-action-trace*))))

(defun physp-press-key (key)
  (actr-time-fct *physp-type-latency*)
  (push (list 'type (actr-time-fct) (format nil "~a" key))
        *physp-action-trace*))


;;;
;;;  Main Routines
;;;

(defun physp-experiment (&optional (condition 'pct))
  (setq *physp-mouse-loc* '(0 0))
  (setq *physp-stimuli*  (case condition
                               (pct *physp-pct-stimuli*) (nct *physp-nct-stimuli*)))
  nil)

(defun physp-problem-latencies ()
  (physp-average-lists
   (mapcar #'(lambda (trace)
               (let* ((dones (remove-if-not #'(lambda (a) (equalp 'done (first a))) trace))
                      (times (mapcar #'second dones)))
                 (mapcar #'- times (cons 0 times))))
           *physp-action-traces*)))

(defun physp-references ()
  (physp-average-lists
   (mapcar #'(lambda (trace)
               (let ((clicks (remove-if-not #'(lambda (a) (equalp 'click (first a))) trace)))
                 (mapcar #'(lambda (prefix divisor)
                             (let ((pclicks (remove-if-not
                                             #'(lambda (a) (physp-prefix-p prefix (third a))) clicks)))
                               (/ (length pclicks) divisor)))
                         '("SP" "S1" "S2" "TP") '(3 5 5 15))))
           *physp-action-traces*)))

(defun physp-reference-times ()
  (physp-average-lists
   (mapcar #'(lambda (trace)
               (let ((times (mapcar #'(lambda (x y)
                                        (cons (first x) (cons (- (second y) (second x)) (cddr x))))
                                    trace (cdr trace))))
                 (mapcar #'(lambda (prefix)
                             (let ((pclicks (remove-if-not
                                             #'(lambda (a)
                                                 (and (equalp 'click (first a))
                                                      (if (listp prefix)
                                                          (member (third a) prefix :test #'string=)
                                                          (physp-prefix-p prefix (third a)))))
                                             times)))
                               (if (null pclicks) 0 (/ (apply #'+ (mapcar #'second pclicks))
                                                       (length pclicks)))))
                         '("SP" ("S1 Item 1" "S1 Item 3" "S1 Item 5") ("S1 Item 2" "S1 Item 4")
                           ("S2 Item 1" "S2 Item 3" "S2 Item 5") ("S2 Item 2" "S2 Item 4") "TP"))))
           *physp-action-traces*)))

;;;=========================================================
;;;
;;;  Web Interface
;;;

(defvar *physp-base-level-decay* 0.5)
(defvar *physp-activation-noise* 0.1)
(defvar *physp-gain-noise* 0.2)
(defvar *physp-pc-condition* t)
(defvar *physp-nruns* 1)
(defvar *physp-acttrace* nil)
(defvar *physp-ct* nil)
(defvar *text*)
(defvar *graphic*)
(defvar *overlay*)
(setf *text* t)
(setf *graphic* nil)
(setf *overlay* nil)

(defparameter *physp-observed-problats* '((55.55 15.85 14.87 14.41 13.51) (52.39 23.38 19.13 15.43 14.90)))
(defparameter *physp-reported-problats* '((50.23 14.83 14.59 15.41 14.65) (54.46 14.77 14.82 14.61 15.23)))
(defparameter *physp-observed-refs*     '((1.74 2.11 1.13 1.11) (2.96 1.05 2.22 1.11)))
(defparameter *physp-reported-refs*     '((1.75 2.40 .85 1.00) (2.95 .80 2.20 1.00)))
(defparameter *physp-observed-reftimes* '((2.74 1.77 .94 1.57 .79 1.38) (2.76 1.76 .97 1.59 1.12 1.45)))
(defparameter *physp-reported-reftimes* '((2.84 1.81 .75 1.82 .80 1.20) (2.69 1.87 .75 1.50 .60 1.20)))


(defparameter *physp-all-exp-data* '(((55.55 15.85 14.87 14.41 13.51) (1.74 2.11 1.13 1.11)(2.74 1.77 .94 1.57 .79 1.38))
                                    ((52.39 23.38 19.13 15.43 14.90)(2.96 1.05 2.22 1.11)(2.76 1.76 .97 1.59 1.12 1.45))))

(defun physp-unified-output (data sim)
  (when sim
    (format *standard-output* "~%~%Parameters for run: (~S ~S ~S ~S)~%~%"
            *physp-base-level-decay*  *physp-activation-noise*  *physp-gain-noise*  *physp-nruns*))
  (when *text*
    (physp-output-tables data sim)
    
    (when (and sim *overlay*)
      (physp-output-tables *physp-all-exp-data* nil))
    
    (unless *graphic* (format *standard-output* 
                              "~%</pre>If your browser supports JAVA, you 
                               can display the data in a graph by checking 
                               the Graphic output box on the interface page.<pre>~%~%")))
  (when *graphic*
    (format *standard-output* " 
        <applet 
        code = \"DansGraphs.class\" 
        width = 500 
        height = 400> 

        <PARAM name=\"title\" value=\"Problem Latencies\">
        <PARAM name=\"longestline\" value=\"5\">
        <PARAM name=\"numlines\" value=\"~S\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"5\">
        <PARAM name=\"ymax\" value=\"60.0\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\"5\">
        <PARAM name=\"yspacing\" value=\"10\">
        <PARAM name=\"xspacing\" value=\"1\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;5;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;5;\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"~s\">
        <PARAM name=\"lstyle1\" value=\"~s\">
        <PARAM name=\"name0\" value=\"~a PC condition\">
        <PARAM name=\"name1\" value=\"~a NC condition\">
        <PARAM name=\"xname\" value=\"Problem\">
        <PARAM name=\"yname\" value=\"Latency (s)\">"
            (if (and sim *overlay*) 4 2)
            (if sim 2 6553)
            (if sim 2 6553)
            (if sim "Model" "Experiment")(if sim "Model" "Experiment"))
    
    (dotimes (i 2)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      (dotimes (j 5)
        (format *standard-output* "~3,2f;" (nth j (first (nth i data)))))
      (format *standard-output* "\">"))
    
    (when (and sim *overlay*)
      
      (format *standard-output*
              "<PARAM name=\"xval2\" value=\"1;2;3;4;5;\">
        <PARAM name=\"xval3\" value=\"1;2;3;4;5;\">
        <PARAM name=\"lcolor2\" value=\"0\">
        <PARAM name=\"lcolor3\" value=\"1\">
        <PARAM name=\"lstyle2\" value=\"6553\">
        <PARAM name=\"lstyle3\" value=\"6553\">
         <PARAM name=\"name2\" value=\"Experiment PC condition\">
        <PARAM name=\"name3\" value=\"Experiment NC condition\">")
      
      (dotimes (i 2)
        (format *standard-output* "<PARAM name=\"yval~s\" value=\"" (+ i 2))
        (dotimes (j 5)
          (format *standard-output* "~3,2f;" (nth j (first (nth i *physp-all-exp-data*)))))
        (format *standard-output* "\">"))
      
      
      )
    
    
    
    
    (format *standard-output* "
             <HR> Either your browser does not support JAVA or this graph has scrolled off the top of the display.~%
             </HR></applet>")
    
    
    
    (format *standard-output* " 
        <applet 
        code = \"DansGraphs.class\" 
        width = 500 
        height = 400> 

        <PARAM name=\"title\" value=\"References per Area Item\">
        <PARAM name=\"longestline\" value=\"4\">
        <PARAM name=\"numlines\" value=\"~S\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"4\">
        <PARAM name=\"ymax\" value=\"4.0\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"numxlabels\" value=\"5\">
        <PARAM name=\"xlabels\" value=\" ;SP;S1;S2;TP;\">
        <PARAM name=\"widestxlabel\" value=\"WW\">
        <PARAM name=\"ydiv\" value=\".25\">
        <PARAM name=\"yspacing\" value=\".5\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"~s\">
        <PARAM name=\"lstyle1\" value=\"~s\">
        <PARAM name=\"name0\" value=\"~a PC condition\">
        <PARAM name=\"name1\" value=\"~a NC condition\">
        <PARAM name=\"xname\" value=\"Area\">
        <PARAM name=\"yname\" value=\"References\">"
            (if (and sim *overlay*) 4 2)
            (if sim 2 6553)
            (if sim 2 6553)
            (if sim "Model" "Experiment")(if sim "Model" "Experiment"))
    
    
    
    (dotimes (i 2)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      (dotimes (j 4)
        (format *standard-output* "~3,2f;" (nth j (second (nth i data)))))
      (format *standard-output* "\">"))
    
    
    (when (and sim *overlay*)
      
      (format *standard-output*
              "<PARAM name=\"xval2\" value=\"1;2;3;4;\">
        <PARAM name=\"xval3\" value=\"1;2;3;4;\">
        <PARAM name=\"lcolor2\" value=\"0\">
        <PARAM name=\"lcolor3\" value=\"1\">
         <PARAM name=\"lstyle2\" value=\"6553\">
        <PARAM name=\"lstyle3\" value=\"6553\">
        <PARAM name=\"name2\" value=\"Experiment PC condition\">
        <PARAM name=\"name3\" value=\"Experiment NC condition\">")
      
      (dotimes (i 2)
        (format *standard-output* "<PARAM name=\"yval~s\" value=\"" (+ i 2))
        (dotimes (j 4)
          (format *standard-output* "~3,2f;" (nth j (second (nth i *physp-all-exp-data*)))))
        (format *standard-output* "\">"))
      
      )
    
    
    
    
    (format *standard-output* "
             <HR> Either your browser does not support JAVA or this graph has scrolled off the top of the display.~%
             </HR></applet>")
    
    
    
    
    (format *standard-output* " 
        <applet 
        code = \"DansGraphs.class\" 
        width = 500 
        height = 400> 

        <PARAM name=\"title\" value=\"Reference Times\">
        <PARAM name=\"longestline\" value=\"6\">
        <PARAM name=\"numlines\" value=\"~S\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"6\">
        <PARAM name=\"ymax\" value=\"4.0\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"numxlabels\" value=\"7\">
        <PARAM name=\"xlabels\" value=\" ;SP;S1-var;S1-ops;S2-val;S2-ops;TP;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWW\">
        <PARAM name=\"ydiv\" value=\".25\">
        <PARAM name=\"yspacing\" value=\".5\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;5;6;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;5;6;\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"~s\">
        <PARAM name=\"lstyle1\" value=\"~s\">
        <PARAM name=\"name0\" value=\"~a PC condition\">
        <PARAM name=\"name1\" value=\"~a NC condition\">
        <PARAM name=\"xname\" value=\"Area\">
        <PARAM name=\"yname\" value=\"Time\">"
            (if (and sim *overlay*) 4 2)
            (if sim 2 6553)
            (if sim 2 6553)
            (if sim "Model" "Experiment")(if sim "Model" "Experiment"))
    
    
    
    (dotimes (i 2)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      (dotimes (j 6)
        (format *standard-output* "~3,2f;" (nth j (third (nth i data)))))
      (format *standard-output* "\">"))
    
    
    (when (and sim *overlay*)
      
      (format *standard-output*
              "<PARAM name=\"xval2\" value=\"1;2;3;4;5;6;\">
        <PARAM name=\"xval3\" value=\"1;2;3;4;5;6;\">
        <PARAM name=\"lcolor2\" value=\"0\">
        <PARAM name=\"lcolor3\" value=\"1\">
         <PARAM name=\"lstyle2\" value=\"6553\">
        <PARAM name=\"lstyle3\" value=\"6553\">
        <PARAM name=\"name2\" value=\"Experiment PC condition\">
        <PARAM name=\"name3\" value=\"Experiment NC condition\">")
      
      (dotimes (i 2)
        (format *standard-output* "<PARAM name=\"yval~s\" value=\"" (+ i 2))
        (dotimes (j 6)
          (format *standard-output* "~3,2f;" (nth j (third (nth i *physp-all-exp-data*)))))
        (format *standard-output* "\">"))
      
      )
    
    
    
    
    (format *standard-output* "
             <HR> Either your browser does not support JAVA or this graph has scrolled off the top of the display.~%
             </HR></applet>")))


(defun physp-output-tables (data sim)
  (format *standard-output* "~A results in the PC and NC conditions.~%~%" (if sim "Model" "Experiment"))
  (let ((pc (first (first data)))
        (nc (first (second data))))
    (format *standard-output* "Problem Latencies:~%~%")
    (format *standard-output* "  Problem            1       2       3       4       5~%")
    (format *standard-output* "  ------------------------------------------------------~%")
    (format *standard-output* "  PC Condition: ")
    (dolist (x pc) (format *standard-output* "~8,2f" x))
    (format *standard-output* "~%  NC Condition: ")
    (dolist (x nc) (format *standard-output* "~8,2f" x))
    (format *standard-output* "~%~%~%"))
  (let ((pc (second (first data)))
        (nc (second (second data))))
    (format *standard-output* "References per Area Item:~%~%")
    (format *standard-output* "  Area              SP     S1     S2     TP~%")
    (format *standard-output* "  -------------------------------------------~%")
    (format *standard-output* "  PC Condition: ")
    (dolist (x pc) (format *standard-output* "~7,2f" x))
    (format *standard-output* "~%  NC Condition: ")
    (dolist (x nc) (format *standard-output* "~7,2f" x))
    (format *standard-output* "~%~%~%"))
  (let ((pc (third (first data)))
        (nc (third (second data))))
    (format *standard-output* "References Times:~%~%")
    (format *standard-output* "  Area               SP    S1-var  S1-ops  S2-val  S2-ops    TP~%")
    (format *standard-output* "  --------------------------------------------------------------~%")
    (format *standard-output* "  PC Condition: ")
    (dolist (x pc) (format *standard-output* "~8,2f" x))
    (format *standard-output* "~%  NC Condition: ")
    (dolist (x nc) (format *standard-output* "~8,2f" x))
    (format *standard-output* "~%~%~%")))


(defun physp-output-action-trace ()
  (format *standard-output* "Action Trace:~%~%")
  (when (> *physp-nruns* 1)
    (format *standard-output* "(Note: This trace represents the first of the ~a simulations).~%~%"
            *physp-nruns*))
  (dolist (a (first *physp-action-traces*))
    (let ((s *standard-output*))
      (case (first a)
        (click (format s "  (click ~,2f \"~a\" \"~a\")~%" (second a) (third a) (fourth a)))
        (type  (format s "  (type  ~,2f \"~a\")~%" (second a) (third a)))
        (done  (format s "  (done  ~,2f)~%~%" (second a)))
        (t     nil))))
  (format *standard-output* "~%~%"))

(defun physp-run ()
  (let ((pc-results nil)
        (nc-results nil))
    (unless (<= *physp-nruns* 10)
      (format *standard-output* "Sorry, server can only execute up to 10 simulations.~%"))
    (when (<= *physp-nruns* 10)
      (setf *physp-action-traces* nil)
      (setf *physp-pc-condition* t)
      (dotimes (n *physp-nruns*)
        (reset)
        
        (physp-experiment 'pct)
        
        (sgp-fct (list :bll *physp-base-level-decay*  :an *physp-activation-noise*
                       :egn *physp-gain-noise*        :v  *physp-ct*
                       :ct  *physp-ct*))
        (setf *physp-action-trace* nil)
        (run)
        (setf *physp-action-trace* (reverse *physp-action-trace*))
        (push *physp-action-trace* *physp-action-traces*))

      (when *physp-acttrace* (physp-output-action-trace))
      
      (setf pc-results (list (physp-problem-latencies) (physp-references) (physp-reference-times)))

      (setf *physp-action-traces* nil)
      (setf *physp-pc-condition* nil)
      (dotimes (n *physp-nruns*)
        (reset)
        
        (physp-experiment 'nct) 
        (mod-chunk current-method method solve-by-value)
        (sgp-fct (list :bll *physp-base-level-decay*  :an *physp-activation-noise*
                       :egn *physp-gain-noise*        :v  *physp-ct*
                       :ct  *physp-ct*))
        (setf *physp-action-trace* nil)
        (run)
        (setf *physp-action-trace* (reverse *physp-action-trace*))
        (push *physp-action-trace* *physp-action-traces*))
      (when *physp-acttrace* (physp-output-action-trace))
      
      (setf nc-results (list (physp-problem-latencies) (physp-references) (physp-reference-times)))
      
      (physp-unified-output (list pc-results nc-results) t))))

(defvar *www-interface*)
(setf *WWW-interface*  
      '((:heading "Physics Procedural Model" 2)
        (:table)
          (:table)
            "Base-level decay: "      (:string :sy *physp-base-level-decay* 0.5) (:new-row)
            "Activation noise: "      (:string :sy *physp-activation-noise* 0.1) (:new-row)
            "Expected gain noise: "   (:string :sy *physp-gain-noise*       0.2) (:new-row)
            "Number of simulations (1-10): " (:string :sy *physp-nruns* 1)              
           
          (:table-end)
          (:table)
            (:checkbox "Trace model"   :sy *physp-ct*        nil)  (:new-row)
            (:checkbox "Trace actions"          :sy *physp-acttrace*  nil) (:new-row)
            (:checkbox "Text output" :sy *text*  t) (:new-row)
            (:checkbox "Graphic output" :sy *graphic*  nil) (:new-row)
            (:checkbox "Show simulation and experiment data on same graph" :sy *overlay*  nil) 
      (:table-end)
        (:table-end)
        (:new-para)
        (:button "Show Experiment Results" "(physp-unified-output *physp-all-exp-data* nil)")
        (:new-para)
        (:button "Run model" "(if (and (numberp *physp-base-level-decay*) (numberp *physp-activation-noise*)
                                           (numberp *physp-gain-noise*) (numberp *physp-nruns*))
                                     (progn (physp-initialize-variables)
                                         
                                        (physp-run))
                                     (format *standard-output* \"All parameters must be numbers~%\"))")
        
        (:reset "Default values")
        (:button "Production rules" "(let ((prods (no-output (pp))))
                                       (dolist (x prods)
                                         (pp-fct (list x))
                                         (spp-fct (list x))
                                         (format *standard-output* \"~%\")))")
        (:button "Chunk types" "(chunk-type)")
        (:button "Chunks" "(dm)")
        (:new-para)
        "TIME and SIZE:"
        (:new-para)
        "- It usually takes about 1 minute for 5 model simulations."
        (:new-line)
        "- The model trace of 1 run is approximately 95k (50 pages) in size."
        (:new-line)
        "- The cycle trace of 1 run is approximately 5k (3 pages) in size."
        (:new-para)
        ))

(defun physp-initialize-variables ()
  (setf *physp-action-trace* nil)
  (setf *physp-action-traces* nil)
  (setf *physp-stimuli* nil)
  (setf *physp-mouse-loc* nil)
  (setf *physp-short-move-latency* 0.3)
  (setf *physp-long-move-latency* 0.8)
  (setf *physp-long-move-threshold* 50)
  (setf *physp-short-attend-latency* 0)
  (setf *physp-long-attend-latency* 1.0)
  (setf *physp-type-latency* 1.0))


;;;

;;;
;;;=========================================================
;;;
;;;  ACT-R Model
;;;
;;;  works like old visual interface, not ACT-R/PM
;;;

(clear-all)

(sgp :era t
     :bll 0.5
     :er t
     :ct nil
     :lt nil
     :cst nil
     :act nil
     :an 0.1
     :egn 0.2
)

(setf *optimize-visual* t)
(setf *actr-enabled-p* t)

;;;=========================================================
;;;
;;;  BASICS
;;;
;;;=========================================================

(chunk-type visual-object screen-pos value status)
(chunk-type abstract-object value line-pos bin-pos)
(chunk-type (abstract-letter (:include abstract-object)))
(chunk-type (abstract-number (:include abstract-object)))

(chunk-type method)
(chunk-type current-method method)

(chunk-type object string type)
(chunk-type (quantity (:include object)) letter)
(chunk-type (variable (:include object)))
(chunk-type (integer (:include object)) value next)
(chunk-type (operator (:include object)))

(chunk-type area)

(add-dm

 (t isa chunk)
 (done isa chunk)
 (success isa chunk)
 (failure isa chunk)
 (correct isa chunk)
 (incorrect isa chunk)
 (covered isa chunk)
 (uncovered isa chunk)
 (quantity isa chunk)
 (nothing isa chunk)
 (result isa chunk)
 (variable isa chunk)
 (integer isa chunk)
 (operator isa chunk)

 (solve-by-variable isa method)
 (solve-by-value isa method)
 (current-method isa current-method method solve-by-variable)

 (angle isa quantity letter a string "angle" type quantity)
 (area isa quantity letter a string "area" type quantity)
 (capacitance isa quantity letter c string "capacitance" type quantity)
 (constant isa quantity letter c string "constant" type quantity)
 (current isa quantity letter c string "current" type quantity)
 (distance isa quantity letter d string "distance" type quantity)
 (factor isa quantity letter f string "factor" type quantity)
 (flux isa quantity letter f string "flux" type quantity)
 (force isa quantity letter f string "force" type quantity)
 (index isa quantity letter i string "index" type quantity)
 (inductance isa quantity letter i string "inductance" type quantity)
 (length isa quantity letter l string "length" type quantity)
 (mass isa quantity letter m string "mass" type quantity)
 (new-length isa quantity letter n string "new-length" type quantity)
 (number isa quantity letter n string "number" type quantity)
 (pressure isa quantity letter pv string "pressure" type quantity)
 (resistance isa quantity letter r string "resistance" type quantity)
 (spacing isa quantity letter s string "spacing" type quantity)
 (temperature isa quantity letter tv string "temperature" type quantity)
 (thickness isa quantity letter tv string "thickness" type quantity)
 (velocity isa quantity letter v string "velocity" type quantity)
 (volume isa quantity letter v string "volume" type quantity)
 (wavelength isa quantity letter w string "wavelength" type quantity)
 (width isa quantity letter w string "width" type quantity)

 (a isa variable string "a" type variable)
 (c isa variable string "c" type variable)
 (d isa variable string "d" type variable)
 (f isa variable string "f" type variable)
 (i isa variable string "i" type variable)
 (l isa variable string "L" type variable)
 (m isa variable string "m" type variable)
 (n isa variable string "n" type variable)
 (pv isa variable string "p" type variable)
 (r isa variable string "r" type variable)
 (s isa variable string "s" type variable)
 (tv isa variable string "t" type variable)
 (v isa variable string "v" type variable)
 (w isa variable string "w" type variable)

 (type isa area)
 (sp isa area)
 (ss isa area)
 (tp isa area)
 (ts isa area)

 (i1 isa integer value 1 next i2 string "1" type integer)
 (i2 isa integer value 2 next i3 string "2" type integer)
 (i3 isa integer value 3 next i4 string "3" type integer)
 (i4 isa integer value 4 next i5 string "4" type integer)
 (i5 isa integer value 5 next i6 string "5" type integer)
 (i6 isa integer value 6 next i7 string "6" type integer)
 (i7 isa integer value 7 next i8 string "7" type integer)
 (i8 isa integer value 8 next i9 string "8" type integer)
 (i9 isa integer value 9 next nil string "9" type integer)

 (+ isa operator string "+" type operator)
 (- isa operator string "-" type operator)
 (* isa operator string "*" type operator)
 (/ isa operator string "/" type operator))

(sdp :references 1000.0)

;;;=========================================================
;;;
;;;  VISUAL INTERFACE
;;;
;;;=========================================================

(chunk-type word-position area row col x y cover)

(add-dm
 (type-1   isa word-position area type row i1 col i1 x 270 y 120 cover uncovered)
 (sp-qty1  isa word-position area sp row i1 col i1 x 20  y 20    cover covered)
 (sp-val1  isa word-position area sp row i1 col i2 x 130 y 20    cover uncovered)
 (sp-qty2  isa word-position area sp row i2 col i1 x 20  y 50    cover covered)
 (sp-val2  isa word-position area sp row i2 col i2 x 130 y 50    cover uncovered)
 (sp-qty3  isa word-position area sp row i3 col i1 x 20  y 80    cover covered)
 (sp-val3  isa word-position area sp row i3 col i2 x 130 y 80    cover uncovered)
 (s1q-pos1 isa word-position area ss row i1 col i1 x 130 y 120   cover covered)
 (s1o-pos2 isa word-position area ss row i1 col i2 x 150 y 120   cover covered)
 (s1q-pos3 isa word-position area ss row i1 col i3 x 170 y 120   cover covered)
 (s1o-pos4 isa word-position area ss row i1 col i4 x 190 y 120   cover covered)
 (s1q-pos5 isa word-position area ss row i1 col i5 x 210 y 120   cover covered)
 (s1-pos6  isa word-position area ss row i1 col i6 x 230 y 120   cover covered)
 (s2q-pos1 isa word-position area ss row i2 col i1 x 130 y 150   cover covered)
 (s2o-pos2 isa word-position area ss row i2 col i2 x 150 y 150   cover covered)
 (s2q-pos3 isa word-position area ss row i2 col i3 x 170 y 150   cover covered)
 (s2o-pos4 isa word-position area ss row i2 col i4 x 190 y 150   cover covered)
 (s2q-pos5 isa word-position area ss row i2 col i5 x 210 y 150   cover covered)
 (s2-pos6  isa word-position area ss row i2 col i6 x 230 y 150   cover covered)
 (tp-qty1  isa word-position area tp row i1 col i1 x 270 y 20    cover uncovered)
 (tp-val1  isa word-position area tp row i1 col i2 x 380 y 20    cover covered)
 (tp-qty2  isa word-position area tp row i2 col i1 x 270 y 50    cover uncovered)
 (tp-val2  isa word-position area tp row i2 col i2 x 380 y 50    cover covered)
 (tp-qty3  isa word-position area tp row i3 col i1 x 270 y 80    cover uncovered)
 (tp-val3  isa word-position area tp row i3 col i2 x 380 y 80    cover covered)
 (ts-ok    isa word-position area ts row i1 col i1 x 400 y 150   cover uncovered)
)

(sdp :references 1000.0)

(chunk-type read-screen-object area row col x y cover object string result result-type)
(chunk-type type-symbol symbol)
(chunk-type goto-next-screen)

(p retrieve-word-position
   =goal>
      isa read-screen-object
      area =area
      row =row
      col =col
      x nil
   =word-position>
      isa word-position
      area =area
      row =row
      col =col
      x =x
      y =y
      cover =cover
==>
   =goal>
      x =x
      y =y
      cover =cover)

(p read-screen-object-covered
   =goal>
      isa read-screen-object
      area =area
      row =row
      col =col
      x =x
      y =y
      cover covered
      object nil
   !bind! =loc (physp-find-location :attended nil :screen-x =x :screen-y =y)
==>
   !eval! (physp-move-mouse =loc)
   !eval! (physp-click-mouse)
   !bind! =object (physp-move-attention :location =loc :scale 'word)
   =goal>
      object =object)

(p read-screen-object-uncovered
   =goal>
      isa read-screen-object
      area =area
      row =row
      col =col
      x =x
      y =y
      cover uncovered
      object nil
   !bind! =loc (physp-find-location :attended nil :screen-x =x :screen-y =y)
==>
   !bind! =object (physp-move-attention :location =loc :scale 'word)
   =goal>
      object =object)

(p read-screen-object-attended-covered
   =goal>
      isa read-screen-object
      area =area
      row =row
      col =col
      x =x
      y =y
      cover covered
      object nil
   !bind! =loc (physp-find-location :attended t :screen-x =x :screen-y =y)
==>
   !eval! (physp-move-mouse =loc)
   !eval! (physp-click-mouse)
   !bind! =object (physp-move-attention :location =loc :scale 'word)
   =goal>
      object =object)

(p read-screen-object-attended-uncovered
   =goal>
      isa read-screen-object
      area =area
      row =row
      col =col
      x =x
      y =y
      cover uncovered
      object nil
   !bind! =loc (physp-find-location :attended t :screen-x =x :screen-y =y)
==>
   !bind! =object (physp-move-attention :location =loc :scale 'word)
   =goal>
      object =object)

(p read-screen-object-nothing
   =goal>
      isa read-screen-object
      object nil
==>
   =goal>
      result nothing
   !pop!)
(spp read-screen-object-nothing :r .3)

(p return-screen-object
   =goal>
      isa read-screen-object
      object =object
   =object>
      isa visual-object
      value =string
   =something>
      isa object
      string =string
      type =type
==>
   =goal>
      result =something
      result-type =type
   !pop!)
(spp return-screen-object :strength 10)

(p type-symbol
   =goal>
      isa type-symbol
      symbol =symbol
   =symbol>
      isa object
      string =string
==>
   !eval! (physp-press-key =string)
   !pop!)
(spp type-symbol :strength 10)

(p goto-next-screen
   =goal>
      isa goto-next-screen
   =word-position>
      isa word-position
      area ts
      x =x
      y =y
==>
   !bind! =loc (list =x =y)
   !eval! (physp-move-mouse =loc)
   !eval! (physp-click-mouse)
   !pop!)

;;;=========================================================
;;;
;;;  ANALOGY HELPER PRODUCTIONS
;;;
;;;=========================================================

(chunk-type solve-problem equation pos type value qvalue)
(chunk-type find-test-problem-value quantity pos found result)

;=============================================================================
;;;
;;;  equation representation
;;;

(chunk-type equation type)

(p pop-equation
   =goal>
      isa equation
==>
   !pop!)

;=============================================================================
;;;
;;;  create-dependency
;;;

(chunk-type create-dependency type equation pos value
                              dep-goal dep-subgoal dep-subgoal2 dep-modgoal)

(p set-quantity-dependency-goal
   =goal>
      isa create-dependency
      type quantity
      equation =equation
      pos =pos
      dep-goal nil
==>
   =dep-goal>
      isa solve-problem
      equation =equation
      pos =pos
   =goal>
      dep-goal =dep-goal)

(p set-quantity-dependency-subgoal1
   =goal>
      isa create-dependency
      type quantity
      value =value
      dep-subgoal nil
==>
   =dep-subgoal>
      isa find-test-problem-value
      quantity =value
      result result
   =goal>
      dep-subgoal =dep-subgoal)

(p set-quantity-dependency-subgoal2
   =goal>
      isa create-dependency
      type quantity
      dep-subgoal2 nil
==>
   =dep-subgoal2>
      isa type-symbol
      symbol result
   =goal>
      dep-subgoal2 =dep-subgoal2)

(p set-quantity-dependency-modgoal
   =goal>
      isa create-dependency
      type quantity
      equation =equation
      pos =pos
      dep-modgoal nil
   =pos>
      isa integer
      next =next
==>
   =dep-modgoal>
      isa solve-problem
      equation =equation
      pos =next
   =goal>
      dep-modgoal =dep-modgoal)

(p create-quantity-dependency
   =goal>
      isa create-dependency
      type quantity
      equation =equation
      dep-goal =dep-goal
      dep-subgoal =dep-subgoal
      dep-subgoal2 =dep-subgoal2
      dep-modgoal =dep-modgoal
==>
   =dependency>
      isa dependency
      goal =dep-goal
      stack (!eval! (list =dep-subgoal =dep-subgoal2))
      modified =dep-modgoal
      generals result
      specifics =equation
   !focus-on! =dependency)

(p set-constant-dependency-goal
   =goal>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      dep-goal nil
==>
   =dep-goal>
      isa solve-problem
      equation =equation
      pos =pos
   =goal>
      dep-goal =dep-goal)

(p set-constant-dependency-subgoal
   =goal>
      isa create-dependency
      type constant
      value =value
      dep-subgoal nil
==>
   =dep-subgoal>
      isa type-symbol
      symbol =value
   =goal>
      dep-subgoal =dep-subgoal)

(p set-constant-dependency-modgoal
   =goal>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      dep-modgoal nil
   =pos>
      isa integer
      next =next
==>
   =dep-modgoal>
      isa solve-problem
      equation =equation
      pos =next
   =goal>
      dep-modgoal =dep-modgoal)

(p create-constant-dependency
   =goal>
      isa create-dependency
      type constant
      equation =equation
      dep-goal =dep-goal
      dep-subgoal =dep-subgoal
      dep-modgoal =dep-modgoal
==>
   =dependency>
      isa dependency
      goal =dep-goal
      stack =dep-subgoal
      modified =dep-modgoal
      specifics =equation
   !focus-on! =dependency)

(p pop-dependency
   =goal>
      isa dependency
==>
   !pop!)

;=============================================================================
;;;
;;;  recall-or-create-equation
;;;

(chunk-type recall-or-create-equation type result)

(p subgoal-equation
   =goal>
      isa recall-or-create-equation
      type =type
      result nil
==>
   =equation>
      isa equation
      type =type
   =goal>
      result t
   !push! =equation)

(p return-equation
   =goal>
      isa recall-or-create-equation
      type =type
      result t
   =equation>
      isa equation
      type =type
==>
   =goal>
      result =equation
   !pop!)

;=============================================================================
;;;
;;;  read-problem-type
;;;

(chunk-type read-problem-type result)

(p subgoal-read-problem-type
   =goal>
      isa read-problem-type
      result nil
==>
   =subgoal1>
      isa read-screen-object
      area type
      row i1
      col i1
      result =result
   =goal>
      result =result
   !push! =subgoal1)

(p done-read-problem-type
   =goal>
      isa read-problem-type
      - result nil
==>
   !pop!)

;=============================================================================
;;;
;;;  read-problem-line
;;;

(chunk-type read-problem-line pos quantity value result)
(chunk-type problem-mapping quantity value)

(p read-problem-line-quantity
   =goal>
      isa read-problem-line
      pos =pos
      quantity nil
==>
   =subgoal1>
      isa read-screen-object
      area sp
      col i1
      row =pos
      result =result
   =goal>
      quantity =result
   !push! =subgoal1)

(p read-problem-line-value
   =goal>
      isa read-problem-line
      pos =pos
      value nil
==>
   =subgoal1>
      isa read-screen-object
      area sp
      col i2
      row =pos
      result =result
   =goal>
      value =result
   !push! =subgoal1)

(p return-problem-mapping
   =goal>
      isa read-problem-line
      quantity =quantity
      - quantity nothing
      value =value
      - value nothing
==>
   =problem-mapping>
      isa problem-mapping
      quantity =quantity
      value =value
   =goal>
      result =problem-mapping
   !pop!)

(p empty-problem-line
   =goal>
      isa read-problem-line
==>
   =goal>
      result done
   !pop!)
(spp empty-problem-line :r .5)

;=============================================================================
;;;
;;;  read-problem
;;;

(chunk-type read-problem pos mapping)

(p subgoal-read-problem-line
   =goal>
      isa read-problem
      pos =pos
      mapping nil
==>
   =subgoal1>
      isa read-problem-line
      pos =pos
      result =result
   =goal>
      mapping =result
   !push! =subgoal1)

(p read-next-problem-line
   =goal>
      isa read-problem
      pos =pos
      - mapping nil
      - mapping done
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      mapping nil)

(p done-read-problem
   =goal>
      isa read-problem
      mapping done
==>
   !pop!)

;=============================================================================
;;;
;;;  find-sample-problem-quantity
;;;

(chunk-type find-sample-problem-quantity value pos symbol result)

(p subgoal-read-value
   =goal>
      isa find-sample-problem-quantity
      pos =pos
      symbol nil
==>
   =subgoal1>
      isa read-screen-object
      area sp
      row =pos
      col i2
      result =result
   =goal>
      symbol =result
   !push! =subgoal1)

(p found-value
   =goal>
      isa find-sample-problem-quantity
      value =value
      pos =pos
      - symbol nil
      symbol =value
      result nil
==>
   =subgoal1>
      isa read-screen-object
      area sp
      row =pos
      col i1
      result =result
   =goal>
      result =result
   !push! =subgoal1)

(p done-find-sample-problem-quantity
   =goal>
      isa find-sample-problem-quantity
      - result nil
==>
   !pop!)

(p value-not-found
   =goal>
      isa find-sample-problem-quantity
      value =value
      pos =pos
      - symbol nil
      - symbol =value
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      symbol nil)

;=============================================================================
;;;
;;;  read-step1
;;;

(chunk-type read-step1 pos value)

(p subgoal-read-variable
   =goal>
      isa read-step1
      pos =pos
      value nil
==>
   =subgoal1>
      isa read-screen-object
      area ss
      row i1
      col =pos
      result =result
   =goal>
      value =result
   !push! =subgoal1)

(p read-next-variable
   =goal>
      isa read-step1
      pos =pos
      - value nil
      - value nothing
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      value nil)

(p done-read-step1
   =goal>
      isa read-step1
      value nothing
==>
   !pop!)

;=============================================================================
;;;
;;;  find-same-letter-quantity
;;;

(chunk-type find-same-letter-quantity letter pos qletter result)

(p read-test-quantity
   =goal>
      isa find-same-letter-quantity
      pos =pos
      result nil
==>
   =subgoal1>
      isa read-screen-object
      area tp
      row =pos
      col i1
      result =result
   =goal>
      result =result
   !push! =subgoal1)

(p retrieve-quantity-letter
   =goal>
      isa find-same-letter-quantity
      qletter nil
      result =quantity
   =quantity>
      isa quantity
      letter =letter
==>
   =goal>
      qletter =letter)

(p found-quantity
   =goal>
      isa find-same-letter-quantity
      letter =letter
      qletter =letter
==>
   !pop!)

(p try-next-quantity
   =goal>
      isa find-same-letter-quantity
      letter =letter
      - qletter nil
      - qletter =letter
      pos =pos
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      qletter nil
      result nil)

(p quantity-not-found
   =goal>
      isa find-same-letter-quantity
      letter =letter
      pos =pos
      result nothing
==>
   !pop!)
(spp quantity-not-found :r .5)

;=============================================================================
;;;
;;;  study-step1-symbol
;;;

(chunk-type study-step1-symbol equation pos symbol symtype quantity result)

(p read-step1-symbol
   =goal>
      isa study-step1-symbol
      pos =pos
      symbol nil
==>
   =subgoal1>
      isa read-screen-object
      area ss
      row i1
      col =pos
      result =result
      result-type =symtype
   =goal>
      symbol =result
      symtype =symtype
   !push! =subgoal1)

(p study-step1-symbol-done
   =goal>
      isa study-step1-symbol
      symbol nothing
==>
   =goal>
      result done
   !pop!)

(p store-equation-operator
   =goal>
      isa study-step1-symbol
      equation =equation
      pos =pos
      symbol =operator
      symtype operator
      result nil
==>
   =subgoal1>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      value =operator
   =goal>
      result success
   !push! =subgoal1)

(p store-equation-integer
   =goal>
      isa study-step1-symbol
      equation =equation
      pos =pos
      symbol =integer
      symtype integer
      result nil
==>
   =subgoal1>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      value =integer
   =goal>
      result success
   !push! =subgoal1)

(p subgoal-find-same-letter-quantity
   =goal>
      isa study-step1-symbol
      symbol =var
      symtype variable
      quantity nil
      result nil
==>
   =subgoal1>
      isa find-same-letter-quantity
      letter =var
      pos i1
      result =result
   =goal>
      quantity =result
   !push! =subgoal1)

(p store-equation-quantity
   =goal>
      isa study-step1-symbol
      equation =equation
      pos =pos
      symbol =symbol
      quantity =quantity
      - quantity nothing
      result nil
==>
   =subgoal1>
      isa create-dependency
      type quantity
      equation =equation
      pos =pos
      value =quantity
   =goal>
      result success
   !push! =subgoal1)

(p store-equation-quantity-constant
   =goal>
      isa study-step1-symbol
      equation =equation
      pos =pos
      symbol =symbol
      quantity nothing
      result nil
==>
   =subgoal1>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      value =symbol
   =goal>
      result success
   !push! =subgoal1)

(p study-step1-symbol-success
   =goal>
      isa study-step1-symbol
      result success
==>
   !pop!)

;=============================================================================
;;;
;;;  study-step1
;;;

(chunk-type study-step1 equation pos result)

(p study-step1-symbol
   =goal>
      isa study-step1
      equation =equation
      pos =pos
      result nil
==>
   =subgoal1>
      isa study-step1-symbol
      equation =equation
      pos =pos
      result =result
   =goal>
      result =result
   !push! =subgoal1)

(p study-next-step1-symbol
   =goal>
      isa study-step1
      pos =pos
      result success
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      result nil)

(p done-study-step1
   =goal>
      isa study-step1
      result done
==>
   !pop!)

;=============================================================================
;;;
;;;  study-step2
;;;

(chunk-type study-step2 equation pos symbol symtype quantity)

(p study-value-symbol
   =goal>
      isa study-step2
      pos =pos
      symbol nil
==>
   =subgoal1>
      isa read-screen-object
      area ss
      row i2
      col =pos
      result =result
      result-type =symtype
   =goal>
      symbol =result
      symtype =symtype
   !push! =subgoal1)

(p done-study-step2
   =goal>
      isa study-step2
      symbol nothing
==>
   !pop!)

(p store-value-equation-operator
   =goal>
      isa study-step2
      equation =equation
      pos =pos
      symbol =operator
      symtype operator
   =pos>
      isa integer
      next =next
==>
   =subgoal1>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      value =operator
   =goal>
      pos =next
      symbol nil
   !push! =subgoal1)

(p store-value-equation-constant-variable
   =goal>
      isa study-step2
      equation =equation
      pos =pos
      symbol =variable
      symtype variable
   =pos>
      isa integer
      next =next
==>
   =subgoal1>
      isa create-dependency
      type constant
      equation =equation
      pos =pos
      value =variable
   =goal>
      pos =next
      symbol nil
   !push! =subgoal1)

(p subgoal-find-sample-problem-quantity
   =goal>
      isa study-step2
      symbol =integer
      symtype integer
      quantity nil
==>
   =subgoal1>
      isa find-sample-problem-quantity
      value =integer
      pos i1
      result =result
   =goal>
      quantity =result
   !push! =subgoal1)

(p store-value-equation-quantity
   =goal>
      isa study-step2
      equation =equation
      pos =pos
      symbol =symbol
      quantity =quantity
      - quantity nil
   =pos>
      isa integer
      next =next
==>
   =subgoal1>
      isa create-dependency
      type quantity
      equation =equation
      pos =pos
      value =quantity
   =goal>
      pos =next
      symbol nil
      quantity nil
   !push! =subgoal1)

;=============================================================================
;;;
;;;  check-step2-symbol
;;;

(chunk-type check-step2-symbol equation pos symbol type value quantity result)

(p read-step2-symbol
   =goal>
      isa check-step2-symbol
      pos =pos
      symbol nil
==>
   =subgoal1>
      isa read-screen-object
      area ss
      row i2
      col =pos
      result =result
   =goal>
      symbol =result
   !push! =subgoal1)

(p check-step2-symbol-done
   =goal>
      isa check-step2-symbol
      symbol nothing
==>
   =goal>
      result done
   !pop!)

(p retrieve-dependency-subgoal-for-step2
   =goal>
      isa check-step2-symbol
      equation =equation
      pos =pos
      - symbol nil
      - symbol nothing
      type nil
      value nil
   =dependency-subgoal>
      isa create-dependency
      type =type
      equation =equation
      pos =pos
      value =value
==>
   =goal>
      type =type
      value =value)

(p correct-step2-constant
   =goal>
      isa check-step2-symbol
      - symbol nothing
      symbol =constant
      type constant
      value =constant
==>
   =goal>
      result correct
   !pop!)

(p find-integer-quantity
   =goal>
      isa check-step2-symbol
      symbol =integer
      type quantity
      quantity nil
==>
   =subgoal1>
      isa find-sample-problem-quantity
      value =integer
      pos i1
      result =result
   =goal>
      quantity =result
   !push! =subgoal1)

(p correct-step2-integer
   =goal>
      isa check-step2-symbol
      type quantity
      value =quantity
      quantity =quantity
==>
   =goal>
      result correct
   !pop!)

(p incorrect-step2-integer
   =goal>
      isa check-step2-symbol
      - quantity nil
==>
   =goal>
      result incorrect
   !pop!)
(spp incorrect-step2-integer :r .1)

;=============================================================================
;;;
;;;  check-step2
;;;

(chunk-type check-step2 equation pos check result)

(p check-step2-symbol
   =goal>
      isa check-step2
      equation =equation
      pos =pos
      check nil
==>
   =subgoal1>
      isa check-step2-symbol
      equation =equation
      pos =pos
      result =result
   =goal>
      check =result
   !push! =subgoal1)

(p check-next-step2-symbol
   =goal>
      isa check-step2
      pos =pos
      check correct
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      check nil)

(p check-step2-symbol-failed
   =goal>
      isa check-step2
      check incorrect
==>
   =goal>
      result incorrect
   !pop!)

(p done-check-step2
   =goal>
      isa check-step2
      check done
==>
   =goal>
      result correct
   !pop!)

;=============================================================================
;;;
;;;  find-test-problem-value
;;;

(p init-find-tp-value
   =goal>
      isa find-test-problem-value
      pos nil
==>
   =goal>
      pos i1)

(p read-test-quantity2
   =goal>
      isa find-test-problem-value
      pos =pos
      found nil
      result nil
==>
   =subgoal1>
      isa read-screen-object
      area tp
      row =pos
      col i1
      result =result
   =goal>
      result =result
   !push! =subgoal1)

(p subgoal-read-value2
   =goal>
      isa find-test-problem-value
      quantity =quantity
      pos =pos
      found nil
      result =quantity
==>
   =subgoal1>
      isa read-screen-object
      area tp
      row =pos
      col i2
      result =result
   =goal>
      found t
      result =result
   !push! =subgoal1)

(p done-find-test-problem-value
   =goal>
      isa find-test-problem-value
      - pos nil
      found t
      - result nil
==>
   !pop!)

(p try-next-test-quantity
   =goal>
      isa find-test-problem-value
      quantity =quantity
      pos =pos
      found nil
      - result nil
      - result =quantity
   =pos>
      isa integer
      next =next
==>
   =goal>
      pos =next
      result nil)

;=============================================================================
;;;
;;;  solve-problem
;;;
;;;  productions learned by analogy

;;;=========================================================
;;;
;;;  SBVAR and SBVAL PRODUCTIONS
;;;
;;;=========================================================

;;;
;;;  Types that are different for PC and NC
;;;

(chunk-type read-top-down method type)
(chunk-type study-equation method equation)
(chunk-type check-equation method equation result)

(p read-top-down-get-method
   =goal>
      isa read-top-down
      method nil
   =current-method>
      isa current-method
      method =method
==>
   =goal>
      method =method)

(p study-equation-get-method
   =goal>
      isa study-equation
      method nil
   =current-method>
      isa current-method
      method =method
==>
   =goal>
      method =method)

(p check-equation-get-method
   =goal>
      isa check-equation
      method nil
   =current-method>
      isa current-method
      method =method
==>
   =goal>
      method =method)

;;;
;;;  Solve-by-variable productions
;;;

(p sbvar-read-top-down
   =goal>
      isa read-top-down
      method solve-by-variable
==>
   !output! ("SP~%")
   =subgoal1>
      isa read-problem
      pos i1
   !focus-on! =subgoal1)

(p sbvar-study-equation
   =goal>
      isa study-equation
      method solve-by-variable
      equation =equation
==>
   !output! ("S1~%")
   =subgoal1>
      isa study-step1
      equation =equation
      pos i1
   !focus-on! =subgoal1)

(p sbvar-subgoal-check-equation
   =goal>
      isa check-equation
      method solve-by-variable
      equation =equation
      result nil
==>
   !output! ("SPS2~%")
   =subgoal1>
      isa check-step2
      equation =equation
      pos i1
      result =result
   =goal>
      result =result
   !push! =subgoal1)

(p sbvar-done-check-equation
   =goal>
      isa check-equation
      method solve-by-variable
      equation =equation
      - result nil
==>
   !pop!)

;;;
;;;  Solve-by-value productions
;;;

(p sbval-read-top-down
   =goal>
      isa read-top-down
      method solve-by-value
      type =type
==>
   !output! ("SP~%")
   !output! ("S1~%")
   =subgoal1>
      isa read-problem
      pos i1
   =subgoal2>
      isa recall-or-create-equation
      type nothing
      result =result
   =subgoal3>
      isa study-step1
      equation =result
      pos i1
   !focus-on! =subgoal3
   !push! =subgoal2
   !push! =subgoal1)

(p sbval-study-equation
   =goal>
      isa study-equation
      method solve-by-value
      equation =equation
==>
   !output! ("SPS2~%")
   =subgoal1>
      isa study-step2
      equation =equation
      pos i1
   !focus-on! =subgoal1)

(p sbval-check-equation
   =goal>
      isa check-equation
      method solve-by-value
==>
   =goal>
      result correct
   !pop!)

;;;=========================================================
;;;
;;;  MAIN ANALOGY PRODUCTIONS
;;;
;;;=========================================================

(chunk-type solve-by-analogy stage type equation lasttype read-top checked check first)

(p stage1-read-problem-type
   =goal>
      isa solve-by-analogy
      stage i1
==>
   !output! ("(")
   !output! ("(")
   =subgoal1>
      isa read-problem-type
      result =result
   =goal>
      stage i2
      type =result
   !push! =subgoal1)
(spp stage1-read-problem-type :effort 2.0)

(p stage2-first-read-top-down
   =goal>
      isa solve-by-analogy
      type =type
      stage i2
      read-top nil
      first t
==>
   =subgoal1>
      isa read-top-down
      type =type
   =goal>
      read-top t
      first nil
   !push! =subgoal1)

(p stage2-read-top-down
   =goal>
      isa solve-by-analogy
      type =type
      stage i2
      first nil
==>
   =subgoal1>
      isa read-top-down
      type =type
   =goal>
      read-top t
   !push! =subgoal1)
(spp stage2-read-top-down :a 5.0)

(p stage2-skip-ahead
   =goal>
      isa solve-by-analogy
      stage i2
      read-top nil
      first nil
==>
   =goal>
      stage i3)
(spp stage2-skip-ahead :r .73) ;; estimated, p=.22

(p stage2-move-on
   =goal>
      isa solve-by-analogy
      stage i2
      read-top t
==>
   =goal>
      read-top nil
      stage i3)
(spp stage2-move-on :r .78) ;; estimated, p=.83

(p stage3-study-equation
   =goal>
      isa solve-by-analogy
      stage i3
      type =type
==>
   =subgoal1>
      isa recall-or-create-equation
      type =type
      result =result
   =subgoal2>
      isa study-equation
      equation =result
   !push! =subgoal2
   !push! =subgoal1)
(spp stage3-study-equation :a 5.0)

(p stage3-retrieve-equation
   =goal>
      isa solve-by-analogy
      stage i3
      type =type
   =equation>
      isa equation
      type =type
==>
   =goal>
      stage i4
      equation =equation)
(spp stage3-retrieve-equation :r .75) ;; estimated, p=dynamic

(p stage4-check-equation
   =goal>
      isa solve-by-analogy
      stage i4
      equation =equation
      - checked done
==>
   =subgoal1>
      isa check-equation
      equation =equation
      result =result
   =goal>
      checked t
      check =result
   !push! =subgoal1)
(spp stage4-check-equation :a 5.0)

(p stage4-skip-check
   =goal>
      isa solve-by-analogy
      stage i4
      checked nil
==>
   =goal>
      stage i6)
(spp stage4-skip-check :r .72) ;; estimated, p=.13

(p stage4-move-on
   =goal>
      isa solve-by-analogy
      stage i4
      checked t
==>
   =goal>
      checked done)
(spp stage4-move-on :r .80) ;; estimated, p=.94

(p stage4-ignore-check-result
   =goal>
      isa solve-by-analogy
      stage i4
      checked done
      - check nil
==>
   =goal>
      stage i6
      checked nil
      check nil)

(p stage4-check-correct
   =goal>
      isa solve-by-analogy
      stage i4
      checked done
      check correct
==>
   =goal>
      stage i6
      checked nil
      check nil)

(p stage4-check-incorrect
   =goal>
      isa solve-by-analogy
      stage i4
      equation =equation
      checked done
      check incorrect
   =current-method>
      isa current-method
   =equation>
      isa equation
==>
   !output! ("EPI~%")
   =current-method>
      method solve-by-value
   =equation>
      type nil
   =goal>
      stage i3
      equation nil
      checked nil
      check nil)

(p stage5-review-equation
   =goal>
      isa solve-by-analogy
      stage i5
      type =type
==>
   =subgoal1>
      isa recall-or-create-equation
      type =type
      result =result
   =subgoal2>
      isa study-equation
      equation =result
   !push! =subgoal2
   !push! =subgoal1)
(spp stage5-review-equation :a 5.0)

(p stage5-retrieve-equation
   =goal>
      isa solve-by-analogy
      stage i5
      type =type
   =equation>
      isa equation
      type =type
==>
   =goal>
      stage i6
      equation =equation)
(spp stage5-retrieve-equation :r .81) ;; estimated, p=dynamic

(p stage6-solve-problem
   =goal>
      isa solve-by-analogy
      stage i6
      equation =equation
==>
   !output! ("TPTS~%")
   =subgoal1>
      isa solve-problem
      equation =equation
      pos i1
   =subgoal2>
      isa goto-next-screen
   =goal>
      stage i7
      equation nil
   !push! =subgoal2
   !push! =subgoal1)

(p stage7-read-problem-type
   =goal>
      isa solve-by-analogy
      stage i7
      type =type
==>
   !output! (")")
   =subgoal1>
      isa read-problem-type
      result =result
   =goal>
      stage i8
      lasttype =type
      type =result
   !push! =subgoal1)
(spp stage7-read-problem-type :effort 2.0)

(p stage8-next-problem
   =goal>
      isa solve-by-analogy
      stage i8
      - type nothing
      type =type
      lasttype =type
==>
   !output! ("(")
   =goal>
      stage i5)

(p stage8-next-set
   =goal>
      isa solve-by-analogy
      stage i8
      - type nothing
      type =type
      - lasttype =type
      lasttype =lasttype
==>
   !output! ("(")
   =goal>
      stage i2
      equation nil)

(p stage8-done-problems
   =goal>
      isa solve-by-analogy
      stage i8
      type nothing
==>
   !output! (")")
   !pop!)

(spp :creation-time -3000)

(add-dm (goal isa solve-by-analogy stage i1))
(goal-focus goal)