Today’s Topics FREE Code that will Write Your PhD Thesis, a Best-Selling Novel, or Your Next 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/15CS Fall 2015 (Shavlik©), Lecture 9, Week 41
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/15CS 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 9/29/15CS Fall 2015 (Shavlik©), Lecture 9, Week 4 3 Note: nodes usually a complex data structure (eg, a tree)
CS Fall 2015 (Shavlik©), Lecture 9, Week 4 Recall: Aspects of an AI Search Algorithm 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!’) 9/29/154
Another Task Viewed as AI Search – the ‘8 Puzzle’ 9/29/15CS Fall 2015 (Shavlik©), Lecture 9, Week Start State Goal State 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. Possible heuristic (for scoring nodes)? i) Count of #’s in wrong cell ii) Sum of moves if ‘collisions’ allowed
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/15CS Fall 2015 (Shavlik©), Lecture 9, Week 46
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/15CS Fall 2015 (Shavlik©), Lecture 9, Week 47
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. Return FAILURE // Failed if OPEN exhausted w/o a goal found. 9/29/15CS Fall 2015 (Shavlik©), Lecture 9, Week 48 Called ‘expanding’ a node
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/15CS Fall 2015 (Shavlik©), Lecture 9, Week 49
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/15CS Fall 2015 (Shavlik©), Lecture 9, Week 4 10
Example (via Doc Camera) - assume LOWER scores are better 9/29/15 CS Fall 2015 (Shavlik©), Lecture 9, Week 4 11 Start score = 9 B score = 11 C score = 8 D score = 4 E score = 3 Goal score = 0 Step# OPEN CLOSED X CHILDREN RemainingCHILDREN (this part done on doc camera for BFS, DFS, and BEST) Use these headers for HW2, Problem 2
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 X CHILDREN RemainingCHILDREN 1 { S 9 } { } S 9 { S 9, B 11, C 8 } { B 11, C 8 } 2 { C 8, B 11 } { S 9 } C 8 { G 0 } { G 0 } 3 { G 0, B 11 } { S 9, C 8 } G 0 DONE 9/29/15CS 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 score toUse = - score original Think before you compute! 9/29/15CS Fall 2015 (Shavlik©), Lecture 9, Week 4 13
A ‘Blocks World’ Example ‘Preconditions’ of a legal move(?x, ?y) action: clearTop(?x) ˄ clearTop(?y) ˄ ?x ≠ ?y Heuristic? One possibility: # blocks in correct final position 9/29/15CS Fall 2015 (Shavlik©), Lecture 9, Week 414 C BA Initial State C B A Goal State
An INFINITE Space 9/29/15CS Fall 2015 (Shavlik©), Lecture 9, Week 4 15 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, …
A (Hollywood) Famous AI Puzzle Task: put exactly 4 gallons of water in a 5 gallon jug, given –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/15CS Fall 2015 (Shavlik©), Lecture 9, Week 4 16 From the movie ‘Die Hard’