Title: Problem Solving as Search
1Problem Solving as Search
- Foundations of Artificial Intelligence
2Search and Knowledge Representation
- Goal-based and utility-based agents require
representation of - states within the environment
- actions and effects (effect of an action is
transition from the current state to another
state) - goals
- utilities
- Problems can often be formulated as a search
problem - to satisfy a goal, agent must find a sequence of
actions (a path in the state-space graph) from
the starting state to a goal state. - To do this efficiently, agents must have the
ability to reason with their knowledge about the
world and the problem domain - which path to follow (which action to choose
from) next - how to determine if a goal state is reached OR
how decide if a satisfactory state has been
reached.
3Introduction to Search
- Search is one of the most powerful approaches to
problem solving in AI - Search is a universal problem solving mechanism
that - Systematically explores the alternatives
- Finds the sequence of steps towards a solution
- Problem Space Hypothesis (Allen Newell, SOAR An
Architecture for General Intelligence.) - All goal-oriented symbolic activities occur in a
problem space - Search in a problem space is claimed to be a
completely general model of intelligence
4Problem-Solving Agents
function Simple-Problem-Solving-Agent(percept)
returns action inputs p, a percept
static s, an action sequence, initially empty
state, a description of current world
state g, a goal, initially null
problem, a problem formulation state
Update-State(state, p) if s empty then
g Formulate-Goal(state) problem
Formulate-Problem(state, g) s
Search(problem) endif action first(s) s
remainder(s) return action
- Assumptions on Environment
- Static formulating and solving the problem does
not take any changes into account - Discrete enumerating all alternative courses of
action - Deterministic actions depend only on previous
actions - Observable initial state is completely known
- The agent follows a simple formulate, search,
execute design
5Stating a Problem as a Search Problem
S
- State space S
- Successor function x in S ? SUCCESSORS(x)
- Cost of a move
- Initial state s0
- Goal test
- for state x in S
- ? GOAL?(x) T or F
6Example (Romania)
- Initial Situation
- On Holiday in Romania currently in Arad
- Flight leaves tomorrow from Bucharest
- Formulate Goal
- be in Bucharest
- Formulate Problem
- states various cities
- operators drive between cities
- Find Solution
- sequence of cities
- must start at starting state and end in the goal
state
7Example (Romania)
8Example Vacuum World
- Vacuum World
- Let the world be consist two rooms
- Each room may contain dirt
- The agent may be in either room
- initial both rooms dirty
- goal both rooms clean
- problem
- states each state has two rooms which may
contain dirt (8 possible) - actions go from room to room vacuum the dirt
- Solution
- sequence of actions leading to clean rooms
9Problem Types
- Deterministic, fully-observable gt single-state
problem - agent has enough info. to know exactly which
state it is in - outcome of actions are known
- Deterministic, partially-observable gt
multiple-state problem - sensorless problem Limited/no access to the
world state agent may have no idea which state
it is in - require agent to reason about sets of states it
can reach - Nondeterministic, partially-observable gt
contingency problem - must use sensors during execution percepts
provide new information about current state - no fixed action that guarantees a solution (must
compute the whole tree) - often interleave search, execution
- Unknown State Space gt exploration problem
(online) - only hope is to use learning (reinforcement
learning) to determine potential results of
actions, and information about states
10Example Vacuum World
- Single-State
- start in 5. Solutions?
- Multiple-State
- start in 1,2,3,4,5,6,7,8
- e.g., Right goes to 2,4,6,8. Solutions?
- Contingency
- Start in 5
- e.g., Suck can dirty a clean carpet
- Local sensing dirt, location only. Solutions?
Goal states
11Single-state problem formulation
- A problem is defined by four items
- initial state
- e.g., at Arad''
- operators (or successor function S(x))
- e.g., Arad gt Zerind Arad gt Sibiu
- goal test, can be
- explicit, e.g., x at Bucharest''
- implicit, e.g., NoDirt(x)
- path cost (additive)
- e.g., sum of distances, number of operators
executed, etc. - A solution is a sequence of operators leading
from the initial state to a goal state
12Selecting a state space
- Real world is absurdly complex
- state space must be abstracted for problem
solving - (Abstract) state set of real states
- (Abstract) operator complex combination of real
actions - e.g., Arad gt Zerind represents a complex set
of possible routes, detours, rest stops, etc. - For guaranteed realizability, any real state in
Arad must get to some real state in Zerind - (Abstract) solution set of real paths that are
solutions in the real world - Each abstract action should be easier than the
original problem!
13Example Vacuum World
- States? integer dirt and robot locations (ignore
dirt amounts) - Operators? Left, Right, Suck
- Goal Test? no dirt
- Path Cost? one per move
- What if the agent had no sensors the
multiple-state problem
Goal states
14Example The 8-Puzzle
- States? integer location of tiles
- Operators? move blank left, right, up, down
- Goal Test? goal state (given)
- Path Cost? One per move
- Note optimal solution of n-Puzzle problem is
NP-hard
158-Puzzle Successor Function
16State-Space Graph
- The state-space graph is a representation of all
possible legal configurations of the problem
resulting from applications of legal operators - each node in the graph is a representation a
possible legal state - each directed edge is a representation of a
possible legal move applied to a state (resulting
in a new state of the problem) - States
- representation of states should provide all
information necessary to describe relevant
features of a problem state - Operators
- Operators may be simple functions representing
legal actions - Operators may be rules specifying an action given
that a condition (set of constraints) on the
current state is satisfied - In the latter case, the rules are sometimes
referred to as production rules and the system
is referred to as a production system - This is the case with simple reflex agents.
17Vacuum World State-Space Graph
- State-space graph does not include initial or
goal states - Search Problem Given specific initial and goal
states, find a path in the graph from an initial
to a goal state - An instance of a search problem can be
represented as a search tree whose root note is
the initial state
18(No Transcript)
19Solution to the Search Problem
- A solution is a path connecting the initial to a
goal node (any one) - The cost of a path is the sum of the edge costs
along this path - An optimal solution is a solution path of minimum
cost - There might be no solution !
20(No Transcript)
21State Spaces Can be Very Large
- 8-puzzle ? 9! 362,880 states
- 15-puzzle ? 16! 1.3 x 1012 states
- 24-puzzle ? 25! 1025 states
22Searching the State Space
- Often it is not feasible to build a complete
representation of the state graph - A problem solver must construct a solution by
exploring a small portion of the graph - For a specific search problem (with a given
initial and goal state) we can view the relevant
portion as a search tree
23Searching the State Space
24Searching the State Space
Search tree
25Searching the State Space
Search tree
26Searching the State Space
Search tree
27Searching the State Space
Search tree
28Searching the State Space
Search tree
29Portion of Search Space for an Instance of the
8-Puzzle Problem
30Simple Problem-Solving Agent Algorithm
- s0 ? sense/read initial state
- GOAL? ? select/read goal test
- Succ ? select/read successor function
- solution ? search(s0, GOAL?, Succ)
- perform(solution)
31Example Blocks World Problem
- World consists of blocks A, B, C, and the Floor
- Can move a block that is clear on top of
another clear block or onto the Floor - State representation using the predicate
on(x,y) - on(x,y) means the block x is on top of block y
- on(x, Floor) means block x is on the Floor
- on(_, x) means block x has nothing on it (it is
clear) - Can specify operators as a set of production
rules - 1. on(_, x) ? on (x, Floor)
- 2. on(_, x) and on(_, y) ? on(x, y)
- Initial state some initial configuration
- E.g., on(A, Floor) and on(C, A) and on(B, Floor)
and on(_, B) and on(_, A) - Goal state some specified configuration
- E.g., on(B,C) and on(A,B)
32Blocks World State-Space Graph
on(_, x) ? on (x, Floor)
on(_, x) and on(_, y) ? on(x, y)
1
2
2
2
1
1
2
2
2
2
2
2
2
2
2
2
1
1
2
2
2
2
1
1
2
2
33Blocks World A Search Problem
Search tree for the problem
A
A
B
C
B
C
- Notes
- Repeated states have been eliminated in diagram.
- The highlighted path represents (in this case)
the only solution for this instance of the
problem. - The solution is a sequence of legal actions
move(A, Floor) ? move(B, C) ? move(A, B).
34Some Other Problems
358-Queens Problem
Place 8 queens in a chessboard so that no two
queens are in the same row, column, or diagonal.
A solution
Not a solution
36Formulation 1
- States all arrangements of 0, 1, 2, ..., or 8
queens on the board - Initial state 0 queen on the board
- Successor function each of the successors is
obtained by adding one queen in an empty square - Arc cost irrelevant
- Goal test 8 queens are on the board, with no two
of them attacking each other
? 64x63x...x53 3x1014 states
37Formulation 2
- States all arrangements of k 0, 1, 2, ..., or
8 queens in the k leftmost columns with no two
queens attacking each other - Initial state 0 queen on the board
- Successor function each successor is obtained by
adding one queen in any square that is not
attacked by any queen already in the board, in
the leftmost empty column - Arc cost irrelevant
- Goal test 8 queens are on the board
? 2,057 states
38Path Planning
What is the state space?
39Formulation 1
40Optimal Solution
This path is the shortest in the discretized
state space, but not in the original continuous
space
41Formulation 2
Visibility graph
42Formulation 2
Visibility graph
43Solution Path
The shortest path in this state space is also the
shortest in the original continuous space
44Search Strategies
- Uninformed (blind, exhaustive) strategies use
only the information available in the problem
definition - Breadth-first search
- Depth-first search
- Uniform-cost search
- Heuristic strategies use rules of thumb based
on the knowledge of domain to pick between
alternatives at each step
Graph Searching Applet http//www.cs.ubc.ca/labs/
lci/CIspace/Version4/search/index.html
45Implementation of Search Algorithms
function General-Search(problem, Queuing-Fn)
returns a solution, or failure nodes
Make-Queue(Make-Node(Initial-Stateproblem))
loop do if nodes empty then
return failure nodes Remove-Front(nodes)
if Goal-Testproblem applied to Statenode
succeeds then return node else
nodes Queuing-Fn(nodes, Expand(node,
Operatorsproblem)) return
- A state is a representation of a physical
configuration - A node is a data structure constituting part of a
search tree - includes parent, children, depth, or path cost
- States dont have parents, children, depth, or
path cost - The Expand function creates new nodes, filling in
various fields and using Operators (or
SucessorFn) of the problem to create the
corresponding states
46Search Strategies
- A strategy is defined by picking the order of
node expansion - i.e., how expanded nodes are inserted into the
queue - Strategies are evaluated along the following
dimensions - completeness - does it always find a solution if
one exists - time complexity - number of nodes generated /
expanded - 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 )
47Recall Searching the State Space
Search tree
Note that some states are visited multiple times
48Search Nodes ? States
49Search Nodes ? States
If states are allowed to be revisited,the search
tree may be infinite even when the state space is
finite
50Data Structure of a Node
Depth of a node N length of path from root to
N (Depth of the root 0)
51Node expansion
- The expansion of a node N of the search tree
consists of - Evaluating the successor function on STATE(N)
- Generating a child of N for each state returned
by the function
52Basic Search Procedure
- 1. Start with the start node (root of the search
tree) and place in on the queue - 2. Remove the front node in the queue and
- If the node is a goal node, then we are done
stop. - Otherwise expand the node ? generate its
children using the successor function (other
states that can be reached with one move) - 3. Place the children on the queue according to
the search strategy - 4. Go back to step 2.
53Search Strategies
- Search strategies differ based on the order in
which new successor nodes are added to the queue - Breadth-first ? add nodes to the end of the queue
- Depth-first ? add nodes to the front
- Uniform cost ? sort the nodes on the queue based
on the cost of reaching the node from start node
54Breadth-First Search
1
4
2
3
5
6
7
8
9
10
12
13
11
14
goal
55Example (Romania)
56Breadth-First Search
- Always expand the shallowest unexpanded node
- QueuingFN insert successor at the end of the
queue
Arad
Arad
Zerind
Sibiu
Timisoara
57Breadth-First Search
Arad
Zerind
Sibiu
Timisoara
Oradea
Arad
58Breadth-First Search
Arad
Zerind
Sibiu
Timisoara
Rimnicu Vilcea
Oradea
Oradea
Fagaras
Arad
Arad
59Breadth-First Search
Arad
Sibiu
Timisoara
Zerind
Rimnicu Vilcea
Oradea
Fagaras
Arad
Oradea
Lugoi
Arad
Arad
60Depth d 4 Branching factor b 2
goal
No. of nodes examined through level 3 (d-1) 1
2 22 23 1 2 4 8 15
Avg. no. of nodes examined at level 4 (1 24)
/ 2 (min 1, max 24)
61Breadth-First Search
- Space complexity
- Full tree at depth d uses bd memory nodes
- If you know there is a goal at depth d, you are
done otherwise have to store the nodes at depth
d1 as you generate them so might need bd1
memory nodes - Nodes examined (assume tree has depth d with a
single goal node at that depth) - for large b, this is O(bd) (the fringe dominates)
Number of internal nodes before reaching goal at
depth d
Average number of nodes examined at the fringe (
at depth d)
62Properties of Breadth-First Search
- Complete?
- Yes, if b is finite
- Time Complexity?
- 1 b b2 b3 . . . bd O(bd)
- Space Complexity?
- O(bd) (keeps every node in memory)
- Optimal?
- Yes (if cost 1 per step) but, not optimal in
general - Note biggest problem in BFS is the space
complexity
63Breadth-First Search Time and Space Complexity
- Assume
- branching factor b10
- 1000 nodes/second
- 100 bytes/node
64Depth-First Search
1
13
2
8
12
3
9
14
7
10
4
11
15
goal
6
5
65Depth-First Search
- Always expand the deepestest unexpanded node
- QueuingFN insert successor at the front of the
queue
Arad
Zerind
Sibiu
Timisoara
Oradea
Arad
66Depth-First Search
Arad
Zerind
Sibiu
Timisoara
Arad
Oradea
Note that DFS can perform infinite cyclic
excursions. Need a finite, non-cyclic search
space, or repeated state-checking.
Zerind
Sibiu
Timisoara
67Best case in Depth-First Search Goal node is on
the far left.
Depth d 4 Branching factor b 2
worst case
goal
Highlighted nodes are those that have to be kept
in memory.
In best case, we examine d 1 5 nodes. In
worst case, need all the nodes 1 2 4 8
16 (bd) 31
68Depth-First Search
- Space complexity (assume tree has depth d with a
single goal node at that depth) - The most memory is needed at the first point we
reach depth d - Need to store b-1 nodes at each depth (siblings
of the node already expanded) with one additional
node at depth d (since it hasnt been expanded
yet) - Total space d(b-1) 1 (the 1 additional node
is for the goal at depth d) - Nodes examined (assume tree has depth d with a
single goal node at that depth) - Best case (goal is at far left) gt d 1 nodes
- Worst case gt
- Average case gt
- for large b, this O(bd) (the fringe dominates)
69Properties of Depth-First Search
- Complete?
- No fails in infinite-depth spaces, spaces with
loops - need to modify the algorithm to avoid repeated
states along paths - Time Complexity?
- O(bm) terrible if m is much larger than d
- but, if solutions are dense, may be much faster
that BFS - Space Complexity?
- O(bm) (i.e., linear space)
- Optimal?
- No
70Iterative Deepening
- Depth-Limited Search
- depth-first search with depth limit l
- Nodes at depth l have no successors
function Iterative-Deepening-Search(problem,
Queuing-Fn) returns a solution sequence inputs
problem for depth 0 to do result
Depth-Limited-Search(problem, depth) if
result ¹ cutoff then return result
end
71Iterative Deepening
l 0
Arad
l 1 steps 1 and 2
Arad
Sibiu
Timisoara
Zerind
72Iterative Deepening
Arad
Timisoara
Zerind
Sibiu
l 2 steps 1, 2, and 3
Oradea
Arad
73Iterative Deepening
l 2 step 5
Arad
Sibiu
Timisoara
Zerind
Rimnicu Vilcea
Oradea
Fagaras
Arad
Oradea
Arad
Lugoi
Arad
74Iterative Deepening
- Space complexity
- if the shallowest solution is at depth g, then
the depth-first search to this depth will succeed
(so Iterative Deepening will always return the
shallowest solution). Since each of the
individual searches are performed depth-first,
the amount of memory required is same as
depth-first search. - Nodes examined (assume tree has depth d with a
single goal node at that depth)
No. of nodes examined in the final (successful)
iteration (same as DFS)
(1)
For each of depths j 1, 2, , d-1, must examine
the entire tree
(2)
Total nodes examined in failing searches
75Properties of Iterative Deepening
- Complete?
- Yes
- Time Complexity?
- Adding (1) and (2) from before gives
- Space Complexity?
- O(bd)
- Optimal?
- Yes (if cost 1 per step)
- Can be modified to explore uniform-cost search
O(bd)
76Uniform-Cost Search
- Always expand the least-cost unexpanded node
- Queue insert in order of increasing path cost
Arad
75
118
140
Sibiu
Timisoara
Zerind
lt Zerind, Timisoara, Sibiu lt
77Uniform-Cost Search
Arad
75
118
140
Sibiu
Timisoara
Zerind
7175
7575
Arad
Oradea
lt Timisoara, Sibiu, Oradea, Arad lt
78Uniform-Cost Search
Arad
75
118
140
Sibiu
Timisoara
Zerind
7575
118118
7175
111118
Arad
Arad
Oradea
Lugoi
lt Sibiu, Oradea, Arad, Lugoi, Arad lt
79Uniform-Cost Search
Arad
75
118
140
Sibiu
Timisoara
Zerind
7575
118118
7175
111118
Arad
Arad
Oradea
Lugoi
lt Sibiu, Oradea, Arad, Lugoi, Arad lt
80Uniform Cost Search
- For the rest of the example, let us assume
repeated state checking - If a newly generated state was previously
expanded, then discard the new state - If multiple (unexpanded) instances of a state end
up on the queue, we only keep the instance that
has the least path cost from the start node and
eliminate the other instances.
81Uniform-Cost Search
Arad
75
118
140
Sibiu
Timisoara
Zerind
7175
111118
Oradea
Lugoi
lt Sibiu, Oradea, Lugoi lt
82Uniform-Cost Search
Arad
75
140
118
Zerind
Sibiu
Timisoara
239
220
146
229
Fagaras
Rimnicu
Oradea
Lugoi
lt Oradea, Rimnicu, Lugoi, Fagaras lt
83Uniform-Cost Search
Note Oradea only leads to repeated states.
Arad
75
140
118
Zerind
Sibiu
Timisoara
239
220
146
229
Fagaras
Rimnicu
Oradea
Lugoi
lt Rimnicu, Lugoi, Fagaras lt
84Uniform-Cost Search
85Uniform-Cost Search
Arad
75
140
118
Zerind
Sibiu
Timisoara
239
220
146
229
Rimnicu
Oradea
Fagaras
Lugoi
367
317
Craiova
Pitesti
lt Lugoi, Fagaras, Pitesti, Craiova lt
86Uniform-Cost Search
Arad
75
140
118
Zerind
Sibiu
Timisoara
239
220
146
229
Rimnicu
Oradea
Fagaras
Lugoi
367
317
299
Craiova
Pitesti
Mehadia
lt Fagaras, Mehadia, Pitesti, Craiova lt
87Uniform-Cost Search
Arad
140
118
75
Sibiu
Zerind
Timisoara
239
220
146
229
Rimnicu
Fagaras
Oradea
Lugoi
367
317
450
299
Craiova
Pitesti
Bucharest
Mehadia
lt Mehadia, Pitesti, Craiova, Bucharest lt
88Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
450
Craiova
Pitesti
Mehadia
299
Bucharest
Dobreta
374
lt Pitesti, Craiova, Dobreta, Bucharest lt
89Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
450
Craiova
Pitesti
Mehadia
299
Bucharest
455
418
Bucharest
Dobreta
374
Craiova
lt Craiova, Dobreta, Bucharest lt
90Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
450
Craiova
Pitesti
Mehadia
299
Bucharest
455
418
Bucharest
Dobreta
374
Craiova
lt Craiova, Dobreta, Bucharest lt
91Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
Craiova
Pitesti
Mehadia
299
418
Dobreta
374
Bucharest
Goes to repeated states with higher path costs
than previous visits to those states
lt Craiova, Dobreta, Bucharest lt
92Uniform-Cost Search
93Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
Craiova
Pitesti
Mehadia
299
418
Dobreta
374
Bucharest
lt Bucharest lt
94Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
Craiova
Pitesti
Mehadia
299
418
Dobreta
374
Bucharest
lt Urziceni, Giurgiu lt
519
Pitesti
508
503
Fagaras
629
Giurgiu
Urziceni
95Uniform-Cost Search
Arad
118
140
75
Sibiu
Timisoara
Zerind
239
229
220
146
Rimnicu
Oradea
Fagaras
Lugoi
367
317
Craiova
Pitesti
Mehadia
299
418
Dobreta
374
Bucharest
Solution Path Arad ? Sibiu ? Rimnicu ? Pitesti ?
Bucharest
Total cost 418
Compare this to Arad ? Sibiu ? Fagaras ?
Bucharest with total cost of 450
96Properties of Uniform-Cost Search
- Complete?
- Yes, if b is finite (similar to Breadth-First
search) - Time Complexity?
- Number of nodes with g(n) cost of optimal
solution - Space Complexity?
- Number of nodes with g(n) cost of optimal
solution - Optimal?
- Yes, if the path cost never decreases along any
path - i.e., if g(Successor(n)) ³ g(n), for all
nodes n - What happens if we had operators with negative
costs?