Unit II Notes
Unit II Notes
UNIT II
PROBLEM-SOLVING AGENTS
One kind of goal-based agent is called a problem-solving agent. Problem-solving agents use atomic
representations, where the states of the world are considered as wholes, with no internal structure
visible to the problem solving algorithms.
Step 1: Goal Formulation: It is the first and simplest step in problem-solving. It organizes
the steps/sequence required to formulate one goal out of multiple goals as well as actions to
achieve that goal. Goal formulation is based on the current situation and the agent's
performance measure (discussed below).
• Initial State: It is the starting state or initial step of the agent towards its goal.
• Actions: It is the description of the possible actions available to the agent.
• Transition Model: It describes what each action does.
• Goal Test: It determines if the given state is a goal state.
• Path cost: It assigns a numeric cost to each path that follows the goal. The
problem-solving agent selects a cost function, which reflects its performance measure.
Remember, an optimal solution has the lowest path cost among all the solutions.
Note: Initial state, actions, and transition model together define the state-space of the
problem implicitly. State-space of a problem is a set of all states which can be reached from
the initial state followed by any sequence of actions. The state-space forms a directed map or
graph where nodes are the states, links between the nodes are actions, and the path is a
sequence of states connected by the sequence of actions.
Step 3: Search: It identifies all the best possible sequence of actions to reach the goal state
from the current state. It takes a problem as an input and returns solution as its
output.
Step 4: Solution: It finds the best algorithm out of various algorithms, which may be proven
as the best optimal solution.
Step 5: Execution: It executes the best optimal solution from the searching algorithms to
1
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Example Problems
1. Toy Problem: It can also be called a puzzle-like problem which can be used as a
way to explain a more general problem-solving technique. It is a concise and
exact description of the problem which is used by the researchers to compare the
performance of algorithms. Vacuum cleaner world, 8 Puzzle Problem, Eight queens
puzzle are some examples of Toy problems in AI.
The first example we examine is the vacuum world. This can be formulated as a problem as follows:
States: The state is determined by both the agent location and the dirt locations. The agent is in one of
two locations, each of which might or might not contain dirt. Thus, there are 2 × 2 2= 8
Actions: In this simple environment, each state has just three actions: Left, Right, and Suck. Larger
Transition model: The actions have their expected effects, except that moving Left in the leftmost
square, moving Right in the rightmost square, and Sucking in a clean square have no effect.
Goal test: This checks whether all the squares are clean.
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
3
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Here, we have a 3x3 matrix with movable tiles numbered from 1 to 8 with a blank
space. The tile adjacent to the blank space can slide into that space. The objective is to
reach a specified goal state similar to the goal state, as shown in the below figure.
In the figure, our task is to convert the current state into goal state by sliding digits
into the blank space.
States: A state description specifies the location of each of the eight tiles and the blank in one of the
nine squares.
Initial state: Any state can be designated as the initial state. Note that any given goal can be reached
Actions: The simplest formulation defines the actions as movements of the blank space Left, Right,
Up, or Down. Different subsets of these are possible depending on where the blank is.
Transition model: Given a state and action, this returns the resulting state; for example, if we apply
Left to the start state in Figure 3.4, the resulting state has the 5 and the blank switched.
Goal test: This checks whether the state matches the goal configuration shown in Figure 3.4.
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
4
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
The problem here is to place eight chess queens on an 8*8 chessboard in a way that
no queens will threaten the other. If two queens will come in the same row,
column, or diagonal one will attack another.
Consider the Incremental formulation for this problem: It starts from an empty
state and the operator expands a queen at each step.
Following are the steps involved in this formulation:
Transition model: Returns the new state of the chessboard with the queen
added in a box.
Goal test: Checks whether 8-queens are kept on the chessboard without any
possibility of attack.
Path cost: Path costs are neglected because only final states are counted.
5
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
6
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
7
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
` It means that the strategies have no additional information about states beyond that
provided in the problem definition. It only knows how to traverse and how to distinguish
between a leaf node and goal node and hence it is operated in a brute-force way and so it is
also called brute-force algorithms. It searches every node without any prior knowledge and
is hence also called blind search algorithms.
8
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
1. BREADTH-FIRST SEARCH:
It is the most common searching strategy to traverse a tree or a graph. It uses a breadthwise
searching procedure and hence it is called breadth-first search. The process starts from the
root node of the tree and expands to all the successor nodes at the first level before moving to
the next level nodes. The queue data structure that works on the First In First Out (FIFO)
concept is used to implement this method. As it returns a solution (if it exists) it can be said
as a complete algorithm.
The searching procedure starts from the root node A. Here the goal node is G. from node A it
will traverse in an order of A-B-C-D-G. it will traverse level-wise.
9
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Advantages Disadvantages
2. DEPTH-FIRST SEARCH:
It is a process similar to that of BFS. The searching procedure starts from the root node and
follows each path to the greatest depth and then backtracks before entering the next path. The
stack data structure that works on the concept of Last In First Out (LIFO) is used to
implement the procedure.
Example:
10
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
The searching procedure starts from the root node A. Here the goal node is G. From node A it
will traverse in an order of A-B-D-G. it will traverse depth-wise.
Advantage Disadvantage
3. DEPTH-LIMITED SEARCH:
It is much similar to a depth-first search with a predetermined limit. The main disadvantage
of DFS is the infinite path, this can be solved with a Depth-limited search. Here the node at
the limit is treated like it has no successor nodes further. It can be called an extended and
refined version of DFS.
Standard failure value: Indicates that the given problem doesn’t have any solution
Cutoff failure value: indicates that there is no solution for the given problem within
the given limit.
Example :
11
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
The searching starts from node A. The limit for the procedure is set as l=2. Our goal state is
node G. The process is like A-B-D-E-C-F-G.
Advantage Disadvantage
12
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Example.
If the starting node is A and the goal to reach is G, then it will traverse A-C-G. The cost will
be 3
Advantage Disadvantage
13
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Optimal because the least cost path is May be stuck in an infinite loop
chosen. because the path cost is only
considered.
Example:
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,G
14
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Advantage Disadvantage
Memory efficiency
Example:
Advantage Disadvantage
15
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Definition:
An informed search strategy uses problem-specific knowledge beyond the definition of the
problem itself and it can find solutions more efficiently than an uninformed strategy.
Types:
– Greedy best-first search
– A* search
(i)Expand the node closest to the goal state using estimated cost as the evaluation function is
called greedy best first search.
(ii) Expand the node on the least cost solution path using estimated cost and actual cost as the
evaluation function is called A*search
16
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
algorithms. With the help of best-first search, at each step, we can choose the most promising
node. In the best first search algorithm, we expand the node which is closest to the goal node
and the closest cost is estimated by heuristic function, i.e.
f(n)= g(n)
Where, h(n)= estimated cost from node n to the goal.
The greedy best first algorithm is implemented by the priority queue. We use the straight-line
distance heuristic:
hSLD(n) = straight line distance from node n to goal node as evaluation function.
• Complete? No – can get stuck in loops, e.g., Iasi -> Neamt -> Iasi -> Neamt
• Time? O(b m), (in worst case) but a good heuristic can give dramatic improvement (m is
max depth of search space).
m
• Space? O(b ) -- keeps all nodes in memory.
• Optimal? No (not guaranteed to render lowest cost solution).
Advantages:
Best first search can switch between BFS and DFS by gaining the advantages of both
the algorithms.
This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
It can behave as an unguided depth-first search in the worst case scenario.
It can get stuck in a loop as DFS.
This algorithm is not optimal.
17
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Heuristic function : A good heuristic function for Path/ Route-finding problems is Straight-
Line Distance to the goal and it is denoted as hSLD(n).
Solution :
From the given graph and estimated cost, the goal state is identified as B from A. Apply the
evaluation function h(n) to find a path from A to B
18
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
From F, goal state B is reached. Therefore the path from A to B using greedy search is A - S -
F - B = 450 (i.e) (140 + 99 + 211)
19
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Solution :
From the given graph and estimated cost, the goal state is identified as B u c h a r e s t from
Arad. Apply the evaluation function h (n) to find a path from Arad to Bucharest.
20
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
21
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
22
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
23
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
The first node to be expanded from Arad will be Sibiu, because it is closer to Bucharest than
either Zerind or Timisoara.
The next node to be expanded will be Fagaras, because it is closest. Fagaras in turn generates
Bucharest, which is the goal.
For this particular problem, greedy best-first search using hSLD finds a solution without ever
expanding a node that is not on the solution path; hence, its search cost is minimal. It is not
optimal, however: the path via Sibiu and Fagaras to Bucharest is 32 kilometers longer than
the path through Rimnicu Vilcea and Pitesti. This shows why the algorithm is called
"greedy'-at each step it tries to get as close to the goal as it can.
24
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
A* SEARCH
A* search is the most commonly known form of best-first search. It uses heuristic function
h(n), and cost to reach the node n from the start state g(n). A* search algorithm finds the
shortest path through the search space using the heuristic function. This search algorithm
expands less search tree and provides optimal result faster. A* algorithm uses g(n)+h(n)
instead of g(n).
Properties of A*
• Complete: Yes (unless there are infinitely many nodes with f ≤ f(G) ).
• Time: Exponential.
• Space: Keeps all nodes in memory, so also exponential.
• Optimal: Yes (provided h admissible or consistent).
• Optimally Efficient: Yes (no algorithm with the same heuristic is guaranteed to expand
fewer nodes).
Advantages:
o A* search algorithm is the best algorithm than other search algorithms.
o A* search algorithm is optimal and complete.
o This algorithm can solve very complex problems.
Disadvantages:
o It does not always produce the shortest path as it mostly based on heuristics and
approximation.
25
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Solution:
From the given graph and estimated cost, the goal state is identified as B from A. Apply the
evaluation function to find a path from A to B.
26
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
27
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
From P, goal state B is reached. Therefore the path from A to B using A* search is A – S - R
- P -B : 418 (ie) {140 + 80 + 97 + 101), that the path cost is less than Greedy search path cost.
Time and space complexity: Time complexity depends on the heuristic function and the
admissible heuristic value. Space complexity remains in the exponential order.
28
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
29
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
30
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
31
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
32
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
33
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
34
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
35
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
HEURISTIC FUNCTIONS
Heuristic Functions:
An informed search makes use of heuristic functions in order to reach the goal node in a more
prominent way. Therefore, there are several pathways in a search tree to reach the goal node
from the current node. A good heuristic function is determined by its efficiency. More is the
information about the problem, more is the processing time.
Some toy problems, such as 8-puzzle, 8-queen, tic-tac-toe, etc., can be solved more
efficiently with the help of a heuristic function. Let’s see how:
Consider the following 8-puzzle problem where we have a start state and a goal state. Our
task is to slide the tiles of the current/start state and place it in an order followed in the goal
state. There can be four moves either left, right, up, or down. There can be several ways to
convert the current/start state to the goal state, but, we can use a heuristic function h(n) to
solve the problem more efficiently.
EXAMPLE 1:
Task : Find the shortest solution using heuristic function that never over estimates the
number of steps to the goal.
Solution : To perform the given task two candidates are required, which are named as h1 and
h2
All of the eight tiles are out of position in the above figure, so the start state would have
hl = 8.
hl is an admissible heuristic, because it is clear that any tile that is out of place must be moved
at least once.
36
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
h2 = the sum of the distances of the tiles from their goal positions.
Because tiles cannot move along diagonals, the distance we will count is the sum of the
horizontal and vertical distances. This is called as the city block distance or Manhattan
distance. h2 is also admissible, because any move can do is move one tile one step closer to
the goal. Tiles 1 to 8 in the start state give a Manhattan distance of
h2=3+1+2+2+2+3+3+2=18.
h1=7
h2 = 2 + 3 + 3 + 2 + 4 + 2 + 0 + 2 = 18
37
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
EXAMPLE 3
38
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
It is seen from the above state space tree that the goal state is minimized from h(n)=3 to
h(n)=0. However, we can create and use several heuristic functions as per the requirement. It
is also clear from the above example that a heuristic function h(n) can be defined as the
information required to solve a given problem more efficiently. The information can be
related to the nature of the state, cost of transforming from one state to another, goal
node characteristics, etc., which is expressed as a heuristic function.
39
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Local search algorithms keep track of a single current state and generally move only to
neighbors of that state.
In addition to finding goals, local search algorithms are useful for solving pure optimization
problems, in which the aim is to find the best state according to an objective function.
For example, in the 8-queens problem, what matters is the final configuration of queens, not
the order in which they are added.
The local search problem is explained with the state space land scape. A landscape has:
o Hill climbing algorithm is a local search algorithm which continuously moves in the
direction of increasing elevation/value to find the peak of the mountain or best solution to
40
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
the problem. It terminates when it reaches a peak value where no neighbor has a higher
value.
o Hill climbing algorithm is a technique which is used for optimizing the mathematical
problems. One of the widely discussed examples of Hill climbing algorithm is Traveling-
salesman Problem in which we need to minimize the distance traveled by the salesman.
o It is also called greedy local search as it only looks to its good immediate neighbor state and
not beyond that.
o A node of hill climbing algorithm has two components which are state and value.
o Hill Climbing is mostly used when a good heuristic is available.
o In this algorithm, we don't need to maintain and handle the search tree or graph as it only
keeps a single current state.
41
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Local Maximum: Local maximum is a state which is better than its neighbor states, but there
is also another state which is higher than it.
Global Maximum: Global maximum is the best possible state of state space landscape. It has
the highest value of objective function.
Flat local maximum: It is a flat space in the landscape where all the neighbor states of
current states have the same value.
It examines the neighboring nodes one by one and selects the first neighboring node which
optimizes the current cost as the next node.
42
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Step 1 : Evaluate the initial state. If it is a goal state then stop and return success. Otherwise,
make initial state as current state.
Step 2 : Loop until the solution state is found or there are no new operators present which can
be applied to the current state.
a) Select a state that has not been yet applied to the current state and apply it to produce a
new state.
b) Perform these to evaluate new state
i. If the current state is a goal state, then stop and return success.
ii. If it is better than the current state, then make it current state and proceed further.
iii. If it is not better than the current state, then continue in the loop until a solution is found.
Step 3 : Exit.
It first examines all the neighboring nodes and then selects the node closest to the solution
state as the next node.
Step 1 : Evaluate the initial state. If it is a goal state then stop and return success. Otherwise,
make initial state as current state.
Step 2 : Repeat these steps until a solution is found or current state does not change
a) Select a state that has not been yet applied to the current state.
b) Initialize a new ‘best state’ equal to current state and apply it to produce a new
state.
c) Perform these to evaluate new state
i. If the current state is a goal state, then stop and return success.
ii. If it is better than best state, then make it best state else continue loop with
another new state.
d) Make best state as current state and go to Step 2: b) part.
Step 3 : Exit
43
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
It does not examine all the neighboring nodes before deciding which node to select. It just
selects a neighboring node at random and decides (based on the amount of improvement in
that neighbor) whether to move to that neighbor or to examine another.
Step 1: Evaluate the initial state. If it is a goal state then stop and return success. Otherwise,
make the initial state the current state.
Step 2: Repeat these steps until a solution is found or the current state does not change.
a) Select a state that has not been yet applied to the current state.
b) Apply successor function to the current state and generate all the neighbor states.
c) Among the generated neighbor states which are better than the current state choose a
state randomly (or based on some probability function).
d) If the chosen state is the goal state, then return success, else make it the current state
and repeat step 2: b) part.
Step 3: Exit.
44
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
1. Local Maximum: A local maximum is a peak state in the landscape which is better than
each of its neighboring states, but there is another state also present which is higher than the
local maximum.
2. Plateau: A plateau is the flat area of the search space in which all the neighbor states of
the current state contains the same value, because of this algorithm does not find any best
direction to move. A hill-climbing search might be lost in the plateau area.
Solution: The solution for the plateau is to take big steps or very little steps while
searching, to solve the problem. Randomly select a state which is far away from the
current state so it is possible that the algorithm could find non-plateau region.
3. Ridges: A ridge is a special form of the local maximum. It has an area which is higher than
its surrounding areas, but itself has a slope, and cannot be reached in a single move.
45
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
1. Travelling salesman problem (also called the travelling salesperson problem or TSP
2. Marketing
3. Robotics
4. Job Scheduling
46
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
T decreases slowly enough then simulated annealing search will find a global optimum with
probability approaching one.
Applications
VLSI layout
Airline scheduling
Local beam search is a variation of beam search which is a path based algorithm.
It uses K states and generates successors for K states in parallel instead of one state
and its successors in sequence.
The useful information is passed among the K parallel threads.
The sequence of steps to perform local beam search is given below:
o Keep track of K states rather than just one.
o Start with K randomly generated states.
o At each iteration, all the successors of all K states are generated.
47
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
o If anyone is a goal state stop; else select the K best successors from the
complete list and repeat.
This search will suffer from lack of diversity among K states.
Therefore a variant named as stochastic beam search selects K successors at random,
with the probability of choosing a given successor being an increasing function of its
value.
4. GENETIC ALGORITHMS (GA)
A genetic algorithm (or GA) is a variant of stochastic beam search in which successor states
are generated by combining two parent states, rather than by modifying a single state.
A genetic algorithm is used to solve complicated problems with a greater number of variables
& possible outcomes/solutions. The combinations of different solutions are passed through
the algorithm to find the best solutions.
For Example an 8 queen’s state could be represented as 8 digits, each in the range from 1 to
8.
Individual (or) state: Each string in the initial population is individual (or) state. In one state,
the position of the queen of each column is represented.
48
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Evaluation function (or) Fitness function: A function that returns higher values for better
State. For 8 queens problem the number of non-attacking pairs of queens is defined as fitness
function.
The values of the four states are 24, 23, 20, and 11.
The probability of being chosen for reproduction is directly proportional to the fitness score,
which is denoted as percentage.
24 / (24+23+20+11) = 31%
23 / (24+23+20+11) = 29%
20 / (24+23+20+11) = 26%
11 / (24+23+20+11) = 14%
Selection : A random choice of two pairs is selected for reproduction, by considering the
probability of fitness function of each state. In the example one state is chosen twice
probability of 29%) and the another one state is not chosen (Probability of 14%)
Cross over: Each pair to be mated, a crossover point is randomly chosen. For the first pair
the crossover point is chosen after 3 digits and after 5 digits for the second pair.
49
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
offspring : Offspring is created by crossing over the parent strings in the crossover point.
That is, the first child of the first pair gets the first 3 digits from the first parent and the
remaining digits from the second parent. Similarly the second child of the first pair gets the
first 3 digits from the second parent and the remaining digits from the first parent.
50
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
The initial population in (a) is ranked by the fitness function in (b), resulting in pairs for
mating in (c). They produce offspring in (d), which are subject to mutation in(e).
51
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
es.
We have seen so many techniques like Local search, Adversarial search to solve different
problems. The objective of every problem-solving technique is one, i.e., to find a solution to
reach the goal. Although, in adversarial search and local search, there were no constraints on
the agents while solving the problems and reaching to its solutions.
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.[values]
C: It is a set of constraints which are followed by the set of variables.
The constraint value consists of a pair of {scope, rel}. The scope is a tuple of variables which
participate in the constraint and rel is a relation which includes a list of values which the
variables can take to satisfy the constraints of the problem.
CSP Examples
Constraint satisfaction includes those problems which contains some constraints while
solving the problem. CSP includes the following problems:
Graph Coloring: The problem where the constraint is that no adjacent sides can have
the same color.
52
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Sudoku Playing: The gameplay where the constraint is that no number from 0-9 can
be repeated in the same row or column.
Cryptarithmetic Problem: This problem has one most important constraint that is,
we cannot assign a different digit to the same character. All digits should contain a
unique alphabet.
53
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
54
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
55
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
In local state-spaces, the choice is only one, i.e., to search for a solution. But in CSP, we have
two choices either:
Constraint propagation is a special type of inference which helps in reducing the legal
number of values for the variables. The idea behind constraint propagation is local
consistency.
In local consistency, variables are treated as nodes, and each binary constraint is treated as
an arc in the given problem. There are following local consistencies which are discussed
below:
Node Consistency: A single variable is said to be node consistent if all the values in
the variable’s domain satisfy the unary constraints on the variables.
Arc Consistency: A variable is arc consistent if every value in its domain satisfies the
binary constraints of the variables.
Path Consistency: When the evaluation of a set of two variable with respect to a
third variable can be extended over another variable, satisfying all the binary
constraints. It is similar to arc consistency.
k-consistency: This type of consistency is used to define the notion of stronger forms
of propagation. Here, we examine the k-consistency of the variables.
56
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
57
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
AC-3 algorithm:
AC-3 maintains a queue of arcs which initially contains all the arcs in the CSP. AC-3
then pops off an arbitrary arc (Xi, Xj) from the queue and makes Xi arc-consistent with
respect to Xj. If this leaves Di unchanged, just moves on to the next arc; But if this
revises Di, then add to the queue all arcs (Xk, Xi) where Xk is a neighbor of Xi. If Di is
revised down to nothing, then the whole CSP has no consistent solution, return failure;
Otherwise, keep checking, trying to remove values from the domains of variables until
no more arcs are in the queue. The result is an arc-consistent CSP that have the same
solutions as the original one but have smaller domains.
58
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
59
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Backtracking search, a form of depth-first search, is commonly used for solving CSPs.
Backtracking search: A depth-first search that chooses values for one variable at a
time and backtracks when a variable has no legal values left to assign.
Backtracking algorithm repeatedly chooses an unassigned variable, and then tries all
values in the domain of that variable in turn, trying to find a solution. If an
inconsistency is detected, then BACKTRACK returns failure, causing the previous
call to try another value.
BACKTRACKING-SARCH keeps only a single representation of a state and alters
that representation rather than creating a new ones.
60
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
61
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
62
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
63
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
64
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Types of games in AI
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.
65
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
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
66
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Utility Function / Objective Function / Payoff function: A utility function gives the final
numeric value for a game that ends in terminal state. For Chess, the outcomes are a win, loss,
or draw and its payoff values are +1, -1 or 0.
GAME TREE : The initial and the legal moves for each side define the GAME TREE for
the game. A game tree is a tree where nodes of the tree are the game states and
Edges of the tree are the moves by players. Game tree involves initial state,
actions function, and result Function.
The following figure is showing part of the game-tree for tic-tac-toe game. Following are
some key points of the game:
67
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Even a simple game like tic-tac-toe is too complex for us to draw the entire game tree.
The possible moves for MAX at the root node are labeled al, a2, and a3. The possible replies
to a1 for MIN are b1, b2, b3, and so on.
68
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Complexity : If the maximum depth of the tree is m, and there are b legal moves at each
point then the time complexity of the minimax algorithm is O(bm). This algorithm is a depth
first search, therefore the space requirements are linear in m and b. For real games the
calculation of time complexity is impossible, however this algorithm will be a good basis for
game playing.
69
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
ALPHA–BETA PRUNING
α >= β
Key points about alpha-beta pruning:
o The Max player will only update the value of alpha.
o The Min player will only update the value of beta.
o While backtracking the tree, the node values will be passed to upper nodes instead of
values of alpha and beta.
o We will only pass the alpha, beta values to the child nodes.
Let's take an example of two-player search tree to understand the working of Alpha-beta
pruning
70
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Step 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.
Step 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.
Step 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.
71
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
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.
Step 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.
72
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Step 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.
Step 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.
Step 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.
73
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Step 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.
74
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
75
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Example 2:
Stages in the calculation of the optimal decision for the game tree
76
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Backgammon is a typical game that combines luck and skill. Dice are rolled at the beginning
of a player's turn to determine the legal moves. In the backgammon position of Figure 5.10,
for example, White has rolled a 6–5 and has four possible moves.
77
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
Goal State
The goal of the game is to move all one's pieces off the board. White moves clockwise
toward 25, and black moves counterclockwise toward 0.
Task : In the position shown, White has rolled 6-5. So Find out the legal moves for the set of
the dice thrown as 6 and 5.
Solution :
There are Four legal moves. They are
(5-11,5-10)
(5-11, 19-24)
(10-16,5-10)
(5-11,11-16)
Although White knows what his or her own legal moves are, White does not know what
Black is going to roll and thus does not know what Black’s legal moves will be. That means
78
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
White cannot construct a standard game tree of the sort we saw in chess and tic-tac-toe. A
game tree in backgammon must include chance nodes in addition to MAX and MIN nodes.
Chance nodes are shown as circles. The branches leading from each chance node denote the
possible dice rolls, and each is labelled with the roll and the chance that it will occur. There
are 36 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. The six doubles (1-1 through 6-6) have a 1/36 chance of coming up,
the other 15 distinct rolls a 1/18 chance each.
The next step is to understand how to make correct decisions. Obviously, we still
want to pick the move that leads to the best position. However, positions do not have definite
minimax values. Instead, we can only calculate the expected value of a position: the average
over all possible outcomes of the chance nodes.
where r represents a possible dice roll (or other chance event) and RESULT(s, r) is the same
state as s, with the additional fact that the result of the dice roll is r.
79
CS8691 ARTIFICIAL INTELLIGENCE UNIT - 2
80