SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
Sate Space Search


Puzzle Solving and State Space Search

A state refers to the status of a puzzle or game at a given moment. It could be a the positions of the pieces on a chessboard.
In solving a puzzle or a problem one starts from some initial state and tries to reach a goal state by passing through a series
of intermediate states. In game playing, each move on the game board is a transition from one state to another.

If you think of each state being connected to those states which can follow from it, you have a graph. Such a collection of
interconnected states is called a state space graph.

In state based search, a computer program may start from an initial state, then look at one of its successor or children states
and so on until it reaches a goal state. It may reach a dead end state from where it cannot proceed further. In such a
situation the program may “backtrack”, i.e. undo its last move and try an alternative successor to its previous state. A path
from the initial state to the goal state constitutes a solution.

There are a number of ways of searching through the state space to find a solution. In depth first search, the program will
examine a state, then proceed from it to one of its successor states and then repeats this process with the successor state,
going a level deeper each time.

Breadth first search, BFS, is an alternative approach is where one examines all the successor states of a given state before
proceeding to a new level.

Best first search chooses the next state based on some measure of how promising it is. It does a calculation for each
possible next state and then chooses the best one. Best first search uses a priority queue.


Example: Farmer Wolf Goat Puzzle
A farmer is on the east bank of a river with a wolf, goat and cabbage in his care. Without his presence the wolf would eat
the goat or the goat would eat the cabbage. The wolf is not interested in the cabbage. The farmer wishes to bring his three
charges across the river. However the boat available to him can only carry one of the wolf, goat and cabbage besides
himself. The puzzle is: is there a sequence of river crossings so that the farmer can transfer himself and the other three all
intact to the west bank?




                                                                                                                                 1
Sate Space Search

State Space Search
We envisage a puzzle or game as a network of possible states in any one of which the puzzle can be at a given time. Two
states are linked if there is a valid move that allows the puzzle to go from the first state to the second.

In solving a puzzle or a problem (such as the farmer, wolf, goat and cabbage one – fwgc from hereon) one starts from
some initial state and tries to reach a goal state (e.g. all on west bank) by passing through a series of intermediate states. In
game playing, each move transfers the system from one state to the next. For example in the fwgc problem, crossing the
river in a boat either on his own or with one of the others changes the state of the system. There is usually a set of rules
which tell one how to go from one state to another. Each state may be succeeded by one of a number of others.

For example a valid state change in the fwgc problem is:


                            r                                                            r
                            i              farmer crosses with goat                      i
              fwgc          v                                               w c          v          f g
                            e                                                            e
                            r                                                            r


Such a collection of interconnected states is called a state space graph.

In state based search, a computer program may start from an initial state, then look at one of its successor states and so on
until it reaches a goal state. It may reach a dead end state from where it cannot proceed further. In such a case the program
may “backtrack”, i.e. undo its last move and try an alternative successor to its previous state.


Depth First Search
In depth first search, the program will examine a state, then proceed from it to one of its successor states and then repeats
this process with the successor state. Going a level deeper each time in the state space is referred to as depth first search.

                                                               s1



                                              s2               s3            s4



                                                                        g           s8
                                              s5          s6



                                                          s7


In the state space example above, starting with state s1, DFS with backtracking and would examine the states in the
following order: s1, s2, s5, s6, s3, s4. DFS uses a stack to store states for examination. On examining a state, it pushes all
its successor states onto a stack. The stack is then popped for next state to examine.

For example, on examining s1, stack is [s2, s3, s4], next s2 is removed and examined to give [s5, s6, s3, s4], then s5
removed and examined and so on. The stack contents will vary as follows:

                  [s1]
         s1       [s2, s3, s4]
         s2       [s5, s6, s3, s4]
         s5       [s6, s3, s4]
         s6       [s7, s3, s4]
         s7       [s3, s4]
         s3       [s4]
         s4       [g, s8]

                                                                                                                                    2
Sate Space Search

         g         [s8]               -- goal found

Once the goal state is found, the puzzle is solved. However, it is important that the path from the initial state (s1) to the
goal state (g) is stored and maybe displayed so that whoever run the program can find out the set of moves that lead to the
solution, otherwise the person just finds out that a computer program can solve the puzzle.


Breadth First Search
Breadth first search, BFS, is an alternative approach is where one examines all the successor states of a given state before
proceeding to a new level. BFS would search graph fragment in order of: s1, s2, s3, s4, s5, s6, g. BFS uses a queue to
store states for examination. On examining a state, its successor states are added to the back of the queue. The next state
for examining is taken from the front of the queue.

For example, on examining s1, queue is [s2, s3, s4], next s2 is removed and examined to give [s3, s4, s5, s6], then s3
removed and examined and so on.

The queue contents will be:

                   [s1]
         s1        [s2, s3, s4]
         s2        [s3, s4, s5, s6]
         s3        [s4, s5, s6]
         s4        [s5, s6, g, s8]
         s5        [s6, g, s8]
         s6        [g, s8, s7]
         g         [s8, s7]           -- goal found


Best First Search
This approach chooses the next state based on some measure of how promising it is. It does a calculation for each possible
next state and then chooses the best one. Best first search uses a priority queue – possibly a heap and is outside of our
scope in first year.


In searching through a state space graph, care must be taken so that the program does not go round in cycles. This can be
done by keeping a list of states visited so far in getting to current state and checking that a possible next state is not in the
list.




                                                                                                                                    3
Sate Space Search



Farmer Wolf Goat and Cabbage Sate Space
How to represent the state, i.e. where the farmer, wolf, goat and cabbage are at a given time? Any one of them can have 2
possible positions, on the east or west bank of the river. One way to do this is to store their positions in a 4-tuple or record
with 4 values or fields. (f, w, g, c) where the possible values for f, w, g and c are E or W. For example ( W, W, E, W)
means farmer, wolf and cabbage on west bank and goat on east bank.




                                                          fwgc | |



          wgc | | f                 gc | | fw                                 wc | | fg            wg | | fc




                                                         fwc | | g




                            c | | fwg                                           w | | fgc



   fc | | wg              fgc | | w
                                                                                fwg | | c            fw | | gc


     gc | | fw
                                                  g | | fwc                                          wg | | fc



                                                  fg | | wc



                                                  | | fwgc




Note: states in red are not valid
states or are unsafe and strictly
speaking should not be part of
the graph.




                                                                                                                                   4
Sate Space Search




Depth First Search of State Space with Backtracking




                                                           fwgc | |


                                                               1
        wgc | | f          gc | | fw                                          wc | | fg     wg | | fc
                                                                      2



                                                          fwc | | g
                                               3



                      c | | fwg                                                 w | | fgc
                                       4
                                                                                 7
 fc | | wg          fgc | | w
                                           5                                    fwg | | c     fw | | gc


   gc | | fw                                                              6
                                                   g | | fwc                                  wg | | fc
                                      8

                                                   fg | | wc

                                  9

                                                   | | fwgc




                                                                                                                         5
Sate Space Search



Another Depth First Search of State Space




                                                           fwgc | |


                                                               1
        wgc | | f          gc | | fw                                      wc | | fg     wg | | fc
                                                                      2



                                                          fwc | | g
                                               3


                      c | | fwg                                             w | | fgc
                                       4


 fc | | wg          fgc | | w
                                           5                                fwg | | c     fw | | gc


   gc | | fw
                                                   g | | fwc                              wg | | fc

                                      6

                                                   fg | | wc

                                  7

                                                   | | fwgc




                                                                                                                     6
Sate Space Search



Breadth First Search of State Space

                                                               fwgc | |


                                                                   1
          wgc | | f             gc | | fw                                         wc | | fg     wg | | fc
                                                                          2



                                                              fwc | | g
                                                   3                          4



                           c | | fwg                                                w | | fgc
                                               5                              6

  fc | | wg              fgc | | w
                                               7                                    fwg | | c     fw | | gc


     gc | | fw
                                                       g | | fwc                                  wg | | fc
                                           8

                                                       fg | | wc

                                       9

                                                       | | fwgc




Exercise
Consider how a given state of this puzzle can be represented in Haskell. Then define a function safe which returns true or
false depending on whether a state is safe or not. Safe means the wolf cannot eat the goat and the goat cannot eat the
cabbage.


Exercise
Do you think you could adapt the Haskell code that solves the 8-puzzle to this one? Have a try!




                                                                                                                             7

Mais conteúdo relacionado

Mais procurados

Knowledge Representation in Artificial intelligence
Knowledge Representation in Artificial intelligence Knowledge Representation in Artificial intelligence
Knowledge Representation in Artificial intelligence Yasir Khan
 
5.5 graph mining
5.5 graph mining5.5 graph mining
5.5 graph miningKrish_ver2
 
AI-State Space Representation.pptx
AI-State Space Representation.pptxAI-State Space Representation.pptx
AI-State Space Representation.pptxRatnakar Mikkili
 
I. FSSP(Progression Planner) II. BSSP(Regression Planner
I. FSSP(Progression Planner) II. BSSP(Regression PlannerI. FSSP(Progression Planner) II. BSSP(Regression Planner
I. FSSP(Progression Planner) II. BSSP(Regression Plannervikas dhakane
 
Solving problems by searching
Solving problems by searchingSolving problems by searching
Solving problems by searchingLuigi Ceccaroni
 
M A R K O V C H A I N
M A R K O V  C H A I NM A R K O V  C H A I N
M A R K O V C H A I Nashishtqm
 
Frequent itemset mining methods
Frequent itemset mining methodsFrequent itemset mining methods
Frequent itemset mining methodsProf.Nilesh Magar
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependencyJismy .K.Jose
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data MiningKamal Acharya
 
5.3 mining sequential patterns
5.3 mining sequential patterns5.3 mining sequential patterns
5.3 mining sequential patternsKrish_ver2
 
Problems problem spaces and search
Problems problem spaces and searchProblems problem spaces and search
Problems problem spaces and searchAmey Kerkar
 
Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science Venkata Reddy Konasani
 
Time, Schedules, and Resources in Artificial Intelligence.pptx
Time, Schedules, and Resources in Artificial Intelligence.pptxTime, Schedules, and Resources in Artificial Intelligence.pptx
Time, Schedules, and Resources in Artificial Intelligence.pptxkitsenthilkumarcse
 
Reinforcement learning, Q-Learning
Reinforcement learning, Q-LearningReinforcement learning, Q-Learning
Reinforcement learning, Q-LearningKuppusamy P
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategiesRenas Rekany
 
Problem Formulation
Problem FormulationProblem Formulation
Problem FormulationAdri Jovin
 

Mais procurados (20)

Knowledge Representation in Artificial intelligence
Knowledge Representation in Artificial intelligence Knowledge Representation in Artificial intelligence
Knowledge Representation in Artificial intelligence
 
09
0909
09
 
5.5 graph mining
5.5 graph mining5.5 graph mining
5.5 graph mining
 
AI-State Space Representation.pptx
AI-State Space Representation.pptxAI-State Space Representation.pptx
AI-State Space Representation.pptx
 
I. FSSP(Progression Planner) II. BSSP(Regression Planner
I. FSSP(Progression Planner) II. BSSP(Regression PlannerI. FSSP(Progression Planner) II. BSSP(Regression Planner
I. FSSP(Progression Planner) II. BSSP(Regression Planner
 
Solving problems by searching
Solving problems by searchingSolving problems by searching
Solving problems by searching
 
M A R K O V C H A I N
M A R K O V  C H A I NM A R K O V  C H A I N
M A R K O V C H A I N
 
Frequent itemset mining methods
Frequent itemset mining methodsFrequent itemset mining methods
Frequent itemset mining methods
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependency
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
5.3 mining sequential patterns
5.3 mining sequential patterns5.3 mining sequential patterns
5.3 mining sequential patterns
 
Problems problem spaces and search
Problems problem spaces and searchProblems problem spaces and search
Problems problem spaces and search
 
Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science
 
Time, Schedules, and Resources in Artificial Intelligence.pptx
Time, Schedules, and Resources in Artificial Intelligence.pptxTime, Schedules, and Resources in Artificial Intelligence.pptx
Time, Schedules, and Resources in Artificial Intelligence.pptx
 
Reinforcement learning, Q-Learning
Reinforcement learning, Q-LearningReinforcement learning, Q-Learning
Reinforcement learning, Q-Learning
 
AI simple search strategies
AI simple search strategiesAI simple search strategies
AI simple search strategies
 
AI: AI & Problem Solving
AI: AI & Problem SolvingAI: AI & Problem Solving
AI: AI & Problem Solving
 
Deep Q-learning explained
Deep Q-learning explainedDeep Q-learning explained
Deep Q-learning explained
 
Game playing in AI
Game playing in AIGame playing in AI
Game playing in AI
 
Problem Formulation
Problem FormulationProblem Formulation
Problem Formulation
 

Destaque

Knowledge Representation & Reasoning
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & ReasoningSajid Marwat
 
State Space Search(2)
State Space Search(2)State Space Search(2)
State Space Search(2)luzenith_g
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logicgiki67
 
Knowledge representation and Predicate logic
Knowledge representation and Predicate logicKnowledge representation and Predicate logic
Knowledge representation and Predicate logicAmey Kerkar
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representationSravanthi Emani
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}FellowBuddy.com
 

Destaque (7)

Knowledge Representation & Reasoning
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & Reasoning
 
State Space Search(2)
State Space Search(2)State Space Search(2)
State Space Search(2)
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logic
 
Frames
FramesFrames
Frames
 
Knowledge representation and Predicate logic
Knowledge representation and Predicate logicKnowledge representation and Predicate logic
Knowledge representation and Predicate logic
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 

State space search

  • 1. Sate Space Search Puzzle Solving and State Space Search A state refers to the status of a puzzle or game at a given moment. It could be a the positions of the pieces on a chessboard. In solving a puzzle or a problem one starts from some initial state and tries to reach a goal state by passing through a series of intermediate states. In game playing, each move on the game board is a transition from one state to another. If you think of each state being connected to those states which can follow from it, you have a graph. Such a collection of interconnected states is called a state space graph. In state based search, a computer program may start from an initial state, then look at one of its successor or children states and so on until it reaches a goal state. It may reach a dead end state from where it cannot proceed further. In such a situation the program may “backtrack”, i.e. undo its last move and try an alternative successor to its previous state. A path from the initial state to the goal state constitutes a solution. There are a number of ways of searching through the state space to find a solution. In depth first search, the program will examine a state, then proceed from it to one of its successor states and then repeats this process with the successor state, going a level deeper each time. Breadth first search, BFS, is an alternative approach is where one examines all the successor states of a given state before proceeding to a new level. Best first search chooses the next state based on some measure of how promising it is. It does a calculation for each possible next state and then chooses the best one. Best first search uses a priority queue. Example: Farmer Wolf Goat Puzzle A farmer is on the east bank of a river with a wolf, goat and cabbage in his care. Without his presence the wolf would eat the goat or the goat would eat the cabbage. The wolf is not interested in the cabbage. The farmer wishes to bring his three charges across the river. However the boat available to him can only carry one of the wolf, goat and cabbage besides himself. The puzzle is: is there a sequence of river crossings so that the farmer can transfer himself and the other three all intact to the west bank? 1
  • 2. Sate Space Search State Space Search We envisage a puzzle or game as a network of possible states in any one of which the puzzle can be at a given time. Two states are linked if there is a valid move that allows the puzzle to go from the first state to the second. In solving a puzzle or a problem (such as the farmer, wolf, goat and cabbage one – fwgc from hereon) one starts from some initial state and tries to reach a goal state (e.g. all on west bank) by passing through a series of intermediate states. In game playing, each move transfers the system from one state to the next. For example in the fwgc problem, crossing the river in a boat either on his own or with one of the others changes the state of the system. There is usually a set of rules which tell one how to go from one state to another. Each state may be succeeded by one of a number of others. For example a valid state change in the fwgc problem is: r r i farmer crosses with goat i fwgc v w c v f g e e r r Such a collection of interconnected states is called a state space graph. In state based search, a computer program may start from an initial state, then look at one of its successor states and so on until it reaches a goal state. It may reach a dead end state from where it cannot proceed further. In such a case the program may “backtrack”, i.e. undo its last move and try an alternative successor to its previous state. Depth First Search In depth first search, the program will examine a state, then proceed from it to one of its successor states and then repeats this process with the successor state. Going a level deeper each time in the state space is referred to as depth first search. s1 s2 s3 s4 g s8 s5 s6 s7 In the state space example above, starting with state s1, DFS with backtracking and would examine the states in the following order: s1, s2, s5, s6, s3, s4. DFS uses a stack to store states for examination. On examining a state, it pushes all its successor states onto a stack. The stack is then popped for next state to examine. For example, on examining s1, stack is [s2, s3, s4], next s2 is removed and examined to give [s5, s6, s3, s4], then s5 removed and examined and so on. The stack contents will vary as follows: [s1] s1 [s2, s3, s4] s2 [s5, s6, s3, s4] s5 [s6, s3, s4] s6 [s7, s3, s4] s7 [s3, s4] s3 [s4] s4 [g, s8] 2
  • 3. Sate Space Search g [s8] -- goal found Once the goal state is found, the puzzle is solved. However, it is important that the path from the initial state (s1) to the goal state (g) is stored and maybe displayed so that whoever run the program can find out the set of moves that lead to the solution, otherwise the person just finds out that a computer program can solve the puzzle. Breadth First Search Breadth first search, BFS, is an alternative approach is where one examines all the successor states of a given state before proceeding to a new level. BFS would search graph fragment in order of: s1, s2, s3, s4, s5, s6, g. BFS uses a queue to store states for examination. On examining a state, its successor states are added to the back of the queue. The next state for examining is taken from the front of the queue. For example, on examining s1, queue is [s2, s3, s4], next s2 is removed and examined to give [s3, s4, s5, s6], then s3 removed and examined and so on. The queue contents will be: [s1] s1 [s2, s3, s4] s2 [s3, s4, s5, s6] s3 [s4, s5, s6] s4 [s5, s6, g, s8] s5 [s6, g, s8] s6 [g, s8, s7] g [s8, s7] -- goal found Best First Search This approach chooses the next state based on some measure of how promising it is. It does a calculation for each possible next state and then chooses the best one. Best first search uses a priority queue – possibly a heap and is outside of our scope in first year. In searching through a state space graph, care must be taken so that the program does not go round in cycles. This can be done by keeping a list of states visited so far in getting to current state and checking that a possible next state is not in the list. 3
  • 4. Sate Space Search Farmer Wolf Goat and Cabbage Sate Space How to represent the state, i.e. where the farmer, wolf, goat and cabbage are at a given time? Any one of them can have 2 possible positions, on the east or west bank of the river. One way to do this is to store their positions in a 4-tuple or record with 4 values or fields. (f, w, g, c) where the possible values for f, w, g and c are E or W. For example ( W, W, E, W) means farmer, wolf and cabbage on west bank and goat on east bank. fwgc | | wgc | | f gc | | fw wc | | fg wg | | fc fwc | | g c | | fwg w | | fgc fc | | wg fgc | | w fwg | | c fw | | gc gc | | fw g | | fwc wg | | fc fg | | wc | | fwgc Note: states in red are not valid states or are unsafe and strictly speaking should not be part of the graph. 4
  • 5. Sate Space Search Depth First Search of State Space with Backtracking fwgc | | 1 wgc | | f gc | | fw wc | | fg wg | | fc 2 fwc | | g 3 c | | fwg w | | fgc 4 7 fc | | wg fgc | | w 5 fwg | | c fw | | gc gc | | fw 6 g | | fwc wg | | fc 8 fg | | wc 9 | | fwgc 5
  • 6. Sate Space Search Another Depth First Search of State Space fwgc | | 1 wgc | | f gc | | fw wc | | fg wg | | fc 2 fwc | | g 3 c | | fwg w | | fgc 4 fc | | wg fgc | | w 5 fwg | | c fw | | gc gc | | fw g | | fwc wg | | fc 6 fg | | wc 7 | | fwgc 6
  • 7. Sate Space Search Breadth First Search of State Space fwgc | | 1 wgc | | f gc | | fw wc | | fg wg | | fc 2 fwc | | g 3 4 c | | fwg w | | fgc 5 6 fc | | wg fgc | | w 7 fwg | | c fw | | gc gc | | fw g | | fwc wg | | fc 8 fg | | wc 9 | | fwgc Exercise Consider how a given state of this puzzle can be represented in Haskell. Then define a function safe which returns true or false depending on whether a state is safe or not. Safe means the wolf cannot eat the goat and the goat cannot eat the cabbage. Exercise Do you think you could adapt the Haskell code that solves the 8-puzzle to this one? Have a try! 7