Tutorial 1 Uninformed Search CS 236501 Introduction to AI Tutorial 1 Uninformed Search
Problem Representation Initial state (InitState) Successor function (Succ) Goal predicate (Goal-p) 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Problem Example - Binary Tree The problem is to find the number 12 in a binary tree A state is represented by a number Initial State: 1 Successor Function: Succ(x) = {2*x, 2*x + 1} Goal predicate: Goal-p(x) == true iff x == 12 1 2 3 4 5 6 7 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
General Search Routine SearchFunction (States, Problem, Combiner) { If States == {} return ‘Search Failed’ CurrState <- Get and Remove first state in States if (Problem.Goal-p(CurrState) == true) return CurrState //or any other solution form else successors <- Problem.Succ(CurrState) States <- Combiner(successors,States) SearchFunction(States, Problem, Combiner) } 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Combiner examples Append-Combiner(states1, states2) { // First nodes in states1, then nodes in states2 Result <- states1 For s in states2 Result <- Add s to the end of Result } Prepend-Combiner(states1, states2) { // First nodes in states2, then nodes in states1 Result <- states2 For s in states1 Result <- Add s to the end of Result } 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
General Search Routine First call to the search function: Problem <- Initialize problem with appropriate data Combiner <- Choose state combination method that will characterize the search States <- Problem.InitState Solution = SearchFunction(States, Problem, Combiner) 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich DFS States <- Problem.InitState Solution = SearchFunction(States, Problem, Append-Combiner) 1 2 3 4 5 8 9 16 17 (For this example, solution will not be found) 32 33 … 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Depth Limited DFS LimDepth-Append-Combiner(states1, states2) { Result <- {} For s in states1 if s.depth <= DepthLimit Result <- Add s to the end of Result For s in states2 } DepthLimit is a property of LimDepth-Append-Combiner 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Depth Limited DFS LimDepth-Append-Combiner.DepthLimit <- 5 States <- Problem.InitState Solution = SearchFunction(States, Problem, LimDepth-Append-Combiner) 1 2 3 4 5 6 7 8 9 10 11 12 13 16 17 18 19 20 21 22 23 X X X X X X X X 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Iterative deepening DFS Iterative-Deepening-DFS(Problem, MaxDepth) { for i from 1 to MaxDepth LimDepth-Append-Combiner.DepthLimit <- i States <- Problem.InitState Solution = SearchFunction(States, Problem, LimDepth-Append-Combiner) If Solution != {} return Solution } 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich BFS States <- Problem.InitState Solution = SearchFunction(States, Problem, Prepend-Combiner) 1 2 3 4 5 6 7 8 9 10 11 12 13 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Discussion: DFS Vs. BFS DFS BFS Completeness Optimality of Solution Time complexity O(bd) O(bd) Space complexity O(bd) O(bd) Search in graphs? 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Example: 9-number puzzle The numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 must be put in the depicted square, in such a way that the sums of the numbers in each row, column, and diagonal are equal. Solution: 9 5 1 8 3 4 6 7 2 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Example: 9-number puzzle Problem representation Initial state: Empty board Successors: Put one of the missing numbers in an empty place Goal-p: Board is full, sums are equal, as required 5 6 1 2 … 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Example: 9-number puzzle Which search algorithm should we use? Note that the solution length is exactly 9 DFS with depth limit = 9 will find the solution and will use little memory (linear in solution depth) BFS will find the solution at depth 9, however will require a lot of memory (will eventually hold all the tree till depth 9 in memory) Iterative deepening DFS is unnecessary here, since we know the solution depth in advance. The solution will not be found at shallower depth. 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Example: LightzOut http://www.gamesgnome.com/logicpuzzles/lights/ http://games.hughesclan.com/lights/ http://javaboutique.internet.com/LightzOut/ 5x5 board Each cell can be “On” or “Off” Click on a cell inverts the states of its 4 neighbors The goal is to turn off the lights 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Example: LightzOut Click on the central cell Goal: 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Example: LightzOut Problem representation: Initial state: The board to solve Successors: Boards that are the result of a single click on the current board Goal-p: Empty board 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
LightzOut – solution example . . . . . . 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Example: LightzOut Which search algorithm should we use? Given a board, we cannot predict solution length! Note that if we are in state1, and click on the same cell twice, we return back to state1 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Example: LightzOut DFS with no restrictions might never find a solution for the problem – can get stuck in a loop Depth limited DFS can be used if we know the problem difficulty in advance Iterative deepening DFS is a good solution, especially when we do not know how difficult the problem is BFS? The branching factor is O(N2) BFS may be suitable for very simple problems For difficult problems, the memory amounts required for BFS are immense 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich
Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich Multiple Solutions Do not stop at the first solution MultGoal-SearchFunction (States, Problem, Combiner) { Solutions <- {} If States == {} return ‘Search Failed’ CurrState <- Get and remove first state in States if (Problem.Goal-p(CurrState) == true) Solutions <- Solutions + CurrState successors <- Problem.Succ(CurrState) States <-Combiner.combine(successors,States) SearchFunction(States, Problem, Combiner) } 25-Dec-18 Intro. to AI – Tutorial 1 – By Saher Esmeir & Nela Gurevich