AI & ML-Chapter 2 (First Half)
AI & ML-Chapter 2 (First Half)
Dr. R.Mohandas
Assistant Professor –Grade-2
School of Computing Sciences and Engineering (SCSE)
VIT-Bhopal
Unit-2 -PROBLEM SOLVING METHODS
Unit Contents
2
PROBLEM SOLVING METHODS
Problem-solving agent
The reflex agents are known as the simplest agents because they directly map states into actions.
Unfortunately, these agents fail to operate in an environment where the mapping is too large to store and
learn. Goal-based agent, on the other hand, considers future actions and the desired outcomes.
Here, we will discuss one type of goal-based agent known as a problem-solving agent, which uses atomic
representation with no internal states visible to the problem-solving algorithms
The problem-solving agent performs precisely by defining problems and its several solutions.
According to psychology, “a problem-solving refers to a state where we wish to reach to a definite goal from a
present state or condition.”
Therefore, a problem-solving agent is a goal-driven agent and focuses on satisfying the goal.
3
Steps performed by Problem-solving agent
• Goal Formulation: It is the first and simplest step in problem-solving. It organizes the
steps/sequence required to formulate one goal out of multiple goals as well as actions to
achieve that goal. Goal formulation is based on the current situation and the agent’s
performance measure (discussed below).
4
Steps performed by Problem-solving agent
• Initial State: It is the starting state or initial step of the agent towards its goal.
• Path cost: It assigns a numeric cost to each path that follows the goal. The problem-
solving agent selects a cost function, which reflects its performance measure.
Remember, an optimal solution has the lowest path cost among all the solutions.
5
Steps performed by Problem-solving agent
• Initial state, actions, and transition model together define the state-space of the
problem implicitly. State-space of a problem is a set of all states which can be reached
from the initial state followed by any sequence of actions. The state-space forms a
directed map or graph where nodes are the states, links between the nodes are actions,
and the path is a sequence of states connected by the sequence of actions.
• Search: It identifies all the best possible sequence of actions to reach the goal state from
the current state. It takes a problem as an input and returns solution as its output.
• Solution: It finds the best algorithm out of various algorithms, which may be proven as
the best optimal solution.
• Execution: It executes the best optimal solution from the searching algorithms to reach
the goal state from the current state. 6
Some Toy Problems to demonstrate Problem-
solving agent
• In the figure, our task is to convert the current state into goal state by sliding digits into the blank space.
7
The problem formulation is as follows:
• States: It describes the location of each numbered tiles and the blank tile.
• Initial State: We can start from any state as the initial state.
• Actions: Here, actions of the blank space is defined, i.e., either left, right, up or down
• Transition Model: It returns the resulting state as per the given state and actions.
• Path cost: The path cost is the number of steps in the path where the cost of each step
is 1.
8
8-queens problem
8-queens problem: The aim of this problem is to place eight queens on a chessboard in
an order where no queen may attack another. A queen can attack other queens
either diagonally or in same row and column.
From the following figure, we can understand the problem as well as its correct solution.
9
Following steps are involved in this
formulation:
• States: Arrangement of any 0 to 8 queens on the chessboard.
• Initial State: An empty chessboard
• Actions: Add a queen to any empty box.
• Transition model: Returns the chessboard with the queen added in a box.
• Goal test: Checks whether 8-queens are placed on the chessboard without any attack.
• Path cost: There is no need for path cost because only final states are counted.
10
Search Strategies
• Search is inherent to the problems and methods of artificial intelligence (AI). That is because AI
problems are intrinsically complex. Efforts to solve problems with computers which humans can
routinely solve by employing innate cognitive abilities, pattern recognition, perception and experience,
invariably must turn to considerations of search. All search methods essentially fall into one of two
categories 1) exhaustive (blind) methods and 2) heuristic or informed methods.
• A problem determines the graph and the goal but not which path to select from the frontier. This is
the job of a search strategy. A search strategy specifies which paths are selected from the frontier.
Different strategies are obtained by modifying how the selection of paths in the frontier is
implemented.
11
Search Algorithm Terminologies:
• Search: Searching is a step by step procedure to solve a search-problem
in a given search space. A search problem can have three main factors:
• Search Space: Search space represents a set of possible solutions, which a
system may have.
• Goal test: It is a function which observe the current state and returns whether
the goal state is achieved or not.
12
Types of search algorithms
Based on the search problems we can classify the search algorithms into uninformed
(Blind search) search and informed search (Heuristic search) algorithms
13
Uninformed Search
As the name ‘Uninformed Search’ means the machine blindly follows the algorithm
regardless of whether right or wrong, efficient or in-efficient. These algorithms are
brute force operations, and they don’t have extra information about the search space;
the only information they have is on how to traverse or visit the nodes in the tree.
Thus uninformed search algorithms are also called blind search algorithms. The search
algorithm produces the search tree without using any domain knowledge, which is a
brute force in nature. They don’t have any background information on how to
approach the goal or whatsoever. But these are the basics of search algorithms in AI.
14
Uninformed Search:- Breadth first search
• Breadth-first search is the most common search strategy for traversing a
tree or graph. This algorithm searches breadthwise in a tree or graph, so
it is called breadth-first search.
• BFS algorithm starts searching from the root node of the tree and
expands all successor node at the current level before moving to nodes of
next level.
• The breadth-first search algorithm is an example of a general-graph
search algorithm.
• Breadth-first search implemented using FIFO queue data structure
15
Uninformed Search:- Breadth first search
In the below tree structure, we have shown the traversing of the tree using BFS algorithm
from the root node S to goal node K. BFS search algorithm traverse in layers, so it will
follow the path which is shown by the dotted arrow, and the traversed path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
16
Uninformed Search:- Breadth first search
Advantages:
• BFS will provide a solution if any solution exists.
• If there are more than one solutions 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.
17
Uninformed Search:- Depth first search
Depth-first search is a recursive algorithm for traversing a tree or graph
data structure.
It is called the depth-first search because it starts from the root node and
follows each path to its greatest depth node before moving to the next
path.
18
Uninformed Search:- Depth first search
Example:
In the below search tree, we have shown the flow of depth-first search, and it will follow the order as:
Root node--->Left node ----> right node.
It will start searching from root node S, and traverse A, then B, then D and E, after traversing E, it will
backtrack the tree as E has no other successor and still goal node is not found. After backtracking it will
traverse node C and then G, and here it will terminate as it found goal node.
19
Uninformed Search:- Depth first search
Advantage:
DFS 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 BFS algorithm (if it traverses in the
right path)
Disadvantage:
• There is the possibility that many states keep re-occurring, and there is no guarantee
of finding the solution.
• DFS algorithm goes for deep down searching and sometime it may go to the infinite
loop.
20
Uniform Cost Search
UCS is different from BFS and DFS because here the costs come into play. In other
words, traversing via different edges might not have the same cost. The goal is to find
a path where the cumulative sum of costs is the least.
Cost of a node is defined as:
cost(node) = cumulative cost of all nodes from root
cost(root) = 0
21
Uniform Cost Search
Example:
Question. Which solution would UCS find to move from node S to node G if run on the
graph below?
22
Uniform Cost Search
Solution. The equivalent search tree for the above graph is as follows. The cost of each node is the
cumulative cost of reaching that node from the root. Based on the UCS strategy, the path with the least
cumulative cost is chosen. Note that due to the many options in the fringe, the algorithm explores most
of them so long as their cost is low, and discards them when a lower-cost path is found; these discarded
traversals are not shown below. The actual traversal is shown in blue.
23
Informed Search
Best-first Search Algorithm (Greedy Search):
Greedy best-first search algorithm always selects the path which appears best at that
moment. It is the combination of depth-first search and breadth-first search
algorithms. It uses the heuristic function and search. Best-first search allows us to take
the advantages of both algorithms. With the help of best-first search, at each step, we
can choose the most promising node. In the best first search algorithm, we expand the
node which is closest to the goal node and the closest cost is estimated by heuristic
function, i.e.
f(n)= g(n).
Were, h(n)= estimated cost from node n to the goal
The greedy best first algorithm is implemented by the priority queue.
24
Informed Search -Best-first Search Algorithm (Greedy Search):
25
Informed Search -Best-first Search Algorithm (Greedy Search):
Advantages:
Best first search can switch between BFS and DFS by gaining the advantages of both the algorithms.
This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
It can behave as an unguided depth-first search in the worst case scenario.
It can get stuck in a loop as DFS.
This algorithm is not optimal.
26
Informed Search -Best-first Search Algorithm (Greedy Search):
Example:
Consider the below search problem, and we will traverse it using greedy best-first search. At each
iteration, each node is expanded using evaluation function f(n)=h(n) , which is given in the below table
13 Node H(n)
S
S 13
12 A B 4 A 12
B 4
7 C C 7
3 D E 8 F 2
D 3
E 8
4 H 9 I G 0 F 2
H 4
I 9
G 0
27
Informed Search -Best-first Search Algorithm (Greedy Search):
In this search example, we are using two lists which are OPEN and CLOSED Lists. Following are the
iteration for traversing the above example.
• Expand the nodes of S and put in the CLOSED list
• Initialization: Open [A, B], Closed [S]
• Iteration 1: Open [A], Closed [S, B]
• Iteration 2: Open [E, F, A], Closed [S, B]
Open [E, A], Closed [S, B, F]
Iteration 3: Open [I, G, E, A], Closed [S, B, F]
: Open [I, E, A], Closed [S, B, F, G]
28
Informed Search -A* Search Algorithm:
A* search is the most commonly known form of best-first search. It uses heuristic function h(n), and
cost to reach the node n from the start state g(n). It has combined features of UCS and greedy best-first
search, by which it solve the problem efficiently. A* search algorithm finds the shortest path through
the search space using the heuristic function. This search algorithm expands less search tree and
provides optimal result faster. A* algorithm is similar to UCS except that it uses g(n)+h(n) instead of
g(n).
In A* search algorithm, we use search heuristic as well as the cost to reach the node. Hence we can
combine both costs as following, and this sum is called as a fitness number.
29
Algorithm of A* search:
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation function (g+h), if
node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For each
successor n', check whether n' is already in the OPEN or CLOSED list, if not then compute evaluation
function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the back pointer
which reflects the lowest g(n') value.
Step 6: Return to Step 2.
30
Informed Search -A* Search Algorithm:
Advantages:
A* search algorithm is the best algorithm than other search algorithms.
A* search algorithm is optimal and complete.
This algorithm can solve very complex problems.
Disadvantages:
It does not always produce the shortest path as it mostly based on heuristics and
approximation.
A* search algorithm has some complexity issues.
The main drawback of A* is memory requirement as it keeps all generated nodes in
the memory, so it is not practical for various large-scale problems.
31
Informed Search -A* Search Algorithm:
Example:
In this example, we will traverse the given graph using the A* algorithm. The heuristic value of all states
is given in the below table so we will calculate the f(n) of each state using the formula f(n)= g(n) + h(n),
where g(n) is the cost to reach any node from start state.
Here we will use OPEN and CLOSED list.
32
Informed Search -A* Search Algorithm:
Solution
33
Heuristics
A heuristic, or a heuristic technique, is any approach to problem-solving that uses a practical method or
various shortcuts in order to produce solutions that may not be optimal but are sufficient given a
limited timeframe or deadline
The uninformed search algorithms which looked through search space for all possible solutions of the
problem without having any additional knowledge about search space. But informed search algorithm
contains an array of knowledge such as how far we are from the goal, path cost, how to reach to goal
node, etc. This knowledge help agents to explore less to the search space and find more efficiently the
goal node.
The informed search algorithm is more useful for large search space. Informed search algorithm uses
the idea of heuristic, so it is also called Heuristic search
34
Heuristics
35
Heuristics
36