Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  ·...

24
Problem-Solving: A* and Beyond CSCI 3202, Fall 2010

Transcript of Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  ·...

Page 1: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Problem-Solving: A* and Beyond�

CSCI 3202, Fall 2010

Page 2: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Administrivia

•  Reading for this week: 4.1.1-4.1.3 (up to p. 126) in R+N; 5.1-5.4 (pp. 161-176)

•  Problem Set 1 will be posted by tomorrow morning; due September 23 in class. HARD COPY ONLY, please.

Page 3: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

The Basic Idea Behind A* Search 1)  Devise an "estimation" function, h, that gives you a plausible

underestimate of the distance from a given state to the goal.

2)  Call the cost of getting from the initial state to the current state g.

3)  To measure the quality of a given state s, sum the cost (g) of arriving at this state and the estimate (h) of the cost required to get from s to the goal.

4)  Use best-first search, where g+h is the overall quality function.

Page 4: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Try to find a metric that will be a close (informative) one,but that won’t overestimate (it should be optimistic).�Example: 8-9 puzzle� Number of misplaced tiles� Sum of Manhattan distance of misplaced tiles (better)� Best solution length for (say) tiles 1-4�Example: Choosing a path on a map from city A to B� Euclidean distance to B.

How do you estimate distance to the goal? �(Or: What’s that “h” function?)

Page 5: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

AStar-Best-First-Search [Nodes] IF there are no more nodes THEN FAIL ThisNode <-- First(Nodes) IF ThisNode is the goal THEN return ThisNode ELSE ChildNodes <-- Children(ThisNode) Astar-Best-First-Search (Sort-by-Lowest-Cost-plus-Estimate ChildNodes Remove-Node(ThisNode Nodes))

Page 6: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

A few handy heuristics •  Redefine the “edges” (moves) of the problem

graph, as by chunking (e.g., Rubik’s cube) •  Eliminate chunks of the problem graph, as by

constraints (e.g., SEND+MORE = MONEY problem)

•  Decompose the problem into subproblems (Rubik’s cube again; or Rush Hour)

Page 7: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Means-Ends Analysis

A technique for finding the next step to take in a search process. Here, we find the most important difference between our current state and the goal state and use that difference (recursively) to dictate our next move.

Page 8: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

To SOLVE a problem: If the current state is the goal state, then we’re done. If not, first REDUCE the difference between the current state and goal state to produce a new current state. Then SOLVE the problem from the (presumably better) current state and the original goal.

To REDUCE the difference between the current state and the goal state, find the most important difference between the current state and the goal state. Find an operator that tends to reduce that difference, and APPLY that operator to the current state.

To APPLY an operator to the current state, see if there are any differences between the current state and any preconditions for the operator. If there are, REDUCE the difference between the current state and the preconditions for the operator. If not return the result of APPLYing the operator directly to the current state.

Page 9: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Local search

• Up until now, we’ve imagined a two-phase method: find a solution path (search the problem space graph), and then return that path so that it can be executed. In local search, we simply search the graph directly, hoping to land on a goal state.

•  Two simple methods: hill-climbing and beam search.

Page 10: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Hill-Climb [Current-Node]IF Current-Node is the goal

THEN return Current-NodeELSE

ChildNodes Children (Current-Node)BestChild LooksBest (ChildNodes)Hill-Climb [BestChild]

Beam-Search [Set-of-Nodes]If Set-of-Nodes contains a goal state

THEN return that stateELSE

AllChildNodes Find-All-Children (Set-of-Nodes)BestNChildren N-Best-Looking (AllChildNodes)Beam-Search (BestNChildren)

Page 11: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

An Example of Hill-Climbing: the Word Arithmetic Problem

Let each node of the problem space graph be a candidate solution:

SD*ENM*OYR 0123456789 Each edge from this node leads to a node in which two

assignments have been switched:

SY*ENM*ODR 0123456789

Page 12: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Hill-Climbing Example, Continued

Now we hill-climb by starting at some candidate solution and moving to the adjacent solution that causes the fewest arithmetic errors.

Page 13: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Problem-Solving: The Cognitive Side

• Do we actually use (say) depth-first search? Or breadth-first search? Or A*?

• Is the problem-space formalism useful,or natural?

• How do we solve problems, anyhow?

Page 14: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

"A man gets an unsigned letter telling him to go to the local graveyard atmidnight. He does not generally pay attention to such things, but compliesout of curiosity. It is a deathly still night, lighted by a thin crescent moon.The man stations himself in front of his family's ancestral crypt. The man isabout to leave when he hears scraping footsteps. He yells out, but no oneanswers. The next morning, the caretaker finds the man dead in front of thecrypt, a hideous grin on his face.

Did the man vote for Teddy Roosevelt in the 1904 U.S. presidentialelection?"

-- Poundstone

Page 15: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Hanging from the ceiling are two lengths of string, each six feet long. Thestrings are ten feet apart. As the room is ten feet high, the lower ends of thestrings hang four feet above the floor.

On the floor is a Swiss Army knife, a firecracker, a small glass of water, atwenty-five pound block of ice in an aluminum pan, and a friendly Labradorretriever.

Your job is to tie the two ends of the two strings together.

-- based on Poundstone,"Labyrinths of Reason"

Page 16: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

All astronomers are stamp collectors.

No baseball players are astronomers.

∴ ???

Page 17: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Other ways of looking at the "search" issue;"insight" problems

• Retrieving schemas/searching for patterns

• Restructuring the problem space itself

• Functional fixedness and the "Einstellung" effect

Page 18: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Two Physics Problems

Rolling a quarter around another quarter...

Elastic collision (ping-pong vs. bowling ball)

Page 19: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Mr. Smith and his wife invited four other couples for aparty. When everyone arrived, some of the people in theroom shook hands with some of the others. Of course,nobody shook hands with their spouse and nobody shookhands with the same person twice.

After that, Mr. Smith asked everyone how many times theyshook someone’s hand. He received different answers fromeverybody.

How many times did Mrs. Smith shake someone’s hand?

From Michalewicz and Fogel, How to Solve It: Modern Heuristics

Page 20: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Game-Playing: the Essential Arsenal

•  The idea of a “minimax search” • Avoiding useless search: the alpha-beta

algorithm • Other ways of pruning the search tree • Other central issues in game-playing

Page 21: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

The Game of Sim

Page 22: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

In Scheme:

(define (minimax gamestate myfunction otherfunction level) (cond ((= level 0) (getvalue gamestate)) (else (let* ((successors (getnextmoves gamestate)) (othervals (map (lambda (successor) (minimax successor otherfunction myfunction (- level 1))) successors))) (apply myfunction othervals)))))

Page 23: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

(define (alpha-beta alpha beta level node max?) (cond ((= level 0) (static-value node)) (max? (let ((children (find-next-moves node #t))) (max-look-at-each-child-node children alpha beta level))) (else (let ((children (find-next-moves node #f))) (min-look-at-each-child-node children alpha beta level)))))

(define (max-look-at-each-child-node children alpha beta level) (cond ((null? children) alpha) ((>= alpha beta) alpha) (else (let ((next-value (alpha-beta alpha beta (- level 1) (first children) #f))) (max-look-at-each-child-node (rest children) (max next-value alpha) beta level)))))

(define (min-look-at-each-child-node children alpha beta level) (cond ((null? children) beta) ((>= alpha beta) beta) (else (let ((next-value (alpha-beta alpha beta (- level 1) (first children) #t))) (min-look-at-each-child-node (rest children) alpha (min next-value beta) level)))))

Page 24: Problem-Solving: A* and Beyondl3d.cs.colorado.edu/~ctg/classes/aif10/lecslides/AIF10... ·  · 2010-09-21To SOLVE a problem: If the current state is the goal state, ... The man stations

Calling alpha-beta with starting values of a (very low) alpha value, a (very high) beta value, a depth of 5, the starting game state, and from the point of view of the maximizer.

(alpha-beta -1000 1000 5 initial-game-state #t)