Sei sulla pagina 1di 44

Structures and Strategies for State Space Search

State Space
A state-space problem consists of a set of states a distinguished set of states called the start states

a set of actions available to the agent in each state


an action function that, given a state and an action, returns a new state a set of goal states a criterion that specifies the quality of an acceptable solution.
For example, any sequence of actions that gets the agent to the goal state may be acceptable, or there may be costs associated with actions and the agent may be required to find a sequence that has minimal total cost. This is called an optimal solution.

State Space search


A state-space search is characterized by:
A solution is represented as a path from the start state to a goal state. The search algorithm systematically tests alternate paths to the goal. Backtracking allows recovery from paths that fail to reach a goal. The open list (unexplored states) and closed list (visited states) keep track of the search's progress.

Key Questions to be answered


To successfully design and implement search algorithms, a programmer must be able to analyse and predict their behavior. Questions that need to be answered include:
Is the problem solver guaranteed to find a solution?

Will the problem solver always terminate, or can it become caught in an


infinite loop? When a solution is found, is it guaranteed optimal?

What is the complexity of the search algorithm?


How can the interpreter most effectively reduce complexity? How can an interpreter be designed to most effectively use a representation language?

Theory of State-Space Search


Primary tool for answering these questions
Represent the problems as a state-space graph Use graph theory to analyze the structure and complexity

Graph = (V, E)
Vs are the particular states in a problem solving process Es are the transitions between states
Logical inferences or legal moves of a game

Konigsberg Problem
Euler invented graph theory to solve the "Bridges of Konigsberg problem. The city of Konigsberg occupied two banks and two islands of a river The islands and the riverbanks were connected by seven bridges The bridges of Konigsberg problem asks if there is a walk around the city that crosses each bridge exactly once

Is there any simple path so that all 7 bridges are crossed once and then one returns to the starting point?

Bridges of Konigsberg problem using graph


In the problem, rb1 and rb2 are riverbanks 1 and 2 i1 and i2 are two islands b1,b2,b3,b4,b5,b6,b7 are bridges

What might a predicate calculus representation of this look like?

The predicate calculus representation is equivalent to the graph representation in that the connectedness is preserved. However, the structure of the problem can be visualized more directly in the graph representation. Euler noted that unless a graph contained either exactly zero or two nodes of odd degree, the walk was impossible. If there were two odd-degree nodes, the walk could start at the first and end at the second; if there were no nodes of odd degree, the walk could begin and end at the same node. The walk is not possible for graphs containing any other number of nodes of odd degree.

Graph Theory
Graph = (V,E) Labeled Graph: has labels attached to nodes to distinguish them
Arcs (edges) are indicated by combining the labels of the nodes on each end of the arc Arcs may also be otherwise labeled
Indicates a named relation

Directed graphs vs. non-directed Path: connects successive nodes through a series of edges

A Directed Graph

Rooted Graphs and Trees


Rooted Graphs a node with a root
Root: a node such that there is a path from the node to all other nodes in the graph

Tree A graph in which two nodes have at most one path between them
Eliminates loops (cycles) in the graph Most often trees are rooted Parent, Child, and Sibling Ancestor and Descendent

Example

Definition of Graph
A graph consists of nodes and arcs
A set of nodes N1, N2, , Nn need not be finite.

A set of arcs connects pairs of nodes.

A directed graph has an indicated direction for traversing each arc If a directed arc connects Nj and Nk, then Nj is called the parent of Nk and Nk is called the child of Nj.

A rooted graph has a unique node Ns from which all paths in the graph originate
A tip or leaf node is a node without children. An ordered sequence of nodes [N1, N2, N3, Nn] is called a path of length n-1 in the graph

15

Definition of Graph
On a path in a rooted graph, a node is said to be an ancestor of all nodes positioned after it (to its right) as well as a descendant of all nodes before it (to its left). A path that contains any node more than once is said to contain a cycle or loop. A tree is a graph in which there is a unique path between every pair of nodes Two nodes in a graph are said to be connected if a path exists that includes them both.

16

Finite State Machine


Might be familiar to you
All modern computers and programs can be represented as FSMs

Finite State Machine (FSM) a finite, directed, connected graph, having a set of states, a set of input values, and a state transition function that describes the effect that the elements of the input stream have on the state of the graph
Often used as a tool for compilers/interpreters

Fig 3.5 (a) The finite state graph for a flip flop and (b) its transition matrix.

Fig 3.6 (a) The finite state graph and (b) the transition matrix for string recognition example

The State Space Representation of Problems


Nodes correspond to partial problem solutions
Much like the Moore Machine FSM had nodes that represented states of partial solutions

Also allow the graph to define an initial condition and a goal condition
Again, much like the Moore machine

Formal Definition

Example: The Traveling Salesperson

N all the cities A interconnecting S intermediate list of cities reached and total cost GD State where all cities have been reached and we have returned home WITH the minimum possible cost of all such paths Must be able to compare to all possible costs

State Space?

Note: the notion of dead end states

Dealing with Huge State Spaces


The only way to be guaranteed to find the

answer is to find all the possibilities

In some cases, we have provable algorithms that solve the problem without doing so

Any ideas for general rules?

Nearest Neighbor

Strategies for State Space Search


Data-driven and Goal-driven Search Data-driven (forward chaining)

Begin with given facts and set of legal moves Continually apply rules to facts to create new facts Begin with the goal Work backwards based on applying rules in reverse Work back to the start state

Goal-driven (backward chaining)


When Goal-Driven Search?


1. 2.

A goal is given or can easily by formulated A large number of rules that match the facts of the problem, and thus create an increasing number of goals

Early selection of a goal eliminates most of the branches Goal-driven search can direct problem data acquisition (i.e. medical examiner)

3.

Problem data are not given

When Data-Driven Search?


All (or most) of the data in initial problem

statement

Interpretation problems, etc.

There are a large number of potential goals,

but only a few ways to use the facts and given information of a problem instance It is difficult to form a goal or hypothesis

Implementing Graph Search


Backtracking

Begin at a start state and pursue path until deadend or goal If dead-end backtrack to most recent node in path with unexamined siblings depth-first search

Going to be implemented recursively

Function Backtrack Algorithm


Notation

SL state list, lists the states in the current path, if goal is found, SL lists solution NSL new state list, contains nodes whose descendants have not been generated/searched DE dead end, lists states whose descendents have failed to contain goal CS current state

For graphs (not just trees) need to prevent

loops

Test each new node to see if it is in any set If so, ignore it

Why is it ok to ignore a state once visited?

Function continues until goal or exhausts

state-space

Function backtrack algorithm

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


16

Fig 3.14 Backtracking search of a hypothetical state space space.

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


18

A trace of backtrack on the graph of figure 3.14

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


17

Data or Goal-Driven?
Currently, this is a data-driven search

Takes the root as a start state and evaluates its children

Could be implemented as goal-driven

Let the goal be the root, evaluate descendants backward until a start-state is found

DFS and BFS


2 Design Considerations

Data-driven or goal-driven Depth-first or breadth-first

Going to give general DFS and BFS algorithms

that dont store paths

Each can be augmented easily to do so

Look at Figure 3.15

-on next slide

Fig 3.15 Graph for breadth - and depth - first search examples.

DFS A,B,E,K,S,L,T,F,M,C,G,N,H,O,P,U,D,I,Q,J,R

BFS A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U
Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005
19

Implementing BFS
Use lists

closed keeps track of states that are already examined

The union of dead end (DE) and state list (SL)

open keeps track of nodes that have children that have not been visited

Note: the order in which nodes are removed from open determines the order of the search

Algorithm on next slide

Function breadth_first search algorithm

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


20

A trace of breadth_first_search on the graph of Figure 3.15

Also, look at Fig. 3.17 to see the BFS search solution to an instance of the 8-puzzle (next slide)

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


21

Fig 3.17 Breadth-first search of the 8-puzzle, showing order in which states were removed from open.

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


5

DFS Algorithm
Simplification of the backtrack algorithm

No ancestor information is stored Use open and closed lists as before

open list must be implemented as a stack rather than a queue as in the case of BFS Remember, the order in which items are removed from open determines the structure of the search

Function depth_first_search algorithm

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


3

Fig 3.19 Depth-first search of the 8-puzzle with a depth bound of 5.

How far down do we go?

How many states were we required to visit? What about with BFS?

Luger: Artificial Intelligence, 5th edition. Pearson Education Limited, 2005


7

Potrebbero piacerti anche