You are on page 1of 41

Artificial

Intelligence
ASSISTANT PROFESSOR. KANCHAN TAKSALE
Problem Solving by
Searching
UNIT II
Unit II

Solving Problems by Searching: Problem solving


agents, examples problems, searching for solutions,
uninformed search, informed search strategies,
heuristic functions.

Beyond Classical Search: local search


algorithms, searching with non-deterministic action,
searching with partial observations, online search
agents and unknown environments
Introduction
• Search is an indivisible part of intelligence. An intelligent agent is
the one who can search and select the most appropriate action in
the given situation, among the available set of actions.
• When we play any game like chess, cards, tic-tac-toe, etc.; we know
that we have multiple options for next move, but the intelligent one
who searches for the correct move will definitely win the game.
• In case of travelling salesman problem, medical diagnosis system or
any expert system; all they required to do is to carry out search
which will produce the optimal path, the shortest path with minimum
cost and efforts.
• Hence, this chapter focuses on the searching techniques used in Al
applications. Those are known as un-informed and informed search
techniques.
Problem Solving Agents
• Now let us see how searching play a vital role in solving Al
problems.
• Given a problem, we can generate all the possible states it
can have in real time, including start state and end state.
• To generate solution for the same is nothing but searching
a path from start state to end state.

Asst Prof. Kanchan A. Taksale


• Problem solving agent is the one who finds the
goal state from start state in optimal way by
following the shortest path, thereby saving the
memory and time.

• It's supposed to maximize its performance by


fulfilling all the performance measures.

• Searching techniques can be used in game playing


like Tic-Tac-Toe or navigation problems like
Travelling Salesman Problem.

• First, we will understand the representation of


given problem so that appropriate searching
techniques can be applied to solve the problem.
Asst Prof. Kanchan A. Taksale
Examples on Problems Searching for Solution

• Early work in the field of Al focused on formal tasks, like game


playing, proving theorems, etc.
• Games like chess, checkers received good deal of attention,
because in case of machine opponents, system was using the
experience gained in previous games to improve the moves in next
game.

Asst Prof. Kanchan A. Taksale


• General Problem Solving (GPS) was developed for common
sense reasoning problems.
• GPS used less amount of knowledge for performing simple
tasks.
• When the knowledge base was scaled up for the problems
which needed perception through vision and/or speech,
natural language understanding, problem solving for
medical diagnosis, chemical analysis, etc. it was difficult
because it involved analog signals, which are generally
very noisy compared to digital signals.
• Following are some of the famous search problems in
artificial intelligence.

Asst Prof. Kanchan A. Taksale


Classic Artificial Intelligence Search Problems

3*3*3 Rubik's cube problem

8-Puzzle

N-queen problem

Missionaries and cannibals problem

The river problem

~ Famous Search Problems in Artificial Intelligence

Asst Prof. Kanchan A. Taksale


3*3*3 Rubik's cube problem

Do I need to explain the problem??

• In Rubik’s cube we have a cube


with six Color faces.
• The goal is to arrange the Cuboids
in a such a way that each face of
cube Will show distinct color.

Asst Prof. Kanchan A. Taksale


8-Puzzle

• In 8-puzzle there are 8 tiles


need to be arranged in a way
showed in the goal state.
• The condition is only the blank
tile can be moved to immediate
up down, right or left positions
and the goal state is to be
attained in minimum number of
moves.

Asst Prof. Kanchan A. Taksale


N-queen problem

In-queen, the queens need to be placed on the n*n


board, in such a way that no queen can dash the other
queen, horizontally, vertically or diagonally.

Asst Prof. Kanchan A. Taksale


Missionaries and cannibals problem
• In this problem, there are three
missionaries and three cannibals on the
same side of a river.
• We need to get all of them to the other
side of river through a canoe which can
hold maximum two people at a time.
• The condition is no time during the
process of shifting, number of cannibals
on any of the side should be greater than
the number of missionaries on the same
side.

https://www.youtube.com/watch?v=-ugN9bh1EU0
Asst Prof. Kanchan A. Taksale
The river problem
• There is a farmer who wishes to cross a
river but he is not alone. He also has a
goat, a wolf, and a cabbage along with him.
• There is only one boat available which can
support the farmer and either of the goat,
wolf or the cabbage.
• So at a time, the boat can have only two
objects (farmer and one other).
But the problem is, if the goat and wolf
are left alone (either in the boat or
onshore), the wolf will eat the goat.
• Similarly, if the Goat and cabbage are left
alone, then goat will eat the cabbage.
• The farmer wants to cross the river with
all three of his belongings: goat, wolf, and
cabbage. https://www.youtube.com/watch?v=WAWAQQuyWCc
What strategy he should use to do so?

Asst Prof. Kanchan A. Taksale


Formulating Problems
Given a goal to achieve; problem formulation is the process of deciding what
states to be considered and what actions to be taken to achieve the goal.
This is the first step to be taken by any problem solving agent.

State space: The state space of a problem is the set of all states reachable
from the initial state by executing any sequence of actions. State is
representation of all possible outcomes.
The state space specifies the relation among various problem states thereby,
forming a directed network or graph in which the nodes are states and the links
between nodes represent actions.

State Space Search: Searching in a given space of states pertaining to a


problem under consideration is called a state space search.

Path: A path is a sequence of states connected by a sequence of actions, in a


given state space. Asst Prof. Kanchan A. Taksale
Components of Problems Formulation

Initial State

Actions

Successor Function

Goal test

Path Cost

Asst Prof. Kanchan A. Taksale


Problem can be defined formally using five components as follows:

1. Initial state: The initial state is the one in which the agent starts in.

2. Actions: It is the set of actions that can be executed or applicable in all possible states. A
description of what each action does; the formal name for this is the transition model.

3. Successor function: It is a function that returns a state on executing an action on the


current state.

4. Goal test: It is a test to determine whether the current state is a goal state.
In some problems the goal test can be carried out just by comparing current state with the
defined goal state, called as explicit goal test.
Whereas, in some of the problems, state cannot be defined explicitly but needs to be generated
by carrying out some computations, it is called as implicit goal test.

For example: In Tic-Tac-Toe game making diagonal or vertical or horizontal combination declares
the winning state which can be compared explicitly; but in the case of chess game, the goal state
cannot be predefined but it's a scenario called as "Checkmate", which has to be evaluated
implicitly.
Asst Prof. Kanchan A. Taksale
5.Path cost :
• It is simply the cost associated with each step to be taken to reach to the goal state.
• To determine the cost to reach to each state, there is a cost function, which is chosen by the
problem solving agent.

 Problem solution:
• A well-defined problem with specification of initial state, goal test, successor function, and
path cost.
• It can be represented as a data structure and used to implement a program which can search
for the goal state.
• A solution to a problem is a sequence of actions chosen by the problem solving agent that leads
from the initial state to a goal state.
• Solution quality is measured by the path cost function.

 Optimal solution:
• An optimal solution is the solution with least path cost among all solutions.
• A general sequence followed by a simple problem solving agent is, first it formulates the
problem with the goal to be achieved, then it searches for a sequence of actions that would
solve the problem, and then executes the actions one at a time.
Asst Prof. Kanchan A. Taksale
• Informed Search • Uninformed Search

• It uses knowledge for the searching • It doesn’t use knowledge for searching
process. process.

• It finds solution slow as compared to


• It finds solution more quickly.
informed search.

• It may or may not be complete. • It is always complete.

• Cost is low. • Cost is high.

• It consumes less time. • It consumes moderate time.

• It provides the direction regarding the • No suggestion is given regarding the


solution. solution in it.

• It is less lengthy while implementation. • It is more lengthy while implementation.

• Greedy Search, A* Search, Graph Search • Depth First Search, Breadth First Search
Asst Prof. Kanchan A. Taksale
• An uninformed search is a searching technique that
has no additional information about the distance
from the current state to the goal.

• Informed Search is another technique that has


additional information about the estimate distance
from the current state to the goal.

• Also, the informed search strategy is more


efficient than the uninformed search strategy when
we have information about the goal node but if the
information is not present then the uninformed
search strategy has to be applied.
Asst Prof. Kanchan A. Taksale
• The Uninformed Search (or Blind Search) as the name
suggests is searching without “information” about the
goal node.
• For example, in Breadth-First Search (BFS) the nodes at
the same level are traversed first and then it moves to
the next successive level and it does not stop until it has
found the node with values that it was searching for.
• There is no requirement of information for selecting the
nodes to traverse. You can also think of an uninformed
search strategy as a brute-force search strategy.

Asst Prof. Kanchan A. Taksale


• The Informed Search (or Heuristic Search) is searching
with “information” about the goal node.
• For example, in the A* search algorithm, first, we gather
information about the goal node such as its location in
cartesian coordinate form and then we write a function
that can guide our node traversal, for example, finding
its Euclidean distance, also this function is called
Heuristic Function.
• The heuristic function will guide our node traversal by
stating the distance left from each node to the goal
node. You can also think of Informed Search Strategy as
a Guided Search Strategy.

Asst Prof. Kanchan A. Taksale


• Informed and Uninformed Search strategies are the
heart and soul of Artificial Intelligence. In real life, the
informed and the uninformed searching can look in this
way.
• The uninformed searching occurs when you are looking
from one website to another or flipping through the
magazine books without really any need for finding the
information.
• But the informed searching is the opposite in nature, for
example, you are at work or you are studying something
and there comes a term that you do not understand at
that time you will not be browsing the internet for some
random information but specifically you want to find the
particular term and that is the informed searching.
Asst Prof. Kanchan A. Taksale
Example of Uninformed Search in AI are:

1.Breadth-First Search
2.Depth First Search
3.Bidirectional search
4.Uniform Cost Search

Examples of Informed Search in AI are:

1.A* Algorithm
2.Best First Search
3.AO* Algorithm
4.Constraints satisfaction
5.Hill Climbing algorithm
6.beam search
Asst Prof. Kanchan A. Taksale
Before we go into these search types let’s get to know the few terms
which will be frequently used in the upcoming sections.
* State: It provides all the information about the environment.
* Goal State: The desired resulting condition in a given problem and
the kind of search algorithm we are looking for.
* Goal Test: The test to determine whether a particular state is a
goal state.
* Path/Step Cost: These are integers that represent the cost to
move from one node to another node.
* Space Complexity: A function describing the amount of
space(memory) an algorithm takes in terms of input to the algorithm.
* Time Complexity: A function describing the amount of time the
algorithm takes in terms of input to the algorithm.
* Optimal: Extent of preference of the algorithm
* ‘b‘ – maximum branching factor in a tree.
* ‘d‘ – the depth of the least-cost solution.
* ‘m‘ – maximum depth state space(maybe infinity)
Asst Prof. Kanchan A. Taksale
1. Depth First Search (DFS):
• It is a search algorithm where the search tree will be
traversed from the root node.
• It will be traversing, searching for a key at the leaf of a
particular branch.
• If the key is not found the searching retraces its steps
back to the point from where the other branch was left
unexplored and the same procedure is repeated for that
other branch.
• First, the searching starts in the root node A and then
goes to the branch where node B is present
(lexicographical order).

• Then it goes do node D because of DFS and from D there is the only node to traverse i.e. node H.
• But after node H does not have any child nodes so we retrace the path in which we traversed earlier and
again reach node B but this time we traverse through in the untraced path a traverse through node E.
• There are two branches at node E but let’s traverse node I (lexicographical order) and then retrace the
path as we have no further nodes after E to traverse.
• Then we traverse node J as it is the untraced branch and then again find we are at the end and retrace
the path and reach node B and then we will traverse the untraced branch i.e. through node C and repeat
the same process. This is called the DFS Algorithm.
Asst Prof. Kanchan A. Taksale
Advantages:

•DFS requires very little memory as it only needs to store a stack of the nodes on
the path from the root node to the current node.
•It takes less time to reach the goal node than the BFS algorithm [which is
explained later](if it traverses in the right path).

Disadvantages:
•There is the possibility that many states keep reoccurring, and there is no
guarantee of finding the solution.
•The DFS algorithm goes for deep down searching and sometimes it may go to the
infinite loop.

Conclusions:
It occupies a lot of memory space, and time to execute when the solution is at the
bottom or end of the tree and is implemented using LIFO Stack data structure[DS].
Complete: No
Time Complexity: O(bm)
Space complexity: O(bm)
Optimal: Yes

Asst Prof. Kanchan A. Taksale


2. Breadth-First Search(BFS)
• It is another search algorithm in AI which
traverses breadthwise to search the goal in a tree.
• It begins searching from the root node and
expands the successor node before going
expanding it further expands along breadthwise
and traverses those nodes rather than searching
depth-wise.

• It starts from the root node A and then traverses node B.


• Till this step it is the same as DFS.
• But here instead of expanding the children of B as in the case of DFS we expand
the other child of A i.e. node C because of BFS and then move to the next level and
traverse from D to G and then from H to K in this typical example.
• To traverse here we have only taken into consideration the lexicographical order.
• This is how the BFS Algorithm is implemented.
Asst Prof. Kanchan A. Taksale
Advantages:

•BFS will provide a solution if any solution exists.


•If there is more than one solution for a given problem, then BFS will provide the 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.
•BFS needs lots of time if the solution is far away from the root node.

Conclusions:

It requires a lot of memory space and time consuming if the goal state is at the bottom or end. It
uses a FIFO queue DS to implement.
Complete: Yes (assuming b is finite)
Time Complexity: O(bd)
Space complexity: O(bd)
Optimal: Yes, if Step cost = 1 (i.e. no cost/all step costs are same)

Asst Prof. Kanchan A. Taksale


3. Uniform Cost Search(UCS):

• This algorithm is mainly used when the step costs are not
the same but we need the optimal solution to the goal
state.
• In such cases, we use Uniform Cost Search to find the goal
and the path including the cumulative cost to expand each
node from the root node to the goal node.
• It does not go depth or breadth, it searches for the next
node with the lowest cost and in the case of the same path
cost, let’s consider lexicographical order in our case.
• In the above figure consider S to be the start node and G to be the goal state.
• From node S we look for a node to expand and we have nodes A and G but since it’s a uniform cost
search it’s expanding the node with the lowest step cost so node A becomes the successor rather than
our required goal node G.
• From A we look at its children nodes B and C. So since C has the lowest step cost it traverses through
node C and then we look at successors of C i.e. D and G since the cost to D is low we expand along with
the node D.
• Since D has only one child G which is our required goal state we finally reach the goal state D by
implementing UFS Algorithm.
• If we have traversed this way definitely our total path cost from S to G is just 6 even after traversing
through many nodes rather than going to G directly where the cost is 12 and 6<<12(in terms of step
cost). But this may not work with all cases. (https://www.youtube.com/watch?v=wIEfTDu5gII)
Asst Prof. Kanchan A. Taksale
Advantages:
•Uniform cost search is optimal because at every state the path with
the least cost is chosen.

Disadvantages:
•It does not care about the number of steps involved in searching and
only concerned about path cost. Due to which this algorithm may be
stuck in an infinite loop.

Conclusions:
Complete: Yes (if b is finite and costs are stepped costs are zero)
Time Complexity: O(b(c/ϵ)) where, ϵ -> is the lowest cost, c ->
optimal cost
Space complexity: O(b(c/ϵ))
Optimal: Yes (even for non-even cost)

Asst Prof. Kanchan A. Taksale


4. Depth Limited Search(DLS):

• DLS is an uninformed search algorithm.


• This is similar to DFS but differs only in a few ways.
• The sad failure of DFS is alleviated by supplying a depth-first
search with a predetermined depth limit.
• That is, nodes at depth are treated as if they have no successors.
• This approach is called a depth-limited search.
• The depth limit solves the infinite-path problem.
Depth-limited search can be halted in two
cases:
— Standard Failure Value(SFV): The SFV
tells that there is no solution to the
problem.
— Cutoff Failure Value(CFV): The Cutoff
Failure Value tells that there is no
solution within the given depth-limit.
Asst Prof. Kanchan A. Taksale
• The above figure illustrates the implementation of the DLS algorithm.
• Node A is at Limit = 0, followed by nodes B, C, D, and E at Limit = 1 and nodes F, G, and H at Limit = 2.
• Our start state is considered to be node A and our goal state is node H.
• To reach node H we apply DLS.
• So in the first case let’s, set our limit to 0 and search for the goal.
• Since limit 0, the algorithm will assume that there are no children after limit 0 even if nodes exist
further.
• Now, if we implement it we will traverse only node A as there is only one node in limit 0, which is basically
our goal state.
• If we use SFV, it says that there is no solution to the problem at limit 0 whereas LCV says that there is
no solution for the problem until the set depth limit. Now since we were not able to find the goal we
increase our limit to 1 and apply DFS till limit 1 even though there are further nodes after limit 1.
• But those nodes aren’t expanded as we have set our limit as 1.
• Hence nodes A followed by B, C, D, and E are expanded as the mentioned order.
• As in our first case, if we use SFV, it says that there is no solution to the problem at limit 1 whereas LCV
says that there is no solution for the problem until the set depth limit 1. Hence we again increase our limit
from 1 to 2 in the notion to find the goal.
• Till limit 2 DFS will be implemented from our start node A and its children B, C, D, and E and then from E
it moves to F similarly backtracks the path and explores the unexplored branch where node G is present
and then retraces the path and explores the child of C i.e. node H and then we finally we reach our goal by
applying DLS Algorithm. Suppose we have further successors of node F but only the nodes till the limit 2
will be explored as we have limited the depth and have reached the goal state.

Asst Prof. Kanchan A. Taksale


• This image explains the DLS implementation and could
be referred to for better understanding.
• Depth-limited search can be terminated with two
Conditions of failure:
• Standard Failure: it indicates that the problem does not
have any solutions.
• Cutoff Failure Value: It defines no solution for the
problem within a given depth limit.

Advantages:
Depth-limited search is Memory efficient.

Disadvantages:
The DLS has disadvantages of completeness and is not
optimal if it has more than one goal state.

Conclusions:
Complete: Complete (if solution > depth-limit) s.a.c.e.f.d.g.b.i.h.j
Time Complexity: O(bl) where, l -> depth-limit
sacdbij
Space complexity: O(bl)
Optimal: Yes (only if l > d)
Asst Prof. Kanchan A. Taksale
• The heuristic function is a way to inform the search
about the direction to a goal. It provides an informed way
to guess which neighbor of a node will lead to a goal.
• There is nothing magical about a heuristic function.
• It must use only information that can be readily obtained
about a node.

Asst Prof. Kanchan A. Taksale


List of Algorithms

Asst Prof. Kanchan A. Taksale


A* Algorithm

https://www.youtube.com/watch?v=vP5TkF0xJgI Asst Prof. Kanchan A. Taksale


Genetic Algorithm- Imp Points

Asst Prof. Kanchan A. Taksale


Example:

Asst Prof. Kanchan A. Taksale


Block Problem

Asst Prof. Kanchan A. Taksale


8 Puzzle Problem

Asst Prof. Kanchan A. Taksale

You might also like