44
Lecture 4-2 CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st , 1999 CS250

Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Embed Size (px)

Citation preview

Page 1: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Better Living through Search II

Lecture 4-2

October 21st, 1999

CS250

Page 2: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Announcements

• Assignment #2 answers

• Assignment #3 posted

• Quizzes

Page 3: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Getting around Romania

Zerind

Page 4: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Vacuum Cleaner Example• Percepts

– Vacuum can sense if it's touching something (1) or not (0)

– Dirt under it (1) or not (0) – Home (1) or not

• Actions (Operators)– Movement– Turn off– Suck dirt

• Environment (State space)– Grid of squares

Page 5: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

What Agents Know

• World state– Sensors return percepts– Sometimes assume we know everything

about the world

• Action consequences– If I do X, then Y

Page 6: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Variations on a Theme

• Perfect world information

• Perfect foresight

• Limited world information

• Perfect foresight

Single-state problem

Multiple-state problem

Page 7: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Contingency Problems

• Can’t anticipate everything– Outside agents– Broken sensors– Changing environment

• Can’t predict effects of own actions

• Examples– IJCAI ‘95 Robot competition– Chicago drivers

Page 8: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

No Clue about Action Effects

• No anticipation of action effects

• Exploration & experimentation

• Importance of feedback– Interleaving

• Examples

Page 9: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Well-Defined Problems

• Problem: Information needed by an agent to decide what to do

• Problem def’n = States + Actions

Initial stateOperators

State space

Page 10: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

“Are we there yet?”

• Goal test: Have we reached the goal– Set of states {goal-state1, …, goal-staten}

– Property: checkmate

Page 11: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

What does it cost?

• Some paths are better than others

Page 12: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

8-Puzzle

5 46 1 87 3 2

1 2 38 47 6 5

• States?

• Operators?

• Goal test?

• Path cost?

Page 13: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

8-Queens

• Place 8 queens on a chess board, with no one attacking any other

• Goal test?

• Path cost?

• States

• Operators?

Page 14: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Cryptarithmetic

• Puzzles with numbers replaced by letters

FORTY+ TEN+ TEN=====SIXTY

Page 15: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Missionaries & Cannibals

• Three missionaries and three cannibals wish to cross a river in a boat that won’t hold all three

• States?

• Operators?

• Goal test?

• Path cost?

Page 16: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Real-World Problems

• Route finding

• TSP

• VLSI

• Robot navigation

• Paint booth scheduling

Page 17: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Search Trees

S ta te S p ace

S ta te 1 ,1 ,1 S ta te 1 ,1 ,2

S ta te 1 ,1 S ta te 1 ,2

S ta te 1

S ta te 2 ,2

S ta te 2 S ta te 3

S ta te 0 ,0

(In it ia l s ta te )

Page 18: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

What’s your Problem?

• Initial state

• Operators

• Goal test

• Path cost function

Page 19: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

What’s in a Node?

• State - what state are we in?

• Parent - Which node generated the current node?

• Operator - Action which generated node from parent

• Node depth - Number of nodes from root

• Cost - Path cost from initial node

Page 20: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

(defstructure node "Node for generic search. A node contains a state, a domain- specific representation of a point in the search space. A node also contains bookkeeping information such as the cost so far (g-cost) and estimated cost to go (h-cost). " (state (required)) ; a state in the domain (parent nil) ; the parent node of this node (action nil) ; the domain action leading to state (successors nil) ; list of successor nodes (unexpanded nil) ; not yet examined (SMA* only) (depth 0) ; depth of node in tree (root = 0) (g-cost 0) ; path cost from root to node (h-cost 0) ; estimated dist. from state to goal (f-cost 0) ; g-cost + h-cost (expanded? nil) ; any successors examined? (completed? nil) ; all examined? (SMA* only) )

The Glorious node Structure

Page 21: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Lieutenant Node & General Search

function GENERAL-SEARCH(problem, QUEUEING-FN) returns solution or failure

nodes MAKE-QUEUE(MAKE-NODE(INITIAL-STATE[problem]))

loop do

if nodes is empty then return failure

node REMOVE-FRONT(nodes)

if GOAL-TEST[problem] applied to STATE(node) succeeds then return node

nodes QUEUING-FN(nodes, EXPAND(node, OPERATORS[problem]))

end

Page 22: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Coding Goals

• Create the initial node list– Need the right ordering

• Write a loop

• If test

• Functions for:– QUEUEING-FN, REMOVE-FRONT, GOAL-TEST,

EXPAND, MAKE-NODE

Page 23: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

In Lisp...

(defun general-search (problem queuing-fn)"Expand nodes according to the specification of PROBLEM until we find a solution or run out of nodes to expand.The QUEUING-FN decides which nodes to look at first. [p 73]”

(let ((nodes (make-initial-queue problem queuing-fn))node)

(loop (if (empty-queue? nodes) (RETURN nil))(setq node (remove-front nodes))(if (goal-test problem (node-state node)) (RETURN node))(funcall queuing-fn nodes (expand node problem)))))

Page 24: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

A Few Lisp Ends

(setq var1 form1 var2 form2 ...)

special form: A list (but not a macro), which is a form with special syntax or special evaluation rulesor both, possibly manipulating the evaluation environment or control flow or both.

The first element of a special form is a special operator.

Page 25: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Dumb & Smart Searching

• Does the search algorithm use information beyond GOAL-TEST?– Uninformed/blind searching does not– Informed/heuristic search does

Page 26: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Factors in Search Strategy

• Completeness - Are we guaranteed to find a solution, if one exists?

• Time complexity - How long does it take?

• Space complexity - How much memory?

• Optimality - Find the best of several solutions?

Page 27: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Different Strokes for Different Searches

• Why is GENERAL-SEARCH general?

Page 28: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Breadth-First Search

• Go sideways before you go down– How do we do this in GENERAL-

SEARCH?

• How good is it?– Completeness?– How long does it take?– How much memory?– Optimality?

Page 29: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Uniform Cost

• BFS: No shorter path is a solution, but what about cost?

• Uniform cost expands the lowest cost node

• When is BFS also uniform?

g(node) = DEPTH(node)

Page 30: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Depth-first Search

• Go down before you go sideways

• Saves memory over BFS - why?

• How good is it?– Completeness?– How long does it take?– Optimality?

• Depth-limited search

Page 31: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Iterative Deepening Search

• Repeated depth-limited search– Depth 0, depth 1, depth 2, …

• Same states expanded multiple times– Higher branching factors make repeated

expansion less important

• How good is it?

• Good choice when:– Big search space– Unknown solution depth

Page 32: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Bidirectional Search

• Start from the beginning and the end, meet in the middle

• Divide and conquer– Cut the depth of each search in half– From O(bd) to O(2bd/2)– For b = 10, d = 6:

• BFS searches 1,111,111• Bidirectional searches 2,222

Page 33: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Sounds Great, Where do I Sign Up?

• Searching backwards?

• Calculating predecessors can be tricky

• Many goal states are a problem

• Generate a node and ask, “Is it in the other half of the tree?”

• Different halves can have different searches

Page 34: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Search Comparison

Criterion BFS UniformCost

DFS Depth-Limited

IterativeDeepening

Bidirectional

Time bd bd bm bl bd bd/2

Space bd bd bm bl bd bd/2

Optimal? Yes Yes No No Yes YesComplete Yes Yes No Yes, if

l<=dYes Yes

d = depth of the goal

m = max tree depth

l = depth limit

Page 35: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

And the Winner is...

• Depth-first iterative deepening– Asymptotically optimal in time– Asymptotically optimal in space

among blind searches that find optimal solutions

Page 36: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Constraint Satisfaction Search

• Constraint Satisfaction Problem (CSP): additional structural properties– States are values of a set of variables– Goal test specifies constraints on variable

values• Unary - cryptarithmetic• Binary - 8-Queens• 3rd and higher-order• Absolute or just nice to have

Page 37: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Why Bother?

• Less general than the notion of ‘problem’

• More efficient algorithms– Constraints are fine-grained

Page 38: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

Domains

• Variables Vi have domains, Di

• Discrete or continuous?

Page 39: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 CS250: Intro to AI/Lisp

General Search

(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 40: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 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 41: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 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 42: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 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 43: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 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 44: Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250

Lecture 4-2 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))