Unit 2

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 69

UNIT II

PROBLEM SOLVING METHODS

PROBLEM-SOLVING METHODS IN ARTIFICIAL INTELLIGENCE


Because they immediately transfer states to actions, reflex agents are
characterised as the simplest agents. Unfortunately, these agents are unable to
function in situations where the mapping is too huge to store and learn. On the
other hand, a goal-based agent considers future behaviours as well as the
desired outcomes.
Problem-solving agent
By defining problems and their various solutions, the problem-solving agent
performs exactly.
"A problem-solving state refers to a state when we aim to accomplish a
defined objective from a current state or condition," according to psychology.
According to computer science, problem-solving is a subset of artificial
intelligence that includes a variety of ways to solve a problem, such as
algorithms and heuristics.
As a result, a problem-solving agent is a goal-driven agent who is solely
concerned with achieving the goal.
Steps performed by Problem-solving agent
Goal Formulation: It is the first and most basic stage in fixing a problem. It
organises the steps/sequence needed to construct a single goal from many
goals, as well as the actions needed to achieve that goal. The agent's
performance measure and the current condition are used to formulate goals
(discussed below).
Problem Formulation: It is the most crucial phase in problem-solving since it
determines which actions should be followed to attain the stated goal. In the
formulation of a problem, there are five elements to consider:
Initial State: It is the agent's starting state or first step toward its aim.
Actions: It is a list of the various options available to the agent.
Transition Model: It explains what each action accomplishes.
Goal Test: It decides whether or not the current state is a goal state.
Path cost: Each path that leads to the goal is given a numerical cost. A cost
function is chosen by the problem-solving agent to reflect its performance
measure. Remember that the optimal option has the cheapest path cost of all
the alternatives.
Search: It determines the best potential sequence of actions to get from the
current condition to the goal state. It receives an issue as input and returns a
solution as output.
Solution: It selects the best algorithm from a set of algorithms that can be
demonstrated to be the most optimal solution.
Execution: It uses the best optimal solution found by the searching algorithms
to get from the present state to the goal state.
Example Problems
In general, there are two sorts of problem-solving strategies:
Researchers use the toy problem to compare the performance of algorithms
because it offers a brief and exact explanation of the problem.
Actual-world Problems: These are problems that need to be solved in the real
world. It does not require descriptions, unlike a toy problem, yet we can have
a generic formulation of the problem.
Some Toy Problems
8 Puzzle Problem: A 3x3 matrix with movable tiles numbered 1 to 8 and a
blank area is shown. The tile to the left of the vacant spot can be slid into it.
The goal is to achieve a goal state that is similar to the one indicated in the
diagram below.
Our goal in the diagram below is to slide digits into the vacant space to change
the current state to the goal state.
By sliding digits into the vacant space in the above diagram, we can change
the current (Start) state to the desired state.
The problem formulation is as follows:
States: It shows where each numbered tile and the blank tile are located.
Initial State: We Any state can be used as the starting point.
Actions: The blank space's actions are defined here, i.e., left, right, up, or
down.
Transition Model: It returns the final state, which is determined by the
provided state and actions.
Goal test: It determines if we have arrived at the correct goal-state.
Path cost: The path cost is the number of steps in a path where each step costs
one dollar. The 8-puzzle problem is a form of sliding-block problem that is
used to evaluate new artificial intelligence search engines.
8-queens problem: The goal of this issue is to arrange eight queens on a
chessboard in such a way that none of them can attack another queen. A queen
can attack other queens in the same row and column or diagonally.
We can grasp the problem and its correct solution by looking at the diagram
below.
As can be seen in the diagram above, each queen is put on the chessboard in
such a way that no other queen is placed diagonally, in the same row or
column. As a result, it is one viable solution to the eight-queens dilemma.
There are two primary types of formulations for this problem:
Incremental formulation: It begins with an empty state and progresses in
steps, with the operator augmenting a queen at each step.
Following steps are involved in this formulation:
States: On the chessboard, arrange any number of queens from 0 to 8.
Initial State: A chessboard with no pieces
Actions: Fill any empty box with a queen.
Transition model: The chessboard is returned with the queen in a box.
Goal test: Checks if eight queens can be positioned on the chessboard without
being attacked.
Path cost: Because only final states are counted, there is no requirement for
path cost.
There are approximately 1.8 x 1014 potential sequences to analyse in this
formulation.
Complete-state formulation: It begins with all eight queens on the
chessboard and moves them around the board, avoiding attacks.
Following steps are involved in this formulation
States: Each of the eight queens is arranged in a column, with no queen
assaulting the other.
Actions: Move the queen to a secure spot away from the attackers.
This formulation is superior to the incremental formulation since it shrinks the
state space from 1.8 x 1014 to 2057 and makes finding solutions much easier.
Some Real-world problems
Traveling salesperson problem(TSP): It's a problem of touring, because the
salesman can only visit each city once. The goal is to discover the shortest tour
and sell out all of the merchandise in each place.
VLSI Layout problem: Millions of components and connections are placed
on a chip in order to reduce area, circuit delays, and stray capacitances while
increasing manufacturing yield.
The layout problem is split into two parts:
Cell layout: The circuit's primitive components are arranged into cells, each
of which performs a distinct purpose. Each cell is the same size and shape.
The goal is to arrange the cells on the chip so that they do not overlap.
Channel routing: It determines a unique path through the spaces between the
cells for each wire.
Protein Design: The goal is to develop an amino acid sequence that will fold
into a 3D protein with the ability to treat an illness.
Searching for solutions
We've seen a lot of issues. There is now a need to look for solutions to these
problems.
In this section, we'll look at how the agent can use searching to solve an issue.
For solving different kinds of problem, an agent makes use of different
strategies to reach the goal by searching the best possible algorithms. This
process of searching is known as search strategy.
Measuring problem-solving performance
Before delving into other search tactics, it's important to assess an algorithm's
performance. As a result, there are four ways to evaluate an algorithm's
performance:
Completeness: It assesses if the algorithm is certain to discover a solution (if
any solution exist).
Optimality: It determines whether the approach seeks out the best answer.
Time Complexity: The amount of time it takes for an algorithm to find a
solution.
Space Complexity: The amount of memory needed to conduct a search.
The branching factor, or maximum number of successors, the depth of the
shallowest goal node (i.e., the number of steps from the root to the path), and
the maximum length of any path in a state space all influence the algorithm's
complexity.

Search Algorithms in Artificial Intelligence


Problem-solving agents
Search techniques are universal problem-solving methods in Artificial
Intelligence. These search strategies or algorithms were generally employed
by rational agents or problem-solving agents in AI to solve a given problem
and provide the best outcome. Goal-based agents that use atomic
representation are problem-solving agents. We will learn a variety of problem-
solving search methods in this area.
Search Algorithm Terminologies
Search: A step-by-step procedure for solving a search problem in a given
search space is known as searching. There are three main factors that can
contribute to a search problem:
Search Space: A search space is a collection of possible solutions that a
system could have.
Start State: It's the starting point for the agent's quest.
Goal test: It's a function that looks at the current state and returns whether or
not the goal state has been reached.
Search tree: Search tree is a tree representation of a search problem. The root
node, which corresponds to the initial state, is at the top of the search tree.
Actions: It provides the agent with a list of all available actions.
Transition model: A transition model is a description of what each action does.
Path Cost: It's a function that gives each path a numerical cost.
Solution: It is an action sequence that connects the start and end nodes.
Optimal Solution: If a solution is the cheapest of all the options.
Properties of Search Algorithms
The four most important properties of search algorithms to compare their
efficiency are as follows:
Completeness: If a search method guarantees to return a solution if at least one
solution exists for any random input, it is said to be complete.
Optimality: When a solution for an algorithm is guaranteed to be the best
solution (lowest route cost) among all alternative solutions, it is referred to as
an optimum solution.
Time Complexity: The time complexity of an algorithm is a measure of how
long it takes for it to perform its goal.
Space Complexity: It is the maximum amount of storage space necessary at
any point throughout the search due to the problem's complexity.
Types of search algorithms
We can divide search algorithms into uninformed (Blind search) and informed
(Heuristic search) algorithms based on the search problems.
Uninformed/Blind Search
The uninformed search has no domain knowledge, such as proximity or the
goal's location. It works in a brute-force manner since it simply contains
instructions on how to traverse the tree and locate leaf and goal nodes.
Uninformed search is sometimes known as blind search since it searches a
search tree without any knowledge of the search space, such as initial state
operators and goal tests. It goes through the tree, node by node, until it reaches
the destination node.
It is broken into five categories:
Breadth-first search
Uniform cost search
Depth-first search
Iterative deepening depth-first search
Bidirectional Search
Informed Search
Domain knowledge is used by informed search algorithms. Problem
information is available in an educated search, which can help steer the search.
Informed search tactics are more likely to find a solution than uninformed
search strategies. Heuristic search is another name for informed search.
A heuristic is a method for finding a good answer in a fair amount of time,
even if the optimal solution isn't always guaranteed.
Informed search can solve a lot of complicated problems that can't be solved
any other way.
A travelling salesman issue is an example of an informed search algorithm.
Greedy Search
A* Search
Uninformed Search Algorithms
Uninformed search is a type of general-purpose search method that uses brute
force to find results. Uninformed search algorithms have no other information
about the state or search space except how to traverse the tree, which is why
it's also known as blind search.
The many forms of ignorant search algorithms are as follows:
Breadth-first Search
Depth-first Search
Depth-limited Search
Iterative deepening depth-first search
Uniform cost search
Bidirectional Search
Breadth-first Search
The most frequent search approach for traversing a tree or graph is breadth-
first search. Breadth-first search is the name given to an algorithm that
searches a tree or graph in a breadth-first manner.
Before going on to nodes of the next level, the BFS algorithm starts searching
from the tree's root node and extends all successor nodes at the current level.
A general-graph search algorithm like the breadth-first search algorithm is an
example.
A FIFO queue data structure was used to implement a breadth-first search.
Advantages
If a solution is available, BFS will provide it.
If there are multiple answers to a problem, BFS will present the simplest
solution with the fewest steps.
Disadvantages
It necessitates a large amount of memory since each level of the tree must be
saved into memory before moving on to the next.
If the solution is located far from the root node, BFS will take a long time.
Example
We've showed how to traverse the tree using the BFS method from the root
node S to the destination node K in the diagram below. Because the BFS
search algorithm traverses in layers, it will follow the dotted arrow's path, and
the travelled path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K 

Time Complexity: The number of nodes traversed in BFS till the shallowest


Node can be used to determine the algorithm's time complexity. Where d
represents the depth of the shallowest solution and b represents a node at each
state.
T (b) = 1+b2+b3+.......+ bd= O (bd)
Space Complexity: The space complexity of the BFS algorithm is determined
by Memory size of frontier which is O(bd).
Completeness: BFS is complete, which implies it will discover a solution if
the shallowest target node is at a finite depth.
Optimality: If the path cost is a non-decreasing function of the node depth,
BFS is the best option.
Depth-first Search
A recursive approach for traversing a tree or graph data structure is depth-first
search.
The depth-first search is named after the fact that it begins at the root node and
follows each path to its greatest depth node before moving on to the next path.
DFS is implemented using a stack data structure.
The DFS algorithm works in a similar way as the BFS method.
Advantages
Because DFS only needs to store a stack of nodes on the path from the root
node to the current node, it uses extremely little memory.
The BFS method takes longer to reach the goal node (if it traverses in the right
path).
Disadvantages
There's a chance that many states will recur, and there's no certainty that a
solution will be found.
The DFS algorithm performs deep searching and may occasionally enter an
infinite cycle.
Example
The flow of depth-first search is depicted in the search tree below, and it will
proceed in the following order:
root node—>left node ----> right node.
It will begin its search from root node S and traverse A, B, D, and E; after
traversing E, it will backtrack the tree because E has no successors and the
goal node has yet to be discovered. It will retreat to node C and then to node
G, where it will end because it has reached the goal node.
Completeness: Because it expands every node within a constrained search
tree, the DFS search method is complete within finite state space.
Time Complexity: DFS's time complexity will be proportional to the number
of nodes traversed by the algorithm. It is provided by:
T(n)= 1+ n2+ n3 +.........+ nm=O(nm)
Where m is the greatest depth of any node, which can be significantly greater
than d. (Shallowest solution depth)
Space Complexity: Because the DFS method only has to store one path from
the root node, its space complexity is the same as the size of the fringe set,
which is O (bm).
Optimal: The DFS search algorithm is inefficient since it can result in a large
number of steps or a high cost to reach the goal node.
Depth-Limited Search Algorithm
A depth-limited search algorithm works similarly to a depth-first search but
with a limit. The problem of the endless path in the Depth-first search can be
overcome by depth-limited search. The node at the depth limit will be treated
as if it has no additional successor nodes in this procedure.
Two conditions of failure can be used to end a depth-limited search:
Standard failure value: It denotes the absence of a solution to the situation.
Cutoff failure value: Within a specified depth limit, it defines no solution to
the problem.
Advantage
Memory is saved by using a depth-limited search.
Disadvantage
Incompleteness is another drawback of depth-limited search.
If there are multiple solutions to an issue, it may not be the best option.
Example:

Completeness: If the solution is above the depth-limit, the DLS search


procedure is complete.
Time Complexity: Time complexity for DLS algorithm is O(bℓ).
Space Complexity: Space complexity for DLS algorithm is O(b×ℓ).
Optimal: Depth-limited search can be noted as a special case of DFS, and it is
also not optimal even if ℓ>d.
Uniform-cost Search Algorithm
A searching algorithm for traversing a weighted tree or graph is uniform-cost
search. When a separate cost is provided for each edge, this algorithm is used.
The uniform-cost search's main purpose is to discover the shortest path to the
goal node with the lowest cumulative cost. Uniform-cost search grows nodes
from the root node based on their path costs. It can be used to solve any graph
or tree in which the best cost is required. The priority queue employs a
uniform-cost search algorithm. It gives the lowest total cost the highest
priority. If the path cost of all edges is the same, uniform cost search is
comparable to BFS algorithm.
Advantage
Because the path with the lowest cost is chosen at each state, uniform cost
search is the best option.
Disadvantage
It is unconcerned with the number of steps involved in the search and is just
concerned with the expense of the path. As a result, this algorithm may
become stuck in an endless cycle.
Example

Completeness:
The uniform-cost search is complete, so if a solution exists, UCS will discover
it.
Time Complexity:
Let C* is Cost of the optimal solution, and ε is each step to get closer to the
goal node. Then the number of steps is = C*/ε+1. Here we have taken +1, as
we start from state 0 and end to C*/ε.
Hence, the worst-case time complexity of Uniform-cost search isO(b1 + [C*/ε])/.
Space Complexity:
The same logic is for space complexity so, the worst-case space complexity of
Uniform-cost search is O(b1 + [C*/ε]).
Optimal:
Uniform-cost search is always the best option because it only chooses the
cheapest path.
Iterative deepening depth-first Search
The iterative deepening algorithm is a blend of DFS and BFS algorithms. This
search technique determines the appropriate depth limit by gradually raising it
until a goal is discovered.
This algorithm searches in depth first up to a specific "depth limit," then
increases the depth limit for each iteration until the objective node is
discovered.
This search algorithm combines the speed of breadth-first search with the
memory efficiency of depth-first search.
When the search space is huge and the depth of the goal node is unknown, the
iterative search technique is useful for uninformed search.
Advantages
In terms of quick search and memory efficiency, it combines the advantages of
the BFS and DFS search algorithms.
Disadvantages:
The biggest disadvantage of IDDFS is that it duplicates all of the preceding
phase's work.
Example
The iterative deepening depth-first search is seen in the tree structure below.
The IDDFS algorithm iterates until it can't find the goal node any longer. The
algorithm's iteration is described as follows:

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
The method will find the goal node in the fourth iteration.
Completeness:
If the branching factor is finite, this procedure is complete.
Time Complexity:
Let's suppose b is the branching factor and depth is d then the worst-case time
complexity is O(bd).
Space Complexity:
The space complexity of IDDFS will be O(bd).
Optimal:
If path cost is a non-decreasing function of node depth, the IDDFS algorithm
is optimal.
Bidirectional Search Algorithm
To discover the goal node, the bidirectional search algorithm does two
simultaneous searches, one from the initial state (forward-search) and the
other from the goal node (backward-search). Bidirectional search splits a
single search graph into two small subgraphs, one starting from a beginning
vertex and the other from the destination vertex. When these two graphs
intersect, the search comes to an end.
BFS, DFS, DLS, and other search algorithms can be used in bidirectional
search.
Advantages
Searching in both directions is quick.
It takes less memory to do a bidirectional search.
Disadvantages
The bidirectional search tree is challenging to implement.
In bidirectional search, the objective state should be known ahead of time.
Example
The bidirectional search technique is used in the search tree below. One
graph/tree is divided into two sub-graphs using this approach. In the forward
direction, it begins at node 1 and in the reverse direction, it begins at goal node
16.
The process comes to a halt at node 9, when two searches collide.
Completeness: If we use BFS in both searches, we get a complete bidirectional
search.
Time Complexity: Time complexity of bidirectional search using BFS
is O(bd).
Space Complexity: Space complexity of bidirectional search is O(bd).
Optimal: Bidirectional search is Optimal.
Informed Search Algorithms
So far, we've discussed uninformed search algorithms that scoured the search
space for all possible answers to the problem without having any prior
knowledge of the space. However, an educated search algorithm includes
information such as how far we are from the objective, the cost of the trip, and
how to get to the destination node. This knowledge allows agents to explore
less of the search area and discover the goal node more quickly.
For huge search spaces, the informed search algorithm is more useful. Because
the informed search algorithm is based on the concept of heuristics, it is also
known as heuristic search.
Heuristics function: Informed Search employs a heuristic function to
determine the most promising path. It takes the agent's current state as input
and outputs an estimate of how near the agent is to the goal. The heuristic
method, on the other hand, may not always provide the optimum solution, but
it guarantees that a good solution will be found in a fair amount of time. A
heuristic function determines how close a state is to the desired outcome. It
calculates the cost of an ideal path between two states and is represented by
h(n). The heuristic function's value is always positive.
Admissibility of the heuristic function is given as:
h(n) <= h*(n)
Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence heuristic
cost should be less than or equal to the estimated cost.
Pure Heuristic Search
The simplest type of heuristic search algorithm is pure heuristic search. It
grows nodes according to their heuristic value h. (n). It has two lists: an OPEN
list and a CLOSED list. It places nodes that have previously expanded in the
CLOSED list, and nodes that have not yet been expanded in the OPEN list.
Each iteration, the lowest heuristic value node n is extended, and all of its
successors are generated, and n is added to the closed list. The algorithm keeps
running until a goal state is discovered.
We shall cover two main algorithms in the informed search, which are listed
below:
Best First Search Algorithm(Greedy search)
A* Search Algorithm
Best-first Search Algorithm (Greedy Search)
The greedy best-first search algorithm always chooses the path that appears to
be the most appealing at the time. It's the result of combining depth-first and
breadth-first search algorithms. It makes use of the heuristic function as well
as search. We can combine the benefits of both methods with best-first search.
At each step, we can use best-first search to select the most promising node.
We expand the node that is closest to the goal node in the best first search
method, and the closest cost is determined using a heuristic function, i.e.
f(n)= g(n).   
Were, h(n)= estimated cost from node n to the goal.
The priority queue implements the greedy best first algorithm.
Best first search algorithm:
Stage 1: Place the starting node into the OPEN list.
Stage 2: If the OPEN list is empty, Stop and return failure.
Stage 3: Remove the node n, from the OPEN list which has the lowest value of
h(n), and places it in the CLOSED list.
Stage 4: Expand the node n, and generate the successors of node n.
Stage 5: Check each of node n's descendants to see if any of them is a goal
node. Return success and end the search if any successor node is a goal node;
otherwise, proceed to Stage 6.
Stage 6: The algorithm looks for the evaluation function f(n) for each
successor node, then determines if the node has been in the OPEN or
CLOSED list. Add the node to the OPEN list if it isn't already on both lists.
Stage 7: Return to Stage 2.
Advantages
By combining the benefits of both algorithms, best first search may transition
between BFS and DFS.
This method outperforms the BFS and DFS algorithms in terms of efficiency.
Disadvantages:
In the worst-case scenario, it can act like an unguided depth-first search.
As with DFS, it's possible to get stuck in a loop.
This algorithm isn't the best.
Example:
Consider the search problem below, which we'll solve with greedy best-first
search. Each node is extended at each iteration using the evaluation function
f(n)=h(n), as shown in the table below.
We're going to use two lists in this example: OPEN and CLOSED Lists. The
iterations for traversing the aforementioned example are listed below.

Expand the nodes of S and put in the CLOSED list


Initialization: Open [A, B], Closed [S]
Iteration 1: Open [A], Closed [S, B]
Iteration2: 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]
Hence the final solution path will be: S----> B----->F----> G
Time Complexity: The worst case time complexity of Greedy best first search
is O(bm).
Space Complexity: The worst case space complexity of Greedy best first
search is O(bm). Where, m is the maximum depth of the search space.
Complete: Even if the given state space is finite, greedy best-first search is still
imperfect.
Optimal: Greedy The best-first-search algorithm isn't the greatest.
A* Search Algorithm
The most well-known type of best-first search is the A* search. It employs the
heuristic function h(n) and the cost of getting from state g to node n. (n). It
solves the problem efficiently by combining UCS and greedy best-first search
features. Using the heuristic function, the A* search algorithm finds the
shortest path through the search space. This search algorithm uses a smaller
search tree and delivers the best results faster. The A* method is similar to
UCS, but instead of g(n)it uses g(n)+h(n).
We employ a search heuristic as well as the cost to reach the node in the A*
search algorithm. As a result, we can add both expenses together as follows,
and this total is referred to as a fitness number.

Only the nodes with the lowest value of f(n) are extended at each point in the
search space, and the procedure ends when the goal node is located.
Algorithm of A* search
Stage1: Place the starting node in the OPEN list.
Stage 2: Check if the OPEN list is empty or not, if the list is empty then return
failure and stops.
Stage 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
Stage 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.
Stage 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.
Stage 6: Return to Step 2.
Advantages
The A* search algorithm outperforms all other search algorithms.
The A* search algorithm is ideal and comprehensive.
This method is capable of resolving extremely difficult issues.
Disadvantages
Because it is primarily reliant on heuristics and approximation, it does not
always yield the shortest path.
The A* search algorithm has some concerns with complexity.
The fundamental disadvantage of A* is that it requires a lot of memory
because it maintains all created nodes in memory, which makes it unsuitable
for a variety of large-scale issues.
Example
We'll use the A* method to explore the given graph in this example. We'll
calculate the f(n) of each state using the formula f(n)= g(n) + h(n), where g(n)
is the cost of reaching any node from the start state.
We'll use the OPEN and CLOSED lists here.
Solution

Initialization: {(S, 5)}
Iteration1: {(S--> A, 4), (S-->G, 10)}
Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7),
(S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it provides the
optimal path with cost 6.
Stratagies
A* algorithm returns the path which occurred first, and it does not search for
all remaining paths.
The efficiency of A* algorithm depends on the quality of heuristic.
A* algorithm expands all nodes which satisfy the condition f(n)<="" li="">
Complete: A* algorithm is complete as long as:
Branching factor is finite.
Cost at every action is fixed.
Optimal: A* search algorithm is optimal if it follows below two conditions:
Admissible: The first requirement for optimality is that h(n) be an admissible
heuristic in A* tree search. An acceptable heuristic is one that is optimistic.
Consistency: For only A* graph-search, the second required condition is
consistency.
A* tree search will always identify the least expensive path if the heuristic
function is accepted.
Time Complexity: The A* search algorithm's time complexity is determined
by the heuristic function, and the number of nodes expanded is proportional to
the depth of the solution d. So, where b is the branching factor, the temporal
complexity is O(bd).
Space Complexity: The space complexity of A* search algorithm is O(b^d)
Heuristic Functions in Artificial Intelligence
Heuristic Functions in AI: As we've already seen, an informed search makes
use of heuristic functions in order to get closer to the goal node. As a result,
there are various ways to get from the present node to the goal node in a
search tree. It is undeniably important to choose a decent heuristic function.
The efficiency of a heuristic function determines its usefulness. The more
knowledge about the problem there is, the longer it takes to solve it.
A heuristic function can help solve some toy problems more efficiently, such
as 8-puzzle, 8-queen, tic-tac-toe, and so on. Let's have a look at how:
Consider the eight-puzzle issue below, which has a start and a target state. Our
goal is to slide the tiles from the current/start state into the goal state in the
correct order. There are four possible movements: left, right, up, and down.
There are various ways to transform the current/start state to the desired state,
but we can solve the problem more efficiently by using the heuristic function
h(n).

A heuristic function for the 8-puzzle problem is defined below:


h(n)=Number of tiles out of position.
The following is a heuristic function for the 8-puzzle problem:
h(n)=Number of tiles that are out of place.
So, there are three tiles that are out of place, namely 6, 5, and 4. The empty tile
in the goal state is not counted). h(n)=3 in this case. The value of h(n) =0 must
now be minimised.
To reduce the h(n) value to 0, we can build a state-space tree as shown below:

The objective state is minimised from h(n)=3 to h(n)=0, as seen in the state
space tree above. However, depending on the requirement, we can design and
employ a number of heuristic functions. A heuristic function h(n) can
alternatively be defined as the knowledge needed to solve a problem more
efficiently, as shown in the previous example. The information can be related
to the nature of the state, the cost of changing states, the characteristics of
target nodes, and so on, and is stated as a heuristic function.
Properties of a Heuristic search Algorithm
The following qualities of a heuristic search algorithm result from the use of
heuristic functions in a heuristic search algorithm:
Admissible Condition: If an algorithm gives an optimal solution, it is said to
be acceptable.
Completeness: If an algorithm ends with a solution, it is said to be complete (if
the solution exists).
Dominance Property: If A1 and A2 are both admissible heuristic algorithms
with h1 and h2 heuristic functions, A1 is said to dominate A2 if h1 is better
than h2 for all node n values.
Optimality Property: If an algorithm is complete, acceptable, and dominates
other algorithms, it is the best and will almost always produce the best result.
Local Search Algorithms and Optimization Problem
The informed and uninformed search expands the nodes in two ways: by
remembering different paths and selecting the best suitable path, which leads
to the solution state required to reach the destination node. But, in addition to
these "classical search algorithms," there are some "local search algorithms"
that ignore path cost and focus just on the solution-state required to reach the
destination node.
Instead of visiting numerous paths and following the neighbours of a single
current node, a local search algorithm completes its mission by following the
neighbours of that node in general.
Although local search algorithms are not systematic, still they have the
following two advantages:
 Because they only work on a single path, local search algorithms
consume a little or constant amount of memory.
 In huge or infinite state spaces, where classical or systematic
algorithms fail, they frequently discover a suitable solution.
Is it possible to use the local search algorithm to solve a pure optimised
problem?
Yes, for pure optimised issues, the local search technique works. A pure
optimization problem is one that can be solved by all nodes. However,
according to the objective function, the goal is to discover the optimal state out
of all of them. Unfortunately, the pure optimization issue fails to identify good
solutions for getting from the current condition to the objective state.
In various contexts of optimization issues, an objective function is a function
whose value is either minimised or maximised. An objective function in
search algorithms can be the path cost to the goal node, for example.
Working of a Local search algorithm
Let's look at how a local search algorithm works with the help of an example:
Consider the state-space landscape below, which includes both:
Location: It is defined by the state.
Elevation: The value of the objective function or heuristic cost function
defines it.

The aforementioned terrain is explored by the local search algorithm by


locating the following two points:
Global Minimum: If the cost is equal to the elevation, the challenge is to
identify the lowest valley, also known as the Global Minimum.   
Global Maxima: If the elevation matches an objective function, the highest
point, known as the Global Maxima, is found. It is the valley's highest peak.
In the Hill-climbing search, we will gain a deeper understanding of how these
points work.
Here are some examples of different kinds of local searches:
Hill-climbing Search
Simulated Annealing
Local Beam Search
Hill Climbing Algorithm in AI
Hill Climbing Algorithm: A local search issue, hill climbing is. The goal of
the hill climbing search is to climb a hill and reach its highest peak or summit.
It is based on the heuristic search strategy, in which the person ascending the
hill calculates the direction that will take him to the highest peak.
State-space Landscape of Hill climbing algorithm
Consider the landscape below, which represents the objective state/peak and
the climber's current state, to grasp the concept of a hill climbing algorithm.
The geographical regions depicted in the diagram can be described as follows:
Global Maximum: It is the highest point on the hill, which is the goal state.
Local Maximum: It is the peak higher than all other peaks but lower than the
global maximum.
Flat local maximum: It is the flat area over the hill where it has no uphill or
downhill. It is a saturated point of the hill.
Shoulder: It is also a flat area where the summit is possible.
Current state: It is the current position of the person.

Types of Hill climbing search algorithm


There are following types of hill-climbing search:
Simple hill climbing
Steepest-ascent hill climbing
Stochastic hill climbing
Random-restart hill climbing

Simple hill climbing search


The most basic approach for climbing a hill is simple hill climbing. The goal
is to climb the mountain's highest peak. The climber's movement is
determined by his movements/steps. If he finds his next step better than the
previous one, he continues to move else remain in the same state. This search
is limited to his prior and subsequent actions.
Simple hill climbing Algorithm
Create a CURRENT node, NEIGHBOUR node, and a GOAL node.
If the CURRENT node=GOAL node, return GOAL and terminate the search.
Else  CURRENT node<= NEIGHBOUR node, move ahead.
Loop until the goal is not reached or a point is not found.
Steepest-ascent hill climbing
Simple hill climbing search is not the same as steepest-ascent hill climbing.
Unlike a traditional hill-climbing search, it considers all subsequent nodes,
compares them, and selects the node that is closest to the answer. Because it
focuses on each node instead of just one, steepest hill climbing search is akin
to best-first search.
Parameter: When there is no closer node, both simple and steepest-ascent hill
climbing searches fail.
Steepest-ascent hill climbing algorithm
 Create a CURRENT node and a GOAL node.
 If the CURRENT node=GOAL node, return GOAL and terminate the
search.
 Loop until a better node is not found to reach the solution.
 If there is any better successor node present, expand it.
 When the GOAL is attained, return GOAL and terminate.
Stochastic hill climbing
The focus of stochastic hill climbing is not on all of the nodes. It chooses one
node at random and determines whether it should be extended or replaced.
Random-restart hill climbing
The try-and-try approach is used in the random-restart algorithm. It searches
the node repeatedly, selecting the best option at each step until the goal is not
found. The shape of the hill is the most important factor in determining
success. It is easier to reach the target if there are few plateaus, local maxima,
and ridges.
Limitations of Hill climbing algorithm
The hill climbing algorithm is a lightning-fast method. It quickly determines
the solution state because improving a bad state is relatively simple. However,
this search has the following limitations:
Local Maxima: It is the mountain peak that is higher than all of its neighbours
but lower than the global maxima. Because there is another peak higher than
it, it is not the objective peak.
Plateau: It is a place with a flat surface and no uphill. The climber finds it
challenging to determine the direction he should travel in order to reach the
goal point. The person may become disoriented in the flat region.

Ridges: It's a difficult situation when a person frequently finds two or more


local maxima of the same height. It becomes tough for the person to find the
correct location and remain focused on it.

Simulated Annealing
The hill climbing algorithm is related to simulated annealing. It is effective in
the current circumstances. Instead of choosing the best move, it chooses a
random move. If the move improves the existing situation, it is always
accepted as a step toward the solution state; otherwise, the move with a
probability smaller than one is accepted. This search method was first utilised
to tackle VLSI layout challenges in 1980. It's also used for plant planning and
other large-scale optimization projects.
Local Beam Search
Random-restart search is not the same as local beam search. Instead of simply
one state, it keeps track of k. It chooses k randomly generated states and
expands them one at a time. The search ends with success if any state is a
desired state. Otherwise, it chooses the top k successors from the entire list
and continues the procedure. While each search process works independently
in random-restart search, the essential information is shared throughout the
parallel search processes in local beam search.
Disadvantages of Local Beam search
The absence of diversity among the k states may make this search difficult.
It's a more expensive version of the hill climbing search.
Parameter: Stochastic Beam Search is a version of Local Beam Search that
chooses k successors at random rather than the best k successors.

Searching with Partial Observations


Introduce the concept of belief state: indicates an agent's current belief about
various physical states based on a series of actions and perceptions up to that
point 3 scenarios:
– Searching with no observation
– Searching with observations
- Solving partially observable problems

Searching with no observations: Sensorless


Sensorless = conformant
Example: Vacuum world with no sensors:
– Agent knows geography of environment
– Agent doesn’t know its location or distribution of dirt
– Initial state: {1, 2, 3, 4, 5, 6, 7, 8}
– Action outcomes:
• [Right]: possible successor states: {2, 4, 6, 8}
• [Right, Suck]: {4, 8}
• [Right, Suck, Left Suck]: {7}
Sensorless problems: Search in Space of Belief States
Beliefs are fully observable
Belief states: every possible set of physical states; N 2N belief statesphysical
states
• Initial state: Typically the set of all physical states
• Actions: Either the union or intersection of the legal actions for the current
belief states
• Transition model: set of all possible states that could result from taking any
of the actions in any of the belief states
• Goal test: all states in current belief set are goal states
• Path cost: (it depends. application-specific)
Challenge: size of belief state
Example: belief state for 10 x 10 vacuum world has 100 x 2100 = 1032
physical states!
Alternatives:
Better (more compact) representation
Solving problem incrementally (incremental belief-state search)
E.g.,
solve for first state,
see if it works for other states;
if not, find another solution for first state,
and iterate
But, generally tough to solve w/o sensors!
Belief-state space for deterministic, sensorless vacuum world

there are 28 = 256 possible belief states, but only 12 reachable belief states

Searching with observations


Define PERCEPT(s) that returns the agent’s percept, given the state s
• E.g., In Vacuum world, PERCEPT(state1)=[A,Dirty]
Special cases:
– Fully observable problems: PERCEPT(s) = s
– Sensorless problems: PERCEPT(s) = Null
Vacuum World Examples

• PERCEPT = [A,Dirty] yields belief state {1, 3}


• PERCEPT = [B,Clean] yields belief state {4, 8}
Example Transitions (Grey circles represent belief states)
Deterministic world: Fig A
Slippery World : Fig B
Searching with observations
Prediction: given action a in belief state b, predict the resulting belief state: ƀ =
Predict(b, a)
• Observation prediction: determine set of percepts o that could be observed in
the predicted belief state: POSSIBLE_PERCEPTS(ƀ)= {𝑜: 𝑜 = PERCEPT 𝑠
and 𝑠 𝜖 ƀ}
• Update: determine belief state that results from each possible percept (i.e.,
which set of states in ƀ could have produced the percept)
𝑏0=UPDATE (ƀ,o)= {s: o =PERCEPT 𝑠 and 𝑠 𝜖 ƀ}
• Then, obtain possible belief states resulting from action and subsequent
possible percepts: RESULTS 𝑏, 𝑎
= 𝑏0: 𝑏0= UPDATE(PREDICT(𝑏, 𝑎 , 𝑜) and 𝑜 𝜖 POSSIBLE−PERCEPTS
(PREDICT (𝑏, 𝑎))}

Online search problems


Until now, there was offline search, in which a complete solution was
generated before anything in the physical world changed.
• Online search:
– Interleaves computation and action
• “Solved” by an agent executing actions
– Useful for dynamic environments
– Useful for nondeterministic environments, since it allows agent to focus on
contingencies that actually arise
– not just those that might arise
– Necessary for unknown environments
Online search problems (con’t.)
Agent only knows:
– Actions(s) – list of actions allowed in state s
– Step-cost function c(s, a, s’) can only be determined after s’ discovered
– Goal-Test(s)
Agent might know: admissible heuristic to determine distance to goal state
• Cost: total path cost of the path the agent actually travels
• Competitive ratio: ratio of actual cost to optimal cost (i.e., best case if the
agent knew the search space in advance)
– “1” is optimal actual cost
Irreversible actions: fall off cliff!
• Dead-end state: locked in a freezer!
• No algorithm can avoid dead ends in all state spaces
• Easier: assume state space is safely explorable, where some goal is reachable
from every state
Constraint Satisfaction Problems in Artificial Intelligence
We've seen a variety of strategies, such as local search and adversarial search,
used to solve various problems. Every problem-solving technique has a single
goal: to find a solution that will allow you to achieve your goal. Although
there were no limits on the agents in adversarial search and local search when
solving issues and finding solutions, there were in adversarial search and local
search.
In this section, we'll look at the Constraint Satisfaction Technique, which is a
form of problem-solving technique. Constraint satisfaction, as the name
implies, is the process of solving a problem while adhering to specific
constraints or norms.
Constraint satisfaction is a problem-solving strategy in which the values of a
problem satisfy specific restrictions or criteria. A strategy like this leads to a
better grasp of the problem's structure and complexity.
Constraint satisfaction is determined by three factors:
X: It is a set of variables.
D: It is a set of domains where the variables reside. There is a specific domain
for each variable.
C: It is a set of constraints which are followed by the set of variables.
Domains are the spaces where variables exist in constraint fulfilment,
following the problem-specific constraints. The three major components of a
constraint satisfaction technique are as follows. A pair of {scope and rel}
make up the constraint value. The scope is a tuple of variables that are part of
the constraint, and rel is a relation that gives a list of possible values for the
variables to meet the problem's constraints.
Solving Constraint Satisfaction Problems
The following are the prerequisites for solving a constraint satisfaction
problem (CSP):
 A state-space
 The notion of the solution.
Assigning values to some or all variables, such as {X1=v1, X2=v2,} and so
on, defines a state in state-space.
There are three methods for assigning values to a variable:
Consistent or Legal Assignment: A consistent or legal assignment is one that
does not break any constraints or rules.
Complete Assignment: An assignment in which each variable has a value and
the CSP solution remains consistent. The term "complete assignment" refers to
such a task.
A partial assignment is one in which only some of the variables are assigned
values. Partial assignments are the name for this type of assignment.
Types of Domains in CSP
The variables are divided into two sorts of domains:
Discrete Domain: It's an endless domain with several variables in a single
state. For each variable, for example, a start state can be assigned an endless
number of times.
Finite Domain: It's a finite domain with continuous states that describe a
single domain for a single variable. A continuous domain is another name for
it.
Constraint Types in CSP
With respect to the variables, basically there are following types of
constraints:
Unary Constraints: It is the simplest type of constraints that restricts the value
of a single variable.
Binary Constraints: It is the constraint type which relates two variables. A
value x2 will contain a value which lies between x1 and x3.
Global Constraints: It is the constraint type which involves an arbitrary
number of variables.
Some special types of solution algorithms are used to solve the following
types of constraints:
Linear Constraints: These type of constraints are commonly used in linear
programming where each variable containing an integer value exists in linear
form only.
Non-linear Constraints: These type of constraints are used in non-linear
programming where each variable (an integer value) exists in a non-linear
form.
Parameter: A special constraint which works in real-world is known
as Preference constraint.
Constraint Propagation
In local state-spaces, there is just one option, which is to look for a solution.
However, in CSP, we have two options: we can either look for a solution or
we can create one.
Constraint propagation is a type of inference that we can undertake.
Constraint propagation is a sort of reasoning that aids in limiting the number
of legal values for variables. Constraint propagation is based on the concept of
local consistency.
Variables are represented as nodes in local consistency, and each binary
constraint is handled as an arc in the given issue. The following local
consistency issues are mentioned further down:
Node Consistency: If all of the values in a variable's domain fulfil the unary
restrictions on the variables, the variable is said to be node consistent.
Arc Consistency: If every value in a variable's domain satisfies the variables'
binary requirements, the variable is said to be arc consistent.
Path Consistency: When the evaluation of a set of two variables in relation to a
third variable can be extended to another variable while still meeting all binary
restrictions. It's comparable to the concept of arc consistency.
k-consistency: The concept of stronger kinds of propagation is defined by this
type of consistency. The k-consistency of the variables is investigated here.
CSP Problems
Constraint satisfaction refers to problems that have some constraints that must
be met in order to be solved. The following issues are included in CSP:
Graph Coloring: The limitation here is that no two neighbouring sides can
have the same colour.

Sudoku Playing: The game has a rule that no number from 0 to 9 can be


repeated in the same row or column.
n-queen problem: The constraint in the n-queen problem is that no queens
should be placed diagonally, in the same row or column.
Crossword: The constraint in a crossword puzzle is that the words must be
correctly formed and meaningful.
Latin square Problem: The goal of this game is to find the pattern that appears
multiple times during the game. They may be jumbled, but the numerals will
be the same.

Cryptarithmetic Problem
Cryptarithmetic Problem is a form of constraint fulfilment problem where the
game is about digits and their unique substitution either with alphabets or
other symbols. The digits (0-9) are substituted by some conceivable alphabets
or symbols in a cryptarithmetic problem. In a cryptarithmetic problem, the
goal is to replace each digit with an alphabet to achieve an arithmetically
correct solution.
The following are the rules or constraints for a cryptarithmetic problem:
 A unique digit should be substituted for a unique alphabet.
 The outcome must adhere to the predetermined arithmetic rules, such
as 2+2 = 4, and nothing else.
 Only digits from 0 to 9 should be used.
 When conducting an addition operation on a problem, there should
only be one carry forward.
 The problem can be approached from either the lefthand side (L.H.S)
or the righthand side (R.H.S) (R.H.S)
 Let's use an example to better grasp the cryptarithmetic problem and its
constraints:
 S E N D + M O R E = M O N E Y is a cryptarithmetic problem.

In this case, combining the phrases S E N D and M O R E gives M O N E Y.


To break down the given problem into its component pieces, follow the
procedures below:
S and M are the terms that begin on the left hand side (L.H.S). Assign a digit
that will produce an acceptable result. Assign S to 9 and M to 1.

As a result of adding up the words, we get a satisfactory result and an


assignment for O as O->0.
Continue to the next terms E and O to obtain N as the output.

Adding E and O results in 5+0=0, which is not allowed due to cryptarithmetic


constraints prohibiting the assignment of the same digit to two letters. As a
result, we must think more deeply and assign a different value.
We will obtain one carry if we solve further, and if we apply it, the answer
will be satisfied.
Furthermore, by combining the next two terms N and R, we arrive at

However, E->5 has already been assigned. As a result, because we get a


different value for E, the given result does not satisfy the values. As a result,
we must think more deeply. We will obtain a carryover on this term after
answering the entire problem, thus our solution will be satisfied.        

where 1 is carried through to the term above


Let's get started.
We get Y as a result of adding the final two terms, i.e., the rightmost terms D
and E.
Here 1 will be  carry forward to the above term
Keeping all the constraints in mind, the final resultant is as follows:

The assignment of the numerals to the alphabets is depicted in the diagram


below.

More cryptarithmical problems can be found here:


On the cryptarithmatic issues, we can also perform multiplication.
Constraint propagation
Although forward checking catches a large number of discrepancies, it does
not catch them all. The phrase "constraint propagation" refers to the process of
spreading the effects of a constraint on one variable to other variables.
Arc Consistency

k-Consistency
Local Search for CSPs

The Structure of Problems Problem Structure


Independent Subproblems

Tree-Structured CSPs

Backtracking Search for CSPs


Backtracking search refers to a depth-first search that selects values for one
variable at a time and then backtracks when there are no more legal values to
assign to that variable. Figure 1 depicts the algorithm.
For the constraint fulfilment problem, a simple backtracking technique is used.
The recursive depth-first search is used to model the algorithm.

For the map-coloring problem, part of the search tree was constructed using
simple backtracking.
Forward checking
Forward checking is a technique for making better use of constraints during
search. When a variable X is assigned, the forward checking procedure
examines each unassigned variable Y that is linked to X by a constraint and
deletes any value from Y's domain that is incompatible with the value chosen
for X. The progress of a map-coloring search with forward checking is shown
in the diagram below.

The progress of a forward-checked map-coloring search. WA = red is assigned


initially, and then red is removed from the domains of the adjoining variables
NT and SA via forward checking. Green is removed from the domains of NT,
SA, and NSW after Q = green. Following the deletion of V = blue, blue from
the domains of NSW and SA, SA is left with no legal values.
Adversarial Search
Adversarial search is a type of search in which we look at the issue that
develops when we try to plan ahead of the world while other agents plan
against us.
We've looked at search methods that are solely linked with a single agent that
attempts to find a solution, which is commonly stated as a series of actions, in
prior subjects.
However, there may be times when more than one agent is searching for the
same answer in the same search space, which is common in game play.
The environment with more than one agent is referred to as a multi-agent
environment, in which each agent is an adversary to the other and competes
against them. Each agent must think about the actions of other agents and how
they affect their own performance.
Adversial searches, often known as Games, are searches in which two or more
players with opposing aims try to explore the same search space for a solution.
Games are modelled as a Search problem and a heuristic evaluation function,
which are the two primary variables that aid in the modelling and solving of
games in AI.
Types of Games in AI

Deterministic Chance Moves

Perfect information Chess, Checkers, go, Othello Backgammon,


monopoly

Imperfect information Battleships, blind, tic-tac-toe Bridge, poker,


scrabble, nuclear
war

Perfect information: A game with perfect information is one in which agents


are able to look at the entire board. Agents have access to all game
information and can watch each other's movements. Chess, Checkers, Go, and
other games are examples.
Imperfect information: Such games as tic-tac-toe, Battleship, blind, Bridge,
and others are known as games with incomplete information since the agents
do not have all of the knowledge about the game and are unaware of what is
going on.
Deterministic games: Deterministic games are those that follow a strict pattern
and set of rules, with no element of chance. Chess, Checkers, Go, tic-tac-toe,
and other games are examples.
Non-deterministic games: Non-deterministic games are those with a variety of
unpredictable events and a chance or luck aspect. Dice or cards are used to
introduce the element of chance or luck. These are unpredictably generated,
and each action reaction is unique. These games are sometimes known as
stochastic games.
Example: Backgammon, Monopoly, Poker, etc.
Zero-Sum Game
Zero-sum games are adversarial searches in which there is only one winner.
Each agent's gain or loss of utility in a zero-sum game is exactly balanced by
the losses or gains of another actor.
One player attempts to maximise a single value, while the other attempts to
minimise it.
A ply is a single move made by one player in the game.
A Zero-sum game is something like chess or tic-tac-toe.
Zero-sum game: Embedded thinking
In the Zero-sum game, one agent or player is trying to find out what to do.
What factors should be considered when making a decision?
He must also consider his opponent.
The opponent is likewise considering what to do.
Each player is trying to figure out how his opponent will react to his moves.
To address gaming problems in AI, this necessitates embedded thinking or
backward reasoning.
Formalization of the problem
A game can be characterised as a sort of AI search that consists of the
following elements:
Initial state: It specifies how the game is set up at the start.
Player(s): It specifies which player has moved in the state space.
Action(s): It returns the set of legal moves in state space.
Result(s, a): It is the transition model, which specifies the result of moves in
the state space.
Terminal-Test(s): If the game is over, the terminal test is true; otherwise, it is
false. Terminal states are the states in which the game comes to a finish.
Utility(s, p): For a game that ends in terminal states s for player p, a utility
function returns the final numeric number. It's also known as the payout
function. Chess has three possible outcomes: win, defeat, or draw, with payoff
values of +1, 0, and 1/2. Utility values for tic-tac-toe are +1, -1, and 0.
Game tree
A game tree is a tree in which the nodes represent game states and the edges
represent player moves. Initial state, actions function, and result function are
all part of the game tree.
Example: Tree of tic-tac-toe games:
The following diagram depicts a portion of the tic-tac-toe game's game tree.
The following are some of the game's significant points:
MAX and MIN are the two players.
Each player takes a turn and begins with MAX.
MAX maximises the game tree's result, whereas MIN minimises it.

Example Explanation:
 MAX has 9 possible moves from the start because he is the first player.
Both players alternately place x and o until we reach a leaf node where
one player has three in a row or all squares are filled.
 Both players will compute the best possible utility versus an optimum
adversary for each node, called the minimax value.
 Assume that both players are well-versed in tic-tac-toe and are playing
their best game. Each player is trying everything he can to keep the
other from winning. In the game, MIN is working against Max.
 So, in the game tree, we have a Max layer, a MIN layer, and each layer
is referred to as Ply. The game proceeds to the terminal node, with
Max placing x and MIN placing o to prevent Max from winning.
 Either MIN or MAX wins, or the game ends in a tie. This game-tree
represents the entire search universe of possibilities in which MIN and
MAX are tic-tac-toeing taking turns alternatively
As a result, the process for adversarial Search for the Minimax is as follows:
 Its goal is to figure out the best way for MAX to win the game.
 It employs a depth-first search strategy.
 The ideal leaf node in the game tree could appear at any level of the
tree.
 Minimax values should be propagated up the tree until the terminal
node is found.
The optimal strategy in a particular game tree can be determined by looking at
the minimax value of each node, which can be expressed as MINIMAX (n). If
MAX prefers to move to a maximum value state and MIN prefers to move to a
minimum value state, then:
Mini-Max Algorithm
 In decision-making and game theory, the mini-max algorithm is a
recursive or backtracking method. It suggests the best move for the
player, provided that the opponent is likewise playing well.
 The Mini-Max algorithm searches the game tree using recursion.
 In AI, the Min-Max algorithm is mostly employed for game play.
Chess, checkers, tic-tac-toe, go, and other two-player games are
examples. This Algorithm calculates the current state's minimax
choice.
 The game is played by two players, one named MAX and the other
named MIN, in this algorithm.
 Both players are fighting it, since the opponent player receives the
smallest benefit while they receive the greatest profit.
 Both players in the game are adversaries, with MAX selecting the
maximum value and MIN selecting the minimum value.
 For the investigation of the entire game tree, the minimax method uses
a depth-first search strategy.
 The minimax algorithm descends all the way to the tree's terminal
node, then recursively backtracks the tree.
Pseudo-code for MinMax Algorithm

function minimax(node, depth, maximizingPlayer) is  
if depth ==0 or node is a terminal node then  
return static evaluation of node  
if MaximizingPlayer then      // for Maximizer Player  
maxEva= -infinity            
 for each child of node do  
 eva= minimax(child, depth-1, false)  
maxEva= max(maxEva,eva)        //gives Maximum of the values  
return maxEva    
else                         // for Minimizer player  
 minEva= +infinity   
for each child of node do  
eva= minimax(child, depth-1, true)  
minEva= min(minEva, eva)         //gives minimum of the values  
return minEva
Initial call:
Minimax(node, 3, true)
Working of Min-Max Algorithm
 A simple example can be used to explain how the minimax algorithm
works. We've included an example of a game-tree below, which
represents a two-player game.
 There are two players in this scenario, one named Maximizer and the
other named Minimizer.
 Maximizer will strive for the highest possible score, while Minimizer
will strive for the lowest possible score.
 Because this algorithm uses DFS, we must go all the way through the
leaves to reach the terminal nodes in this game-tree.
The terminal values are given at the terminal node, so we'll compare them and
retrace the tree till we reach the original state. The essential phases in solving
the two-player game tree are as follows:
Step-1: The algorithm constructs the full game-tree in the first phase, then
applies the utility function to obtain the utility values for the terminal states.
Let's assume A is the tree's initial state in the diagram below. Assume that the
maximizer takes the first turn with a worst-case initial value of -infinity, and
the minimizer takes the second turn with a worst-case initial value of +infinity.
Step 2: Now, we'll locate the Maximizer's utilities value, which is -, and
compare each value in the terminal state to the Maximizer's initial value to
determine the upper nodes' values. It will select the best option from all of
them.
For node D         max(-1,- -∞) => max(-1,4)= 4
For Node E         max(2, -∞) => max(2, 6)= 6
For Node F         max(-3, -∞) => max(-3,-5) = -3
For node G         max(0, -∞) = max(0, 7) = 7
Step 3: In the next step, it's a turn for minimizer, so it will compare all nodes
value with +∞, and will find the 3rd layer node values.
For node B= min(4,6) = 4
For node C= min (-3, 7) = -3

Step 4: Now it's Maximizer's turn, and it'll choose the maximum value of all
nodes and locate the root node's maximum value. There are only four layers in
this game tree, so we can go to the root node right away, but there will be
more layers in real games.
For node A max(4, -3)= 4

That was the complete workflow of the minimax two player game.
Properties of Mini-Max algorithm
Complete- Min-Max algorithm is Complete. It will definitely find a solution
(if exist), in the finite search tree.
Optimal- Min-Max algorithm is optimal if both opponents are playing
optimally.
Time complexity- As it performs DFS for the game-tree, so the time
complexity of Min-Max algorithm is O(bm), where b is branching factor of the
game-tree, and m is the maximum depth of the tree.
Space Complexity- Space complexity of Mini-max algorithm is also similar to
DFS which is O(bm).
Limitation of the minimax Algorithm
The biggest disadvantage of the minimax algorithm is that it becomes
extremely slow while playing complex games like chess or go. This style of
game contains a lot of branching, and the player has a lot of options to choose
from.
Alpha-Beta Pruning
A modified variant of the minimax method is alpha-beta pruning. It's a way
for improving the minimax algorithm.
The number of game states that the minimax search algorithm must investigate
grows exponentially with the depth of the tree, as we saw with the minimax
search method. We can't get rid of the exponent, but we can reduce it by half.
As a result, there is a technique known as pruning that allows us to compute
the correct minimax choice without having to inspect each node of the game
tree.. It's named alpha-beta pruning because it involves two threshold
parameters, Alpha and beta, for future expansion. Alpha-Beta Algorithm is
another name for it.
Alpha-beta pruning can be done at any depth in a tree, and it can sometimes
prune the entire sub-tree as well as the tree leaves.
The two-parameter can be defined as:
a. Alpha: The best (highest-value) choice we have found so far at
any point along the path of Maximizer. The initial value of
alpha is -∞.
b. Beta: The best (lowest-value) choice we have found so far at
any point along the path of Minimizer. The initial value of beta
is +∞.
The Alpha-beta pruning to a standard minimax algorithm produces the same
result as the regular approach, but it removes those nodes that aren't really
effecting the final decision but are slowing down the procedure. As a result,
reducing these nodes speeds up the process.
Condition for Alpha-beta pruning
The main condition which required for alpha-beta pruning is:
α>=β  
Key points about alpha-beta pruning
Only the value of alpha will be updated by the Max player.
Only the beta value will be updated by the Min player.
Instead of alpha and beta values, node values will be sent to upper nodes while
retracing the tree.
Only the alpha and beta values will be passed to the child nodes.

Pseudo-code for Alpha-beta Pruning


function minimax(node, depth, alpha, beta, maximizingPlayer) is  
if depth ==0 or node is a terminal node then  
return static evaluation of node 
if MaximizingPlayer then      // for Maximizer Player  
maxEva= -infinity            
for each child of node do  
eva= minimax(child, depth-1, alpha, beta, False)  
maxEva= max(maxEva, eva)   
alpha= max(alpha, maxEva)      
if beta<=alpha  
break  
return maxEva  
else                         // for Minimizer player  
minEva= +infinity   
for each child of node do  
eva= minimax(child, depth-1, alpha, beta, true)  
minEva= min(minEva, eva)   
 beta= min(beta, eva)  
 if beta<=alpha  
break          
 return minEva 

Working of Alpha-Beta Pruning


To better understand how Alpha-beta pruning works, consider a two-player
search tree.
Stage 1: At the first step the, Max player will start first move from node A
where α= -∞ and β= +∞, these value of alpha and beta passed down to node B
where again α= -∞ and β= +∞, and Node B passes the same value to its child
D.

Stage 2: At Node D, the value of α will be calculated as its turn for Max. The
value of α is compared with firstly 2 and then 3, and the max (2, 3) = 3 will be
the value of α at node D and node value will also 3.
Stage 3: Now algorithm backtrack to node B, where the value of β will change
as this is a turn of Min, Now β= +∞, will compare with the available
subsequent nodes value, i.e. min (∞, 3) = 3, hence at node B now α= -∞, and
β= 3.
In the next step, algorithm traverse the next successor of Node B which is
node E, and the values of α= -∞, and β= 3 will also be passed.
Stage 4: At node E, Max will take its turn, and the value of alpha will change.
The current value of alpha will be compared with 5, so max (-∞, 5) = 5, hence
at node E α= 5 and β= 3, where α>=β, so the right successor of E will be
pruned, and algorithm will not traverse it, and the value at node E will be 5.

Stage 5: At next step, algorithm again backtrack the tree, from node B to node
A. At node A, the value of alpha will be changed the maximum available
value is 3 as max (-∞, 3)= 3, and β= +∞, these two values now passes to right
successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values will be passed on to node F.
Stage 6: At node F, again the value of α will be compared with left child
which is 0, and max(3,0)= 3, and then compared with right child which is 1,
and max(3,1)= 3 still α remains 3, but the node value of F will become 1.

Stage 7: Node F returns the node value 1 to node C, at C α= 3 and β= +∞, here
the value of beta will be changed, it will compare with 1 so min (∞, 1) = 1.
Now at C, α=3 and β= 1, and again it satisfies the condition α>=β, so the next
child of C which is G will be pruned, and the algorithm will not compute the
entire sub-tree G.
Stage 8: C now returns the value of 1 to A here the best value for A is max (3,
1) = 3. Following is the final game tree which is the showing the nodes which
are computed and nodes which has never computed. Hence the optimal value
for the maximizer is 3 for this example.

Move Ordering in Alpha-Beta pruning


The order in which each node is reviewed has a significant impact on the
success of alpha-beta pruning. The order in which moves are made is crucial
in alpha-beta pruning.
There are two types of it:
Worst way to order: In some circumstances, the alpha-beta pruning method
does not trim any of the tree's leaves and works in the same way as the
minimax algorithm. Because of the alpha-beta factors, it also takes more time
in this scenario; this type of trimming is known as worst ordering. The optimal
move is on the right side of the tree in this situation. For such an order, the
temporal complexity is O(bm).
Ideal ordering: When there is a lot of pruning in the tree and the best moves
happen on the left side, the perfect ordering for alpha-beta pruning occurs. We
use DFS because it searches the left side of the tree first and then goes deep
twice as fast as the minimax method. In perfect ordering, the complexity is
O(bm/2).
Rules to find good ordering
The following are some guidelines for finding effective alpha-beta pruning
ordering:
The optimal move should be made from the shallowest node.
In the tree, sort the nodes so that the best ones are checked first.
When deciding on the right step, make use of your subject knowledge. For
example, in Chess, attempt the following order: captures first, threats second,
forward moves third, backward moves fourth.
We can keep track of the states because there's a chance they'll happen again.
Stochastic Games in Artificial Intelligence
In real life, many unpredictable external events can put us in unfavourable
situations. To represent this unpredictability, many games, such as dice
tossing, include a random element. Stochastic games are what they're called.
Backgammon is a classic game in which skill and luck are combined. The
permitted movements are established by rolling dice at the start of each
player's turn. In the backgammon scenario described below, white, for
example, has rolled a 6–5 and has four options.
This is a typical backgammon setup. The goal is to remove all of one's pieces
from the board as rapidly as possible. White progresses toward 25 in a
clockwise direction, while Black moves toward 0 in a counterclockwise
direction. A piece can progress to any position unless there are numerous
opponent pieces; if there is just one opponent, it is caught and must start
over. White has rolled a 6–5 and must choose between four legitimate
moves: (5–10,5–11), (5–11,19–24), (5–10,10–16), and (5–11,11–16), where
the notation (5–11,11–16) denotes moving one piece from 5 to 11 and then
another from 11 to 16.
Stochastic game tree for a backgammon position
White is aware of his or her legal options, but has no idea how Black will
roll, and so has no idea what Black's legal options will be. That means White
will be unable to construct a standard game tree, such as in chess or tic-tac-
toe. A game tree in backgammon must have chance nodes in addition to M A
X and M I N nodes. Chance nodes are depicted as circles in the diagram
below. The branches extending from each chance node reflect the various
dice rolls; each branch is labelled with the throw and its probability. There
are 36 potential ways to roll two dice, each equally likely, but because a 6–5
is the same as a 5–6, there are only 21 distinct rolls. Because each of the six
doubles (1–1 through 6–6) has a probability of 1/36, P (1–1) = 1/36. Each of
the remaining 15 rolls has a 1/18 chance of occurring.

The next step is to learn how to make wise decisions. Naturally, we want to
make the decision that will put us in the best possible situation. The
minimum and maximum values for positions, on the other hand, are not
specified. Instead, we can only calculate the expected value of a position,
which is the average of all possible outcomes of the chance nodes.
As a result, for games with chance nodes, we can generalise the deterministic
minimax value to an expected-minimax value. Terminal nodes, MAX and
MIN nodes (for which the dice roll is known), and MAX and MIN nodes (for
which the dice roll is unknown) all work the same way they did before. The
sum of all outcomes, weighted by the likelihood of each chance action, is the
expected value for chance nodes.

where r is a possible dice roll (or other random events) and RESULT(s,r)
denotes the same state as s, but with the addition that the dice roll’s result is
r.

You might also like