0% found this document useful (0 votes)
37 views46 pages

AI Uint 2 PDF

AI

Uploaded by

Krishna Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views46 pages

AI Uint 2 PDF

AI

Uploaded by

Krishna Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT 2 SEARCHING

Searching-Searching for solutions, uniformed search strategies – Breadth first search, depth first Search. Search
with partial information (Heuristic search) Hill climbing, A* ,AO* Algorithms, Problem reduction, Game
Playing- Adversial search, Games, mini-max algorithm, optimal decisions in multiplayer games, Problem in Game
playing, Alpha-Beta pruning, Evaluation functions.

PROBLEM SOLVING: STATE-SPACE SEARCH AND CONTROL


STRATEGIES INTRODUCTION
Problem solving is a method of deriving solution steps beginning from initial description of the problem
to the desired solution. In AI, the problems are frequently modeled as a state space problem where the state
space is a set of all possible states from start to goal states.
The 2 types of problem-solving methods that are generally followed include general purpose & special
purpose methods.
A general purpose method is applicable to a wide variety of problems, where a special purpose method
is a tailor method made for a particular problem.
The most general approach for solving a problem is to generate the solution & test it. For generating new
state in the search space, an action/operator/rule is applied & tested whether the state is the goal state or not. In
case the state is not the goal state, the procedure is repeated. The order of application of rules to the current state
is called control strategy.

GENERAL PROBLEM SOLVING

Production System:
Production System (PS) is one of the formalisms that help AI programs to do search process more
conveniently in state-space problems. This system consists of start (initial) state(s) & goal (final) state(s) of the
problem along with one or more databases consisting of suitable & necessary information for a particular task.
Production System consists of a number of production rules.

Example 1: Water Jug Problem

Problem Statement: There are two jugs a 4-Gallon one and 3-Gallon one. Neither has any measuring marker on
it. There is a pump that can be used to fill the jugs with water. How can we get exactly 2- Gallon water into the
4- Gallon jug?
The state space for this problem can be described as the set of ordered pairs of integers (x, y) such that x
= 0, 1, 2, 3 or 4 and y = 0, 1, 2 or 3. x represents the number of gallons of water in the 4- Gallon jug. y represents
1
the number of gallons of water in the 3- Gallon jug. The start state is (0, 0). The goal state is (2, n) for any
value of n.
Note:0fxf4 and 0fyf3
S.No Rule Result Rule Description

1. (x, y) if x<4 (4, y) fill the 4-g jug

2. (x, y) if y<3 (x, 3) fill the 3-g jug

3. (x, y) if x>0 (x-d, y) pour some water out of the 4-g jug

4. (x, y) if y>0 (x, y-d) pour some water out of the 3-g jug

5. (x, y) if x>0 (0, y) Empty the 4-g jug on the ground

6. (x, y) if y>0 (x,0) Empty the 3-g jug on the ground

7. (x, y) if x + y g 4 & y>0 (4,y-(4-x)) Pour water from the 3-g jug into the 4-g jug
until the 4-g jug is full

8. (x, y) if x + yg3 & x>0 (x-(3-y), 3) pour water from the 4-g jug into the 3-g jug
until the 3-g jug is full

9. (x, y) if x + y f 4 & y>0 (x+y,0) Pour all the water from 3-g jug into 4-g jug.

10. (x, y) If x + y f 3 & x>0 (0,x+y) Pour all the water from the 4-g jug into

One Solution to the Water Jug Problem:

Gallon in the 4-Gallon Jug Gallon in the 3-Gallon Jug Rule Applied
0 0 2
0 3 9
3 0 2
3 3 7
4 2 5/12
0 2 9/11
2 0

2
Example 2: Water Jug Problem

Problem Statement: There are two jugs a 5-Gallon one and 3-Gallon one. Neither has any measuring marker on
it. There is a pump that can be used to fill the jugs with water. How can we get exactly 4- Gallon water into the
5- Gallon jug?
The state space for this problem can be described as the set of ordered pairs of integers (x, y) such that x
= 0, 1, 2, 3, 4 or 5 and y = 0, 1, 2 or 3. x represents the number of gallons of water in the 5- Gallon jug. y
represents the number of gallons of water in the 3- Gallon jug. The start state is (0, 0). The goal state is (4,
n) for any value of n.

Table: Production Rules for Water Jug Problem

One Solution to the Water Jug Problem:

Rule Applied 5-G Jug 3-G Jug


Start State 0 0
1 5 0
8 2 3
4 2 0
6 0 2
1 5 2
8 4 3
Goal State 4 0

3
Example 3: Missionaries & Cannibals Problem

Problem Statement: Three missionaries & three cannibals want to cross a river. There is a boat on their side of
the river that can be used by either 1 (or) 2 persons. How should they use this boat to cross the river in such a
way that cannibals never outnumber missionaries on either side of the river? If the cannibals ever outnumber the
missionaries (on either bank) then the missionaries will be eaten. How can they cross over without eaten?
Consider Missionaries as 8M9, Cannibals as 8C9 & Boat as 8B9 which are on the same side of the river.

Initial State: ([3M, 3C, 1B], [0M, 0C, 0B]) Goal State: ([0M, 0C, 0B], [3M, 3C, 1B])

Production rules are as follows:


Rule 1: (0, M): One Missionary sailing the boat from Bank-1 to Bank-2.
Rule 2: (M, 0): One Missionary sailing the boat from Bank-2 to Bank-1.
Rule 3: (M, M): Two Missionaries sailing the boat from Bank-1 to Bank-2.
Rule 4: (M, M): Two Missionaries sailing the boat from Bank-2 to Bank-1.
Rule 5: (M, C): One Missionary & One Cannibal sailing the boat from Bank-1 to Bank-2.
Rule 6: (C, M): One Cannibal & One Missionary sailing the boat from Bank-2 to Bank-1.
Rule 7: (C, C): Two Cannibals sailing the boat from Bank-1 to Bank-2.
Rule 8: (C, C): Two Cannibals sailing the boat from Bank-2 to Bank-1.
Rule 9: (0, C): One Cannibal sailing the boat from Bank-1 to Bank-2.
Rule 10: (C, 0): One Cannibal sailing the boat from Bank-2 to Bank-1.

S.No Rule Applied Persons on River Bank-1 Persons on River Bank-2


1 Start State 3M,3C,1B 0M,0C,0B
2 5 2M,2C,0B 1M,1C,1B
3 2 3M,2C,1B 0M,1C,0B
4 7 3M,0C,0B 0M,3C,1B
5 10 3M,1C,1B 0M,2C,0B
6 3 1M,1C,0B 2M,2C,1B
7 6 2M,2C,1B 1M,1C,0B
8 3 0M,2C,0B 3M,1C,1B
9 10 0M,3C,1B 3M,0C,0B
10 7 0M,1C,0B 3M,2C,1B
11 10 0M,2C,1B 3M,1C,0B
12 7 0M,0C,0B 3M,3C,1B

4
State Space Search:
A State Space Search is another method of problem representation that facilitates easy search. Using this
method, we can also find a path from start state to goal state while solving a problem. A state space basically
consists of 4 components:
1. A set S containing start states of the problem.
2. A set G containing goal states of the problem.
3. Set of nodes (states) in the graph/tree. Each node represents the state in problem-solving process.
4. Set of arcs connecting nodes. Each arc corresponds to operator that is a step in a problem-solving
process.
A solution path is a path through the graph from a node in S to a node in G. The main objective of a search
algorithm is to determine a solution path in graph. There may be more than one solution paths, as there may be
more than one ways of solving the problem.

Example: The Eight-Puzzle Problem

Problem Statement: The eight-puzzle problem has a 3X3 grid with 8 randomly numbered (1 to 8) tiles arranged
on it with one empty cell. At any point, the adjacent tile can move to the empty cell, creating a new empty cell.
Solving this problem involves arranging tiles such that we get the goal state from the start state.

A state for this problem should keep track of the position of all tiles on the game board, with 0 representing the
blank (empty cell) position on the board. The start & goal states may be represented as follows with each list
representing corresponding row:
1. Start state: [ [3, 7, 6], [5, 1, 2], [4, 0, 8] ]
2. Goal state: [ [5, 3, 6], [7, 0, 2], [4, 1, 8] ]
3. The operators can be thought of moving {Up, Down, Left, Right}, the direction in which blank space
5
effectively moves.

Solution: Following is a Partial Search Tree for Eight Puzzle Problem

The search will be continued until the goal state is reached.

Search Tree for Water Jug Problem:

6
Example: Chess Game (One Legal Chess Move)
Chess is basically a competitive 2 player game played on a chequered board with 64 squares arranged in
an 8 X 8 square. Each player is given 16 pieces of the same colour (black or white). These include 1 King, 1
Queen, 2 Rooks, 2 Knights, 2 Bishops & 8 pawns. Each of these pieces move in a unique manner. The player
who chooses the white pieces gets the first turn to play. The players get alternate chances in which they can
move one piece at a time. The objective of this game is to remove the opponent9s king from the game. The
opponent9s King has to be placed in such a situation where the king is under immediate attack & there is no
wayto save it from the attack. This is known as Checkmate.
For a problem playing chess the starting position can be described as an 8 X 8 array where each position
contains a symbol standing for appropriate piece in the official chess opening position. We can define our goal
as any board position in which the opponent does not have a legal move & his/her king is under attack. The
legal moves provide the way of getting from initial state to goal state. They can be described easily as a set of
rules consisting of 2 parts
 A left side that serves as a pattern to be matched against the current board position
 A right side that describes the change to be made to the board position to reflect the move

Fig: One Legal Chess Move


Note: There will be several number of Rules.

Fig: Another way to Describe Chess Moves

7
Control Strategies:

Control strategy is one of the most important components of problem solving that describes the order of
application of the rules to the current state. Control strategy should be such that it causes motion towards a
solution. The second requirement of control strategy is that it should explore the solution space in a systematic
manner.

Depth-First & Breadth-First are systematic control strategies. There are 2 directions in which a search
could proceed
 Data-Driven Search, called Forward Chaining, from the Start State
 Goal-Driven Search, called Backward Chaining, from the Goal State

Forward Chaining: The process of forward chaining begins with known facts & works towards a solution. For
example, in 8-puzzle problem, we start from the start state & work forward to the goal state. In this case, we
begin building a tree of move sequences with the root of the tree as the start state. The states of next level of the
tree are generated by finding all rules whose left sides match with root & use their right side to create the new
state. This process is continued until a configuration that matches the goal state is generated.
Backward Chaining: It is a goal directed strategy that begins with the goal state & continues working backward,
generating more sub-goals that must also be satisfied to satisfy main goal until we reach to start state. Prolog
(Programming in Logic) language uses this strategy.
In this case, we begin building a tree of move sequences with the goal state of the tree as the start state.
The states of next level of the tree are generated by finding all rules whose right sides match with goal state &
use their left side to create the new state. This process is continued until a configuration that matches the start
state is generated.
Note: We can use both Data-Driven & Goal-Driven strategies for problem solving, depending on the nature of
the problem.

8
SEARCH STRATEGIES

EXHAUSTIVE SEARCHES (OR) UNIFORMED SEARCHES/BLIND SEARCH


It does not have additional information about states beyond problem definition. Total search space is
involved for the solution.
 Breadth-First Search

 Depth-First Search
 Depth-First Iterative Deepening
 Bidirectional Search

1. Breadth-First Search (BFS):


 BFS is the most common search strategy for traversing a tree or graph. This algorithm searches
breadth wise in a tree or graph.
 BFS algorithm starts searching from the root node of the tree and expands all successor nodes 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. Algorithm:
1. Create a variable called NODE-LIST and set it to the initial state.
2. Loop until the goal state is found or NODE-LIST is empty.
a. Remove the first element, say E, from the NODE-LIST. If NODE-LIST was empty then quit.
b. For each way that each rule can match the state described in E do:
i. Apply the rule to generate a new state.
ii. If the new state is the goal state, quit and return this state.
iii. Otherwise add this state to the end of NODE-LIST

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:
 BFS requires lots of memory since each level of the tree must be saved into memory to expand the next

9
level.
 BFS needs lots of time if the solution is far away from the root

node. Example 1: S---> A--->B---->C--->D---->G--->H--->E---->F---->I

--- >K.

Example 3: BFS for Water Jug Problem

Example 4: 8-Puzzle Problem

10
2. Depth-First Search (DFS):
 DFS 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.
 DFS uses a stack data structure for its

implementation. Algorithm:
1. If the initial state is a goal state, quit and return success.
2. Otherwise, loop until success or failure is signaled.
a) Generate a state, say E, and let it be the successor of the initial state. If there are no more
successors, signal failure.
b) Call Depth-First Search with E as the initial state.
c) If success is returned, signal success. Otherwise continue in this loop.

Advantages:
 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).

11
Disadvantages:
 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. Example 1:

Note: 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.

Example 3: Water Jug Problem

12
Example 4: 8- Puzzle Problem

Example 5: 8- Puzzle Problem

3. Depth-First Iterative Deeping (DFID):


 DFID is a combination of DFS and BFS algorithms. This search algorithm finds out the best depth
limit and does it by gradually increasing the limit until a goal is found.
 This algorithm performs depth-first search up to a certain "depth limit", and it keeps increasing the
depth limit after each iteration until the goal node is found.
 This Search algorithm combines the benefits of Breadth-first search's fast search and depth-first
search's memory efficiency.
 The iterative search algorithm is useful uninformed search when search space is large, and depth of
goal node is unknown.

13
Example:

Iteration 1: A

Iteration 2: A, B, C

Iteration 3: A, B, D, E, C, F, G

Iteration 4: A, B, D, H, I, E, C, F, K, G

14
Advantages:
 It combines the benefits of BFS and DFS search algorithm in terms of fast search and memory efficiency.

Disadvantages:
 Repeats all the work of the previous phase.

4. Bidirectional Search:
Bidirectional search is a graph search algorithm that runs 2 simultaneous searches. One search moves
forward from the start state & other moves backward from the goal state & stops when the two meet in the
middle. It is useful for those problems which have a single start state & single goal state.

Advantages:
 Bidirectional search is fast.
 Bidirectional search requires less
memory Disadvantages:
 Implementation of the bidirectional search tree is difficult.
 In bidirectional search, one should know the goal state in

advance. Example:

Fig: Graph to be Searched using Bidirectional Search

If match is found, then path can be traced from start to the matched state & from matched to the goal
state. It should be noted that each node has link to its successors as well as to its parent. These links will be
generating complete path from start to goal states.

15
The trace of finding path from node 1 to 16 using Bidirectional Search is as given below. The Path
obtained is 1, 2, 6, 11, 14, 16.

5. Analysis of Search Methods:

Time Complexity: Time required by an algorithm to find a solution.


Space Complexity: Space required by an algorithm to find a solution.

Search Technique Time Space


BFS O( bd ) O( bd )
DFS O( bd ) O(d)
DFID O( bd ) O(d)
Bidirectional O( bd/2 ) O( bd/2 )

Table: Performance Comparison


16
HEURISTIC SEARCH TECHNIQUES

Heuristic: It is helpful in improving the efficiency of search process.


 Generate & Search
 Branch & Bound Search (Uniformed Cost Search)
 Hill Climbing
 Best-First Search (A* Algorithm)

Generate & Test:


The Generate and test strategy is the simplest of all approaches. This method generates a solution for the given
problem and tests the generated solution with the required solution.
Algorithm:

Start
 Generate a possible solution
 Test if it is a goal
 If not go to start else
quit End

Advantage:
 Guarantee in finding a solution if a solution really exists.

Disadvantage:
 Not suitable for the larger problems

Branch & Bound Search (Uniform Cost Search):


Uniform-cost search is a searching algorithm used for traversing a weighted tree or graph. This
algorithm comes into play when a different cost is available for each edge. The primary goal of the uniform-
costsearch is to find a path to the goal node which has the lowest cumulative cost. Uniform-cost search expands
nodes according to their path costs from the root node. It can be used to solve any graph/tree where the optimal

17
cost is in demand.

A uniform-cost search algorithm is implemented by the priority queue. It gives maximumpriority to the
lowest cumulative cost.
Example:

Advantage:
 Uniform cost search is optimal because at every state the path with the least cost is chosen.

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

Hill Climbing:
 Simple Hill Climbing
 Steepest-Ascent Hill Climbing (Gradient Search)

Simple Hill Climbing:


Simple hill climbing is the simplest way to implement a hill climbing algorithm. It only evaluates the
neighbor node state at a time and selects the first one which optimizes current cost and set it as a current state. It
only checks it's one successor state, and if it finds better than the current state, then move else be in the same
state. There is no backtracking process.

Algorithm:
Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
Step 2: Loop Until a solution is found or there is no new operator left to apply.

18
Step 3: Select and apply an operator to the current state.
Step 4: Check new state:
 If it is goal state, then return success and quit.
 Else if it is better than the current state then assign new state as a current state.
 Else if not better than the current state, then return to step2.
Step 5: Exit.

Steepest-Ascent Hill Climbing (Gradient Search):


The steepest-Ascent algorithm is a variation of simple hill climbing algorithm. This algorithm examines
all the neighboring nodes of the current state and selects one neighbor node which is closest to the goal state.
This algorithm consumes more time as it searches for multiple neighbors.

Algorithm:
Step 1: Evaluate the initial state, if it is goal state then return success and stop, else make current state as initial
state.
Step 2: Loop until a solution is found or the current state does not change.
a) Let SUCC be a state such that any successor of the current state will be better than it.
b) For each operator that applies to the current state:
 Apply the new operator and generate a new state.
 Evaluate the new state.
 If it is goal state, then return it and quit, else compare it to the SUCC.
 If it is better than SUCC, then set new state as SUCC.
 If the SUCC is better than the current state, then set current state to
SUCC. Step 5: Exit.

19
Disadvantages of Hill Climbing:

1. Local Maximum: It is a state that is better than all its neighbours but not better than some other states which
are far away. From this state all moves looks to be worse. In such situation backtrack to some earlier state &
try going in different direction to find a solution.

2. Plateau: It is a flat area of the search space where all neighboring states has the same value. It is not possible
to determine the best direction. In such situation make a big jump to some direction & try to get to new
section of the search space.

3. Ridge: It is an area of search space that is higher than surrounding areas but that cannot be traversed by
single moves in any one direction. It is a special kind of Local Maxima.

20
Best-First Search:

It is a way of combining the advantages of both Depth-First and Breadth-First Search into a single
method. At each step of the best-first search process, we select the most promising of the nodes we have
generated so far. This is done by applying an appropriate heuristic function to each of them.
In some cases we have so many options to solve but only any one of them must be solved. In AI this can
be represented as OR graphs. In this among all available sub problems either of them must be solved. Hence the
name OR graph.

To implement such a graph-search procedure, we will need to use two lists of nodes.

 OPEN: This list contains all the nodes those have been generated and have had the heuristic function
applied to them but which have not yet been examined. OPEN is actually a priority queue in which the
elements with the highest priority are those with the most promising value of the heuristic function.
 CLOSED: This list contains all the nodes that have already been examined. We need to keep these
nodes in memory if we want to search a graph rather than a tree.

Algorithm:
1. Start with OPEN containing just the initial state
2. Until a goal is found or there are no nodes left on OPEN do:
a) Pick the best node on OPEN
b) Generate its successors
c) For each successor do:
i. If it has not been generated before, evaluate it, add it to OPEN, and record its parent.
ii. If it has been generated before, change the parent if this new path is better than the
previous one. In that case, update the cost of getting to this node and to any successors
that this node may already have.
Example:

21
Fig: A Best-First Search

Step 1: At this level we have only one node, i.e., initial node A

Step 2: Now we generate the successors A, three new nodes are generated namely B, C, and D with the costs of
3, 5 and 1 respectively. So these nodes are added to the OPEN list and A can be shifted to CLOSED list since it
is processed.

Among these three nodes D is having the least cost, and hence selected for expansion. So this node is shifted to
CLOSED list.

Step 3: At this stage the node D is expanded generating the new nodes E and F with the costs 4 and 6
respectively. The newly generated nodes will be added to the OPEN list. And node D will be added to
CLOSEDlist.

22
Step 4: At this stage node B is expanded generating the new nodes G & H with costs 6 and 5 respectively. The
newly generated nodes will be added to the OPEN list. And node B will be added to CLOSED list.

Step 5: this stage node E is expanded generating the new nodes I & J with costs 2 and 1 respectively. The newly
generated nodes will be added to the OPEN list. And node E will be added to CLOSED list.

A* Algorithm:
A* is a Best First Search algorithm that finds the least cost path from initial node to one goal node (out of
one or more possible goals)

f l(x) = g(x) + hl(x)

Where, f l = estimate of cost from initial state to goal state, along the path that generated current
node. g = measure of cost getting from initial state to current node.
hl = estimate of the additional cost of getting from current node to a goal state.

23
Example 1:

Example 2: Assuming all the values (arcs) as 819

Optimal Solution by A* Algorithm:


 Underestimation
 Overestimation
Underestimation: From the below start node A is expanded to B, C & D with f values as 4, 5 & 6 respectively.
Here, we are assuming that the cost of all arcs is 1 for the sake of simplicity. Note that node B has minimum f
value, so expand this node to E which has f value as 5. Since f value of C is also 5, we resolve in favour of E,
the path currently we are expanding. Now node E is expanded to node F with f value as 6. Clearly, expansion

24
of

25
a node F is stopped as f value of C is not the smallest. Thus, we see that by underestimating heuristic value, we
have wasted some effort by eventually discovered that B was farther away than we thought. Now we go back
& try another path & will find the optimal path.

Overestimation: Here we are overestimating heuristic value of each node in the graph/tree. We expand B to E, E
to F & F to G for a solution path of length 4. But assume that there is direct path from D to a solution giving a
path of length 2 as h value of D is also overestimated. We will never find it because of overestimating h(D). We
may find some other worse solution without ever expanding D. So, by overestimating h, we cannot be
guaranteed to find the shortest path.

Admissibility of A*:
A search algorithm is admissible, if for any graph, it always terminates in an optional path from start
state to goal state, if path exists. We have seen earlier that if heuristic function 8h9 underestimates the actual
value from current state to goal state, then it bounds to give an optimal solution & hence is called
admissible
26
function. So, we can say that A* always terminates with the optimal path in case h is an admissible heuristic
function.

ITERATIVE-DEEPING A*
IDA* is a combination of the DFID & A* algorithm. Here the successive iterations are corresponding to
increasing values of the total cost of a path rather than increasing depth of the search. Algorithm works as
follows:

 For each iteration, perform a DFS pruning off a branch when its total cost (g+h) exceeds a given
threshold.
 The initial threshold starts at the estimate cost of the start state & increases for each iteration of the
algorithm.
 The threshold used for the next iteration is the minimum cost of all values exceeded the current
threshold.
 These steps are repeated till we find a goal state.

Let us consider as example to illustrate the working IDA* Algorithm as shown below. Initially, the
threshold value is the estimated cost of the start node. In the first iteration, threshold=5. Now we generate all the
successors of start node & compute their estimated values as 6, 8, 4, 8 & 9. The successors having values
greater than 5 are to be pruned. Now for next iteration, we consider the threshold to be the minimum of the
pruned nodes value, that is, threshold=6 & the node with 6 value along with node with value 4 are retained for
further expansion.

Advantages:
 Simpler to implement over A* is that it does not use Open & Closed lists
 Finds solution of least cost or optimal solution
 Uses less space than A*

27
Fig: Working of IDA*

CONSTRAINT SATISFACTION
Many problems in AI can be viewed as problems of constraint satisfaction in which the goal is to
discover some problem state that satisfies a given set of constraints instead of finding a optimal path to the
solution. Such problems are called Constraint Satisfaction (CS) problems. Constraint satisfaction is a two step
process.
 First, constraints are discovered and propagated as far as possible throughout the system. Then, if there
is still not a solution then the search begins. A guess about something is made and added as a new
constraint
 Propagation then occurs with this new constraint.

28
Start State: all the variables are unassigned.
Goal State: all the variables are assigned which satisfy constraints. Rules:
 Values of variables should be from 0 to 9
 No 2 variables have same value

Algorithm:
1. Propagate available constraints. To do this, first set OPEN to the set of all objects that must have values
assigned to them in a complete solution. Then do until an inconsistency is detected or until OPEN is empty:
a) Select an object OB from OPEN. Strengthen as much as possible the set of constraints that apply to OB.
b) If this set is different from the set that was assigned the last time OB was examined or if this is the first
time OB has been examined, then add to OPEN all objects that share any constraints with OB
c) Remove OB from OPEN.
2. If the union of the constraints discovered above defines a solution, then quit and report the solution.
3. If the union of the constraints discovered above defines a contradiction, then return failure.
4. If neither of the above occurs, then it is necessary to make a guess at in order to proceed. To do this, loop
until a solution is found or all possible solutions have been eliminated
a) Select an object whose value is not yet determined and select a way of strengthening the constraints on
that object.
b) Recursively invoke constrain satisfaction with the current set of constraints augmented by the
strengthening constraint just selected.

Example:

Let us consider M=1, because by adding any 2 single digit number, at maximum we get one as carry.

29
Assume that S=8 (or) 9, S + M = 0 (or) S + M + C3 = 0
If S = 9, S + M = 9 + 1 =10 (with no carry)
If S = 8, S + M + C3 = 8 + 1 + 1 = 10 (with carry). So, we get O value as 0.
Therefore, M = 1, S = 9 & O = 0.

So, here E + 0 = N. Then E =N (It is not possible because no 2 variables should have same value).
E + O + c2 = N
E + 0 + 1 = N which gives E + 1 = N
Estimate E value from the remaining possible numbers i.e.., 2, 3, 4, 5, 6, 7, 8. So, from our estimation the E &
N values satisfies at E = 5. So, E + 1 = 5 + 1 = 6 i.e.., N = 6 (E + 1 = N)
Therefore, M = 1, S = 9, O = 0, E = 5 & N = 6.

So, here N + R + C1 = E. We already know that E = 5. So, the E value is satisfied by taking R = 8.
6 + 8 + 1 = 15.
Therefore, M = 1, S = 9, O = 0, E = 5, N = 6 & R = 8

Here, D + E = Y. It has to satisfy C1 = 1, so that D = 7.


Then, 7 + 5 = 12. So, Y = 2.
Final Values are M = 1, S = 9, O = 0, E = 5, N = 6, R = 8, D = 7 & Y = 2.

30
By substituting all the above values,

Some other Examples of Constraint Satisfaction are as below,

Example 2:

Example 3:

Example 4:

Example 5:

Example 6:

PROBLEM REDUCTION
An effective way of solving a complex problem is to reduce it to simpler parts & solve each part
separately. Problem Reduction enables us to obtain an elegant solution to the problem. The structure called
AND-OR graph (or tree) is useful for representing the solution of complicated problems. The decomposition of

31
a complex problem generates arcs which we call AND arcs. One AND arc may point to any number of
successors, all of which must be solved.

PROBLEM REDUCTION (AND-OR Graph) (or) AO* ALGORITHM


In real-world applications, complicated problems can be divided into simpler sub-problems. The
solution of each sub-problem may then be combined to obtain the final solution. A given problem may be
solved in a number of ways. An AND-OR Graph provides a simple representation of a complex problem that
leads to better understanding. For instance, if you wish to own a cellular phone then it may be possible that
either someone gifts one to you or you earn money & buy one for yourself. The AND-OR graph which depicts
these possibilities is as shown below

Fig: A simple AND-OR Graph

Let us consider a problem known as Towers of Hanoi to illustrate the need of problem reduction
concept. The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods and a number of disks
of different sizes, which can slide onto any rod. The puzzle starts with the disks being stacked in descending
order of their sizes, with the largest at the bottom of the stack & the smallest at the top thus making a conical
shape. The objective of the puzzle is to move the entire stack to another rod by using the following rules:

 Only one disk can be moved at a time.


 Each move consists of taking the uppermost disk from one of the rods and sliding it onto another rod,
on top of the other disks that may already be present on that rod.
 No larger disk may be placed on top of a smaller disk.

With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a Tower
of Hanoi puzzle is 2n − 1, where n is the number of disks which is as follows

32
Consider an AND-OR graph where each arc with a single successor has a cost of 1. Let us assume that
the numbers listed in parenthesis ( ) denote the estimated costs, while the numbers in the square brackets [ ]
represent the revised costs of the path. Thick lines indicate paths from a given node.

Let us begin search from start node A & compute the heuristic values for each of its successors, say B &
(C,D) as 19 and (8,9) respectively. The estimated cost of paths from A to B is 20 (19 + cost of one arc from A to
B) & that from A to (C,D) is 19 (8 + 9 + cost of 2 arcs, A to C & A to D). The path from A to (C,D) seems to be
better than that from A to B. So, we expand this AND path by extending C to (G,H) and D to (I,J). Now,
heuristic values of G, H, I & J are 3, 4, 8 & 7 respectively which lead to revised costs of C & D as 9 & 17
respectively. These values are then propagated up & the revised costs of path from A to (C,D) is calculated as28
(9 + 17+ Cost of arc A to C & A to D).
Note that the revised cost of this path is now 28 instead of the earlier estimation of 19. Thus, this path is
no longer the best path now. Therefore, choose the path from A to B for expansion. After expansion we see that
the heuristic value of node B is 17 thus making the cost of the path from A to B equal to 18. This path is the
best so far. Therefore, we further explore the path from A to B. The process continues until either a solution is
found (or) all paths lead to dead ends, indicating that there is no solution.
33
Algorithm for AND-OR Graphs:

1. Initialize graph with start node.


2. While (start node is not labeled as solved or unsolved through all paths)
{
 Traverse the graph along the best path & expand all the unexpanded nodes on it.
 If node is terminal & heuristic value of the node is 0, label it as solved else label it as unsolved &
propagate the status up to the start node.
 If node is non terminal, add its successors with the heuristic values in the graph.
 Revise the cost of the expanded node & propagate this change along the path till the start node.
 Choose the current best path.
}
3. If (start node = solved), the leaf nodes of the best path from root are the solution nodes, else no solution
exists.
4. Stop.

34
Game Playing- Adversial search, Games, mini-max algorithm, optimal decisions in multiplayer
games, Problem in Game playing, Alpha-Beta pruning, Evaluation functions.

Game Playing
Game Playing is an important domain of artificial intelligence. Games require the only
knowledge we need to provide is the rules, legal moves and the conditions of winning or losing
the game. Both players try to win the game. So, both of them try to make the best move possible
at each turn. Searching techniques like BFS(Breadth First Search) are not accurate for this as the
branching factor is very high, so searching will take a lot of time. So, we need other search
procedures that improve
• Generate procedure so that only good moves are generated.
• Test procedure so that the best move can be explored first.
The most common search technique in game playing is Minimax search procedure. It is
depthfirst depth-limited search procedure. It is used for games like chess and tic-tac-toe.

Adversial search
Adversarial search is a search, where we examine the problem which arises when we try to plan
ahead of the world and other agents are planning against us.
o We have studied the search strategies which are only associated with a single agent that
aims to find the solution which often expressed in the form of a sequence of actions.
o But, there might be some situations where more than one agent is searching for the
solution in the same search space, and this situation usually occurs in game playing.
o The environment with more than one agent is termed as multi-agent environment, in
which each agent is an opponent of other agent and playing against each other. Each
agent needs to consider the action of other agent and effect of that action on their
performance.
o So, Searches in which two or more players with conflicting goals are trying to
explore the same search space for the solution, are called adversarial searches, often
known as Games.
o Games are modeled as a Search problem and heuristic evaluation function, and these are
the two main factors which help to model and solve games in AI.
Types of Games:

Deterministic Chance Moves

Perfect information Chess, Checkers, go, Othello Backgammon, monopoly

Imperfect information Battleships, blind, tic-tac-toe Bridge, poker, scrabble, nuclear war

o Perfect information: A game with the perfect information is that in which agents can
look into the complete board. Agents have all the information about the game, and they
can see each other moves also. Examples are Chess, Checkers, Go, etc.
o Imperfect information: If in a game agents do not have all information about the game
and not aware with what's going on, such type of games are called the game with
imperfect information, such as tic-tac-toe, Battleship, blind, Bridge, etc.
o Deterministic games: Deterministic games are those games which follow a strict pattern
and set of rules for the games, and there is no randomness associated with them.
Examples are chess, Checkers, Go, tic-tac-toe, etc.
o Non-deterministic games: Non-deterministic are those games which have various
unpredictable events and has a factor of chance or luck. This factor of chance or luck is
introduced by either dice or cards. These are random, and each action response is not
fixed. Such games are also called as stochastic games.
Example: Backgammon, Monopoly, Poker, etc.

Zero-Sum Game
o Zero-sum games are adversarial search which involves pure competition.
o In Zero-sum game each agent's gain or loss of utility is exactly balanced by the losses or
gains of utility of another agent.
o One player of the game tries to maximize one single value, while other player tries to
minimize it.
o Each move by one player in the game is called as ply.
o Chess and tic-tac-toe are examples of a Zero-sum game.
Mini-Max Algorithm
The minimax algorithm is a fundamental technique in adversarial search, specifically
designed for making optimal decisions in competitive, two-player, zero-sum games. In such
games, the success of one player is directly tied to the failure of the other, meaning their goals
are in direct conflict. The minimax algorithm helps a player maximize their chances of winning
or minimizing their chances of losing by considering the best possible moves and their
outcomes, given that the opponent will make the moves most detrimental to the first player.
Concepts of Maximizing and Minimizing Players:
Maximizing Player (Max): This is the player who aims to maximize their own utility or score. In a
game, the maximizing player seeks moves that will lead to the highest possible score. For
instance, in chess, the maximizing player would want to make moves that increase their chances
of winning.
Minimizing Player (Min): The minimizing player aims to minimize the maximizing player's utility
or score. They act as adversaries, making moves to counter the maximizing player's strategies
and reduce their chances of success. In chess, the minimizing player tries to thwart the
maximizing player's winning chances.
The min max search procedure is a depth first, depth limited search procedure. The idea
is to start at the current position and use the possible move to generate the set of possible
successor positions. To decide one move, it explores the possibilities of winning by looking
ahead to more than one step. This is called a ply. Thus in a two ply search, to decide the current
move, game tree would be explored two levels farther.

Minimax algorithm uses two functions –


MOVEGEN: It generates all the possible moves that can be generated from the current
position.
STATICEVALUATION: It returns a value depending upon the goodness from the viewpoint of
two-player
This algorithm is a two player game, so we call the first player as PLAYER1 and second player as
PLAYER2. The value of each node is backed-up from its children. For PLAYER1 the backed-
up value is the maximum value of its children and for PLAYER2 the backed-up value is the
minimum value of its children. It provides most promising move to PLAYER1, assuming
that the PLAYER2 has make the best move. It is a recursive algorithm, as same procedure
occurs at each level.

Figure 1: Before backing-up of values

4 levels are generated. The value to nodes H, I, J, K, L, M, N, O is provided by


STATICEVALUATION function. Level 3 is maximizing level, so all nodes of level 3 will take
maximum values of their children. Level 2 is minimizing level, so all its nodes will take
minimum values of their children. This process continues. The value of A is 23. That means A
should choose C move to win.
MIN MAX (CURRENT, 0, PLAYER-ONE)
(If player is to move)
Or
MIN MAX (CURRENT, 0, PLAYER-TWO)
(If player two is to move)
Let us follow the algorithm of MIN MAX
Algorithm: MINMAX (position, depth, player)
1. If LAST PLY (position, depth)
Then RETURN VALUE = STATIC (position, player)
PATH = nil.
2. Else, generate one more ply of the tree by calling the function MOVE_GEN
(position, player) and set SUCCESORS to the list it returns.
3. If SUCESSORS is empty,
THEN no moves to be
made
RETURN the same structure that would have been returned if LAST_PLY had
returned TRUE.
4. If SUCCESORS is not empty,
THEN examine each element in turn and keep track of the best one.
5. After examining all the nodes,
RETURN VALUE = BEST- SCORE
PATH = BEST- PATH
When the initial call to MIN MAX returns, the best move from CURRENT is the first element in
the PATH.

Optimal Decisions in Multiplayer Games


The optimal solution would be a sequence of actions leading to a goal state-a terminal state that
is a win. In adversarial search, MIN has something to say about it. MAX therefore must find a
contingent strategy, which specifies MAX's move in the initial state, then MAX's moves in the
states resulting from every possible response by
MIN, then MAX's moves in the states resulting from every possible response by MIN to
those moves, and so on. an optimal strategy leads to outcomes at least as good as any other
strategy when one is playing an infallible opponent. We begin by showing how to find this
optimal strategy.
Even a simple game like tic-tac-toe is too complex for us to draw the entire game tree on
one page. The possible moves for MAX at the root node are labeled a1, a2, and a3. The possible
replies to a1 for MIN are b1, l>2, bs, and so on. This particular game ends after one move each
by MAX and MIN. (In game parlance, we say that this tree is one move deep, consisting of two
half-moves, each of which is called a ply.) The utilities of the terminal states in this game range
from 2 to 14.
Given a game tree, the optimal strategy can he determined from the minimax value of
each node, which we write as MINIMAX(n). The minimax value of a node is the utility (for
MAX) of being in the corresponding state, assuming that both players play optimally from there
to the end of the game. Obviously, the minimax value of a terminal state is just its utility.
Furthermore, given a choice, MAX prefers to move to a state of maximum value, whereas MIN
prefers a state of minimwn value. So we have the following:
The terminal nodes on the bottom level get their utility values from the game's UTILITY
function. The first MIN node, labeled B, has three successor states with values 3, 12, and 8, so
its minimax value is 3. Similarly, the other two MIN nodes have minimax value 2. The root node
is a MAX node; its successor states have minimax values 3, 2, and 2; so it has a minimax value
of
3. We can also identify the minimax decision at the root: action a1 is the optimal choice for
MAX because it leads to the state with the highest minimax value.

Problem in Game playing


Game playing in artificial intelligence (AI) presents several challenges and problems due to the
complexity and variability of games.
1. Exponential Growth of the Game Tree
• State Space Explosion: As the number of possible moves increases, the game tree grows
exponentially. This makes it computationally infeasible to explore all possible states,
particularly in complex games.
• Depth Limitation: To manage the computational load, algorithms often limit the depth
of the search tree, which can affect the quality of the decisions made.
2. Real-Time Constraints
• Speed Requirements: Many games require decisions to be made within strict time
limits. Balancing the depth of the search with the need for quick responses is a significant
challenge.
• Computational Resources: Real-time constraints often limit the amount of
computational resources available, affecting the algorithm's ability to explore the game
tree thoroughly.
3. Incompleteness of Information
• Imperfect Information: In games like Poker or Battleship, players do not have complete
knowledge of the game state, which complicates decision-making and strategy
development.
• Hidden Information: Handling uncertainty and making predictions about opponents'
hidden cards or moves adds complexity to game-playing AI.
4. Multi-Agent Dynamics
• Multiple Players: In multiplayer games, strategies are not just about opposing a single
opponent but interacting with multiple players whose strategies can be highly variable.
5. Opponent Modeling
• Predicting Opponent Moves: Accurate prediction of opponents' strategies and moves is
crucial but difficult, especially when opponents use deceptive or unpredictable tactics.
• Adaptation: Adapting strategies in response to opponents’ changes and learning from
their behavior over time adds to the complexity.
6. Complex Evaluation Functions
• Designing Heuristics: Crafting effective evaluation functions that accurately assess the
desirability of game states is challenging, especially in complex games.
• Balancing Factors: The evaluation function must balance various factors (e.g., material
advantage, positional strength) and accurately reflect the game’s goals.
7. Handling Uncertainty
• Stochastic Elements: Games with random elements (e.g., dice rolls) introduce uncertainty
that makes it difficult to predict outcomes and devise optimal strategies.
• Probabilistic Reasoning: Incorporating probabilistic reasoning and managing risk
becomes essential but complex.
8. Complexity of Game Rules
• Game Variability: Different games have different rules, and AI needs to be tailored for
each game’s specific dynamics and strategies.
• Rule Complexity: Some games have intricate rules and interactions that require
sophisticated algorithms to handle effectively.
9. Ethical and Fair Play Concerns
• Exploitation of Game Mechanics: Ensuring that AI does not exploit unintended flaws
or loopholes in the game is important for maintaining fair play.
• Ethical Use: Using AI in games should adhere to ethical standards, avoiding unfair
advantages and ensuring it respects the spirit of the game.
10. Adaptation and Learning
• Reinforcement Learning: While reinforcement learning can help AI improve through
experience, it requires significant computational resources and time to converge to
effective strategies.
• Transfer Learning: Adapting strategies learned in one game or scenario to different
games or contexts is challenging but can be highly beneficial.

Alpha- Beta (α-β) Pruning


` When a number of states of a game increase and it cannot be predicted about the states, then we
can use the method pruning. Pruning is a method which is used to reduce the no. of states in a
game. Alpha- beta is one such pruning technique. The problem with minmax search is that the
number of game states it has to examine is exponential in the number of moves. Unfortunately
we cannot eliminate the exponent, but we can effectively cut it in half. Alpha-beta pruning is one
of the solutions to the problem of minmax search tree. When α-β pruning is applied to a standard
minmax tree, it returns the same move as minmax would, but prunes away branches that cannot
possibly influence the final decision
The idea of alpha beta pruning is very simple. Alpha beta search proceeds in a depth first
fashion rather than searching the entire space. Generally two values, called alpha and beta, are
created during the search. The alpha value is associated with MAX nodes and the beta value is
with MIN values. The value of alpha can never decrease; on the other hand the value of beta
never increases. Suppose the alpha value of A MAX node is 5. The MAX node then need not
consider any transmitted value less than or equal to 5 which is associated with any MIN node
below it. Similarly, if a MIN has a beta value of 5, it need not further consider any MAX node
below it that has a value of 6 or more.
The general principal is that: Consider a node η somewhere in the search tree, such that player has
a choice of moving to that node. If player has a better choice К either at the parent node of η or
at any choice point further up, then η will never be reached in actual play. So once we have
found out enough about η (by examining some of its descendents) to reach this conclusion, we
can prune it. Consider the following example
Here at MIN ply, the best value from three nodes is - 4, 5, 0. These will be back
propagated towards root and a maximizing move 5 will be taken. Now the node E has the value
8 is far more, then accepted as it is minimizing ply. So, further node E will not be explored. In
the situation when more plies are considered, whole sub tree below E will be pruned. Similarly if
α=0, β=7, all the nodes and related sub trees having value less than 0 at maximizing ply and
more than 7 at minimizing ply will be pruned.
Alpha beta search updates the value of α and β as it goes along and prunes the remaining
branches at a node as soon as the value of the current node is known to be worse than the current
α and β value for MAX or MIN respectively.

Evaluation Functions
In 1950, Shannon proposed that programs should cut off the search earlier to apply a
heuristic evaluation function to states in the search. This technique effectively turns non-terminal
nodes into terminal leaves. The idea is to replace the utility function by a heuristic evaluation
function (EVAL), which estimates the position’s utility, and replace the terminal test by a cutoff
test that decides when to apply EVAL An evaluation function returns an estimate of the expected
utility of the game from a give position, just as heuristic functions return an estimate of the
distance to the goal.
The performance of a game-playing program depends strongly on the quality of its
evaluation function – but how to design?
(1) The evaluation function should order the terminal states in the same way as the true
utility function: states that are wins must evaluate better than draws, etc.
(2) The computation must not take too long!
(3) For nonterminal states, the evaluation function should be strongly correlated with the
actual “chances of winning.”
This function is often known as Evaluation Function. It is sometimes also called a
Heuristic Function. Note that if the search must be cut off at nonterminal states, then the
algorithm will necessarily be uncertain about the final outcomes of those states.
Usually, an evaluation function calculates various features of a state. The features define
various categories or equivalence classes of states: the states in each category have the same
values for all features. Most evaluation functions compute separate numerical contributions from
each feature and then combine them to find the total value.
Typical evaluation function is a linear sum of
features Eval(s) = w1 f 1 (s) + w2 f 2 (s) + … + wn fn (s)
w1 = 9
f 1 (s) = number of white queens – number of black queens etc.
Game tree
Evaluation function

• Evaluation function f(n) measures “goodness” of board configuration n. Assumed to be


better estimate as search is deepened (i.e., at lower levels of game tree).
• Evaluation function here: “Number of possible wins (rows, columns, diagonals) not
blocked by opponent, minus number of possible wins for opponent not blocked by
current player.”
MiniMax Search

You might also like