Pai m2 Notes
Pai m2 Notes
Pai m2 Notes
PAI M2 Notes
Module: 2
Problem‐solving: Problem‐solving agents, Example problems, Searching for Solutions,
Uninformed Search Strategies: Breadth First search, Depth First Search, Iterative Deepening
Depth First Search
Textbook 1: Chapter 3- 3.1, 3.2, 3.3, 3.4
Chapter 3
SOLVING PROBLEMS BY SEARCHING
• Agent: It is something which perceives (senses) and acts in an environment.
• Agent Function: The agent function for an agent specifies the action taken by
the agent in response to any percept sequence.
• An agent is anything that can perceive its environment through sensors and acts upon
that environment through actuators.
• Agent can be anything and everything!
PROBLEM-SOLVING AGENTS
• Problem formulation is the process of deciding what actions and states to consider,
given a goal.
• Goal-based agents use more advanced factored or structured representations are usually
• called planning agents.
• Goals help organize behavior by limiting the objectives that the agent is trying to
achieve and hence the actions it needs to consider.
• Goal formulation, based on the current situation and the agent’s performance measure,
is the first step in problem solving.
• Consider a goal to be a set of world states—exactly those states in which the goal is
satisfied. The agent’s task is to find out how to act, now and in the future, so that it
reaches a goal state. Before it can do this, it needs to decide (or we need to decide on its
behalf) what sorts of actions and states it should consider.
• In general, an agent with several immediate options of unknown value can decide what
to do by first examining future actions that eventually lead to states of known value.
• The process of looking for a sequence of actions that reaches the goal is called search.
• A search algorithm takes a problem as input and returns a solution in the form of an
action sequence.
• It first formulates a goal and a problem, searches for a sequence of actions that
would solve the problem, and then executes the actions one at a time. When
this is complete, it formulates another goal and starts over.
refer to any state reachable from a given state by a single action. For
example, we have
RESULT(In(Arad),Go(Zerind)) = In(Zerind) .
Together, the initial state, actions, and transition model implicitly
define the state space of the problem—the set of all states reachable from the
initial state by any sequence of actions. A path in the state space is a sequence
of states connected by a sequence of actions.
iv. The goal test, which determines whether a given state is a goal state.
Sometimes there is an explicit set of possible goal states, and the test simply
checks whether the given state is one of them.
The agent’s goal in Romania is the singleton set {In (Bucharest)}
v. A path cost function that assigns a numeric cost to each path. The problem-
solving agent chooses a cost function that reflects its own performance
measure. The step cost of taking action, a in state, s to reach state, s is
denoted by c (s, a, s’). The step costs for Romania are shown in Figure below
as route distances.
Formulating Problems
EXAMPLE PROBLEMS
• A toy problem is intended to illustrate or exercise various problem-solving methods. It
can be given a concise, exact description and hence is usable by different researchers to
compare the performance of algorithms.
• A real-world problem is one whose solutions people actually care about. Such problems
tend not to have a single agreed-upon description, but we can give the general flavor of
their formulations.
Standardized Problems
A. Toy Problem
• States: The state is determined by both the agent location and the dirt locations.
The agent is in one of two locations, each of which might or might not contain
dirt. Thus, there are 2 × 22 = 8 possible world states.
• Initial state: Any state can be designated as the initial state.
DEPT OF AI&ML, VKIT MODULE 2 NOTES
• Actions: In this simple environment, each state has just three actions: Left,
Right, and Suck. Larger environments might also include Up and Down.
• Transition model: The actions have their expected effects, except that moving
Left in the leftmost square, moving Right in the rightmost square, and Sucking
in a clean square have no effect. The complete state space is shown in Figure
below.
• Goal test: This checks whether all the squares are clean.
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
B. 8-Puzzle Problem
- The 8-puzzle, an instance of which is shown in Figure below, consists of a 3×3 board
with eight numbered tiles and a blank space.
- A tile adjacent to the blank space can slide into the space. The object is to reach a
specified goal state, such as the one shown on the right of the figure.
• States: A state description specifies the location of each of the eight tiles and the
blank in one of the nine squares.
• Initial state: Any state can be designated as the initial state. Note that any given
goal can be reached from exactly half of the possible initial states
• Actions: The simplest formulation defines the actions as movements of the blank
space Left, Right, Up, or Down. Different subsets of these are possible depending
on where the blank is.
• Transition model: Given a state and action, this returns the resulting state; for
example, if we apply Left to the start state in Figure 3.4, the resulting state has the
5 and the blank switched.
• Goal test: This checks whether the state matches the goal configuration shown in
Figure.
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.
C. 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.)
- Figure shows an attempted solution that fails: the queen in the rightmost column is
attacked by the queen at the top left.
D. Knuth’s Problem
• Our final toy problem was devised by Donald Knuth (1964) and illustrates how
infinite state spaces can arise.
• Knuth conjectured that, starting with the number 4, a sequence of factorial,
square root, and floor operations will reach any desired positive integer.
• For example, we can reach 5 from 4 as follows:
B. Touring problems
- It is closely related to route-finding problems, but with an important difference.
- Consider, for example, the problem “Visit every city in Figure – Route Map of Romania
at least once, starting and ending in Bucharest.”
- As with route finding, the actions correspond to trips between adjacent cities.
- The state space, however, is quite different.
- 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}).
- The goal test would check whether the agent is in Bucharest and all 20 cities have been
visited.
C. VLSI Layout
- A 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. In 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 (size and shape) and requires a certain number of
connections to each of the other cells.
• States: It might be anywhere on the cell layout
• Initial State: Starting position from where the routing starts.
• Actions: Each channel routing finding a specific route for each wire through
the gaps.
• Transition model: Continuous channel routing between the components.
• Goal: 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.
• Path Cost: Time taken for channel routing to finds a specific route for perfect
positioning.
D. 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.
The function CHILD-NODE takes a parent node and an action and returns the resulting child
node:
- The frontier needs to be stored in such a way that the search algorithm can easily choose
the next node to expand according to its preferred strategy.
- The appropriate data structure for this is a queue. The operations on a queue are as
follows:
• EMPTY?(queue) returns true only if there are no more elements in the queue.
• POP(queue) removes the first element of the queue and returns it.
• INSERT(element, queue) inserts an element and returns the resulting queue.
- Queues are characterized by the order in which they store the inserted nodes.
- Three common variants are:
→ The first-in, first-out or FIFO queue, which pops the oldest element of the queue;
→ The last-in, first-out or LIFO queue (also known as a stack), which pops the newest
element of the queue;
→ The priority queue, which pops the element of the queue with the highest priority
according to some ordering function.
BREADTH-FIRST SEARCH
- Breadth-first search is a simple strategy in which the root node is expanded first, then
all the successors of the root node are expanded next, then their successors, and so on.
- In general, all the nodes are expanded at a given depth in the search tree before any
nodes at the next level are expanded.
- Since this algorithm searches breadth-wise in a tree or graph, so it is called Breadth-
First Search.
- It is an example of general graph search algorithm.
- It is implemented using FIFO queue data structure
Advantages:
- Breadth-first search will provide a solution if any solution exists.
- If there are more than one solution for a given problem, the breadth-first search will
provide minimal solution which requires the least number of steps.
Disadvantages:
- It requires lots of memory since each level of the tree must be saved into memory to
expand the next level.
- Breadth first search needs a lot of time since it reads all the nodes from the root node.
- Let us consider an example, the path between A to G.
• Complete: Yes.
• Optimal: Yes, if the path cost is non-decreasing function of depth.
• Time complexity: O(bd)
• Space complexity: O(bd), if the search is in finite space.
DEPTH-FIRST SEARCH
- Depth first search always expands the deepest node in the current frontier of the search
tree.
- It is a recursive algorithm for traversing a tree or graph data structure, it is called so
- Depth-first search uses a LIFO queue. A LIFO queue means that the most recently
generated node is chosen for expansion.
- A variant of depth-first search called backtracking search uses still less memory.
Advantages:
- It requires very less memory as it only needs to store a stack of the nodes on the path
from root node to the current node.
- It takes less time to reach to the goal node than breadth-first search algorithm.
Disadvantages:
- There is the possibility that many states keep re-occurring and there is no guarantee of
finding the solution.
- It goes for deep down searching and sometime it may go to the infinite loop.
Depth-First Search
Advantages:
- It has the benefits of both DFS and BFS.
- It is a complete and optimal algorithm with modest memory requirements, making it
suitable for searching in large or unknown search spaces.
Disadvantages:
- Repeats the all the processes of previous phase.
Consider an example given below showing the iterative deepening depth-first search
from the node A to G. IDDFS algorithm performs various iterations until it does not
find the goal node. The iteration performed by the algorithm is given as:
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.
• Complete: Yes.
• Optimal: Yes, if the step path cost is 1.
• Time complexity: O(bd)
• Space complexity: O(bd).