Uninformed Search in Prolog

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Knowledge Representation
Advertisements

Additional Topics ARTIFICIAL INTELLIGENCE
Heuristic Search techniques
Announcements Course TA: Danny Kumar
AI Pathfinding Representing the Search Space
Artificial Intelligence Presentation
14 Jan 2004CS Blind Search1 Solving problems by searching Chapter 3.
Informed Search Strategies
State Space Representation and Search
Traveling Salesperson Problem
PROBLEM SOLVING AND SEARCH
Informed Search Methods How can we improve searching strategy by using intelligence? Map example: Heuristic: Expand those nodes closest in “as the crow.
Problem Solving by Searching Copyright, 1996 © Dale Carnegie & Associates, Inc. Chapter 3 Spring 2007.
Comp 307 Problem Solving and Search Formalize a problem as State Space Search Blind search strategies Heuristic search.
Problem Solving Agents A problem solving agent is one which decides what actions and states to consider in completing a goal Examples: Finding the shortest.
1 Lecture 3 Uninformed Search. 2 Uninformed search strategies Uninformed: While searching you have no clue whether one non-goal state is better than any.
Solving Problems by Searching Currently at Chapter 3 in the book Will finish today/Monday, Chapter 4 next.
CS 480 Lec 3 Sept 11, 09 Goals: Chapter 3 (uninformed search) project # 1 and # 2 Chapter 4 (heuristic search)
Search Techniques MSc AI module. Search In order to build a system to solve a problem we need to: Define and analyse the problem Acquire the knowledge.
Artificial Intelligence (CS 461D)
September 26, 2012Introduction to Artificial Intelligence Lecture 7: Search in State Spaces I 1 After our “Haskell in a Nutshell” excursion, let us move.
Artificial Intelligence Lecture No. 7 Dr. Asad Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Problem Solving by Searching Copyright, 1996 © Dale Carnegie & Associates, Inc. Chapter 3 Spring 2004.
MAE 552 – Heuristic Optimization Lecture 27 April 3, 2002
Artificial Intelligence Chapter 3: Solving Problems by Searching
Using Search in Problem Solving
Uninformed Search Reading: Chapter 3 by today, Chapter by Wednesday, 9/12 Homework #2 will be given out on Wednesday DID YOU TURN IN YOUR SURVEY?
Using Search in Problem Solving
Artificial Intelligence Methods Rao Vemuri Searching - 2.
Solving problems by searching
Solving Problems by Searching
Informed Search Idea: be smart about what paths to try.
Review: Search problem formulation Initial state Actions Transition model Goal state (or goal test) Path cost What is the optimal solution? What is the.
WAES 3308 Numerical Methods for AI
Do you drive? Have you thought about how the route plan is created for you in the GPS system? How would you implement a cross-and- nought computer program?
Representing and Using Graphs
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
Dr.Abeer Mahmoud ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information.
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
CS.462 Artificial Intelligence SOMCHAI THANGSATHITYANGKUL Lecture 02 : Search.
A RTIFICIAL I NTELLIGENCE UNIT : 2 Search Techniques.
For Friday Finish reading chapter 4 Homework: –Lisp handout 4.
For Monday Read chapter 4, section 1 No homework..
Lecture 3: Uninformed Search
G5BAIM Artificial Intelligence Methods Graham Kendall Searching.
Artificial Intelligence University Politehnica of Bucharest Adina Magda Florea
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
CSCI 4310 Lecture 2: Search. Search Techniques Search is Fundamental to Many AI Techniques.
Introduction to Artificial Intelligence (G51IAI) Dr Rong Qu Blind Searches - Introduction.
G5AIAI Introduction to AI
For Monday Read chapter 4 exercise 1 No homework.
Artificial Intelligence Solving problems by searching.
Lecture 3 Problem Solving through search Uninformed Search
Lecture 3: Uninformed Search
Uniformed Search (cont.) Computer Science cpsc322, Lecture 6
Introduction to Artificial Intelligence
Problem Solving by Searching
Uniformed Search (cont.) Computer Science cpsc322, Lecture 6
SOLVING PROBLEMS BY SEARCHING
Problem Solving and Searching
What to do when you don’t know anything know nothing
CSE 4705 Artificial Intelligence
Artificial Intelligence
Searching for Solutions
Problem Solving and Searching
Artificial Intelligence
Artificial Intelligence Chapter 8 Uninformed Search
CO Games Development 1 Week 8 Depth-first search, Combinatorial Explosion, Heuristics, Hill-Climbing Gareth Bellaby.
ARTIFICIAL INTELLIGENCE
CMSC 471 Fall 2011 Class #4 Tue 9/13/11 Uninformed Search
Presentation transcript:

Uninformed Search in Prolog Artificial Intelligence Sartra Wongthanavasu, Ph.D. 26/08/2012 2608/2012 CS@KKU: AI lab: State-Space Search in Prolog

CS@KKU: AI lab: State-Space Search Many problems in AI take the form of state-space search. The states might be legal board configurations in a game, towns and cities in some sort of route map, collections of mathematical propositions, etc. The state-space is the configuration of the possible states and how they connect to each other e.g. the legal moves between states. When we don't have an algorithm which tells us definitively how to negotiate the state-space we need to search the state-space to find an optimal path from a start state to a goal state. We can only decide what to do (or where to go), by considering the possible moves from the current state, and trying to look ahead as far as possible. Chess, for example, is a very difficult state-space search problem. 22/08/2012 CS@KKU: AI lab: State-Space Search

An example problem: Searching a graph link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). A B Initial State C Goal State D E F State-Space H G go(X,X,[X]). go(X,Y,[X|T]):- link(X,Z), go(Z,Y,T). | ?- go(a,c,X). X = [a,e,f,c] ? ; X = [a,b,f,c] ? ; X = [a,b,c] ? ; no Simple search algorithm Consultation 22/08/2012 AI Lecture CS@KKU: State-Space Search in Prolog

State-Space Representation link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). Initial state is the root A E B D F F C C C An abstract representation of a state-space is a downwards growing tree. Connected nodes represent states in the domain. The branching factor denotes how many new states you can move to from any state. This problem has an average of 2. The depth of a node denotes how many moves away from the initial state it is. ‘C’ has two depths, 2 or 3. 22/08/2012 AI Lecture CS@KKU: State-Space Search

Searching for the optimum State-space search is all about finding, in a state-space (which may be extremely large: e.g. chess), some optimal state/node. `Optimal' can mean very different things depending on the nature of the domain being searched. For a puzzle, `optimal' might mean the goal state. For a route-finder, like our problem, which searches for shortest routes between towns, or components of an integrated circuit, `optimal' might mean the shortest path between two towns/components. For a game such as chess, in which we typically can't see the goal state, `optimal' might mean the best move we think we can make, looking ahead to see what effects the possible moves have. 22/08/2012 AI CS@KKU: State-Space Search in Prolog

AI CS@KKU: State-Space Search in Prolog Implementing To implement state-space search in Prolog, we need: A way of representing a state e.g. the board configuration link(a,e). A way of generating all of the next states reachable from a given state; go(X,Y,[X|T]):- link(X,Z), go(Z,Y,T). A way of determining whether a given state is the one we're looking for. Sometimes this might be the goal state (a finished puzzle, a completed route, a checkmate position); other times it might simply be the state we estimate is the best, using some evaluation function; go(X,X,[X]). A mechanism for controlling how we search the space. 22/08/2012 AI CS@KKU: State-Space Search in Prolog

AI CS@KKU, State-Space Search in Prolog Depth-First Search A go(X,X,[X]). go(X,Y,[X|T]):- link(X,Z), go(Z,Y,T). E B | ?- go(a,c,X). X = [a,e,f,c] ? ; X = [a,b,f,c] ? ; X = [a,b,c] ? ; no D F F C C C This simple search algorithm uses Prolog’s unification routine to find the first link from the current node then follows it. It always follows the left-most branch of the search tree first; following it down until it either finds the goal state or hits a dead-end. It will then backtrack to find another branch to follow. = depth-first search. 22/08/2012 AI CS@KKU, State-Space Search in Prolog

Iterative Deepening If the optimal solution is the shortest path from the initial state to the goal state depth-first search will usually not find this. We need to vary the depth at which we look for a solution; increasing the depth every time we have exhausted all nodes at a particular depth. We can take advantage of Prolog’s backtracking to implement this very simply. Depth-First go(X,X,[X]). go(X,Y,[X|T]):- link(X,Z), go(Z,Y,T). Iterative Deepening go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). Check if current node is goal. Find an intermediate node. Check whether intermediate links with goal. 22/08/2012 AI Lecture CS@KKU: State-Space Search in Prolog

Iterative Deepening: how it works? |?- go(a,c,Sol). go(a,a,[a]). go(a,Z,[a]). link(Z,c). Z=a go(a,a,[a]). link(a,c). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). |?- go(a,c,S). 18/10/04 AIPP Lecture 8: State-Space Search

Iterative Deepening: how it works? |?- go(a,c,Sol). go(a,a,[a]). go(a,Z,Sol). link(Z,c). go(a,a,[a]). go(a,Z1,Sol). link(Z1,Z). Z1=a link(a,Z). go(a,a,[a]). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). |?- go(a,c,S). 18/10/04 AIPP Lecture 8: State-Space Search

Iterative Deepening: how it works? |?- go(a,c,Sol). go(a,a,[a]). go(a,e,[e,a]). link(e,c). go(a,a,[a]). go(a,Z1,[a]). link(a,e). Z1=a link(a,e). go(a,a,[a]). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). |?- go(a,c,S). 18/10/04 AIPP Lecture 8: State-Space Search

Iterative Deepening: how it works? |?- go(a,c,[c,b,a]). go(a,a,[a]). go(a,b,[b,a]). link(b,c). go(a,a,[a]). go(a,Z1,[a]). link(a,b). Z1=a link(a,b). go(a,a,[a]). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). |?- go(a,c,S). S = [c,b,a]? 18/10/04 AIPP Lecture 8: State-Space Search

Iterative Deepening: how it works? |?- go(a,c,Sol). go(a,a,[a]). go(a,Z,Sol). link(Z,c). go(a,a,[a]). go(a,Z1,Sol). link(Z1,Z). go(a,a,[a]). go(a,Z2,Sol). link(Z2,Z1). go(a,a,[a]). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). |?- go(a,c,S). S = [c,b,a]?; 18/10/04 AIPP Lecture 8: State-Space Search

Iterative Deepening: how it works? |?- go(a,c,Sol). go(a,a,[a]). go(a,f,Sol). link(f,c). go(a,a,[a]). go(a,e,Sol). link(e,f). go(a,a,[a]). go(a,a,Sol). link(a,e). go(a,a,[a]). link(a,e). link(g,h). link(g,d). link(e,d). link(h,f). link(e,f). link(a,e). link(a,b). link(b,f). link(b,c). link(f,c). go(X,X,[X]). go(X,Y,[Y|T]):- go(X,Z,T), link(Z,Y). |?- go(a,c,S). S = [c,b,a]?; S = [c,f,e,a]? 22/08/2012 AI Lecture CS@KKU: State-Space Search

Iterative Deepening (3) Iterative Deepening search is quite useful as: it is simple; reaches a solution quickly, and with minimal memory requirements as at any point in the search it is maintaining only one path back to the initial state. However: on each iteration it has to re-compute all previous levels and extend them to the new depth; may not terminate (e.g. loop); may not be able to handle complex state-spaces; can’t be used in conjunction with problem-specific heuristics as keeps no memory of optional paths. 22/08/2012 AI Lecture CS@KKU: State-Space Search

AI Lecture CS@KKU: State-Space Search Breadth-First Search A | ?- go(a,c,X). X = [a,b,c] ? ; X = [a,e,f,c] ? ; X = [a,b,f,c] ? ; no E B 1st Depth-first = A,ED,FC,BFC,C Breadth-first = A,EB,DFFC,CC D F F C 2nd C C 3rd A simple, common alternative to depth-first search is: breadth-first search. This checks every node at one level of the space, before moving onto the next level. It is distinct from iterative deepening as it maintains a list of alternative candidate nodes that can be expanded at each depth 22/08/2012 AI Lecture CS@KKU: State-Space Search

Depth-first vs. Breadth-first Advantages of depth-first: Simple to implement; Needs relatively small memory for storing the state-space. Disadvantages of depth-first: Can sometimes fail to find a solution; Not guaranteed to find an optimal solution; Can take a lot longer to find a solution. Advantages of breadth-first: Guaranteed to find a solution (if one exists); Depending on the problem, can be guaranteed to find an optimal solution. Disadvantages of breadth-first: More complex to implement; Needs a lot of memory for storing the state space if the search space has a high branching factor. 22/08/2012 AI Lecture CS@KKU: State-Space Search

AI Lecture CS@KKU: State-Space Search Agenda-based search Both depth-first and breadth-first search can be implemented using an agenda (breadth-first can only be implemented with an agenda). The agenda holds a list of states in the state space, as we generate (‘expand') them, starting with the initial state. We process the agenda from the beginning, taking the first state each time. If that state is the one we're looking for, the search is complete. Otherwise, we expand that state, and generate the states which are reachable from it. We then add the new nodes to the agenda, to be dealt with as we come to them. 22/08/2012 AI Lecture CS@KKU: State-Space Search

Prolog for agenda-based search Example Agenda = [[c,b,a],[c,f,e,a],[c,f,b,a]] Here's a very general skeleton for agenda-based search: search(Solution) :- initial_state(InitialState), agenda_search([[InitialState]], Solution). agenda_search([[Goal|Path]|_], [Goal|Path]) :- is_goal(Goal). agenda_search([[State|Path]|Rest], Solution) :- get_successors([State|Path], Successors), update_agenda(Rest, Successors, NewAgenda), agenda_search(NewAgenda, Solution). 22/08/2012 AI Lecture CS@KKU: State-Space Search

Prolog for agenda-based search (2) To complete the skeleton, we need to implement: initial_state/1, which creates the initial state for the state-space. is_goal/1, which succeeds if its argument is the goal state. get_successors/2 which generates all of the states which are reachable from a given state (should take advantage of findall/3, setof/3 or bagof/3 to achieve this). update_agenda/2, which adds new states to the agenda (usually using append/3). 22/08/2012 AI Lecture CS@KKU: State-Space Search

Implementing DF and BF search With this basic skeleton, depth-first search and breadth-first search may be implemented with a simple change: Adding newly-generated agenda items to the beginning of the agenda implements depth-first search: update_agenda(OldAgenda, NewStates, NewAgenda) :- append(NewStates, OldAgenda, NewAgenda). Adding newly-generated agenda items to the end of the agenda implements breadth-first search: append(OldAgenda, NewStates, NewAgenda). = We control how the search proceeds, by changing how the agenda is updated. 22/08/2012 AI Lecture CS@KKU: State-Space Search

AIPP Lecture 8: State-Space Search Summary State-Space Search can be used to find optimal paths through problem spaces. A state-space is represented as a downwards-growing tree with nodes representing states and branches as legal moves between states. Prolog’s unification strategy allows a simple implementation of depth-first search. The efficiency of this can be improved by performing iterative deepening search (using backtracking). Breadth-first search always finds the shortest path to the goal state. Both depth and breadth-first search can be implemented using an agenda: depth-first adds new nodes to the front of the agenda; breadth-first adds new nodes to the end. 18/10/04 AIPP Lecture 8: State-Space Search