Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
-
Upload
lester-mills -
Category
Documents
-
view
217 -
download
0
Transcript of Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
![Page 1: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/1.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Intelligent Agents
Lecture 3-2
October 14th, 1999
CS250
![Page 2: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/2.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Announcements
• No class on Thursday (October 7th)
• Next week– Tuesday, Thursday in class– Wednesday 7-9pm in Maclab
![Page 3: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/3.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Assignment 1
• Turing’s comments– Skimpy answers– Most support: Continuity of nervous
system, consciousness
• Undecidability and intractability
• Following instructions– Genes = programs
• Lisp code
![Page 4: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/4.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Answering Lisp Questions
• Style counts– setf is evil
• Think it works? Try it out!
• Grab your functions by the tail
![Page 5: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/5.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Intelligent Agents
• What’s an agent?– Little people in the TV
![Page 6: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/6.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
(defstructure environment "The world in which agents exist.”;; A list of the agents in the environment (agents '());; The number of time steps simulated so far (step 0);; Stop the simulation after this number (max-steps 1000);; Stream to display output on (stream t);; Have we run initialize on this environment yet? (initialized nil);; Current state of the environment; other subtypes;; add new slots to hold various state information (state nil))
![Page 7: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/7.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
(defstructure agent "Agents take actions (based on percepts and the agent program) and receive a score (based on the performance measure). An agent has a body which can take action, and a program to choose the actions, based on percepts." (program #'nothing); fn: percept -> action (body (make-agent-body)) (score 0) (percept nil) (action nil) (name nil))
![Page 8: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/8.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
(defun run-environment (env) "Basic environment simulator. It gives each agent its percept, gets an action from each agent, and updates the environment. It also keeps score for each agent, and optionally displays intermediate results. [p 48]" (initialize env) (display-environment env) (dotimes (i (environment-max-steps env)) (incf (environment-step env)) ;; Deliver percept and get action from each agent (for each agent in (environment-agents env) do
(setf (agent-percept agent) (get-percept env agent)) (setf (agent-action agent) (funcall (agent-program agent) (agent-percept agent))))
;; Execute the actions and otherwise update the world (update-fn env) ;; Update the agent scores, then optionally display the current state (for each agent in (environment-agents env) do
(setf (agent-score agent) (performance-measure env agent))) (display-environment env) (when (termination? env) (RETURN))) env)
![Page 9: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/9.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
“Get Your Red-Hot Lists Here!”
• Conses are pairs of pointers– First pointer is the car– Rest is the cdr
• Lists are conses in which:– First pointer is the first element– Second pointer is the rest of the list– No intermediate pointers makes last
expensive
USER(104): (last (list 'a 'b 'c))(C)
![Page 10: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/10.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Box & Pointer• Represent a cons graphically
(list ‘a (list ‘b ‘c) ‘d)
![Page 11: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/11.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Some Things are More Equal than Others
• Lisp has multiple definitions of equality
• Decreasing order of strictness– eq, eql, equal
![Page 12: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/12.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
eq
• True if its arguments are the same, identical object; otherwise, returns false
(eq 'a 'b) => false (eq 'a 'a) => true (eq 3 3) => true
OR => false (eq 3 3.0) => false
![Page 13: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/13.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
eql• True of two objects, x and y, in the
folowing cases:1. If x and y are eq.
2. If x and y are both numbers of the same type and the same value.
3. If they are both characters that represent the same character.
(eql 'a 'b) => false (eql 'a 'a) => true (eql 3 3) => true (eql 3 3.0) => false (eql 3.0 3.0) => true (eql #c(3 -4) #c(3 -4)) => true (eql #c(3 -4.0) #c(3 -4)) => false
![Page 14: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/14.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
equal
• Generally, returns true if two objects print the same
> (setf x (cons ‘a nil))
(A)
> (eql x x)
T
> (equal x (cons ‘a nil))
T
![Page 15: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/15.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Mapping over lists
• Need to do something to every element in a list? Try a mapping function:– mapcar for using the car of successive
cdr’s– maplist for successive cdr’s themselves
![Page 16: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/16.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
mapcar in Action
USER(115): (mapcar #'list '(a b c) '(1 2 3 4))
USER(116): (mapcar #'list '(a b c) '(1 2))
((A 1) (B 2) (C 3))
((A 1) (B 2))
![Page 17: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/17.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Creating an N-Queens Problem
(defun nqueens-initial-state (n &optional (explicit? nil) (complete? nil)) (let ((s (make-CSP-state
:unassigned (mapcar #'(lambda (var) (make-CSP-var :name var
:domain (iota n)))(iota n))
:assigned nil :constraint-fn (if explicit?
(let ((constraints (nqueens-constraints n))) #'(lambda (var1 val1 var2 val2) (CSP-explicit-check var1 val1 var2 val2 constraints)))
#'nqueens-constraint-fn)))) (if complete? (CSP-random-completion s) s)))
![Page 18: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/18.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Unassigned Variables
(mapcar #'(lambda (var)(make-CSP-var :name var
:domain (iota n)))(iota n))
USER(105): (iota 8)(0 1 2 3 4 5 6 7)
![Page 19: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/19.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Unassigned Variables II
((0 (0 1 2 3 4 5 6 7) NIL NIL) (1 (0 1 2 3 4 5 6 7) NIL NIL) (2 (0 1 2 3 4 5 6 7) NIL NIL) (3 (0 1 2 3 4 5 6 7) NIL NIL) (4 (0 1 2 3 4 5 6 7) NIL NIL) (5 (0 1 2 3 4 5 6 7) NIL NIL) (6 (0 1 2 3 4 5 6 7) NIL NIL) (7 (0 1 2 3 4 5 6 7) NIL NIL))
![Page 20: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/20.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Recursion Again
• Recursive function = Base case + Recursive step– Base case will be a conditional test, plus a
call that returns
• Example: General-Search(defun general-search-helper (problem nodes
queuing-fn) (let ((node (first nodes))) (if (null node) nil
:
![Page 21: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/21.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Recursive General Search
(if (goal-test problem (node-state node))node
(general-search-helper problem (funcall queuing-fn (rest nodes) (expand node problem)) queuing-fn)...)
If we’ve got a node, what do we do next?
What if it’s not the goal?
![Page 22: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/22.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Put it Together
(defun general-search-helper (problem nodes queuing-fn) (let ((node (first nodes))) (if (null node) nil (if (goal-test problem (node-state node))
node (general-search-helper problem
(funcall queuing-fn(rest nodes)(expand node problem))
queuing-fn)))))
![Page 23: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/23.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Getting it Started...
(let ((nodes (make-initial-queue problem queuing-fn))node)
(defun make-initial-queue (problem queuing-fn) (let ((q (make-empty-queue))) (funcall queuing-fn q
(list (create-start-node problem))) q))
From simple.lisp:
General-Search function
How does Make-Initial-Queue work?
![Page 24: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/24.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Top-level Function
(defun general-search-recursive (problem queueing-fn) "Recursive version of general search" (general-search-helper problem
(list (create-start-node problem)) queueing-fn))
![Page 25: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/25.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
BFS with a List
(defun breadth-first-search (problem) (general-search-recursive problem #'append))
What’s the rule for node exploration in BFS? How are new nodes added?
![Page 26: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/26.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Sets
• Sets let you treat lists as sets– Membership– Union, intersection– Set difference
![Page 27: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/27.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Sequences
• Sequences include more than just lists– Ordered series– Lists and vectors
• Many functions operate on sequences, not just lists:– length, sort, subseq, reverse, every, some, elt
![Page 28: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/28.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Structures
• Create records in Lisp
• Define structures with the defstruct macro:
(defstruct point
x
y)
![Page 29: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/29.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
“Big Sale on Constructors & Accessors!”
• Creating a structure creates:– Constructor (make-point)
• Arguments are passed by keyword
– Copy constructor (copy-point)– Slot accessor functions (point-x, point-y)
– Type predicate (point-p)
• New structures are new types
![Page 30: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/30.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Default Values for Structure Fields
• Add a default value
(defstruct midterm (difficulty (progn
(format t “How hard was it?”) (read))) (max-grade 54) (num-completed nil))
![Page 31: Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.](https://reader036.fdocuments.us/reader036/viewer/2022062423/56649e9e5503460f94b9f1e8/html5/thumbnails/31.jpg)
Lecture 2-1 CS250: Intro to AI/Lisp
Customize Automatic Functions
(defstruct (point (:conc-name p)
(:print-function print-point))
(x 0)
(y 0))
(defun print-point (p stream depth)
(format stream “#<~A, ~A>” (px p) (py p)))