Transcript lecture9

Today’s Topics
• FREE Code that will Write Your PhD Thesis,
a Best-Selling Novel, or Your Next Email
• Methods for Intelligently/Efficiently Searching
a Space of Possible Solutions
– Depth, Breadth, Best first search
• Casting a Task as a Search Problem
• An Infinite Space
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
1
Generating Great Text is Easy,
Discarding the Junk is Hard
WriteMyThesis(int expectedLengthInChars)
let text = “”
while (random() > 1.0 / expectedLengthInChars)
text += getRandomASCIIcharacter()
if (acceptable(text)) return text
else return WriteMyThesis(expectedLengthInChars)
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
2
Visualizing AI Search as Discrete Graphs
• Nodes:
an importance aspect of the problem
• Directed Arcs: legal transitions between nodes
• Weights (optional):
cost of traversing an arc
Note: nodes
usually a
complex data
structure (eg,
a tree)
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
3
Recall: Aspects of an
AI Search Algorithm
9/29/15
Search Space
Where we are looking; for now this will be a
DISCRETE SPACE
Operators
Legal ways to move from one node to another
Search Strategy
How we decide which move to make next
Heuristic
Function
Some search methods score nodes to help guide
search strategy (optional)
Start Node(s)
Where we start (usually a single node, but could
be a set)
Goal Node(s)
How we know we are done (sometimes we’ll
have an end test, ie code that says ‘DONE!’)
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
4
Another Task Viewed
as AI Search – the ‘8 Puzzle’
Start
State
1
2
3
5
6
7
4
8
Possible heuristic (for scoring nodes)?
i) Count of #’s in wrong cell
ii) Sum of moves if ‘collisions’ allowed
Legal moves: slide number into empty cell
(‘state space’ drawn on paper using document camera)
In AI we build the state space as
we go, and rarely generate the
whole space.
In HWs and textbooks, we often
are given the WHOLE space, but
that is misleading.
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
1
2
3
4
5
6
7
8
Goal
State
5
Designing Heuristics
• One good method is to think of a ‘relaxed’
(ie, simplified version) of the task
– This guides the search algo, while the search algo
works out the details of the unrelaxed version
• Eg, in ROUTE PLANNING, assume one can
‘drive as the crow flies’ directly to the goal
state (aka, ‘straight-line’ or Euclidean distance)
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
6
The KEY Question of AI Search
Given a set of search-space nodes,
which one should we ‘consider’ next?
1.
The youngest (most recently created)?
This is DEPTH-FIRST SEARCH (DFS)
2.
The oldest (least recently created)?
This is BREATH-FIRST SEARCH (BFS)
3.
A random choice?
SIMULATED ANNEALING (SA) does this
4.
The best (need some scoring function)?
This is BEST-FIRST SEARCH (BEST)
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
7
General Pseudocode
for Searching
The following is the basic outline for the various search algorithms (some steps
need to be modified depending on the specifics of the search being used).
OPEN = { startNode } // Nodes under consideration.
CLOSED = { }
// Nodes that have been expanded.
While OPEN is not empty
Remove the first item from OPEN. Call this item X.
If goalState?(X) return the solution found.
// Expand node X if it isn’t a goal state.
Add X to CLOSED. // Prevents infinite loops.
Generate the immediate neighbors (i.e., children) of X.
Eliminate those children already in OPEN or CLOSED.
Based on the search strategy, insert the remaining
children into OPEN.
Called
‘expanding’
a node
Return FAILURE // Failed if OPEN exhausted w/o a goal found.
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
8
Variations of “Return Solution”
• Might simply return SUCCESS
• Or return the GOAL node (this is what ID3 does)
• Or return PATH found from START to GOAL
eg, if planning a route to travel in a GPS
• Proper choice is problem specific
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
9
Data Structures for OPEN
Breadth-first
Use a ‘queue’ (first in, first out; FIFO)
OPEN  OPEN + RemainingChildren
Depth-first
Use a ‘stack’ (last in, first out; LIFO)
OPEN  RemainingChildren + OPEN
Best-first
Use a ‘priority queue’
OPEN  sort(OPEN + RemainingChildren)
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
10
Example (via Doc Camera)
- assume LOWER scores are better
Start
score = 9
B
score = 11
Use these
headers
for HW2,
Problem 2
Step# OPEN
D
score = 4
E
score = 3
C
score = 8
Goal
score = 0
CLOSED X CHILDREN RemainingCHILDREN
(this part done on doc camera for BFS, DFS, and BEST)
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
11
BFS - remember we fill out line n+1 while working on line n
Step# OPEN CLOSED
X CHILDREN RemainingCHILDREN
1
{S}
{ }
S
{ S, B, C}
{ B, C }
2
{ B, C } { S }
B
{D}
{D}
3
{ C, D } { S, B }
C
{G}
{G}
4
{ D, G } { S, B, C}
D
{E}
{E}
5
{ G, E} { S, B, C, D} G
DONE
- note we check for GOALS upon removal from OPEN in
order to get SHORTEST PATHS in later algo’s
BEST - we now need to record the heuristic score and sort OPEN
Step# OPEN CLOSED
1
{ S9 }
{ }
2
{ C8, B11 } { S9 }
3
{ G0, B11 } { S9, C8}
9/29/15
X CHILDREN RemainingCHILDREN
S9 { S9, B11, C8}
{ B11, C8 }
C8 { G0 }
{ G0 }
G0 DONE
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
Lecture 1, Slide 12
LOWER Better or Worse?
• Need to carefully check if lower
scores are better or worse
• Our default is lower is better, because often the
score is ‘estimated distance to goal’
• For algo’s where HIGHER is better (hill climbing,
simulated annealing), use
scoretoUse = - scoreoriginal
Think before you compute!
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
13
A ‘Blocks World’ Example
Initial State
Goal State
A
B
A
B
C
C
‘Preconditions’ of a legal move(?x, ?y) action:
clearTop(?x) ˄ clearTop(?y) ˄ ?x ≠ ?y
Heuristic?
One possibility: # blocks in correct final position
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
14
An INFINITE Space
Legal actions:
A) add TWO blocks (from an infinite bin) to an existing tower
B) add ONE block to an existing tower
Initial state: ONE block on the table (ie, a tower of height 1)
Goal state: a tower of height TWO
What might go wrong?
might produce tower of height 3, of height 5, …
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
15
A (Hollywood) Famous
AI Puzzle
• Task: put exactly 4 gallons of water
in a 5 gallon jug, given
From the
movie
‘Die Hard’
– a hose and an infinite supply of water
– a 3 gallon jug (no ‘depth’ markings on the jug)
– a 5 gallon jug
• Operators
– Can fully empty or fill either jug
– Can pour Jug ?A into Jug ?B
until ?A empty or ?B full
9/29/15
CS 540 - Fall 2015 (Shavlik©), Lecture 9, Week 4
16