Ke 2
Ke 2
LECTURE NOTES
ON
IT407 KNOWLEDGE ENGINEERING
MODULE 2
Prepared By
MESCE, DEPT OF IT 1
IT 407 - Knowledge Engineering 2
Module 2
OR Graph
We will call a graph as an OR - graph, since each of its branches represents alternative problem-
solving path. The Best First Search, selects the most promising of the nodes we have generated
so far. This can be achieved by applying appropriate Heuristic function to each of them.
Heuristic function:
f(n) = h(n)
where,
To implement the graph search procedure, we will need to use two list of nodes.
OPEN- nodes that have been generated but have not been visited yet
Algorithm:
1. The 1st step is to define the OPEN list with a single node, the starting node.
2. The 2nd step is to check whether or not OPEN is empty. If it is empty, then the
algorithm returns failure and exits.
3. The 3rd step is to remove the node with the best score, n, from OPEN and place it in
CLOSED.
4. The 4th step “expands” the node n, where expansion is the identification of successor
nodes of n.
MESCE, DEPT OF IT 2
IT 407 - Knowledge Engineering 3
5. The 5th step then checks each of the successor nodes to see whether or not one of
them is the goal node. If any successor is the goal node, the algorithm returns success
and the solution, which consists of a path traced backwards from the goal to the start
node. Otherwise, proceeds to the sixth step.
6. In 6th step, for every successor node, the algorithm applies the evaluation function, f,
to it, then checks to see if the node has been in either OPEN or CLOSED. If the node
has not been in either, it gets added to OPEN.
7. Finally, the 7th step establishes a looping structure by sending the algorithm back to
the 2nd step. This loop will only be broken if the algorithm returns success in step 5
or failure in step 2.
Initialization
S (15)
Expand the nodes of S and add S into CLOSED
Iteration 1:
1)Add the successors of node s into OPEN and compute f(n)
2)Choose the most promising node (min h(n))
3)Remove the node from OPEN and add it into the CLOSED
MESCE, DEPT OF IT 3
IT 407 - Knowledge Engineering 4
Iteration 2:
1)Add the successors of node B into OPEN and compute f(n)
2)Choose the most promising node (min h(n))
3)Remove the node from OPEN and add it into the CLOSED
Iteration 3:
1)Add the successors of node F into OPEN and compute f(n)
2)Choose the most promising node (min h(n))
3)Remove the node from OPEN and add it into the CLOSED
MESCE, DEPT OF IT 4
IT 407 - Knowledge Engineering 5
A* Algorithm
For Puzzle, A* algorithm, using these evaluation functions, can find optimal
solutions to these problems. In addition, A* makes the most efficient use of the
given heuristic function in the following sense: among all shortest-path
algorithms using the given heuristic function h(n). A* algorithm expands the
fewest number of nodes.
The main drawback of A* algorithm and indeed of any best-first search is its
memory requirement. Since at least the entire open list must be saved, A*
algorithm is severely space-limited in practice, and is no more practical than best-
first search algorithm on current machines. For example, while it can be run
successfully on the eight puzzle, it exhausts available memory in a matter of
minutes on the fifteen puzzle.
MESCE, DEPT OF IT 5
IT 407 - Knowledge Engineering 6
Admissibility
Let's characterize a class of admissible heuristic search strategies, using the evaluation
function:
g(n) represents the actual distance at which the state n has been found in the graph, and h(n) is
the heuristic estimate of the distance from n to a goal state. So f(n) represents an estimate of
the total cost of the path from the start, through n to the goal.
where g*(n) is the cost of the shortest path from the start to n and h*(n) returns the actual cost
of the shortest path from n to the goal. So f* is the actual cost of the optimal path from the
start to the goal through n.
We call the function f* an oracle -- an ideal evaluation function that can see the shortest path
from the start to the goal. Of course, oracles of this type don't exist for most search problems.
For real problems, we want an evaluation function, f, that approaches f*
Algorithm A: In algorithm A, g(n), the cost of the current path from start to n, is a reasonable
estimate of g*:
g(n) and g*(n) will only be equal if the search has found the optimal path to n. Similarly, in
algorithm A, we replace h*(n) with h(n). Although we can't actually compute h*(n), we can
MESCE, DEPT OF IT 6
IT 407 - Knowledge Engineering 7
often determine whether h(n) is bounded by h*(n) -- that is, we can often determine that h(n)
is less than or equal to the actual cost of a minimal path (h*(n)).
Algorithm A: Algorithm A is the best-first search algorithm plus the evaluation function f(n)
= g(n) + h(n).
for all states n. In other words, if an algorithm uses an evaluation function that underestimates
the cost to the goal it is an A* algorithm.
f(n) = g(n) + 0
In other words, bread-first search uses a trivial estimate of the distance to the goal.
Route Finding Example: For route-finding problems, the straight-line distance from city n to
a goal city is a lower bound on the distance of an optimal route from n to the goal.
Eight puzzle Example: The heuristic of number of tiles out-of-place is certainly less than
the actual number of moves to the goal state. So this heuristic (combined with best-first search)
is an admissible algorithm. So is the heuristic sum of the distances of out-of-place tiles,
because it too underestimates the actual number of moves required to reach a goal state.
Monotonicity
This property asks if an algorithm is locally admissible---that is, it always underestimates the
cost between any two states in the search space. Recall that A* does not require that g(n) =
g*(n). A heuristic function, h is monotone if:
1. For all states ni and nj, where nj is a descendant of ni, h(ni) - h(nj) <= cost(ni,nj).
2. The heuristic evaluation of the goal state is 0: h(Goal) = 0.
MESCE, DEPT OF IT 7
IT 407 - Knowledge Engineering 8
In other words, for any two states in the search space, a monotonic heuristic always
underestimates the cost of going from ni to nj. The heuristic is everywhere admissible.
If best-first search is used with a monotonic heuristic, you can skip checking for shortest path
when a state is encountered a second time. The second occurrence will not be shorter because
the heuristic finds the shortest path to any state the first time the state is found. Thus, for a
monotone heuristic, searching a graph is no different from searching a tree.
Any monotonic heuristic is admissible. For some sequence of states, s1, s2, s3,...,sg, from start
to goal, if the heuristic underestimates the cost of going from s1 to s2 and from s2 to s3 and so
on, then it underestimates the cost of going from any state to the goal. So, it follows that h(n)
<= cost(sn, sg) = h*(n).
Informedness
For two A* heuristics, h1 and h2, if h1(n) <= h2(n), for all states n in the search space, then
heuristic h2 is more informed than h1.
Eight Puzzle Heuristics: Breadth-first search is an A* heuristic with h1(n) = 0, for all n. We
have also noted that h2, the number of tiles out of place is a lower bound for h*. So, it follows
that:
Therefore, "number of tiles out of place" is more informed than breadth-first heuristic. This
should be obvious.
The following figure illustrates the difference between the "number of out-of-place tiles"
heuristic and breadth-first search. The heavy dark line shows the optimal solution path. The
states that show the numbers on the tiles are the states that would be the portion of the space
that is searched by the "tiles out-of-place" heuristic. The rest of the states shown are the ones
that would also be examined by breadth-first search. You can see the significant pruning done
by the more informed heuristic.
MESCE, DEPT OF IT 8
IT 407 - Knowledge Engineering 9
AO* Algorithm
Minimax is a recursive algorithm which is used to choose an optimal move for a player
assuming that the other player is also playing optimally. It is used in games such as tic-tac-toe,
go, chess, isola, checkers, and many other two-player games. Such games are called games of
perfect information because it is possible to see all the possible moves of a particular game.
There can be two-player games which are not of perfect information such as Scrabble because
the opponent’s move cannot be predicted.
MESCE, DEPT OF IT 9
IT 407 - Knowledge Engineering 10
It is similar to how we think when we play a game: “if I make this move, then my opponent
can only make only these moves,” and so on.
Minimax is called so because it helps in minimizing the loss when the other player chooses the
strategy having the maximum loss.
Terminology
Game Tree: It is a structure in the form of a tree consisting of all the possible moves which
allow you to move from a state of the game to the next state.
• Initial state: It comprises the position of the board and showing whose move it is.
• Successor function: It defines what the legal moves a player can make are.
• Terminal state: It is the position of the board when the game gets over.
• Utility function: It is a function which assigns a numeric value for the outcome of a game.
For instance, in chess or tic-tac-toe, the outcome is either a win, a loss, or a draw, and
these can be represented by the values +1, -1, or 0, respectively. There are games that have
a much larger range of possible outcomes; for instance, the utilities in backgammon varies
from +192 to -192. A utility function can also be called a payoff function.
There are two players involved in a game, called MIN and MAX. The player MAX tries to get
the highest possible score and MIN tries to get the lowest possible score, i.e., MIN and MAX
try to act opposite of each other.
Step 1: First, generate the entire game tree starting with the current position of the game all the
way upto the terminal states. This is how the game tree looks like for the game tic-tac-
toe.
MESCE, DEPT OF IT 10
IT 407 - Knowledge Engineering 11
1. The initial state is the first layer that defines that the board is blank it’s MAX’s turn to
play.
2. Successor function lists all the possible successor moves. It is defined for all the layers
in the tree.
3. Terminal State is the last layer of the tree that shows the final state, i.e whether the
player MAX wins, loses, or ties with the opponent.
4. Utilities in this case for the terminal states are 1, 0, and -1 as discussed earlier, and they
can be used to determine the utilities of the other nodes as well.
MESCE, DEPT OF IT 11
IT 407 - Knowledge Engineering 12
Step 2: Apply the utility function to get the utility values for all the terminal states.
Step 3: Determine the utilities of the higher nodes with the help of the utilities of the terminal
nodes. For instance, in the diagram below, we have the utilities for the terminal states
written in the squares.
Let us calculate the utility for the left node(red) of the layer above the terminal. Since it is the
move of the player MIN, we will choose the minimum of all the utilities. For this case, we
have to evaluate MIN{3, 5, 10}, which we know is certainly 3. So the utility for the red node
is 3.
Similarly, for the green node in the same layer, we will have to evaluate MIN{2,2} which is
2.
MESCE, DEPT OF IT 12
IT 407 - Knowledge Engineering 13
Step 4: Calculate the utility values with the help of leaves considering one layer at a time until
the root of the tree.
Step 5: Eventually, all the backed-up values reach to the root of the tree, i.e., the topmost point.
At that point, MAX has to choose the highest value. In our example, we only have 3
layers so we immediately reached to the root but in actual games, there will be many
more layers and nodes. So we have to evaluate MAX{3,2} which is 3.
Therefore, the best opening move for MAX is the left node(or the red one). This move is called
the minimax decision as it maximizes the utility following the assumption that the opponent is
also playing optimally to minimize it.
To summarize,
Alpha-beta pruning
The method that we are going to look in this article is called alpha-beta pruning. If we apply
alpha-beta pruning to a standard minimax algorithm, it returns the same move as the standard
one, but it removes (prunes) all the nodes that are possibly not affecting the final decision.
Let us understand the intuition behind this first and then we will formalize the algorithm.
Suppose, we have the following game tree:
MESCE, DEPT OF IT 13
IT 407 - Knowledge Engineering 14
In this case,
Minimax Decision = MAX{MIN{3,5,10}, MIN{2,a,b}, MIN{2,7,3}}
= MAX{3,c,2}
=3
You would be surprised! How could we calculate the maximum with a missing value? Here is
the trick. MIN{2,a,b} would certainly be less than or equal to 2, i.e., c<=2 and hence
MAX{3,c,2} has to be 3. The question now is do we really need to calculate c? Of course not.
We could have reached to the conclusion without looking at those nodes. And this is where
alpha-beta pruning comes into the picture.
A few definitions:
Alpha: It is the best choice so far for the player MAX. We want to get the highest possible
value here.
Beta: It is the best choice so far for MIN, and it has to be the lowest possible value.
Note: Each node has to keep track of its alpha and beta values. Alpha can be updated only
when it’s MAX’s turn and, similarly, beta can be updated only when it’s MIN’s chance.
1. Initialize alpha = -infinity and beta = infinity as the worst possible cases. The condition to
prune a node is when alpha becomes greater than or equal to beta.
MESCE, DEPT OF IT 14
IT 407 - Knowledge Engineering 15
2. Start with assigning the initial values of alpha and beta to root and since alpha is less than
beta we don’t prune it.
3. Carry these values of alpha and beta to the child node on the left. And now from the
utility value of the terminal state, we will update the values of alpha and be, so we
don’t have to update the value of beta. Again, we don’t prune because the condition
remains the same. Similarly, for the third child node also. And then backtracking to
the root we set alpha=3 because that is the minimum value that alpha can have.
4. Now, alpha=3 and beta=infinity at the root. So, we don’t prune. Carrying this to the center
node, and calculating MIN{2, infinity}, we get alpha=3 and beta=2.
5. Prune the second and third child nodes because alpha is now greater than beta.
6. Alpha at the root remains 3 because it is greater than 2. Carrying this to the rightmost child
node, evaluate MIN{infinity,2}=2. Update beta to 2 and alpha remains 3.
7. Prune the second and third child nodes because alpha is now greater than beta.
8. Hence, we get 3, 2, 2 at the left, center, and right MIN nodes, respectively. And calculating
MAX{3,2,2}, we get 3. Therefore, without even looking at four leaves we could correctly
find the minimax decision.
MESCE, DEPT OF IT 15