Chapter 3 - Basics of Search
Chapter 3 - Basics of Search
2
Wollo University ,Kombolicha Institute of Technology
By Ashenafi Workie(MSc.)
KIOT@SE by Ashenafi Workie
Major chapters outlines
5
Problem Solving by Searching
6
Problem-solving agents
7
Problem-solving agents
• Such agent is not reflex or model based reflex agent because this agent
needs to achieve some target (goal)
• It can be goal based or utility based or learning agent
• Intelligent agent knows that to achieve certain goal, the state of the
environment will change sequentially and the change should be towards the
goal
8
Problem-solving agents
9
Agent program
10
Steps to undertake during searching/Problem formulation:
Involves:
Abstracting the real environment configuration into state information
using a preferred data structure
Describe the initial state according to the data structure
deciding the set of all possible action
The set of actions possible on a given state at a specific point in the
process.
The cost of the action at each state
11
Cont’d
For the vacuum world problem, the problem formulation involves:
State is described as list of 3 elements where the first element
describe information about block A, the second element describe
information about block B and the last element describe the location
of the Agent
[dirty, dirty, A]
Suck, moveRight, moveLeft
Determine which of the above activities is valid for a given action
Cost can be determined in many ways
12
Steps to undertake during searching
Goal formulation: refers to the understanding of the objective of the
agent based on the state description of the final environment
For example, for the vacuum world problem, the goal can be formulated as
[clean, Clean, agent at any block]
13
Problem solution
14
Steps to undertake during searching
Solution is a sequence of world state in which the final state satisfy the
goal or solution is action sequence in which the last action will result the
goal state.
Each action change one state to the next state of the world
A search algorithm take a problem as input and returns a solution in the
form of action or state sequence.
To achieve the goal, the action sequences must be executed accordingly
The general “formulate-search-execute” algorithm of search is given
below:
15
Example Road map Ethiopia
Aksum
100
200
Mekele
Gondar 80
180
Lalibela
110 250
150
Bahr dar
Dessie
170
400
330
Jima
Addis Ababa
100
430 Nazarez 370
Awasa
16
Road map Ethiopia
Solution
• Current position of the agent: Awasa.
• Needs to arrive to: Gondar
• Formulate goal:
• be in Gondar
• Formulate problem:
• states: various cities
• actions: drive between cities
• Find a solution:
• sequence of cities, e.g., Awasa, Nazarez, Addis Ababa, Dessie, Gondar
17
Types of problem
• Four types of problems exist in the real situations:
1. Single-state problem
• The environment is Deterministic and fully observable
• Out of the possible state space, agent knows exactly which state
it will be in; solution is a sequence
2. Sensor less problem (conformant problem)
• The environment is non-observable
• It is also called multi-state problem
• Agent may have no idea where it is; solution is a sequence
18
Types of problem
3. Contingency problem
The environment is nondeterministic and/or partially
observable
It is not possible to know the effect of the agent action
percepts provide new information about current state
4. Exploration problem
The environment is partially observable
It is also called unknown state space
19
Single state problem
Single-state
Starting state us known say in #5.
What is the Solution?
• Single-state, start in #5.
Solution? [Right, Suck]
20
Sensorless problem
• Sensorless,
• It doesn’t know what the current
state is
• So the current start is either of the
following: {1,2,3,4,5,6,7,8}
21
Sensorless
• Sensorless Solution
• Right goes to {2,4,6,8}
Solution?
• [Right,Suck,Left,Suck]
22
Contingency problem
Nondeterministic: Suck may
dirty a clean carpet
Partially observable:
Hence we have partial information
Let’s assume the current percept is: [L, Clean]
i.e. start in #5 or #7
23
Contingency
• Contingency Solution
[Right, if dirt then Suck]
Move right
suck
24
Exploration
Example 1:
Assume the agent is some where outside the blocks and wants to clean the block. So how
to get into the blocks? No clear information about their location
What will be the solution?
Solution is exploration
Example 2:
• The agent is at some point in the world and want to reach a city called CITY which is
unknown to the agent.
• The agent doesn’t have any map
• What will be the solution?
• Solution is exploration
25
Steps to undertake during searching
Solution is a sequence of world state in which the final state satisfy the
goal or solution is action sequence in which the last action will result the
goal state.
Each action change one state to the next state of the world
A search algorithm take a problem as input and returns a solution in the
form of action or state sequence.
To achieve the goal, the action sequences must be executed accordingly
The general “formulate-search-execute” algorithm of search is given
below:
26
Example Road map Ethiopia
Aksum
100
200
Mekele
Gondar 80
180
Lalibela
110 250
150
Bahr dar
Dessie
170
400
330
Jima
Addis Ababa
100
430 Nazarez 370
Awasa
27
Road map Ethiopia
Solution
• Current position of the agent: Awasa.
• Needs to arrive to: Gondar
• Formulate goal:
• be in Gondar
• Formulate problem:
• states: various cities
• actions: drive between cities
• Find a solution:
• sequence of cities, e.g., Awasa, Nazarez, Addis Ababa, Dessie, Gondar
28
Well-defined problems and solutions
29
Well-defined problems and solutions
A problem is defined by four items:
1. initial state e.g., "at Awasa"
2. actions or successor function S(x) = set of action–state pairs
e.g., S(Awasa) = {<Awasa Addis Ababa, Addis Ababa>, <Awasa
Nazarez, Nazarez>, … }
Note: <AB, B> indicates action is A B and next state is B
3. goal test, can be
explicit, e.g., x = "at Gonder"
implicit, e.g., CheckGoal(x)
30
Well-defined problems and solutions
A problem is defined by four items:
1. initial state e.g., "at Awasa"
2. actions or successor function S(x) = set of action–state pairs
e.g., S(Awasa) = {<Awasa Addis Ababa, Addis Ababa>, <Awasa
Nazarez, Nazarez>, … }
Note: <AB, B> indicates action is A B and next state is B
3. goal test, can be
explicit, e.g., x = "at Gonder"
implicit, e.g., CheckGoal(x)
31
Missionaries and Cannibals problem and Cannibals problem-
32
Missionaries and Cannibals problem and Cannibals problem-
0 Initial setup: MMMCCC B –
1 Two cannibals cross over: MMMC B CC
MMM CCC
2 One comes back: MMMCC BC
3 Two cannibals go over again: MMM B CCC 1.MMMC B B CC
4 One comes back: MMMC B CC B
2.MMMCC <-C
5 Two missionaries cross: MC B MMCC
6 A missionary & cannibal return: MMCC B MC 3.MMM B BCCC
7 Two missionaries cross again: CC B MMMC
4.MMMC B <-CC
8 A cannibal returns: CCC B MMM
9 Two cannibals cross: C B MMMCC 5.MC B BMMCC
10 One returns: CC B MMMC
6MMCC B MC
11 And brings over the third: - B MMMCCC
7.CC B BMMMC
8.CCC 33
Missionaries and Cannibals problem and Cannibals problem-
MMM CCC
MMM CCC
1.MMMC B B CC
8.CCC B <- MMM
2.MMMCC B <-C
9.C B B CCMMM
3.MMM B BCCC
10. CC B <-CMMM
4.MMMC B <-CC
11. B CCCMMM
5.MC B BMMCC
6MMCC B MC
7.CC B BMMMC
34
Some more problems that can be solved by searching
• We have seen two such problems: The road map problem and the
vacuum cleaner world problem
• The following are some more problems
The three mice and the three cats problem
The three cannibals and the three missionaries problem
The water jug problem
The colored block world problem
35
3 cat and 3 mice puzzle (class work)
Three cat and three mice come to a crocodile infested river. There is a boat on their sides
that can be used by one or two “persons”. If cats outnumber the mice at any time, the cats
eat the mice. How can they use the boat to cross the river so that all mice survive.
State description
[#of cats to the left side,
#of mice to the left side,
boat location,
#of cats to the right side,
#of mice to the right side]
Initial state
[3,3,Left,0,0]
Goal
[0,0,Right,3,3]
36
Three cat and cat problem
0 Initial setup: CCCRRRB –
1 Two Cat cross over: RRRC B CC
RRR CCC
2 One comes back: RRRCC BC
3 Two cannibals go over again: RRR B CCC 1RRRC B B CC
4 One comes back: RRRC B CC B
2.RRRCC <-C
5 Two missionaries cross: RRC B RRCC
6 A Rat & Cat return: RRCC B RC 3.RRR B BCCC
7 Two Rat cross again: CC B RRRC
4.RRRC B <-CC
8 A Cat returns: CCC B RRR
9 Two Cat cross: C B MMMCC 5.RC B BRRCC
10 One returns: CC B MMMC
6RRCC B RC
11 And brings over the third: - B MMMCCC
7.CC B BRRRC
8.CCC 37
Missionaries and Cannibals problem and Cannibals problem-
RRR CCC
MMM CCC
1RRRC B B CC
8.CCC B <- RRR
2.RRRCC B <-C
9.C B B CCRRR
3.RRR B BCCC
10. CC B <-CRRR
4.RRRC B <-CC
11. B CCCRRR
5.RC B BRRCC
6RRCC B RC
7.CC B BRRRC
8.CCC
38
Assignment I 7.5%(submit in email)
1. Three missionaries and three cannibals come to the bank of a river they wish to cross.
There is a boat that will hold only two and any of the group is able to row. If there are
ever more missionaries than cannibals on any side of the river the cannibals will get
converted. How can they use the boat to cross the river without conversion?
2. We have one 3 liter jug, one 5 liter jug and unlimited supply of water. The goal is to
get exactly one liter of water in either of the jug. Either jug can be emptied, filled or
poured into the other.
State description
Identify states and goal
Action and all possible solutions
State the solution in diagram
39
Selecting a state space
Real world problem can not be directly represented in the agent architecture since it is
absurdly complex
state space must be abstracted for problem solving
(Abstract) state = set of real states
(Abstract) action = complex combination of real actions
e.g., “Awasa Addis Ababa" represents a complex set of possible routes, detours,
rest stops, etc.
(Abstract) solution = set of real paths that are solutions in the real world
Each abstract action should be "easier" than the original problem
40
Vacuum world state space graph
• states?
• actions?
• goal test?
• path cost?
41
Vacuum world state space graph
42
Example: 8 puzzle
• states?
• actions?
• goal test?
• path cost?
43
Example: 8 puzzle
46
Searching For Solution (Tree search algorithms)
Given state space, and network of states via actions.
The network structure is usually a graph
Tree is a network in which there is exactly one path defined from the root to
any node
Given state S and valid actions being at S
the set of next state generated by executing each action is called successor of S
Searching for solution is a simulated exploration of state space by generating
successors of already-explored states
47
Implementation issue: states vs. nodes
A state is a (representation of) a physical configuration
A node is a data structure constituting part of a search tree
It includes:
• state,
• parent node,
• action,
• depth and
• one or more costs [like path cost g(x), heuristic cost h(x), evaluation
function cost f(x)]
48
Implementation issue: states vs. nodes
• Example
49
Searching For Solution (Tree search algorithms)
•The Successor-Fn generate all the successors state and the action that leads moves the
current state into the successor state
•The Expand function creates new nodes, filling in the various fields of the node using
the information given by the Successor-Fn and the input parameters
50
Tree search Example
Awasa
BahrDar AA
Lalibela AA Gondar
Gondar Debre M.
51
Implementation: general tree search)
52
Search strategies
• A search strategy is defined by picking the order of node expansion
• Strategies are evaluated along the following dimensions:
• completeness: does it always find a solution if one exists?
• time complexity: number of nodes generated
• space complexity: maximum number of nodes in memory
• optimality: does it always find a least-cost solution?
• Time and space complexity are measured in terms of
• b: maximum branching factor of the search tree
• d: depth of the least-cost solution
• m: maximum depth of the state space (may be ∞)
• Generally, searching strategies can be classified in to two as uninformed and informed
search strategies
53
Search strategies
54
Uninformed search (blind search) strategies
Uninformed search strategies use only the information available in the problem definition
They have no information about the number of steps or the path cost from the current
state to the goal
They can distinguish the goal state from other states
They are still important because there are problems with no additional information.
Six kinds of such search strategies will be discussed and each depends on the order of
expansion of successor nodes.
1. Breadth-first search
2. Uniform-cost search
3. Depth-first search
4. Depth-limited search
5. Iterative deepening search
6. Bidirectional search
55
Breadth-first search
• Breadth-first search is a simple strategy in which the root node is expanded first,
then all the SEARCH successors of the root node are expanded next, then their
successors, and so on.
Expand shallowest unexpanded node
Finds the shallowest goal state
Implementation:
Fringe (open list) is a FIFO queue, i.e., new successors go at the end(ABCDEFG)
56
Breadth-first search
57
Breadth-first search
58
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
59
Depth-first search
61
Depth-first search
Complete? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path (see graph search)
complete in finite spaces
Time? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first
Space? O(bm), i.e., linear space!
When the strategy move one step down the tree, it will add m nodes into
the fringe and will have bm nodes at the worst case.
Optimal? No
62
Depth-limited search
We can see that the breadth search is complete which can be taken as its
advantage though its space complexity is the worst
Similarly the depth first search strategy is best in terms of space complexity
even if it is the worst in terms of its completeness and time complexity
compared to breadth first search
Hence, we can find an algorithm that incorporate both benefits and avoid the
limitation
Such algorithm is called depth limited search and its improved version is
called iterative deepening search strategy.
These two strategies will be explored in the following sections
63
Depth-limited search
• Depth-first search with depth limit l, will truncate all nodes having depth value greater than l from the
search space and apply depth first search on the rest of the structure
• It return solution if solution exist, if there is no solution
• it return cutoff if l < m, failure otherwise
Recursive implementation:
64
Depth-limited search
• Complete? No (fail if all solution exist at depth > l
• Time? O(bl)
• Space? O(bl)
• Optimal? No
65
Depth-limited search
66
Iterative deepening search
• Depth limit search never return a solution due to the limitation of the limit if all
solution node exist at depth >l. This limitation can be avoided by applying
iterative deepening search strategy
• Prototype
67
Iterative deepening search
1'st Iteration-----> A
2'nd Iteration----> A, B, C
3'rd Iteration------>A, B, D, E, C, F, G
4'th Iteration------>A, B, D, H, I, E, C, F, K, G
In the fourth iteration, the algorithm will
find the goal node.
68
Iterative deepening search
69
Iterative deepening search
70
Properties of Iterative deepening search algorithm
• Complete? Yes
• Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
• Space? O(bd)
• Optimal? Yes, if step cost = 1
71
Uniform-cost search
• Expand least-cost unexpanded node
• Implementation:
• fringe = queue ordered by path cost
• Equivalent to breadth-first if step costs all equal S
1 10 S
5 B 5
S G
A, 1 B, 5 C, 15
C 5
15
G, 11
S
A, 1 B, 5 C, 15
G, 11 G, 10 72
Uniform-cost search
• It finds the cheapest solution if the cost of a path never decrease as we go along the
path
• i.e. g(sucessor(n)) ≥ g(n) for every node n.
• Complete? Yes
• Time? # of nodes with g ≤ cost of optimal solution
• let ε be the minimum step cost in the search tree, C* is the total cost of the optimal solution and
branching factor b.
• Now you can ask, In the worst case, what will be the number of nodes that exist all of which has
step cost = ε, branching factor b and path cost ≤ C*. The resulting tree will have depth value
floor(C*/ ε)
• Hence the total node will be bceilling(C*/ ε) . Therefore, time complexity becomes O(bceiling(C*/ ε))
• Space? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε))
• Optimal? Yes – nodes expanded in increasing order of g(n)
73
Uniform-cost search
74
Bidirectional search
• Simultaneously search forward from the initial state and backward from
the goal state and terminate when the two search meet in the middle
• Reconstruct the solution by backward tracking towards the root and
forward tracking towards the goal from the point of intersection
• These algorithm is efficient if there any very limited one or two nodes
with solution state in the search space
75
Bidirectional search
76
Summary of algorithms
Repeated states
• Failure to detect repeated states can turn a linear problem into an
exponential one!
77
Graph search
78
Assignment I 15%(submit in email)
1. Three Cat and three cannibals come to the bank of a river they wish to cross. There is
a boat that will hold only two and any of the group is able to row. If there are ever
more missionaries than cannibals on any side of the river the cannibals will get
converted. How can they use the boat to cross the river without conversion?
2. We have one 3 liter jug, one 5 liter jug and unlimited supply of water. The goal is to
get exactly one liter of water in either of the jug. Either jug can be emptied, filled or
poured into the other.
State description
Identify states and goal
Action and all possible solutions
State the solution in diagram
79
Assignment I 7.5%(submit in email)
3. Three missionaries and three cannibals come to the bank of a river they wish to cross.
There is a boat that will hold only two and any of the group is able to row. If there are
ever more missionaries than cannibals on any side of the river the cannibals will get
converted. How can they use the boat to cross the river without conversion?
4. Explain informed search (Best first search and A* search)
Define their deference
Explain it through example
Properties ( completeness ,time, space ,optimality and others
Drawback (if any)
80
End ….
81