AI UNIT2 Anna University Notes
AI UNIT2 Anna University Notes
AI UNIT2 Anna University Notes
Search techniques use an explicit search tree that is generated by the initial
state and the successor function that together define the state space. In general,
we may have a search graph rather than a search tree, when the same state can
be reached from multiple paths
The root of the search tree is a search node corresponding to the initial state,
In(Arad).
Apply the successor function to the current state, and generate a new set of
states
1
CS8691 – Artificial Intelligence
1. Start the sequence with the initial state and check whether it is a goal state or
not.
From the initial state (current state) generate and expand the new set of states.
The collection of nodes that have been generated but not expanded is called as
fringe. Each element of the fringe is a leaf node, a node with no successors in
the tree.
Expanding A
Expanding B
Expanding C
2
CS8691 – Artificial Intelligence
There are many ways to represent nodes, but we will assume that a node is a
data structure with five components:
STATE: the state in the state space to which the node corresponds
PARENT-NODE: the node in the search tree that generated this node;
ACTION (RULE): the action that was applied to the parent to generate the
node;
PATH-COST: the cost, traditionally denoted by g(n) , of the path from the initial
state to the node
DEPTH: the number of steps along the path from the initial state.
The collection of nodes represented in the search tree is defined using set or
queue representation.
3
CS8691 – Artificial Intelligence
Set : The search strategy would be a function that selects the next node to be
expanded from the set
Queue: Collection of nodes are represented, using queue. The queue operations
are defined as:
4
CS8691 – Artificial Intelligence
5
CS8691 – Artificial Intelligence
6
CS8691 – Artificial Intelligence
No information about the number of The path cost from the current state to
steps (or) path cost from current state the goal state is calculated, to select
to goal state the minimum path cost as the next
state.
Less effective in search method More effective in search method
Problem to be solved with the given Additional information can be added as
information assumption to solve the problem
Breadth-first search
7
CS8691 – Artificial Intelligence
The path in the 2nd depth level is selected, (i.e) SBG{or) SCG.
8
CS8691 – Artificial Intelligence
Algorithm :
Example:
Time complexity
2 d
= 1 +b + b +.......+b
d
= O(b )
The space complexity is same as time complexity because all the leaf nodes of
d
the tree must be maintained in memory at the same time = O(b )
Completeness: Yes
Optimality: Yes, provided the path cost is a non decreasing function of the
depth of the node
9
CS8691 – Artificial Intelligence
Advantage: Guaranteed to find the single solution at the shallowest depth level
Uniform-cost search
Breadth-first search is optimal when all step costs are equal, because it always
expands the shallowest unexpanded node. By a simple extension, we can find an
algorithm that is optimal with any step cost function. Instead of expanding the
shallowest node, uniform-cost search expands the node n with the lowest
path cost. Note that if all step costs are equal, this is identical to breadth-first
search.
Uniform-cost search does not care about the number of steps a path has, but
only about their total cost.
10
CS8691 – Artificial Intelligence
B to be expanded next
No need to expand the next path SC, because its path cost is high to reach C
from S, as well as goal state is reached in the previous path with minimum cost.
Time complexity is same as breadth first search because instead of depth level
the minimum path cost is considered.
11
CS8691 – Artificial Intelligence
d d
Time complexity: O(b ) Space complexity: O(b )
Depth-first search
Depth-first search always expands the deepest node in the current fringe of
the search tree
The search proceeds immediately to the deepest level of the search tree, where
the nodes have no successors. As those nodes are expanded, they are dropped
from the fringe, so then the search "backs up" to the next shallowest node that
still has unexplored successors. This strategy can be implemented by TREE-
SEARCH with a last-in-first-out (LIFO) queue, also known as a stack.
12
CS8691 – Artificial Intelligence
G Algorithm:
In the worst case depth first search has to expand all the
m
nodes Time complexity : O(b ).
The nodes are expanded towards one particular direction requires memory for
only that nodes.
13
CS8691 – Artificial Intelligence
b=2
m = 2 :. bm=4
Completeness: No
Optimality: No
Advantage: If more than one solution exists (or) number of levels is high then
DFS is best because exploration is done only in a small portion of the whole
space.
1. Definition: A cut off (maximum level of the depth) is introduced in this search
technique to overcome the disadvantage of depth first search. The cutoff value
depends on the number of states.
The number of states in the given map is 5. So, it is possible to get the goal
state at a maximum depth of 4. Therefore the cutoff value is 4
14
CS8691 – Artificial Intelligence
15
CS8691 – Artificial Intelligence
The worst case time complexity is equivalent to BFS and worst case DFS.
l
Time complexity : O(b )
Optimality: No, because not guaranteed to find the shortest solution first in
the search technique.
Limit = 0
Limit = 1
16
CS8691 – Artificial Intelligence
Limit = 2
Solution: The goal state G can be reached from A in four ways. They are:
1. A – B – D - E – G ------- Limit 4
2. A - B - D - E - G ------- Limit 4
3. A - C - E - G ------- Limit 3
4. A - F - G ------ Limit2
Since it is a iterative deepening search it selects lowest depth limit (i.e.) A-F-G
is selected as the solution path.
Iterative deepening combines the advantage of breadth first search and depth
first search (i.e) expansion of states is done as BFS and memory requirement is
equivalent to DFS.
d
Time complexity : O(b )
17
CS8691 – Artificial Intelligence
Bidirectional search
The forward and backward searches done at the same time will lead to the
d/2 d/2
solution in O(2b ) = O(b )step, because search is done to go only halfway
18
CS8691 – Artificial Intelligence
If the two searches meet at all, the nodes of at least one of them must all be
d/2
retained in memory requires O(b ) space.
Optimality: Yes, because the order of expansion of states is done in both
the directions.
19
CS8691 – Artificial Intelligence
The repeated states can be avoided using three different ways. They are:
1. Do not return to the state you just came from (i.e) avoid any successor that is
the same state as the node's parent.
2. Do not create path with cycles (i.e) avoid any successor of a node that is the
same as any of the node's ancestors.
3. Do not generate any state that was ever generated before.
If the current node matches a node on the closed list, then it is discarded and it
is not considered for expansion. This is done with GRAPH-SEARCH algorithm.
This algorithm is efficient for problems with many repeated states
The worst-case time and space requirements are proportional to the size of the
d
state space, this may be much smaller than O(b )
Informed search and exploration
20
CS8691 – Artificial Intelligence
The general approach is best first search that uses an evaluation function in
TREE-SEARCH or GRAPH-SEARCH.
The node with the lowest evaluation is selected for expansion, because the
evaluation measures distance to the goal.
Best-first search can be implemented within our general search framework via a
priority queue, a data structure that will maintain the fringe in ascending order of
f –values
h(n) h(n) = estimated cost of the cheapest path from node n to a goal node.
(i) Expand the node closest to the goal state using estimated cost as the
evaluation is called greedy best first search.
21
CS8691 – Artificial Intelligence
(ii) Expand the node on the least cost solution path using estimated cost and
actual cost as the evaluation function is called A*search
Definition : A best first search that uses h(n) to select next node to expand is
called greedy search
Evaluation function : The estimated cost to reach the goal state, denoted by
the letter h(n)
Algorithm :
22
CS8691 – Artificial Intelligence
Note : The values of hSLD(n) cannot be computed from the problem description
itself. Moreover, it takes a certain amount of experience
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 Burcharest from A to B
23
CS8691 – Artificial Intelligence
The first node to be expanded from Arad will be Sibiu, because it is closer to
Bucharest than either Zerind or Timisoara.
For this particular problem, greedy best-first search using h SLD 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.
Time and space complexity : Greedy search resembles depth first search,
since it follows one path to the goal state, backtracking occurs when it finds a
dead end. The worst case time complexity is equivalent to depth first search,
m
that is O(b ), where m is the maximum depth of the search space. The greedy
24
CS8691 – Artificial Intelligence
m
search retains all nodes in memory, therefore the space complexity is also O(b )
The time and space complexity can be reduced with good heuristic function.
Optimality : It is not optimal, because the next level node for expansion is
selected only depends on the estimated cost and not the actual cost.
Completeness : No, because it can start down with an infinite path and never
return to try other possibilities.
Solution :
From the given graph and estimated cost, the goal state IS identified as B from
A.
25
CS8691 – Artificial Intelligence
From F, goal state B is reached. Therefore the path from A to Busing greedy
search is A - S - F - B = 450 (i.e) (140 + 99 + 211)
A* Algorithm
26
CS8691 – Artificial Intelligence
27
CS8691 – Artificial Intelligence
28
CS8691 – Artificial Intelligence
Solution:
From the given graph and estimated cost, the goal state is identified as B from A
Apply the evaluation function f(n) = g(n) +h(n) to find a path from A to B
29
CS8691 – Artificial Intelligence
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.
Monotonicity (Consistency)
In search tree any path from the root, the f- cost never decreases. This condition
is true for almost all admissible heuristics. A heuristic which satisfies this property
is called monotonicity(consistency).
A heuristic h(n) is consistent if, for every node n and every successor n' of n
generated by any action a, the estimated cost of reaching the goal from n is no
30
CS8691 – Artificial Intelligence
greater than the step cost of getting to n' plus the estimated cost of reaching the
goal from n':
For example
Let us consider two nodes n and n’, where n is the parent of n’. For example
To reach the node n the cost value is 7, from there to reach the node n' the
value of cost has to increase as per monotonic property. But the above example
does not satisfy this property. So, it is called as non-monotonic heuristic.
We have to check each time when we generate anew node, to see if its f-cost is
less that its parent’s f-cost; if it is we have to use the parent’s f- cost instead.
31
CS8691 – Artificial Intelligence
Optimality
Completeness
Drawback
A* usually runs out of space because it keeps all generated nodes in memory
The simplest way to reduce memory requirements for A* is to adapt the idea of
iterative deepening to the heuristic search context, resulting in the iterative-
deepening A" (IDA*) algorithm.
Depth first search is modified to use an f-cost limit rather than a depth limit for
IDA* algorithm.
Each iteration in the search expands all the nodes inside the contour for the
current f-cost and moves to the next contour with new f - cost.
Time complexity depends on the number of different values that the heuristic
function can take on
32
CS8691 – Artificial Intelligence
Disadvantage: It will require more storage space in complex domains (i.e) Each
contour will include only one state with the previous contour. To avoid this, we
increase the f-cost
limit by a fixed amount on each iteration, so that the total number of iteration
is proportional to 1/ . Such an algorithm is called admissible.
A recursive algorithm with best first search technique uses only linear space.
It is similar to recursive depth first search with an inclusion (i.e.) keeps track of
the f-value of the best alternative path available from any ancestor of the
current node.
If the current node exceeds this limit, the recursion unwinds back to the
alternative path and replaces the f-value of each node along the path with the
best f-value of its children.
The main idea lies in to keep track of the second best alternate node (forgotten
node) and decides whether it's worth to reexpand the subtree at some later
time.
33
CS8691 – Artificial Intelligence
34
CS8691 – Artificial Intelligence
Example:
a) After expanding A, S, and R, the current best leaf(P) has a value that is worse
than the best alternative path (F)
f-limit value of each recursive call is shown on top of each current node. After
expanding R, the condition f[best] >f-limit (417 > 415) is true and returns
f[best] to that node.
Here the f[best] is 450 and which is greater than the f-limit of 417. Therefore if
returns and unwinds with f[best] value to that node.
35
CS8691 – Artificial Intelligence
The best alternative path through T costs at least 447, therefore the path
through R and P is considered as the best one.
SMA* algorithm can make use of all available memory to carry out the search.
36
CS8691 – Artificial Intelligence
Queue<-MAKE-QUEUE({MAKE-NODE(INITIAL-STATE[problem])} )
loop do
if Queue is empty then return failure
n<-deepest least-f-cost node in Queue
if GOAL-TEST(n) then return success
s<-NEXT-SUCCESSOR(n)
if s is not a goal and is at maximum depth then
f{s)<-
else
f{s)<- MAX(f(n), g(s)+h(s))
if all of n’s successors have been generated then
update n’s f-cost and those of its ancestors if necessary
if SUCCESSORS(n) all in the memory then remove n from
Queue
if memory is full then
delete shallowest, highest-f-cost node in Queue
remove it from its parent's successor list
insert its parent on Queue if necessary
insert s on Queue
end
37
CS8691 – Artificial Intelligence
Example:
The values at the nodes are given as per the A* function i.e. g+h=f
From the Figure we identified that the goal states are D,F,J,I because the h
value of these nodes are zero (marked as a square)
Solution:
38
CS8691 – Artificial Intelligence
HEURISTIC FUNCTIONS
Given :
39
CS8691 – Artificial Intelligence
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.
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.
Example :
h1=7
h2 = 2 + 3 + 3 + 2 + 4 + 2 + 0 + 2 = 18
40
CS8691 – Artificial Intelligence
In the search tree, if the total number of nodes expanded by A* for a particular
problem is N, and the solution depth is d, then b* is the branching factor that a
uniform tree of depth d, would have N nodes. Thus:
2 3 d
N= l+ b* + (b*) + (b*) + +(b*)
Example:
For example, if A* finds a solution at depth 5 using 52 nodes, then the effective
branching factor is 1.92.
Depth = 5
N = 52
Relaxed problem
In many optimization problems, the path to the goal is irrelevant; the goal state
itself is the solution.
The best state is identified from the objective function or heuristic cost function.
In such cases, we can use local search algorithms (ie) keep only a single current
state, try to improve it instead of the whole search space explored so far
For example, in the 8-queens problem, what matters is the final configuration of
queens, not the order in which they are added.
Local search algorithms operate a single current state (rather than multiple
paths) and generally move only to neighbors of that state. Typically, the paths
followed by the search are not retained.
41
CS8691 – Artificial Intelligence
(1) They use very little memory-usually a constant amount; (2) They can often
find reasonable solutions in large or infinite (continuous) state spaces for which
systematic algorithms are unsuitable.
The local search problem is explained with the state space land scape. A
landscape has:
A complete local search algorithm always finds a goal if one exists, an optimal
algorithm always finds a global minimum/maximum.
Applications
42
CS8691 – Artificial Intelligence
To illustrate hill-climbing, we will use the 8-queens, where each state has 8
queens on the board, one per column. The successor function returns all possible
states generated by moving a single queen to another square in the same
column (so each state has 8 x 7 = 56 successors).
The heuristic cost function h is the number of pairs of queens that are attacking
each other, either directly or indirectly.
43
CS8691 – Artificial Intelligence
The global minimum of this function is zero, which occurs only at perfect
solutions.
An 8-queens state with heuristic cost estimate h = 17, showing the value of h for
each possible successor obtained by moving a queen within its column. The best
moves are marked.
A local minimum in the 8-queens state space; the state has h = 1 but every
successor has a higher cost.
44
CS8691 – Artificial Intelligence
Local maxima or foot hills : a local maximum is a peak that is higher than
each of its neighboring states, but lower than the global maximum
Example :
The evaluation function value is maximum at C and from their there is no path
exist for expansion. Therefore C is called as local maxima. To avoid this state,
random node is selected using back tracking to the previous node.
Example :
The evaluation function value of B C D are same, this is a state space of plateau.
To avoid this state, random node is selected or skip the level (i.e) select the
node in the next level
45
CS8691 – Artificial Intelligence
Ridges: Ridges result in a sequence of local maxima that is very difficult for
greedy algorithms to navigate. But the disadvantage is more calculations to be
done function
Variants of hill-climbing
An algorithm which combines hill climbing with random walk to yield both
efficiency and completeness
When the search stops at the local maxima, we will allow the search to take
some down Hill steps to escape the local maxima by allowing some "bad" moves
but gradually decrease their size and frequency. The node is selected randomly
and it checks whether it is a best move or not. If the move improves the
situation, it is executed. E variable is introduced to calculate the probability of
worsened. A Second parameter T is introduced to determine the probability.
46
CS8691 – Artificial Intelligence
T decreases slowly enough then simulated annealing search will find a global
optimum with probability approaching one
Applications
VLSI layout
Airline scheduling
47
CS8691 – Artificial Intelligence
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
The values of the four states are 24, 23, 20, and 11.
24 / (24+23+20+11) = 31%
23 / (24+23+20+11) = 29%
20 / (24+23+20+11) = 26%
11 / (24+23+20+11) = 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
50
CS8691 – Artificial Intelligence
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).
repeat
new-population <- empty set
loop for i from 1 to SIZE(population) do
x <- RANDOM-SELECTION(Population,FITNESS-FN)
y <- RANDOM-SELECTION(Population,FITNESS-
FN) child <- REPRODUCE(yX),
if (small random probability) then child MUTATE(chi1d)
add child to new-population
population <- new-population
until some individual is fit enough, or enough time has
elapsed
return the best individual in population, according to
FITNESS-FN
function REPRODUCE(x,y), returns an
individual inputs: x, y, parent individuals
n <- LENGTH(x)
c <- random number from 1 to n
return APPEND(SUBSTRING(x,1,c),SUBSTRING(y, c + 1, n ))
51
CS8691 – Artificial Intelligence
Local Search in continuous space is the one that deals with the real world
problems.
x <- x + f(x)
- small constant
f(x) - magnitude & direction of the steepest slope.
Empirical gradient, line search, Newton-Raphson method can be
applied in this domain to find the successor state.
Local search methods in continuous space may also lead to local
maxima, ridges and plateau. This situation is avoided by using random
restart method.
Online search agent operates by interleaving computation and action, that is first
it takes an action, then it observes the environment and computes the next
action, whereas ,the offline search computes complete solution (problem solving
agent) before executing the problem solution.
Online search is a necessary idea for an exploration problem, where the states
and actions are unknown to the agent. For example, consider a newborn baby
for exploration problem and the baby's gradual discovery of how the world works
is an online search process
52
CS8691 – Artificial Intelligence
53
CS8691 – Artificial Intelligence
A cryptarithmetic problem
All these examples and other real life problems like time table scheduling,
transport scheduling, floor planning etc. are instances of the same pattern,
The map coloring problem. The task of coloring each region red, green or blue in
such a way that no neighboring regions have the same color.
We are given the task of coloring each region either red, green, or blue in such a
way that the neighboring regions must not have the same color.
54
CS8691 – Artificial Intelligence
To formulate this as CSP, we define the variable to be the regions: WA, NT, Q,
NSW, V, SA, and T.
The constraints require neighboring regions to have distinct colors: for example,
the allowable combinations for WA and NT are the pairs
{(red,green),(red,blue),(green,red),(green,blue),(blue,red),(blue,green)}.
Initial state : the empty assignment {},in which all variables are
unassigned.
Successor function : a value can be assigned to any unassigned
variable, provided that it does not conflict with previously assigned
variables.
Goal test : the current assignment is complete.
Path cost : a constant cost(E.g.,1) for every step.
55
CS8691 – Artificial Intelligence
Varieties of CSPs
Discrete variables
Finite Domains
Infinite domains
Finite domains
The simplest kind of CSP involves variables that are discrete and have finite
domains.
Map coloring problems are of this kind. The 8-queens problem can also be
viewed as finite-domain CSP, where the variables Q1,Q2,…..Q8 are the positions
each queen in columns 1,….8 and each variable has the domain
{1,2,3,4,5,6,7,8}.
If the maximum domain size of any variable in a CSP is d, then the number of
n
possible complete assignments is O(d ) – that is, exponential in the number of
variables.
Finite domain CSPs include Boolean CSPs, whose variables can be either true
or false.
Infinite domains
Discrete variables can also have infinite domains – for example, the set of
integers or the set of strings. With infinite domains, it is no longer possible to
describe constraints by enumerating all allowed combination of values. For
example, if Jobl, which takes five days, must precede Jobs, then we would need
a constraint language of algebraic inequalities such as
CSPs with continuous domains are very common in real world. For example, in
operation research field, the scheduling of experiments on the Hubble Telescope
requires very precise timing of observations; the start and finish of each
56
CS8691 – Artificial Intelligence
Varieties of constraints :
The aim is to find a substitution of digits for letters such that the resulting sum is
arithmetically correct, with the added restriction that no leading zeros are
allowed.
57
CS8691 – Artificial Intelligence
O + O = R + 10 . X1
X1 + W + W = U + 10 . X2
X1 + T + T = O + 10 . X3
X3 = F
Where X1, X2, and X3 are auxiliary variables representing the digit (0 or 1)
carried over into the next column.
The term backtracking search is used for depth-first search that chooses
values for one variable at a time and backtracks when a variable has no legal
values left to assign.
58
CS8691 – Artificial Intelligence
Which variable should be assigned next and what order should be tried?
What are the implications of the current variable assignments for the other
unassigned variables?
Can we detect inevitable failure early?
If the tie occurs among most constrained variables then most constraining
variable is chosen (i.e.) choose the variable with the most constraints on
remaining variable. Once a variable has been selected, choose the least
constraining value that is the one that rules out the fewest values in the
remaining variables.
So far our search algorithm considers the constraints on a variable only at the
time that the variable is chosen by SELECT-UNASSIGNED-VARIABLE. But by
looking at some of the constraints earlier in the search, or even before the
search has started, we can drastically reduce the search space.
Forward checking
One way to make better use of constraints during search is called forward
checking. Whenever a variable X is assigned, the forward checking process
59
CS8691 – Artificial Intelligence
Constraint propagation
Although forward checking detects many inconsistencies, it does not detect all of
them.
Node consistency
60
CS8691 – Artificial Intelligence
Arc Consistency
In the constraint graph, binary constraint corresponds to arc. Therefore this type
of consistency is called arc consistency.
Arc (vi, vj) is arc consistent if for every value X the current domain of vi there is
some value Y in the domain of vj such vi =X and vj=Y is permitted by the binary
constraint between vi and vj
An arc (vi, vj) can be made consistent by simply deleting those values from the
domain of Di for which there is no corresponding value in the domain of D j such
that the binary constraint between Vi and vj is satisfied - It is an earlier detection
of inconstency that is not detected by forward checking method.
The different versions of Arc consistency algorithms are exist such as AC-I,
AC2,AC-3, AC-4, AC-S; AC-6 & AC-7, but frequently used are AC-3 or AC-4.
AC - 3 Algorithm
Remove the first arc from the queue and check for consistency.
61
CS8691 – Artificial Intelligence
If it is inconsistent remove the variable from the domain and add a new
arc to the queue
Repeat the same process until queue is empty
A CSP is k-consistent if, for any set of k - 1 variables and for any consistent
assignment to those variables, a consistent value can always be assigned to any
kth variable
62
CS8691 – Artificial Intelligence
Alldiff constraint - All the variables involved must have distinct values.
The inconsistency arises in Alldiff constraint when m>n (i.e.) m variables are
involved in the constraint and n possible distinct values are there. It can be
avoided by selecting the variable in the constraint that has a singleton. domain
and delete the variable's value from the domain of remaining variables, until the
singleton variables are-exist. This simple algorithm will resolve the inconsistency
of the problem.
Intelligent backtracking
63
CS8691 – Artificial Intelligence
Completely independent sub problems are rare, in most cases sub problems of a
CSP are connected
The way how to convert the constraint graph of a tree structure CSP into linear
ordering of the variables consistent with the tree is shown in Figure. Any two
variables are connected by atmost one path in the tree structure
64
CS8691 – Artificial Intelligence
If the constraint graph of a CSP forms a tree structure then it can be solved in
linear time number of variables). The algorithm has the following steps.
1. Choose any variable as the root of the tree and order the variables from the
root to the leaves in such a way that every node's parent in the tree preceeds it
in the ordering label the variables X l .... Xn in order, every variable except the
root has exactly one parent variable.
2. For j from n down 2, apply arc consistency to the arc (Xi , Xj), where Xi is the
parent of Xj removing values from Domain [Xi] as necessary.
3. For j from 1 to n, assign any value for Xj consistent with the value assigned
for Xi, where Xi is the parent of Xj Keypoints of this algorithm are as follows:
General constraint graphs can be reduced to trees on two ways. They are:
65
CS8691 – Artificial Intelligence
Delete the value assigned variable from the list and from the domains of
the other variables any values that are inconsistent with the value chosen
for the variable.
This works for binary CSP's and not suitable for higher order constraints.
If the value chosen for the variable to be deleted for tree structure is
wrong, then the following algorithm is executed.
66
CS8691 – Artificial Intelligence
* Remove from the deomains of the remaining variables any values that
are inconsistent with the assignment for S.
* If the remaining CSP has a solution, return it with the assignment for S.
Solution
Adversarial Search
67
CS8691 – Artificial Intelligence
Competitive environments, in which the agent’s goals are in conflict, give rise to
adversarial search problems-often known as games.
In game playing to select the next state, search technique is required. Game
playing itself is considered as a type of search problems. But, how to reduce the
search time to make on a move from one state to another state.
The pruning technique allows us to ignore positions of the search tree that
make no difference to the final choice.
Heuristic evaluation function allows us to find the utility (win, loss, and
draw) of a state without doing a complete search.
Max, makes a move first by choosing a high value and take turns moving
until the game is over
Min, makes a move as a opponent and tries to minimize the max player
score, until the game is over.
At the end of the game (goal state or time), points are awarded to the winner.
Initial state - Which includes the board position and an indication of whose
move and identifies the player to the move.
Successor function - which returns a list of (move, state) pairs, each indicating
a legal move and the resulting state.
Terminal test - Which determines the end state of the game. States where the
game has ended are called terminal states.
68
CS8691 – Artificial Intelligence
The initial state and the legal moves for each side define the game tree for the
game
From the initial state, MAX has nine possible moves. Play alternates between
MAX'S placing an X and MIN'S placing an O until we reach leaf nodes
corresponding to ter.mina1 states such that one player has three in a row or all
the squares are filled.
Goal State : We have three different types of goal state, any one to be
reached.
i) If the O’s are placed in one column, one row (or) in the diagonal
continuously then, it is a goal state of min player. (Won by Min Player)
ii) If the X’s are placed in one column, one row (or) in the diagonal
continuously then, it is a goal state of min player. (Won by Max Player)
iii) If the all the nine squares are filled by either X or O and there is no
win condition by Max and Min player then it is a state of Draw between
two players.
Some terminal states
X O X
O
O
69
CS8691 – Artificial Intelligence
X O X
X
X 0 O
X O X
O O X
X O O
Utility function
Optimal strategies
Given a game tree, the optimal strategy can be determined by examining the
minimax value of each node, which we write as MINIMAX- VALUE(n).
70
CS8691 – Artificial Intelligence
MINIMAX- VALUE(n) =
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.
A Two Ply Game Tree
The minimax algorithm computes the minimax decision from the current
state. It uses a simple recursive computation of the minimax values of each
successor state, directly implementing the defining equations. The recursion
proceeds all the way down to the leaves of the tree, and then the minimax
values are backed up through the tree as the recursion unwinds.
71
CS8691 – Artificial Intelligence
For Example
The algorithm first recurses down to the three bottom left nodes, and uses the
UTILITY function on them to discover that their values are 3, 12, and 8
respectively. Then it takes the minimum of these values, 3, and returns it as the
backed-up value of node B. A similar process gives the backed up values of 2 for
C and 2 for D. Finally, we take the maximum of 3,2, and 2 to get the backed-up
value of 3 for the root node.
Generate the whole game tree, all the way down to the terminal state.
Apply the utility function to each terminal state to get its value.
72
CS8691 – Artificial Intelligence
Use utility functions of the terminal state one level higher than the current
value to determine Max or Min value.
Minimax decision maximizes the utility under the assumption that the
opponent will play perfectly to minimize the max player score.
Complexity : If the maximum depth of the tree is m, and there are b legal
m
moves at each point then the time complexity of the minimax algorithm is O(b ).
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.
Alpha ( ) : Best choice for the value of MAX along the path (or) lower bound
on the value that on maximizing node may be ultimately assigned.
Beta ( ) : Best choice for the value of MIN along the path (or) upper bound
on the value that a minimizing node may be ultimately assigned.
Alpha - Beta pruning : The and values are applied to a minimax tree, it
returns the same move as minimax, but prunes away branches that cannot
possibly influence the final decision is called Alpha - Beta pruning (or) Cutoff
Consider again the two-ply game tree from
Let the two unevaluated successors of node C have values x and y and let z be
the minimum of x and y. The value of the root node is given by
73
CS8691 – Artificial Intelligence
MINIMAX-VALUE(ROOT)=max((min(3,12,8),min(2,x ,y),min(l4,5,2 ))
= max(3, min(2, x, y), 2)
= max(3, z, 2) where z <=2
= 3.
In other words, the value of the root and hence the minimax decision are
independent of the values of the pruned leaves x and y.
Stages in the calculation of the optimal decision for the game tree
(a) The first leaf below B has the value 3. Hence, B, which is a MIN node, has a
value of at most 3
(b) The second leaf below B has a value of 12; MIN would avoid this move, so
the value of B is still at most 3
c) The third leaf below B has a value of 8; we have seen all B's successors, so
the value of B is exactly 3. Now, we can infer that the value of the root is at
least 3, because MAX has a choice worth 3 at the root.
(d) The first leaf below C has the value 2. Hence, C, which is a MIN node, has a
value of at most 2. But we know that B is worth 3, so MAX would never choose
C. Therefore, there is no point in looking at the other successors of C. This is an
example of alpha-beta pruning.
74
CS8691 – Artificial Intelligence
(e) The first leaf below D has the value 14, so D is worth atmost 14. This is still
higher than MAX'S best alternative (i.e., 3), so we need to keep exploring D's
successors. Notice also that we now have bounds on all of the successors of the
root, so the root's value is also at most 14.
75
CS8691 – Artificial Intelligence
76
CS8691 – Artificial Intelligence
The minimax algorithm generates the entire game search space, whereas the
alpha-beta algorithm allows us to prune large parts of it. However, alpha-beta
still has to search all the way to terminal states for at least a portion of the
search space. This depth is usually not practical, because moves must be made
in a reasonable amount of time-typically a few minutes at most.
Shannon’s proposed instead that programs should cut off the search earlier and
apply a heuristic evaluation function to states in the search, effectively turning
non terminal nodes into terminal leaves.
2. Evaluation function
In chess problem each material (Queen, Pawn, etc) has its own value that is
called as material value. From this depends on the move the evaluation function
is calculated and it is applied to the search tree.
This suggests that the evaluation function should be specified by the rules of
probability.
For example If player A has a 100% chance of winning then its evaluation
function is 1.0 and if player A has a 50% chance of winning, 25% of losing and
25% of being a draw then the probability is calculated as; 1x0.50 -lxO.25 +
OxO.25 = 0.25.
As per this example is concerned player A is rated higher than player B. The
material value, of each piece can be calculated independently with-out
considering other pieces in the board is also called as one kind of evaluation
function and it is named as weighted linear function. It can be expressed as
77
CS8691 – Artificial Intelligence
Horizon problem - When the program is facing a move by the opponent that
causes serious damage and is ultimately unavoidable
Example:
1. Beginning of the search - one ply
2. This diagram shows the situation of horizon problem that is when one level is
generated from B, it causes bad value for B
3. When one more successor level is generated from E and F and situation
comes down and the value of B is retained as a good move. The time B is waited
for this situation is called waiting for quiescence.
78
CS8691 – Artificial Intelligence
Backgammon Game
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.
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
Move to any position except where two or more of the opponent pieces.
If it moves to a position with one opponent piece it is captured and again it has
to start from Beginning
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 :
(5-11,5-10)
(5-11, 19-24)
79
CS8691 – Artificial Intelligence
(10-16,5-10)
(5-11,11-16)
The resulting positions do not have definite minimax values. Instead, we have to
only calculate the expected value, where the expectation is taken over all the
possible dice rolls that could occur.
Terminal nodes and MAX and MIN nodes (for which the dice roll is known) work
exactly the same way as before; chance nodes are evaluated by taking the
weighted average of the values resulting from all possible dice rolls, that is,
EXPECTIMINIMAX(n)=
80
CS8691 – Artificial Intelligence
where the successor function for a chance node n simply augments the state of
n with each possible dice roll to produce each successor s and P(s) is the
probability that that dice roll occurs.
Card games
Card games are interesting for many reasons besides their connection with
gambling.
Imagine two players, MAX and MIN, playing some practice hands of four-card
two handed bridge with all the cards showing.
MAX : 6, 6, 9, 8
MIN : 2, 4, 10 , 5
Suppose that MAX leads wiht 9. MIN must now follow suit, playing either with
10 or 5 . MIN plays with 10 and wins the trick.
MIN goes next turn leads the with 2. MAX has no spades (and so cannot win
the trick) and therefore must throw away some card. The obvious choice is the
6 because the other two remaining cards are winners.
Now, whichever card MIN leads for the next trick, MAX will win both remaining
tricks and the game will be tied at two tricks each.
81