State space search

Timothy Makgato
Timothy MakgatoSales em Apex Digital Systems
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

Recomendados

State Space Search in ai por
State Space Search in aiState Space Search in ai
State Space Search in aivikas dhakane
602 visualizações10 slides
AI: Learning in AI por
AI: Learning in AI AI: Learning in AI
AI: Learning in AI DataminingTools Inc
41.1K visualizações14 slides
Planning por
Planning Planning
Planning Amar Jukuntla
3.4K visualizações76 slides
Machine learning ppt. por
Machine learning ppt.Machine learning ppt.
Machine learning ppt.ASHOK KUMAR
1.5K visualizações19 slides
AI: AI & Searching por
AI: AI & SearchingAI: AI & Searching
AI: AI & SearchingDataminingTools Inc
5.1K visualizações12 slides
Logical Clocks (Distributed computing) por
Logical Clocks (Distributed computing)Logical Clocks (Distributed computing)
Logical Clocks (Distributed computing)Sri Prasanna
26.7K visualizações30 slides

Mais conteúdo relacionado

Mais procurados

Unification and Lifting por
Unification and LiftingUnification and Lifting
Unification and LiftingMegha Sharma
6.2K visualizações11 slides
Digit recognition por
Digit recognitionDigit recognition
Digit recognitionbtandale
4.3K visualizações12 slides
Brief Introduction to Boltzmann Machine por
Brief Introduction to Boltzmann MachineBrief Introduction to Boltzmann Machine
Brief Introduction to Boltzmann MachineArunabha Saha
11.8K visualizações96 slides
Logic programming in python por
Logic programming in pythonLogic programming in python
Logic programming in pythonPierre Carbonnelle
7.7K visualizações20 slides
Rule based system por
Rule based systemRule based system
Rule based systemDr. C.V. Suresh Babu
3.4K visualizações21 slides
Stuart russell and peter norvig artificial intelligence - a modern approach... por
Stuart russell and peter norvig   artificial intelligence - a modern approach...Stuart russell and peter norvig   artificial intelligence - a modern approach...
Stuart russell and peter norvig artificial intelligence - a modern approach...Lê Anh Đạt
6.3K visualizações1152 slides

Mais procurados(20)

Unification and Lifting por Megha Sharma
Unification and LiftingUnification and Lifting
Unification and Lifting
Megha Sharma6.2K visualizações
Digit recognition por btandale
Digit recognitionDigit recognition
Digit recognition
btandale4.3K visualizações
Brief Introduction to Boltzmann Machine por Arunabha Saha
Brief Introduction to Boltzmann MachineBrief Introduction to Boltzmann Machine
Brief Introduction to Boltzmann Machine
Arunabha Saha11.8K visualizações
Logic programming in python por Pierre Carbonnelle
Logic programming in pythonLogic programming in python
Logic programming in python
Pierre Carbonnelle7.7K visualizações
Stuart russell and peter norvig artificial intelligence - a modern approach... por Lê Anh Đạt
Stuart russell and peter norvig   artificial intelligence - a modern approach...Stuart russell and peter norvig   artificial intelligence - a modern approach...
Stuart russell and peter norvig artificial intelligence - a modern approach...
Lê Anh Đạt6.3K visualizações
Unit3:Informed and Uninformed search por Tekendra Nath Yogi
Unit3:Informed and Uninformed searchUnit3:Informed and Uninformed search
Unit3:Informed and Uninformed search
Tekendra Nath Yogi1.1K visualizações
Buffer cache unix ppt Mrs.Sowmya Jyothi por Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi387 visualizações
Ooad unit – 1 introduction por Babeetha Muruganantham
Ooad unit – 1 introductionOoad unit – 1 introduction
Ooad unit – 1 introduction
Babeetha Muruganantham43.2K visualizações
Image filtering in Digital image processing por Abinaya B
Image filtering in Digital image processingImage filtering in Digital image processing
Image filtering in Digital image processing
Abinaya B27.4K visualizações
Solving problems by searching por Luigi Ceccaroni
Solving problems by searchingSolving problems by searching
Solving problems by searching
Luigi Ceccaroni12.8K visualizações
3. planning in situational calculas por Ankush Kumar
3. planning in situational calculas3. planning in situational calculas
3. planning in situational calculas
Ankush Kumar2.3K visualizações
Markov decision process por Hamed Abdi
Markov decision processMarkov decision process
Markov decision process
Hamed Abdi3.1K visualizações
Problem Solving por Amar Jukuntla
Problem Solving Problem Solving
Problem Solving
Amar Jukuntla8.1K visualizações
Planning por ahmad bassiouny
PlanningPlanning
Planning
ahmad bassiouny26K visualizações
And or graph por Ali A Jalil
And or graphAnd or graph
And or graph
Ali A Jalil4.9K visualizações
mlcourse.ai fall2019 Live Session 0 por Yury Kashnitsky
mlcourse.ai fall2019 Live Session 0mlcourse.ai fall2019 Live Session 0
mlcourse.ai fall2019 Live Session 0
Yury Kashnitsky1.5K visualizações
Artificial intelligence- Logic Agents por Nuruzzaman Milon
Artificial intelligence- Logic AgentsArtificial intelligence- Logic Agents
Artificial intelligence- Logic Agents
Nuruzzaman Milon20K visualizações
TM - Techniques por Rajendran
TM - TechniquesTM - Techniques
TM - Techniques
Rajendran 14.2K visualizações

Destaque

(Radhika) presentation on chapter 2 ai por
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 aiRadhika Srinivasan
9.3K visualizações34 slides
Problems problem spaces and search por
Problems problem spaces and searchProblems problem spaces and search
Problems problem spaces and searchAmey Kerkar
12.5K visualizações18 slides
Knowledge Representation & Reasoning por
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & ReasoningSajid Marwat
18.9K visualizações34 slides
State Space Search(2) por
State Space Search(2)State Space Search(2)
State Space Search(2)luzenith_g
8.2K visualizações15 slides
Predicate Logic por
Predicate LogicPredicate Logic
Predicate Logicgiki67
42.1K visualizações71 slides
Frames por
FramesFrames
Framesamitp26
24K visualizações17 slides

Destaque(9)

(Radhika) presentation on chapter 2 ai por Radhika Srinivasan
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai
Radhika Srinivasan9.3K visualizações
Problems problem spaces and search por Amey Kerkar
Problems problem spaces and searchProblems problem spaces and search
Problems problem spaces and search
Amey Kerkar12.5K visualizações
Knowledge Representation & Reasoning por Sajid Marwat
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & Reasoning
Sajid Marwat18.9K visualizações
State Space Search(2) por luzenith_g
State Space Search(2)State Space Search(2)
State Space Search(2)
luzenith_g8.2K visualizações
Predicate Logic por giki67
Predicate LogicPredicate Logic
Predicate Logic
giki6742.1K visualizações
Frames por amitp26
FramesFrames
Frames
amitp2624K visualizações
Knowledge representation and Predicate logic por Amey Kerkar
Knowledge representation and Predicate logicKnowledge representation and Predicate logic
Knowledge representation and Predicate logic
Amey Kerkar69.7K visualizações
Issues in knowledge representation por Sravanthi Emani
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
Sravanthi Emani60K visualizações
Heuristic Search Techniques {Artificial Intelligence} por FellowBuddy.com
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
FellowBuddy.com32.6K visualizações

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