Module 1 (AI)
Module 1 (AI)
Make 2 Return 5 if the center square of the board is blank, that is, if Board[5]=2. Otherwise, will return
any blank noncorner square (2, 4, 6 or 8)
Posswin(P) Return 0 if the player P can’t win on his next move; otherwise, it returns the number of square
that says winning move.
This function will enable the program both to win and to block the opponent’s win.
Posswin operates by checking, one at a time, each of the row, column, and diagonals. Because
the way values are numbered, it can to see if it is possible to win by multiplying the values of its
squares together.
If the product is 18(3 x 3 x 2), the X can win, if product is 50(5 x 5 x 2), then O can win.
If we find a winning row, we determine which element is blank, and return the number of the
square.
Go(n) Makes a move in square n. This procedure sets Board[n] to 3 if Turn is odd, or 5 if Turn is even.
It also increments Turn by one.Vishwesh Jayashkear, GSSSIETW, Mysuru
What is an AI Technique: Tic-Tac-Toe
Program 2
The Algorithm
The algorithm has built in strategy for each move it may have to make. It makes the odd numbered moves if it is
playing X, the even numbered moves if it is playing O. The strategy for each turn is as follows:
Turn 1 Go(1)(under left corner).
Turn 2 If Board[5] is blank, Go(5),else Go(1).
Turn 3 If Board[5] is blank, Go(9),else Go(3).
Turn 4 If Posswin(X) is not 0, then Go(Posswin(X))[i.e.,block opponent’s win], else Go(Make2).
Turn 5 If Posswin(X) is not 0, then Go(Posswin(X))[i.e.,win] else if Posswin(O) is not 0, then Go(Posswin(O)) [i.e.,win], else
if Board[7] is blank, then Go(7), else Go(3). [Here the program is trying to make fork.]
Turn 6 If Posswin(O) is not 0, then Go(Posswin(O)), else if Posswin(X) is not 0,then Go(Posswin(X)),else Go(Make2).
Turn 7 If Posswin(X) is not 0, then Go(Posswin(X)), else if Posswin(O) is not 0,then Go(Posswin(O)),else go anywhere that
is blank.
Turn 8 If Posswin(O) is not O, then Go(Posswin(O)), else if Posswin(X) is not 0,then Go(Posswin(X)),else go anywhere that
is blank.
Vishwesh Jayashkear, GSSSIETW, Mysuru
Turn 9 Same as Turn 7.
What is an AI Technique: Tic-Tac-Toe
Program 2
Comments
1. Not efficient in time, as it has to check several conditions before making each move.
2. Easier to understand the program’s strategy.
3. Hard to generalize.
4. Checking for a possible win is quicker.
5. Human finds the row-scan approach is easier, while computer finds the number-counting approach is more
efficient.
ON(B,C) ON(A,B)
Put B on C
ON(B,C) CLEAR(A) ON(A,B)
Move A to table Put A on B
CLEAR(A) ON(A,B)
Vishwesh Jayashkear, GSSSIETW, Mysuru
Problem Characteristics Can the solution step be Ignored or Undone?
• In mathematical theorem proving the solution steps can be ignore. In bridge or chess the solution steps are not
ignorable. Also they are not recoverable. But in 8 puzzle problem the solution steps are recoverable.
• Classes of problems
o Ignorable ( Eg: Theorem Proving ) in which solution steps can be ignored
o Recoverable(Eg: 8 Puzzle ) in which solution steps can be undone
o Irrecoverable(Eg: Chess) in which solution steps cannot be undone.
• Consider the 8-puzzle: The 8-puzzle is a square tray in which 8 tiles are placed. Remaining 9th square is
uncovered. Each tile has a number on it. A tile which is adjacent to the blank space can be slid into that space.
• The game consists of starting position & a specified goal position. The goal is to transform the starting position
into the goal position by sliding the tiles around.
For Path: Contrast to this with The Water Jug Problem, what we really must
report is not the final state but the path that we found to that state. Thus a
statement of solution to this problem must be a sequence of operation that
produces the final state. This problem solution is Path to the state.
Algorithm:
1. Generate a possible solution. For some problems, this means generating a particular point in
the problem space. For others it means generating a path from a start state.
2. Test to see if this is actually a solution by comparing the chosen point or the endpoint of the
chosen path to the set of acceptable goal states.
3. If a solution has been found, quit, Otherwise return to step 1.
Three differences for Simulated Annealing from the Simple Hill-Climbing procedure:
• The annealing schedule must be maintained.
• Moves to worse states may be accepted.
• It is a good idea to maintain, in addition to the current state, the best state found so far. Then,
if the final state is worse than that earlier state, the earlier state is still available.
• The algorithm is basically hill-climbing except instead of picking the best move,
it picks a random move. If the selected move improves the solution, then it is
always accepted. Otherwise, the algorithm makes the move anyway with some
probability less than 1.
• The probability is given by the function:𝒑 = 𝒆−∆𝑬/𝒌𝑻
In order to describe an algorithm for searching an AND-OR graph, we need to exploit a value that we call FUTILITY.
Vishwesh Jayashkear, GSSSIETW, Mysuru
Informed (Heuristic) Search Problem Reduction AND-OR Graphs
In order to describe an algorithm for searching an AND-OR graph, we need to exploit a value that we call
FUTILITY.
• If the estimated cost of a solution becomes grater than the value of FUTILITY which is a threshold, then we
abandon the search, even if it could ever be found.
Step 3: Node D is chosen for expansion. This process produces one new arc, the
AND arc to E and F, with a combined cost estimate of 10. So we update the f’
value of D to 10. Going back one more level, we see that this makes the AND arc
B-C better than the arc to D, so it is labeled as the current best path.
Step 4: We traverse the arc from A and discover the unexpanded nodes B and C. If
we going to find a solution along this path, we will have to expand both B and C
eventually, so let’s choose to explore B first. This generates two new arcs, the ones
to G and to H. Propagating their f’ values backward, we update f’ of B to 6. This
requires updating the cost of the AND arc B-C to 12 (B+C+2). After doing that, the
arc to D is again the better path from A, so we record that as the current best path
and either node E or node F will chosen for expansion at step 4.
Vishwesh Jayashkear, GSSSIETW, Mysuru
Informed (Heuristic) Search Problem Reduction The AO* Algorithm
Algorithm: The AO* Algorithm
1. Let G be a graph with only starting node INIT.
2. Repeat the followings until INIT is labeled SOLVED or h’(INIT) > FUTILITY
a) Select an unexpanded node from the most promising path from INIT (call it NODE)
b) Generate successors of NODE. If there are none, set h’(NODE) = FUTILITY (i.e., NODE is unsolvable);
otherwise for each SUCCESSOR that is not an ancestor of NODE do the following:
i. Add SUCCESSSOR to G.
ii. If SUCCESSOR is a terminal node, label it SOLVED and set h’(SUCCESSOR) = 0.
iii. If SUCCESSPR is not a terminal node, compute its h’
c) Propagate the newly discovered information up the graph by doing the following: let S be set of SOLVED
nodes or nodes whose h’ values have been changed and need to have values propagated back to their
parents. Initialize S to Node. Until S is empty repeat the followings:
i. Remove a node from S and call it CURRENT.
ii. Compute the cost of each of the arcs emerging from CURRENT. Assign minimum cost of its
successors as its h’.
iii. Mark the best path out of CURRENT by marking the arc that had the minimum cost in step ii.
iv. Mark CURRENT as SOLVED if all of the nodes connected to it through new labeled arc have been
labeled SOLVED.
v. If CURRENT has been labeled SOLVED or its cost was just changed, propagate its new cost back up
through the graph. So add all of the ancestors
Vishwesh of CURRENT
Jayashkear, GSSSIETW, Mysuru to S.
Informed (Heuristic) Search Problem Reduction The AO* Algorithm
Example: The AO* Algorithm
1. From Column 5, M=1, since it is only carry-over possible from sum of 2 single digit number in column 4.
2. Now in column 4, we know the value of M = 1 and so S+1=O. This S+1=O should generate the carry to the
next column to make M=1. If S=9, then 9+1=10. This means that S=9 and O=0.
3. In column 3, we have E+0=N E=N, which is not possible because E and N cannot be the same value. To
be E and N different values, we should get the carry. This leads to E+1=N.
4. Since column 3 has a carry from column 2. In column 2, E value has to generate the carry and its value
cannot be 10 since value 0 is already assigned to O. So, E value should be greater then 10. we can write E
has E + 10. Over all we can write the equation has N + R (+1) = E +10. Here (+1) means, we may or may
not consider this value.
5. Now, if we solve the equations E + 1 = N and N + R (+1) = E +10. We will get R (+1) = 9. R value cannot
be 9 since 9 is assigned to S. Means column 2 has to receive a carry from column 1. So, considering the carry
R becomes 8. Means, R= 8 and this columnVishwesh
has aJayashkear,
carry. GSSSIETW, Mysuru
Informed (Heuristic) Search Constraint Satisfaction
Example: Cryptarithmetic Problems
1. From Column 5, M=1, since it is only carry-over possible from sum of 2 single digit number in column 4.
2. Now in column 4, we know the value of M = 1 and so S+1=O. This S+1=O should generate the carry to the
next column to make M=1. If S=9, then 9+1=10. This means that S=9 and O=0.
3. In column 3, we have E+0=N E=N, which is not possible because E and N cannot be the same value. To
be E and N different values, we should get the carry. This leads to E+1=N.
4. Since column 3 has a carry from column 2. In column 2, E value has to generate the carry and its value
cannot be 10 since value 0 is already assigned to O. So, E value should be greater then 10. we can write E
has E + 10. Over all we can write the equation has N + R (+1) = E +10. Here (+1) means, we may or may
not consider this value.
5. Now, if we solve the equations E + 1 = N and N + R (+1) = E +10. We will get R (+1) = 9. R value cannot
be 9 since 9 is assigned to S. Means column 2 has to receive a carry from column 1. So, considering the carry
R becomes 8. Means, R= 8 and this column has a carry.
6. Now, still we don’t know the value for N and E. Let’s see column 1 equation which is D + E = Y . We know
column 1 has to generate the carry, means Y has to send carry and Y value cannot be 10 or 11 since 0
assigned to O and 1 assigned to M. This say Y should be more than 11. So, different combination of D + E
can be 7 + 5 or 7 + 6. So, if we take D = 7 and E = 5, Y becomes Y = 2.
7. And hence N becomes, N = 6. Vishwesh Jayashkear, GSSSIETW, Mysuru
Informed (Heuristic) Search Constraint Satisfaction
Example: Cryptarithmetic Problems
Key idea is to reduce difference between the current state and the goal
state.
Table shows the difference table Push Carry Walk Pickup Putdown Place