autility-and-reward-1.lisp 29 KB
Newer Older
root's avatar
root committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
;;;  -*- mode: LISP; Syntax: COMMON-LISP;  Base: 10 -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; Author      : Dan Bothell & John Anderson
;;; Copyright   : (c) 2006 Dan Bothell
;;; Availability: Covered by the GNU LGPL, see LGPL.txt
;;; Address     : Department of Psychology 
;;;             : Carnegie Mellon University
;;;             : Pittsburgh, PA 15213-3890
;;;             : db30@andrew.cmu.edu
;;; 
;;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; Filename    : utility-and-reward-1.lisp
;;; Version     : 2.2
;;; 
;;; Description : The procedural utility computation functions and a module
;;;             : for handling the "reward" given to a production.
;;;             :
;;; 
;;; Bugs        : 
;;;
;;; To do       : [ ] Consider a new command or switch which allows one to
;;;             :     not clear the history after propigating a reward.
;;; 
;;; ----- History -----
;;; 2006.05.22 Dan
;;;             : * Initial creation.
;;; 2006.08.24 Dan
;;;             : * Added the "null" learning event marker (the way setting
;;;             :   both success and failure worked in the old system) - if
;;;             :   trigger-reward is passed nil it just clears the history.
;;; 2006.09.08 Dan
;;;             : * Changed the :at parameter check from posnum to nonneg.
;;; 2006.10.06 Dan
;;;             : * Changed some print-warning calls to model-warning so that
;;;             :   they turn off with the model-warning parameter.
;;; 2006.11.15 Dan
;;;             : * Changed trigger-reward so that it tests its parameter
;;;             :   before generating the event instead of waiting for the 
;;;             :   propigate-reward function to catch problems.
;;;             : * Changed the name of the module from utility-2 to utility
;;;             :   in preparation for making it the default module.
;;;             : * Changed the doc string for the module and updated the
;;;             :   version to 2.0.
;;; 2007.10.26 Dan [2.1]
;;;             : * Added the Utility threshold parameter back into the system.
;;; 2008.02.26 Dan
;;;             : * Fixed a bug in propigate-reward which allowed utility 
;;;             :   learning to go on even when :esc was set to nil.
;;; 2008.07.22 Dan
;;;             : * Changed compute-utility so that it has an optional parameter
;;;             :   which indicates whether or not to store the computed value.
;;; 2008.07.23 Dan
;;;             : * Added the require of "CENTRAL-PARAMETERS" since it does use
;;;             :   it - don't rely on procedural or declarative to load it first.
;;;             : * Added call to the new register-subsymbolic-parameters to
;;;             :   note which parameters should trigger the warning.
;;; 2008.08.01 Dan
;;;             : * Procedural now owns :dat.
;;; 2009.08.12 Dan
;;;             : * Added a utility-offset hook which is called after the normal
;;;             :   utility for a production is computed and set (without noise).  
;;;             :   If it returns a number that's added into the utility value.
;;; 2011.02.16 Dan [2.2]
;;;             : * Added a utility learning trace parameter which prints out
;;;             :   the utility changes when a reward occurs.
;;;             : * Also changed linear-update-utility to take the utility module
;;;             :   as a parameter.
;;; 2011.04.26 Dan
;;;             : * Updated to use millisecond timer for production times.
;;; 2011.04.28 Dan
;;;             : * Suppress warnings about extending productions at initial load.
;;; 2011.05.16 Dan
;;;             : * Changed the formatting of the utility trace slightly.
;;; 2011.06.02 Dan
;;;             : * Added a reward-hook parameter to allow one to override the
;;;             :   effective reward provided to a production.
;;; 2011.08.03 Dan
;;;             : * Added a reward-notify-hook which can hold multiple functions
;;;             :   and gets called during propagate-reward with the reward value.
;;; 2012.03.14 Dan
;;;             : * Added the ability to set :reward to t using spp to be the
;;;             :   equivalent of calling trigger-reward with nil (can't set the
;;;             :   :reward parameter to nil since that's the default and seems
;;;             :   like a bad idea to change that).  
;;;             : * That change requires making a change to how production 
;;;             :   compilation determines whether or not there is a :reward 
;;;             :   setting for the new production.  The old rule still holds --
;;;             :   if they're both numbers then it's the max, and if only one is
;;;             :   a number then the new one gets that number (now that means
;;;             :   the other can be either t or nil).  The new change is that now 
;;;             :   if neither is a number but at least one is t then the new
;;;             :   production will get a value of t.
;;; 2012.06.18 Dan
;;;             : * Fixed a bug with the test for warning about parameter changes 
;;;             :   so that it warns if there are any productions defined.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; General:
;;;
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Description:
;;;
;;; See the new-utility.doc file for all the details, but the general idea is
;;; that instead of tracking P and C and effectively boolean successes/failures
;;; we collapse things into one value called utility and have the successes and
;;; failures represented numerically as a single reward value.
;;; [Note the variable noise value discussed in the doc is not implemented at this
;;; time.]
;;;
;;; Thus, a production only has a utility value, U, which is adjusted over time based
;;; on the rewards received.  The equation for the learning of production i is now:
;;;
;;; Ui(n) = Ui(n-1) + A[Ri(n) - Ui(n-1)]
;;; 
;;; A := the learning rate set by a parameter (defaults to .2).
;;; Ri(n) := the effective reward value of production i at time n.  Like the old 
;;; mechanim, the learning only occurs when directly triggered and all productions 
;;; that have fired since the last "learning event" are updated.  The effective
;;; reward is the reward value at time n minus the time since the selection of
;;; production i.
;;; Ui(0) := set by a parameter (defaults to 0).
;;;
;;; The new meechanism only needs the following general parameters:
;;;
;;; dat := default action time, same as before - the default time to fire a 
;;;        production.
;;; egs := the expected gain s value, same as before.
;;;
;;; utility-hook := a function that can bypass the utility computation for
;;;                 productions, same as before.
;;;
;;; ul := utility learning, either t or nil to indicate whether or not to 
;;;       apply the equation above.  There is no decaying version as there is with
;;;       the old parameter pl at this time.
;;; alpha := the learning rate for the equation above.  Note this differs from
;;;          the alpha parameter in the old system because that only applied to
;;;          the production compilation mechanism.
;;; iu := the default U(0) value for an initial (user defined) production.
;;; nu := the default U(0) value for a newly learned (production compilation) production.
;;; reward-hook := a function to bypass the R(n) calculation.
;;;
;;; In addition, this parameter from the old system is now available again
;;; 
;;; ut := the utility threshold.  Productions with a utility less than this
;;;       value will not be chosen during conflict resolution.
;;;
;;; ult := The utility learing trace parameter.  When a reward occurs
;;;        display the utility changes which occur.
;;;
;;; For a production, only the following parameters are needed for spp:
;;;
;;; at := The action time of the production, how long between the production's
;;;       selection and when it fires.  Can be set directly, defaults to :dat.
;;; name := returns the name of the production.  Cannot be set directly.
;;; u := the current U(n) value for the production.  Can be set directly.
;;; utility := the last computed utility value of the production during 
;;;            conflict resolution.  Cannot be set directly.
;;; reward := a reward value to apply when this production fires if set to a
;;;           non-nil value (the default is nil).  Can be set directly.
;;;
;;;
;;; Note: in the old system there was a parameter called value which
;;; was used to order productions if :esc was nil, but now the u value is used
;;; regardless of the setting of esc.  However learning cannot take palce if esc
;;; is nil.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 
;;; The code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#+:packaged-actr (in-package :act-r)
#+(and :clean-actr (not :packaged-actr) :ALLEGRO-IDE) (in-package :cg-user)
#-(or (not :clean-actr) :packaged-actr :ALLEGRO-IDE) (in-package :cl-user)


;;; since this module uses :esc make sure that module is available
(require-compiled "CENTRAL-PARAMETERS" "ACT-R6:support;central-parameters")

;;; The structure that is the utility module

(defstruct utility
  egs esc dat ul ut
  iu nu alpha
  utility-hook 
  reward-hook
  history
  offsets
  trace
  reward-notify-hooks)

;;; Functions necessary to support the production parameters


(defun get-default-action-time (p)
  (declare (ignore p))
  (car (no-output (sgp :dat))))

(defun get-default-u (p)
  (declare (ignore p))
  (car (no-output (sgp :iu))))

;;; The parameters added to productions for utility purposes

(suppress-extension-warnings)

(extend-productions utility)
(extend-productions u :default-function get-default-u)
(extend-productions at :default-function get-default-action-time)
(extend-productions reward)

(unsuppress-extension-warnings)

;;; The functions that are required by the procedural system

(defun note-production-selection (production)
  (let ((u (get-module utility)))
    (when (and u (utility-ul u))
      (push-last (cons production (mp-time-ms)) (utility-history u)))))


(defun productions-action-time (production)
  (production-at production))

(defun compute-utility (production &optional (save nil))
  (let ((u (get-module utility)))
    (when u
      (let* ((over-ride (awhen (utility-utility-hook u)
                          (funcall it production)))
             (utility (if (numberp over-ride)
                          over-ride
                        (+ (if (and (utility-esc u) (utility-egs u) (not (zerop (utility-egs u))))
                               (act-r-noise (utility-egs u))
                             0)
                           (production-u production)
                           (if (utility-offsets u)
                               (reduce #'+ (mapcar  (lambda (x) 
                                             (let ((val (funcall x production)))
                                               (if (numberp val)
                                                   val
                                                 0)))
                                             (utility-offsets u)))
                             0)))))
        (if save
            (setf (production-utility production) utility)
          utility)))))


(defun minimum-utility ()
  (let ((u (get-module utility)))
    (when u
      (utility-ut u))))

(defun linear-update-utility (module production reward)
  (let ((old (production-u production))
        (alpha (utility-alpha module)))
    (setf (production-u production) (+ old (* alpha (- reward old))))))

(defun trigger-reward (value)
  (cond ((null (current-model))
         (print-warning "No current model.  Trigger-reward has no effect."))
        ((numornil value)
         (schedule-event-relative 0 'propagate-reward :module 'utility
                                  :priority :max :params (list value)
                                  :output 'medium)
         t)
        (t
         (print-warning "Trigger-reward must be called with a number or nil."))))
                           
(defun propagate-reward (value)  
  (let ((u (get-module utility)))
    
    (dolist (hook (utility-reward-notify-hooks u))
      (funcall hook value))
    
    (if (numberp value)
        (if (and (utility-esc u) (utility-ul u))
            (progn
              (when (utility-trace u)
                (model-output " Utility updates with Reward = ~f   alpha = ~f" value  (utility-alpha u)))
              
              (dolist (p-t (utility-history u))
                (let* ((override (and (utility-reward-hook u) (funcall (utility-reward-hook u) (car p-t) value (ms->seconds (- (mp-time-ms) (cdr p-t))))))
                       (r (if (numberp override) override (- value (ms->seconds (- (mp-time-ms) (cdr p-t)))))))
                  
                  (when (utility-trace u)
                    (model-output "  Updating utility of production ~S" (car p-t))
                    (model-output "   U(n-1) = ~f   R(n) = ~f [~:[~f - ~f seconds since selection~;~f - ~f from reward-hook function~]]" 
                                  (production-u (car p-t)) r override value (ms->seconds (- (mp-time-ms) (cdr p-t)))))
                  (linear-update-utility u (car p-t) r)
                  (when (utility-trace u)
                    (model-output "   U(n) = ~f" (production-u (car p-t))))))
              (setf (utility-history u) nil))
          (print-warning "Trigger-reward can only be used if utility learning is enabled."))
      (if value
          (print-warning "Trigger-reward must be called with a numeric value or nil not ~S." value)
        (progn
          (when (utility-trace u)
            (model-output "  Null reward clears utility learning history."))
          (setf (utility-history u) nil))))))


(defun learn-parameters (production)
  (awhen (production-reward production)
         (if (numberp it) (trigger-reward it) (trigger-reward nil))))


;;;Added by Chris Dancy for ACT-RPhi module
;;Added to add affect params to compiled production
(defun initialize-affect-for-compiled-production (new-p p1 p2)
 (let* ((sVal1 (production-sValue p1))
	    (sVal2 (production-sValue p2))
		(sVal (if (and (numberp sVal1) (numberp sVal2)) (max sVal1 sVal2)
			  (if (numberp sVal1) sVal1 (if (numberp sVal2) sVal2 nil))))
		(sFunc (if sVal 'cond nil))
		(fVal1 (production-fValue p1))
		(fVal2 (production-fValue p2))
		(fVal (if (and (numberp fVal1) (numberp fVal2)) (max fVal1 fVal2)
			(if (numberp fVal1) fVal1 (if (numberp fVal2) fVal2 nil))))
		(fFunc (if fVal 'cond nil)))
  (setf (production-sFunction new-p) sFunc)
  (setf (production-sValue new-p) sVal)
  (setf (production-fValue new-p) fVal)
  (setf (production-fFunction new-p) fFunc)))

;Changed to make call to initialize-affect function
(defun initialize-utility-for-compiled-production (new-p p1 p2)
  (let* ((at1 (production-at p1))
         (at2 (production-at p2))
         (at (max at1 at2))
         (r1 (production-reward p1))
         (r2 (production-reward p2))
         (reward (or (and (numberp r1)
                          (numberp r2)
                          (max r1 r2))
                     (and (numberp r1) r1)
                     (and (numberp r2) r2)
                     r1 r2)))
	(initialize-affect-for-compiled-production new-p p1 p2)
    (setf (production-at new-p) at)
    (setf (production-reward new-p) reward)
    (setf (production-u new-p) (car (no-output (sgp :nu))))))


(defun update-utility-for-compiled-production (p3 p1 p2)
  (declare (ignore p2))
  (let ((u (get-module utility)))
    (when (and u (utility-ul u))
      
      (when (utility-trace u)
        (model-output "  Updating utility of production ~S from production compilation" p3)
        (model-output "    U(n-1) = ~f   R(n) = ~f [U(n) for first parent] alpha = ~f" (production-u p3)
                      (production-u p1) (utility-alpha u)))
      
      (linear-update-utility u p3 (production-u p1))
      
      (when (utility-trace u)
        (model-output "    U(n) = ~f" (production-u p3))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Here's the actual module and associated support code

(defun utility-module-params (u param)
  (cond ((consp param)
         
         ;; Changing utility parameters may lead to
         ;; to possibly trigger conflict-resolution if it were waiting
         
         (un-delay-conflict-resolution)
         
         (when (and (> (length (all-productions)) 0)
                    (member (car param) '(:esc :dat :ul :iu)))
           (model-warning "Changing procedural parameters when productions exist unsupported.")
           (model-warning "Results may not be what one expects."))
         
         (case (car param)
           (:esc (setf (utility-esc u) (cdr param)))
           
           (:egs (setf (utility-egs u) (cdr param)))
           (:ul (setf (utility-ul u) (cdr param)))
           (:dat (setf (utility-dat u) (cdr param)))
           (:iu (setf (utility-iu u) (cdr param)))
           (:nu (setf (utility-nu u) (cdr param)))
           
           (:alpha (setf (utility-alpha u) (cdr param)))
           (:ult (setf (utility-trace u) (cdr param)))
           
           (:ut (setf (utility-ut u) (cdr param)))
           
           (:utility-hook 
            (when (and (cdr param) (utility-utility-hook u))
              (print-warning 
               "Utility-hook was set to ~S and is being overwritten"
               (utility-utility-hook u)))
            (setf (utility-utility-hook u) (cdr param)))
           (:reward-hook 
            (when (and (cdr param) (utility-reward-hook u))
              (print-warning 
               "Reward-hook was set to ~S and is being overwritten"
               (utility-reward-hook u)))
            (setf (utility-reward-hook u) (cdr param)))
           (:utility-offsets
            (if (cdr param)
              (if (member (cdr param) (utility-offsets u))
                (print-warning 
                 "Setting parameter ~s failed because ~s already on the hook."
                 :activation-offsets
                 (cdr param))
                (push (cdr param) (utility-offsets u)))
              (setf (utility-offsets u) nil)))
           (:reward-notify-hook
            (if (cdr param)
              (if (member (cdr param) (utility-reward-notify-hooks u))
                (print-warning 
                 "Setting parameter ~s failed because ~s already on the hook."
                 :reward-notify-hook
                 (cdr param))
                (push-last (cdr param) (utility-reward-notify-hooks u)))
              (setf (utility-reward-notify-hooks u) nil)))))
        (t 
         (case param
           (:egs  (utility-egs u))
           (:ul (utility-ul u))
           
           (:iu (utility-iu u))
           (:nu (utility-nu u))
           
           (:ut (utility-ut u))

           (:alpha (utility-alpha u))
           (:ult (utility-trace u))
           (:utility-hook (utility-utility-hook u))
           (:reward-hook (utility-reward-hook u))
           (:utility-offsets (utility-offsets u))
           (:reward-notify-hook (utility-reward-notify-hooks u))))))


(defun reset-utility-module (u)
  (setf (utility-history u) nil))


(define-module-fct 'utility nil
  (list (define-parameter :esc :owner nil)
        
        (define-parameter :egs :valid-test #'numberp :default-value 0.0
          :warning "a number" :documentation "Expected Gain S")
        (define-parameter :ul :valid-test #'tornil :default-value nil
          :warning "T or nil" :documentation "Utility learning switch")
        (define-parameter :dat :owner nil)
        (define-parameter :iu :valid-test #'numberp :default-value 0
          :warning "a number" :documentation "default U(0) value for an initial (user defined) production")
        (define-parameter :nu :valid-test #'numberp :default-value 0
          :warning "a number" :documentation "default U(0) value for a newly learned production")
        
        (define-parameter :ut :valid-test #'numornil :default-value nil
          :warning "a number or nil" :documentation "Utility Threshold")

        (define-parameter :alpha :default-value .2
          :valid-test #'numberp :warning "a number"
          :documentation "Production learning rate")
        
        (define-parameter :ult :valid-test #'tornil :default-value nil
          :warning "T or nil" :documentation "Utility learning trace")
        
        (define-parameter :utility-hook :valid-test #'fctornil 
          :default-value nil
          :warning "a function or nil" 
          :documentation "Utility computation hook")
        (define-parameter :reward-hook :valid-test #'fctornil 
          :default-value nil
          :warning "a function or nil" 
          :documentation "Reward value hook")
        (define-parameter :utility-offsets :valid-test #'fctornil 
          :default-value nil
          :warning "a function or nil" 
          :documentation "Add additional utility equation components")
        (define-parameter :reward-notify-hook :valid-test #'fctornil 
          :default-value nil
          :warning "a function or nil" 
          :documentation "Functions to call when there is a reward provided"))
  
  :version "2.2" 
  :documentation  "A module that computes production utilities"
    
  :creation (lambda (x) (declare (ignore x)) (make-utility))
  :reset #'reset-utility-module
  :params #'utility-module-params)

(register-subsymbolic-parameters :egs :ul :iu :nu :alpha)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This section is all support for spp 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Taken and modified from ACT-R 5 code

(defun production-parameter-fct (production-name &optional parameters)
  "Returns the value of the production parameter(s), or print all if none given."
  (let ((esc (car (no-output (sgp :esc))))
        (ul (car (no-output (sgp :ul))))
        (value nil)
        (values nil))
    (cond (production-name
           (command-output "Parameters for production ~S:" production-name)
           
           (cond (parameters
                  (dolist (parameter parameters)
                    (setf value 
                      (case parameter
                        (:name production-name)
                        (:utility (production-utility production-name))
                        (:u (production-u production-name))
                        (:at (production-at production-name))
                        (:reward (production-reward production-name))
                        
                        (t (print-warning "NO PARAMETER ~A DEFINED FOR PRODUCTIONS." parameter)
                           :error)))
                    (push-last value values)
                    (command-output " ~S ~6,3F" parameter value))
                  values)
                 (t
                  (when esc 
                    (command-output " :utility ~6,3F" (production-utility production-name)))

                  (command-output " :u  ~6,3F" (production-u production-name))
                  (command-output " :at ~6,3F" (production-at production-name))
                  
                    
                  (when (and esc ul)
                    (command-output " :reward ~6,3F" (production-reward production-name)))
                  
                  production-name)))
          (t :error))))

;;; This name is dangerously close to what I use in the sgp code, but
;;; for now I'll leave it as is.

(defmacro set-parameter (slot parameter test warning &rest housekeeping)
  "Sets parameter of production p in slot if value passes test, otherwise issue warning."
  `(cond (,test
          (setf (,slot p) value)
          ,@housekeeping
          value)
         (t
          (print-warning
           "PARAMETER ~A CANNOT TAKE VALUE ~S BECAUSE IT MUST BE ~:@(~A~)."
           ,parameter value ,warning)
          :error)))


(defun parameters-fct (p parameters)
  "Sets the parameters of the production (internal - user should use spp)."
  
  ;; Changing procedural parameters reschedules conflict resolution
  ;; if it's waiting to happen
         
  (un-delay-conflict-resolution)
         
  ;; Having the name of the production be p and the
  ;; value to be set called value are critical to
  ;; the functioning of the set-parameter macro...
  
  (let ((values nil)
        (ul (car (no-output (sgp :ul)))))
    (if p
        (loop
          
          (unless parameters 
            (return values))
        
        (let* ((parameter (pop parameters))
               (value (pop parameters)))
          
          ;; not sure about this, but I'll leave it in for now
          
          (when (and (listp value) 
                     (eq (first value) 'quote))
            (setf value (second value)))  
          
          (push-last
           (case parameter
             (:name
              (print-warning "PARAMETER NAME CANNOT BE SET.")
              :error)
             (:utility
              (print-warning "PARAMETER UTILITY CANNOT BE SET.")
              :error)
             
             
             (:u
              (set-parameter production-u :u
                             (numberp value) "a number"))
             
             (:at
              (set-parameter production-at :at
                             (nonneg value)
                             "a positive number"))
             
             (:reward
              (if ul
                  (set-parameter production-reward :reward
                                 (or (eq value t) (numberp value))
                                 "a number or t")
                (print-warning "PARAMETER REWARD CAN ONLY BE SET WHEN UL IS T.")))
             
             (t
              (print-warning
               "NO PARAMETER ~A DEFINED FOR PRODUCTIONS." parameter)
              :error))
           values)))
      :error)))


(defmacro spp (&rest production-parameters)
  "Inspects and sets production parameters."
  `(spp-fct ',production-parameters))


(defun spp-fct (parameters)
  "Inspects and sets production parameters."
  (let ((results nil))  
    (if (null parameters) ; print all parameters for all productions
        (dolist (production (all-productions))
          (push-last (production-parameter-fct production) results))
      (dolist (description (if (or (keywordp (first parameters))
                                   (keywordp (second parameters))
                                   (and (listp (first parameters))
                                        (null (second parameters))
                                        (not (keywordp 
                                              (second 
                                               (first parameters))))))
                               (list parameters) parameters))
        (when (atom description) (setf description (list description)))
        (if (keywordp (first description))
            (dolist (production (all-productions))
              (push-last
               (if (and (cdr description) 
                        (not (keywordp (second description))))
                   (parameters-fct production description)
                 (production-parameter-fct production description))
               results))
          
          (dolist (production (if (atom (first description))
                                  (list (first description))
                                (first description)))
            (if (get-production production)
                (push-last
                 (if (and (cddr description) 
                          (not (keywordp (third description))))
                     (parameters-fct production (rest description))
                   (production-parameter-fct production (rest description)))
                 results)
              (progn
                (model-warning "Spp cannot adjust parameters because production ~S does not exist" production)
                (push-last :error results)))))))
    results))




#|
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
|#