Chapter 3
Problem solving by
searching
Solving Problems by
Searching
• Reflex agent is simple
• Base on their actions
• A direct mapping from states to actions
• But cannot work well in environments
• which this mapping would be too large to store
• and would take too long to learn
• Hence, goal-based agent is used
• The concepts of asymptotic complexity (that is, O()
notation) and NP-completeness
Problem-solving agent
• Problem-solving agent
• A kind of goal-based agent
• It solves problem by
• finding sequences of actions that lead to desirable states
(goals)
• To solve a problem,
• The first step is the goal formulation, based on the current
situation
• Goals help organize behavior by limiting the objectives that
the agent is trying to achieve and hence the actions it needs to
consider.
Steps to maximize the
performance measure
• Goal formulation
• Problem formulation
• Search
• Execution
Goal formulation
• The goal is formulated
• as a set of world states, in which the goal is satisfied
• Reaching from initial state goal state
• Actions are required
• Actions are the operators
• Causing transitions between world states
• Actions should be abstract enough at a certain degree,
instead of very detailed
• E.g., turn left VS turn left 30 degree, etc.
Problem formulation
• The process of deciding
• what actions and states to consider given a goal.
• E.g., Driving Amman Zarqa
• In-between states and actions defined
• States: Some places in Amman & Zarqa
• Actions: Turn left, Turn right, go straight, accelerate &
brake, etc.
Search
• Because there are many ways to achieve the same
goal
• Those ways are together expressed as a tree
• Multiple options of unknown value at a point,
• the agent can examine different possible sequences of actions, and
choose the best
• This process of looking for the best sequence is called
search
• The best sequence is then a list of actions, called solution
Execution
• Once the solution has found, the actions that are
recommended by search process can be carried out
and it is known as execution phase.
Search algorithm
• Defined as
• taking a problem and returns a solution
• Once a solution is found
• the agent follows the solution and carries out the list of
actions – execution phase
• Design of an agent
• “Formulate, search, execute”
Well-defined problems and
solutions
A problem is defined by 5 components:
• Initial state
• Actions
• Transition model /(Successor functions)
• Goal Test.
• Path Cost.
Well-defined problems and
solutions
• A problem is defined by 4 components:
• The initial state
• that the agent starts in
• The set of possible actions
• Transition model: description of what each action does.
(successor functions): refer to any state reachable from given
state by a single action
• Initial state, actions and Transition model define the state
space
• the set of all states reachable from the initial state by any sequence
of actions.
• A path in the state space:
• any sequence of states connected by a sequence of actions.
Well-defined problems and
solutions
• The goal test
• Applied to the current state to test
• If the agent is in its goal
-Sometimes there is an explicit set of possible goal states.
(Example: in Amman).
-Sometimes the goal is described by the properties
• Instead of stating explicitly the set of states
• Example: Chess
• The agent wins if it can capture the KING of the opponent on next
move ( checkmate).
• No matter what the opponent does
Well-defined problems and
solutions
• A path cost function,
• Assigns a numeric cost to each path = performance
measure
• Denoted by g to distinguish the best path from others
• Usually the path cost is
• The sum of the step costs of the individual actions (in
the action list)
Well-defined problems and
solutions
• Together a problem is defined by
• Initial state
• Actions
• Successor function
• Goal test
• Path cost function
• The solution of a problem is then
• a path from the initial state to a state satisfying the goal test
• Optimal solution
• the solution with lowest path cost among all solutions
Ex.
• Initial state:
In(Arad)
• ACTIONS(s) { Go(Sibiu), Go(Timisoara), Go(Zerind) }
• The Transition model, specified by a function
• RESULT(s, a) that returns the state that results from
TRANSITION MODEL doing action a in state s.
RESULT(In(Arad),Go(Zerind)) = In(Zerind)
• The step cost of taking action a in state s to reach state s
is denoted by c(s, a, s)
Formulating problems
• Besides the four components for problem formulation
• anything else?
• Abstraction
• the process to take out the irrelevant information
• leave the most essential parts to the description of the states
( Remove detail from representation)
• Conclusion: Only the most important parts that are
contributing to searching are used
Evaluation Criteria
• Formulation of a problem as search task
• Basic search strategies
• Important properties of search strategies
• Selection of search strategies for specific tasks
(The ordering of the nodes in FRINGE defines the
search strategy)
Problem-Solving Agents
• Agents whose task is to solve a particular problem
(steps)
• Goal formulation
• what is the goal state
• what are important characteristics of the goal state
• how does the agent know that it has reached the goal
• are there several possible goal states
• are they equal or are some more preferable
• Problem formulation
• what are the possible states of the world relevant for solving the
problem
• what information is accessible to the agent
• how can the agent progress from state to state
Example: Romania
Single-state problem formulation
A problem is defined by four items:
1. Initial state e.g., "at Arad"
2. Actions or successor function S(x) = set of action–state
pairs
1. E.g., S(Arad) =
RESULT(IN(ARAB),GO(ZERIND)]=IN(Zerind))
3. Goal test, can be
1. Explicit, E.g., x = "at Bucharest"
2. Implicit, E.g., Checkmate(x)
4. Path cost (additive)
1. E.g., sum of distances, number of actions executed, etc.
2. c(x,a,y) is the step cost, assumed to be ≥ 0
• A solution is a sequence of actions leading from the initial
state to a goal state
Example problems
• Toy problems
• those intended to illustrate or exercise various problem-
solving methods
• E.g., puzzle, chess, etc.
• Real-world problems
• tend to be more difficult and whose solutions people
actually care about
• E.g., Design, planning, etc.
Toy problems- Vacuum
World
Compared with the real world, this toy problem has discrete locations, discrete dirt, reliable
cleaning, and it never gets any dirtier.
27
Number of states:
8 Initial state:
Any Number of
actions: 4 suck,
left, right,
noOp
Goal: clean up all
dirt
Goal states: {7,
8}
Path Cost:
Each step costs 1
28
29
8-puzzle
A tile adjacent to the blank space can slide into the space. The object is to reach a
specified goal state
Prepared by Sharika T R,
SNGCE
30
DEPARTMENT OF CSE SNGCE
31
The 8-puzzle has 9!/2 = 181, 440 reachable states and
is easily solved. The 15-puzzle (on a 4×4 board) has
around 1.3 trillion states, and random
instances can be solved optimally in a few milliseconds
by the best search algorithms.
The 24-puzzle (on a 5 × 5 board) has around 1025 states,
and random instances take several hours to solve
optimally.
32
8-queens problem
The goal of the 8-queens problem is to place eight queens on
a chessboard such that no queen attacks any other.
A queen attacks any piece in the same row, column or
diagonal.
33
There are two main kinds of formulation
◦An incremental formulation
involves operators that augment the state description starting from an empty state
Each action adds a queen to the state
States:
any arrangement of 0 to 8 queens on board
Successor function:
add a queen to any empty square
◦A complete-state formulation
starts with all 8 queens on the board
move the queens individually around
States:
any arrangement of 8 queens, one per column in the leftmost columns
Operators: move an attacked queen to a row, not attacked by any other
othe right formulation makes a big difference to the size of the search space
34
Incremental formulation
In this formulation, we have 64 · 63 ··· 57 ≈ 1.8 × 1014 possible sequences to investigate. A better
formulation would prohibit placing a queen in any square that is already attacked:
This formulation reduces the 8-queens state space from 1.8 × 1014 to just 2,057, and solutions are easy
to find. On the other hand, for 100 queens the reduction is from roughly 10400 states to about 1052
states (Exercise 3.5)—a big improvement, but not enough to make the problem tractable
35
Real-world problems-
Route-Finding problem
Route-finding problems
Touring problems
Traveling Salesman problem
VLSI layout problem
Robot navigation
Automatic assembly sequencing
Internet searching
36
Airline travel
problems
A really good system should include contingency plans such as backup reservations on alternate flights to the
extent that these are justified by the cost and likelihood of failure of the original plan.
37
Touring
problems
“Visit every city in at least once, starting and ending in Bucharest.”
Actions correspond to trips between adjacent cities
Each state must include not just the current location but also the set of
cities the agent has visited.
So the initial state would be In(Bucharest), Visited({Bucharest}), a
typical
Intermediate state would be In(Vaslui), Visited({Bucharest, Urziceni,
Vaslui}),
Goal test would check whether the agent is in Bucharest and all 20
cities have been visited.
39
Traveling salesperson
problem
The traveling salesperson problem (TSP) is a touring problem in
which each city must be visited exactly once. The aim is to find the
shortest tour.
The problem is known to be NP-hard, but an enormous amount of
effort has been expended to improve the capabilities of TSP
algorithms.
In addition to planning trips for traveling salespersons, these
algorithms have been used for tasks such as planning movements
of automatic circuit-board drills and of stocking machines on
shop floors
40
VLSI layout
VLSI layout problem requires positioning millions of components and
connections on a chip to minimize area, minimize circuit delays, minimize stray
capacitances, and maximize manufacturing yield.
The layout problem comes after the logical design phase and is usually split into
two parts:
◦ Cell layout and Channel routing.
Cell layout
◦ The primitive components of the circuit are grouped into cells, each of which
performs some recognized function.
◦ Each cell has a fixed footprint and requires a certain number of connections to
each of the other cells.
◦ The aim is to place the cells on the chip so that they do not overlap and so that
there is room for the connecting wires to be placed between the cells.
Channel routing
◦ Finds a specific route for each wire through the gaps between the cells.
41
Robot navigation
Robot navigation is a generalization of the route-finding
problem.
Rather than following a discrete set of routes, a robot can move
in a continuous space with an infinite set of possible actions
and states.
For a circular robot moving on a flat surface, the space is
essentially two-dimensional.
When the robot has arms and legs or wheels that must also be
controlled, the search space becomes many-dimensional.
Advanced techniques are required just to make the search
space finite.
42
Automatic assembly
Aim is to find an order in which to assemble the parts of some object.
sequencing
If the wrong order is chosen, there will be no way to add some
part later in the sequence without undoing some of the work
already done.
Checking a step in the sequence for feasibility is a difficult
geometrical search problem closely related to robot navigation.
Thus, the generation of legal actions is the expensive part of assembly
sequencing.
Any practical algorithm must avoid exploring all but a tiny fraction of
the state space.
Protein design is an automatic assembly problem in which the goal
is to find a sequence of amino acids that will fold into a three-
dimensional protein with the right properties to cure some disease.
43
Searching for
solution
A solution is an action sequence, so search
algorithms work by considering various possible
action sequences.
The possible action sequences starting at the initial
state form a search tree with the initial state at the root
The branches are actions and the nodes correspond to
states in the state space of the problem.
44
Search tree
for finding a
route from
Arad to
Bucharest
45
Searching for solutions
Searching for solutions
• Finding out a solution is done by
• Searching through the state space
• All problems are transformed
• As search tree
• Generated by the initial state and successor function
Search tree
• Initial state
• The root of the search tree is a search node
• Expanding
• Applying successor function to the current state
• There by generating a new set of states
• leaf nodes
• The states having no successors
Fringe: Set of search nodes that have not been expanded yet.
Tree search example
Tree search example
Search tree
• The essence of searching
• in case the first choice is not correct
• choosing one option and keep others for later inspection
• Hence we have the search strategy
• which determines the choice of which state to expand
• good choice fewer work faster
• Important:
• state space ≠ search tree
Search tree
• State space
• has unique states {A, B}
• while a search tree may have cyclic paths: A-B-A-B-A-
B- …
• A good search strategy should avoid such paths
Contd.,
• The way to avoid exploring redundant/cyclic paths is to
remember where one has been.
• To do this, we need to augment the TREE-SEARCH
algorithm with a data structure called the explored set (also
EXPLORED SET known as the closed list), which
remembers every expanded node.
• Newly generated nodes that match previously generated
nodes—ones in the explored set or the frontier—can be
discarded instead of being added to the frontier.
• Frontier: The set of all leaf nodes available for expansion at
any given point is called the Frontier.
TREE-SEARCH
algorithm
With a data structure called the explored set (also known as
the closed list), which remembers every expanded node.
Newly generated nodes that match previously generated nodes
ones in the explored set or the frontier can be discarded
instead of being added to the frontier
Graph Search algorithm
GRAPH-SEARCH algorithm
Each state in the graph only once. But, it
appearsmay appear in the tree
multiple times
contains at most one copy of each state, so we can
think of it as growing a tree directly on the state-
space graph,
Don’t add a node if its state has already been
expanded or a
node pointing to the same state is already in the
frontier.
so that every path from the initial state to an
unexplored state has to pass through a state in
the frontier.
As every step moves a state from the frontier
into the explored region while moving some
states from the unexplored region into the
frontier,
we see that the algorithm is systematically
examining the
states in the state space, one by one, until it
finds a solution.
Search tree
• A node is having five components:
• STATE: which state it is in the state space
• PARENT-NODE: from which node it is generated
• ACTION: which action applied to its parent-node to generate it
• PATH-COST: the cost, g(n), from initial state to the node n
itself
• DEPTH: number of steps along the path from the initial state
Measuring problem-solving
performance
• The evaluation of a search strategy
• Completeness:
• Is the strategy guaranteed to find a solution when there is one?
• Optimality:
• Does the strategy find the highest-quality solution when there are
several different solutions?
• Time complexity:
• How long does it take to find a solution?
• Space complexity:
• How much memory is needed to perform the search?
Measuring problem-solving
performance
• In AI, complexity is expressed in
• b, branching factor, maximum number of successors of any
node
• d, the depth of the shallowest goal node.
(depth of the least-cost solution)
• m, the maximum length of any path in the state space
• Time and Space is measured in
• Number of nodes generated during the search
• Maximum number of nodes stored in memory
Measuring problem-solving
performance
• For effectiveness of a search algorithm
• we can just consider the total cost
• The total cost = path cost (g) of the solution found +
search cost
• search cost = time necessary to find the solution
• Tradeoff:
• (long time, optimal solution with least g)
• vs. (shorter time, solution with slightly larger path cost g)
3.4 Uninformed search
strategies
[Link]
3.4 Uninformed search
strategies
• Uninformed search
• No information about the number of steps
• Or the path cost from the current state to the goal
• Search the state space blindly
• Informed search, or heuristic search
• A cleverer strategy that searches toward the goal,
• Based on the information from the current state so far
Uninformed search
strategies
• Breadth-first search
• Uniform cost search
• Depth-first search
• Depth-limited search
• Iterative deepening search
• Bidirectional search
Breadth-first search
• The root node is expanded first (FIFO)
• Frontier is maintained as FIFO list
• All the nodes generated by the root node are then
expanded
• And then their successors and so on
Breadth-first search
Expand shallowest unexpanded node
•
• Implementation:
• fringe is a FIFO queue, i.e., new successors go at end
•
70
Breadth-first search
Expand shallowest unexpanded node
•
• Implementation:
• fringe is a FIFO queue, i.e., new successors go at end
•
71
Breadth-first search
Expand shallowest unexpanded node
•
• Implementation:
• fringe is a FIFO queue, i.e., new successors go at end
•
72
BFS ALGORITHM
Example: Traveling from
Arad to Bucharest
Breadth-first search
(Analysis)
• Breadth-first search
• Complete – find the solution eventually
• Optimal, if step cost is 1
The disadvantage
• if the branching factor of a node is large,
• the space complexity and the time complexity are enormous
Properties of breadth-first
search
Complete? Yes (if b is finite)
Time? 1+b+b2+b3+… +bd = O(bd) (Worst Case)
When goal test is applied when selected for expansion than when generated then it is -
O(bd+1)
b-branch factor
d-depth
Space? O(bd+1) (keeps every node in memory)(The notation O(bd+1)
suggests an exponential growth pattern based on branching factor (b) and
depth (d). )
Optimal? Yes (if cost = 1 per step)
Space is the bigger problem (more than time)
Breadth-first search
(Analysis)
• Assuming 10000 nodes can be processed per second, each with 1000
bytes of storage