;;; ********************************************************************* ;;; Susan Fox ;;; December 27, 2000 ;;; File: robot.ss ;;; This file defines the basic robot "action loop" ;;; ;;; Requires the memory files to have been loaded, and the map ;;; and the state ADT file (if (not (top-level-bound? 'CASEMEM.SSLOADED)) (load "casemem.ss")) (if (not (top-level-bound? 'STATE.SSLOADED)) (load "state.ss")) (define ROBOT.SSLOADED #t) ;;; ********************************************************************* ;;; The basic action loop is very simple.. just check if a new ;;; situation has occurred. If it has not, then simply update the ;;; current state information from the robot end of things and continue ;;; Otherwise, determine a new action... ;;; The State ADT here does the bulk of the work. update-state is an ;;; operation of the State ADt (define act-loop (lambda (state old-state) (if (new-situation? state old-state) (act-loop (get-new-actions state) state) (act-loop (update-state state) state)))) ;;; get-new-actions takes a state, builds a retrieval index, and ;;; then retrieves a new case from memory and takes action appropriately ;;; It does not necessarily update the state... ;;; (define get-new-actions (lambda (state) (let ([new-case (find-best-case (build-plan-index state))]) (cond [(behavior-case? new-case) (install-behavior new-case state)] [(plan-case? new-case) (install-plan new-case state)] [else (invoke-meta-plan new-case state)])))) ;;; find-best-case takes an index, retrieves the matching cases, and ;;; selects the one with the lowest distance value ;;; (define find-best-case (lambda (indx) (letrec ([find-best (lambda (case-list best-case best-score) (cond [(and (null? case-list) (not best-case)) (error 'find-best-case "NO CASES IN MEMORY MATCHED!!")] [(null? case-list) best-case] [(not best-case) (find-best (cdr case-list) (car case-list) (match? (car case-list) indx))] [else (let ([score (match? (car case-list) indx)]) (if (< best-score score) (find-best (cdr case-list) best-case best-score) (find-best (cdr case-list) (car case-list) score)))]))]) (find-best (lookup-cases indx case-memory) #f #f)))) ;;; install-behavior calls the robot's code with a new set of behaviors ;;; to be activated. ;;; (define install-behavior (lambda (beh-case old-state) (turn-off (map behav-name (behaviors-of (plan-part old-state)))) (for-each (lambda (behav) (activate-behavior behav)) (behavior-list beh-case)) (change-behaviors old-state (behavior-list beh-case))))