0% found this document useful (0 votes)
15 views15 pages

Ke 2

Uploaded by

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

Ke 2

Uploaded by

akshaykv2k16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

IT 407 - Knowledge Engineering 1

LECTURE NOTES
ON
IT407 KNOWLEDGE ENGINEERING

MODULE 2

Prepared By

Prof. SHIJU KUMAR P S


Department of Information Technology

M E S COLLEGE OF ENGINEERING, KUTTIPPURAM

MESCE, DEPT OF IT 1
IT 407 - Knowledge Engineering 2

KNOWLEDGE ENGINEERING SYLLABUS

Search Methods - Best First Search - Implementation in Python - OR Graphs, The A *


Algorithm, Problem Reduction AND-OR Graphs, The AO* algorithm, Constraint Satisfaction.
MINIMAX search procedure, Alpha–Beta pruning.

Module 2

Best First Search


In this section, we discuss a new method, best-first search, which is a way of combining the
advantages of both Depth and Breadth First Search

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,

h(n) - estimated straight line distance from node n to goal

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

Closed - nodes that have been already visited

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.

Consider the following graph as an example,

S: Initial state, G: goal.

Table shows the heuristic estimates:

node h(n) node h(n) node h(n)


A 11 E 4 I,J 3
B 5 F 2 S 15
C,D 9 H 7 G 0

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

The shortest path form S -> G is S -> B -> F -> G


Total cost:
1+3+1=4

MESCE, DEPT OF IT 4
IT 407 - Knowledge Engineering 5

A* Algorithm

The A* algorithm combines features of uniform-cost search and pure heuristic


search to efficiently compute optimal solutions. A* algorithm is a best-first search
algorithm in which the cost associated with a node is f(n) = g(n) + h(n), where
g(n) is the cost of the path from the initial state to node n and h(n) is the heuristic
estimate or the cost or a path from node n to a goal. Thus, f(n) estimates the lowest
total cost of any solution path going through node n. At each point a node with
lowest f value is chosen for expansion. Ties among nodes of equal f value should
be broken in favor of nodes with lower h values. The algorithm terminates when
a goal is chosen for expansion.

A* algorithm guides an optimal path to a goal if the heuristic function h(n) is


admissible, meaning it never overestimates actual cost. For example, since airline
distance never overestimates actual highway distance, and manhatten distance
never overestimates actual moves in the gliding tile.

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.

Admissibility, Monotonicity, Informedness

Three questions we ask about heuristic algorithms?

MESCE, DEPT OF IT 5
IT 407 - Knowledge Engineering 6

• Is it guaranteed to find the shortest path?


• Is it better than another heuristic?
• Does it make steady progress toward a goal?

Admissibility

A search algorithm is admissible if it is guaranteed to find a minimal path to a solution


whenever such a solution exists. Breadth first search is admissible, because it looks at every
state at level n before considering any state at level n+1.

The A* (A star) Algorithm

Let's characterize a class of admissible heuristic search strategies, using the evaluation
function:

f(n) = g(n) + h(n)

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.

Let's define the evaluation function f*:

f*(n) = g*(n) + h*(n)

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) >= g*(n)

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).

Algorithm A*: is algorithm A in which

h(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.

Key Point: All A* algorithms are admissible.

Breadth-first Search is an A* algorithm in which

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:

h1 <= h2 < h*(n)

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

1. Initialise the graph to start node


2. Traverse the graph following the current path accumulating nodes that have not yet been
expanded or solved
3. Pick any of these nodes and expand it and if it has no successors call this value FUTILITY
otherwise calculate only f' for each of the successors.
4. If f' is 0 then mark the node as SOLVED
5. Change the value of f' for the newly created node to reflect its successors by back
propagation.
6. Wherever possible use the most promising routes and if a node is marked as SOLVED then
mark the parent node as SOLVED.
7. If starting node is SOLVED or value greater than FUTILITY, stop, else repeat from 2.

What is the Minimax 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.

A game can be defined a search problem with the following components:

• 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.

How does the algorithm work?

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.

The general process of the Minimax algorithm is as follows:

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

Let us understand the defined terminology in terms of the diagram above.

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,

Minimax Decision = MAX{MIN{3,5,10},MIN{2,2}}


= MAX{3,2}
=3

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.

How does alpha-beta pruning work?

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy