HSS @AzDOCUMENTS - in
HSS @AzDOCUMENTS - in
Search Algorithms
Many traditional search algorithms are used in AI applications. For complex problems, the traditional
algorithms are unable to find the solutions within some practical time and spacelimits. Consequently,
many special techniques are developed, using heuristic functions.
The algorithms that use heuristic functions are called heuristic algorithms.
• Heuristic algorithms are not really intelligent; they appear to be intelligent because they achieve better
performance.
• Heuristic algorithms are more efficient because they take advantage of feedback from the datato direct
the search path.
• Uninformed search algorithms or Brute-force algorithms, search through the search space allpossible
candidates for the solution checking whether each candidate satisfies the problem’s statement.
• Informed search algorithms use heuristic functions that are specific to the problem, apply them to
guide the search through the search space to try to reduce the amount of time spent insearching.
A good heuristic will make an informed search dramatically outperform any uninformed search: for
example, the Traveling Salesman Problem (TSP), where the goal is to find is a good solution instead of
finding the best solution.
In such problems, the search proceeds using current information about the problem to predict which path
is closer to the goal and follow it, although it does not always guarantee to find thebest possible solution.
Such techniques help in finding a solution within reasonable time and space (memory). Some prominent
intelligent search algorithms are stated below:
1. Generate and Test Search
2. Best-first Search
3. Greedy Search
4. A* Search
5. Constraint Search
6. Means-ends analysis
There are some more algorithms. They are either improvements or combinations of these.
• Hierarchical Representation of Search Algorithms: A Hierarchical representation of mostsearch
algorithms is illustrated below. The representation begins with two types of search:
• Uninformed Search: Also called blind, exhaustive or brute-force search, it uses no information about
the problem to guide the search and therefore may not be very efficient.
• Informed Search: Also called heuristic or intelligent search, this uses information about theproblem
to guide the search—usually guesses the distance to a goal state and is therefore efficient, but the search
may not be always possible.
1
The first requirement is that it causes motion, in a game playing program, it moves on the boardand in
the water jug problem, filling water is used to fill jugs. It means the control strategies without the motion
will never lead to the solution.
The second requirement is that it is systematic, that is, it corresponds to the need for global motion as
well as for local motion. This is a clear condition that neither would it be rational to fill a jug and empty
it repeatedly, nor it would be worthwhile to move a piece round and round on the board in a cyclic way
in a game. We shall initially consider two systematic approaches forsearching. Searches can be classified
by the order in which operators are tried: depth-first, breadth-first, bounded depth-first.
2
Breadth-first search
A Search strategy, in which the highest layer of a decision tree is searched completely beforeproceeding
to the next layer is called Breadth-first search (BFS).
• In this strategy, no viable solutions are omitted and therefore it is guaranteed that an optimalsolution is
found.
• This strategy is often not feasible when the search space is large.
Algorithm
1. Create a variable called LIST and set it to be the starting state.
2. Loop until a goal state is found or LIST is empty, Do
a. Remove the first element from the LIST and call it E. If the LIST is empty, quit.
b. For every path each rule can match the state E, Do
(i) Apply the rule to generate a new state.
(ii) If the new state is a goal state, quit and return this state.
(iii) Otherwise, add the new state to the end of LIST.
3
Advantages
1. Guaranteed to find an optimal solution (in terms of shortest number of stepsto reach the goal).
2. Can always find a goal node if one exists (complete).
Disadvantages
1. High storage requirement: exponential with tree depth.
Depth-first search
A search strategy that extends the current path as far as possible before backtracking to the lastchoice
point and trying the next alternative path is called Depth-first search (DFS).
• This strategy does not guarantee that the optimal solution has been found.
• In this strategy, search reaches a satisfactory solution more rapidly than breadth first, anadvantage when
the search space is large.
Algorithm
Depth-first search applies operators to each newly generated state, trying to drive directly toward the
goal.
1. If the starting state is a goal state, quit and return success.
2. Otherwise, do the following until success or failure is signalled:
a. Generate a successor E to the starting state. If there are no more successors, then signal failure.
b. Call Depth-first Search with E as the starting state.
c. If success is returned signal success; otherwise, continue in the loop.
Advantages
1. Low storage requirement: linear with tree depth.
2. Easily programmed: function call stack does most of the work of maintaining state of thesearch.
Disadvantages
1. May find a sub-optimal solution (one that is deeper or more costly than the best solution).
2. Incomplete: without a depth bound, may not find a solution even if one exists.
2.4.2.3 Bounded depth-first search
Depth-first search can spend much time (perhaps infinite time) exploring a very deep path that does not
contain a solution, when a shallow solution exists. An easy way to solve this problem isto put a maximum
depth bound on the search. Beyond the depth bound, a failure is generated automatically without
exploring any deeper.
Problems:
1. It’s hard to guess how deep the solution lies.
2. If the estimated depth is too deep (even by 1) the computer time used is dramatically increased, by a
factor of bextra.
3. If the estimated depth is too shallow, the search fails to find a solution; all that computer timeis wasted.
Heuristics
A heuristic is a method that improves the efficiency of the search process. These are like tour guides.
There are good to the level that they may neglect the points in general interesting directions; they are
bad to the level that they may neglect points of interest to particular individuals. Some heuristics help in
the search process without sacrificing any claims to entiretythat the process might previously had. Others
may occasionally cause an excellent path to be overlooked. By sacrificing entirety it increases efficiency.
Heuristics may not find the best
4
solution every time but guarantee that they find a good solution in a reasonable time. These are
particularly useful in solving tough and complex problems, solutions of which would require infinite
time, i.e. far longer than a lifetime for the problems which are not solved in any other way.
Heuristic search
To find a solution in proper time rather than a complete solution in unlimited time we use heuristics. ‘A
heuristic function is a function that maps from problem state descriptions to measures of desirability,
usually represented as numbers’. Heuristic search methods use knowledge about the problem domain
and choose promising operators first. These heuristic search methods use heuristic functions to evaluate
the next state towards the goal state. For finding a solution, by using the heuristic technique, one should
carry out the following steps:
1. Add domain—specific information to select what is the best path to continue searching along.
2. Define a heuristic function h(n) that estimates the ‘goodness’ of a node n.
Specifically, h(n) = estimated cost(or distance) of minimal cost path from n to a goal state.
3. The term, heuristic means ‘serving to aid discovery’ and is an estimate, based on domain specific
information that is computable from the current state description of how close we are toa goal.
Finding a route from one city to another city is an example of a search problem in whichdifferent search
orders and the use of heuristic knowledge are easily understood.
1. State: The current city in which the traveller is located.
2. Operators: Roads linking the current city to other cities.
3. Cost Metric: The cost of taking a given road between cities.
4. Heuristic information: The search could be guided by the direction of the goal city from the current
city, or we could use airline distance as an estimate of the distance to the goal. Heuristic search
techniques
For complex problems, the traditional algorithms, presented above, are unable to find the solution within
some practical time and space limits. Consequently, many special techniques are developed, using
heuristic functions.
• Blind search is not always possible, because it requires too much time or Space (memory).
5
Heuristic search compared with other search
The Heuristic search is compared with Brute force or Blind search techniques below:
Comparison of Algorithms
No knowledge about how far a node Guides search process toward goalnode from goal
state
Prefers states (nodes) that lead close to and not
away from goalstate
Suppose there are N cities, then a solution would be to take N! possible combinations to find theshortest
distance to decide the required route. This is not efficient as with N=10 there are 36,28,800 possible
routes. This is an example of combinatorial explosion.
There are better methods for the solution of such problems: one is called branch and bound. First,
generate all the complete paths and find the distance of the first complete path. If the nextpath is shorter,
then save it and proceed this way avoiding the path when its length exceeds the saved shortest path
length, although it is better than the previous method.
Generate-And-Test Algorithm
Generate-and-test search algorithm is a very simple algorithm that guarantees to find a solution ifdone
systematically and there exists a solution.
Algorithm: Generate-And-Test
1. Generate a possible solution.
2. Test to see if this is the expected solution.
3. If the solution has been found quit else go to step 1.
Potential solutions that need to be generated vary depending on the kinds of problems. For someproblems
the possible solutions may be particular points in the problem space and for some problems, paths from
the start state.
6
Figure: Generate And Test
Generate-and-test, like depth-first search, requires that complete solutions be generated fortesting. In its
most systematic form, it is only an exhaustive search of the problem space.
Solutions can also be generated randomly but solution is not guaranteed. This approach is what isknown
as British Museum algorithm: finding an object in the British Museum by wandering randomly.
Systematic Generate-And-Test
While generating complete solutions and generating random solutions are the two extremes thereexists
another approach that lies in between. The approach is that the search process proceeds systematically
but some paths that unlikely to lead the solution are not considered. This evaluation is performed by a
heuristic function.
Depth-first search tree with backtracking can be used to implement systematic generate-and-test
procedure. As per this procedure, if some intermediate states are likely to appear often in the tree, it
would be better to modify that procedure to traverse a graph rather than a tree.
Generate-And-Test And Planning
Exhaustive generate-and-test is very useful for simple problems. But for complex problems evenheuristic
generate-and-test is not very effective technique. But this may be made effective by combining with
other techniques in such a way that the space in which to search is restricted. AnAI program DENDRAL,
for example, uses plan-Generate-and-test technique. First, the planning process uses constraint-
satisfaction techniques and creates lists of recommended and contraindicated substructures. Then the
generate-and-test procedure uses the lists generated and required to explore only a limited set of
structures. Constrained in this way, generate-and-test proved highly effective. A major weakness of
planning is that it often produces inaccurate solutions as there is no feedback from the world. But if it is
used to produce only pieces of solutions then lack of detailed accuracy becomes unimportant.
7
Hill Climbing
Hill Climbing is heuristic search used for mathematical optimization problems in the field ofArtificial
Intelligence .
Given a large set of inputs and a good heuristic function, it tries to find a sufficiently goodsolution to the
problem. This solution may not be the global optimal maximum.
▪ In the above definition, mathematical optimization problems implies that hill climbing solves the
problems where we need to maximize or minimize a given real function by choosing values from
the given inputs. Example-Travelling salesman problem where weneed to minimize the distance
traveled by salesman.
▪ ‘Heuristic search’ means that this search algorithm may not find the optimal solution to the
problem. However, it will give a good solution in reasonable time.
▪ A heuristic function is a function that will rank all the possible alternatives at any branching step
in search algorithm based on the available information. It helps thealgorithm to select the best
route out of possible routes.
Features of Hill Climbing
1. Variant of generate and test algorithm : It is a variant of generate and test algorithm. Thegenerate
and test algorithm is as follows :
1. Simple Hill climbing : It examines the neighboring nodes one by one and selects the first
neighboring node which optimizes the current cost as next node.
Algorithm for Simple Hill climbing :
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 beapplied
to current state.
a) Select a state that has not been yet applied to the current state and apply it to produce a newstate.
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.
8
2. Steepest-Ascent Hill climbing : It first examines all the neighboring nodes and thenselects the
node closest to the solution state as next node.
Step 1 : Evaluate the initial state. If it is goal state then exit else make the current state as initial state
Step 2 : Repeat these steps until a solution is found or current state does not change
i. Let ‘target’ be a state such that any successor of the current state will be better than it;
ii. for each operator that applies to the current state
a. apply the new operator and create a new state
b. evaluate the new state
c. if this state is goal state then quit else compare with ‘target’
d. if this state is better than ‘target’, set this state as ‘target’
e. if target is better than current state set current state to TargetStep 3 : Exit
3. Stochastic hill climbing : 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 onthe amount of
improvement in that neighbor) whether to move to that neighbor or to examine another.
State Space diagram for Hill Climbing
State space diagram is a graphical representation of the set of states our search algorithm canreach vs the
value of our objective function(the function which we wish to maximize).
X- axis : denotes the state space ie states or configuration our algorithm may reach.
2. Global maximum : It is the best possible state in the state space diagram. This because at this
state, objective function has highest value.
3. Plateua/flat local maximum : It is a flat region of state space where neighboring stateshave the
same value.
4. Ridge : It is region which is higher than its neighbours but itself has a slope. It is a specialkind of
local maximum.
5. Current state : The region of state space diagram where we are currently present duringthe search.
6. Shoulder : It is a plateau that has an uphill edge.Problems in different regions in Hill climbing
Hill climbing cannot reach the optimal/best state(global maximum) if it enters any of the following
regions :
1. Local maximum : At a local maximum all neighboring states have a values which is worse than
than the current state. Since hill climbing uses greedy approach, it will not move to the worse
state and terminate itself. The process will end even though a bettersolution may exist.
To overcome local maximum problem : Utilize backtracking technique. Maintain a list ofvisited
states. If the search reaches an undesirable state, it can backtrack to the previous configuration
and explore a new path.
2. Plateau : On plateau all neighbors have same value . Hence, it is not possible to select thebest
direction.
To overcome plateaus : Make a big jump. Randomly select a state far away from current state. Chances
are that we will land at a non-plateau region
3. Ridge : Any point on a ridge can look like peak because movement in all possibledirections is
downward. Hence the algorithm stops when it reaches this state.
9
To overcome Ridge : In this kind of obstacle, use two or more rules before testing. Itimplies
moving in several directions at once.
In BFS and DFS, when we are at a node, we can consider any of the adjacent as next node. So
both BFS and DFS blindly explore paths without considering any cost function. The idea of Best First
Search is to use an evaluation function to decide which adjacent is most promising and then explore.
Best First Search falls under the category of Heuristic Search or Informed Search.
We use a priority queue to store costs of nodes. So the implementation is a variation of BFS, wejust need
to change Queue to PriorityQueue.
Algorithm:
Best-First-Search(Grah g, Node start)
1) Create an empty PriorityQueuePriorityQueue pq;
2) Insert "start" in pq.pq.insert(start)
3) Until PriorityQueue is empty u = PriorityQueue.DeleteMin
If u is the goalExit
Else
Foreach neighbor v of u If v "Unvisited"
Mark v "Visited"pq.insert(v)
Mark v "Examined"End procedure
Let us consider below example.
We start from source "S" and search forgoal "I" using given costs and Best First search.
10
pq initially contains S
We remove s from and process unvisitedneighbors of S to pq.
pq now contains {A, C, B} (C is putbefore B because C has lesser cost)
A* Search Algorithm
A* is a type of search algorithm. Some problems can be solved by representing the world in the initial
state, and then for each action we can perform on the world we generate states for what theworld would
be like if we did so. If you do this until the world is in the state that we specified as a solution, then the
route from the start to this goal state is the solution to your problem.
In this tutorial I will look at the use of state space search to find the shortest path between twopoints
(pathfinding), and also to solve a simple sliding tile puzzle (the 8-puzzle). Let's look atsome of the terms
used in Artificial Intelligence when describing this state space search.
Some terminology
A node is a state that the problem's world can be in. In pathfinding a node would be just a 2d coordinate
of where we are at the present time. In the 8-puzzle it is the positions of all the tiles.Next all the nodes
are arranged in a graph where links between nodes represent valid steps in solving the problem. These
links are known as edges. In the 8-puzzle diagram the edges are shown as blue lines. See figure 1 below.
State space search, then, is solving a problem by beginning with the start state, and then for eachnode
we expand all the nodes beneath it in the graph by applying all the possible moves that can be made at
each point.
At this point we introduce an important concept, the heuristic. This is like an algorithm, but witha key
difference. An algorithm is a set of steps which you can follow to solve a problem, which always works
for valid input. For example you could probably write an algorithm yourself for
11
multiplying two numbers together on paper. A heuristic is not guaranteed to work but is useful inthat it
may solve a problem for which there is no algorithm.
We need a heuristic to help us cut down on this huge search problem. What we need is to use ourheuristic
at each node to make an estimate of how far we are from the goal. In pathfinding we know exactly how
far we are, because we know how far we can move each step, and we can calculate the exact distance to
the goal.
But the 8-puzzle is more difficult. There is no known algorithm for calculating from a given position
how many moves it will take to get to the goal state. So various heuristics have been devised. The best
one that I know of is known as the Nilsson score which leads fairly directly tothe goal most of the time,
as we shall see.
Cost
When looking at each node in the graph, we now have an idea of a heuristic, which can estimate how
close the state is to the goal. Another important consideration is the cost of getting to wherewe are. In
the case of pathfinding we often assign a movement cost to each square. The cost is the same then the
cost of each square is one. If we wanted to differentiate between terrain types we may give higher costs
to grass and mud than to newly made road. When looking at a node wewant to add up the cost of what it
took to get here, and this is simply the sum of the cost of this node and all those that are above it in the
graph.
8 Puzzle
Let's look at the 8 puzzle in more detail. This is a simple sliding tile puzzle on a 3*3 grid where one tile
is missing and you can move the other tiles into the gap until you get the puzzle into thegoal position.
See figure 1.
There are 362,880 different states that the puzzle can be in, and to find a solution the search hasto find
a route through them. From most positions of the search the number of edges (that's the
12
blue lines) is two. That means that the number of nodes you have in each level of the search is2^d where
d is the depth. If the number of steps to solve a particular state is 18, then that s 262,144 nodes just at
that level.
The 8 puzzle game state is as simple as representing a list of the 9 squares and what's in them. Here are
two states for example; the last one is the GOAL state, at which point we've found thesolution. The first
is a jumbled up example that you may start from.
Pathfinding
In a video game, or some other pathfinding scenario, you want to search a state space and find out how
to get from somewhere you are to somewhere you want to be, without bumping into walls or going too
far. For reasons we will see later, the A* algorithm will not only find a path, ifthere is one, but it will
find the shortest path. A state in pathfinding is simply a position in the world. In the example of a maze
game like Pacman you can represent where everything is using a simple 2d grid. The start state for a
ghost say, would be the 2d coordinate of where the ghost isat the start of the search. The goal state would
be where pacman is so we can go and eat him.
There is also example code to do pathfinding on the github site.
13
Implementing A*
We are now ready to look at the operation of the A* algorithm. What we need to do is start withthe goal
state and then generate the graph downwards from there. Let's take the 8-puzzle in figure 1. We ask how
many moves can we make from the start state? The answer is 2, there are two directions we can move
the blank tile, and so our graph expands.
If we were just to continue blindly generating successors to each node, we could potentially fill the
computer's memory before we found the goal node. Obviously we need to remember the bestnodes and
search those first. We also need to remember the nodes that we have expanded already, so that we don't
expand the same state repeatedly.
Let's start with the OPEN list. This is where we will remember which nodes we haven't yet expanded.
When the algorithm begins the start state is placed on the open list, it is the only statewe know about and
we have not expanded it. So we will expand the nodes from the start and putthose on the OPEN list too.
Now we are done with the start node and we will put that on the CLOSED list. The CLOSED list is a
list of nodes that we have expanded.
f=g+h
Using the OPEN and CLOSED list lets us be more selective about what we look at next in the search.
We want to look at the best nodes first. We will give each node a score on how good wethink it is. This
score should be thought of as the cost of getting from the node to the goal plus the cost of getting to
where we are. Traditionally this has been represented by the letters f, g and
h. 'g' is the sum of all the costs it took to get here, 'h' is our heuristic function, the estimate of what it will
take to get to the goal. 'f' is the sum of these two. We will store each of these in ournodes.
Using the f, g and h values the A* algorithm will be directed, subject to conditions we will lookat further
on, towards the goal and will find it in the shortest route possible.
So far we have looked at the components of the A*, let's see how they all fit together to make the
algorithm :
Pseudocode
Hopefully the ideas we looked at in the preceding paragraphs will now click into place as welook at the
A* algorithm pseudocode. You may find it helpful to print this out or leave the window open while we
discuss it.
To help make the operation of the algorithm clear we will look again at the 8-puzzle problem infigure 1
above. Figure 3 below shows the f,g and h scores for each of the tiles.
14
Figure 3 : 8-Puzzle state space showing f,g,h scores
First of all look at the g score for each node. This is the cost of what it took to get from the startto that
node. So in the picture the center number is g. As you can see it increases by one at each level. In some
problems the cost may vary for different state changes. For example in pathfinding there is sometimes a
type of terrain that costs more than other types.
Next look at the last number in each triple. This is h, the heuristic score. As I mentioned above I am
using a heuristic known as Nilsson's Sequence, which converges quickly to a correct solutionin many
cases. Here is how you calculate this score for a given 8-puzzle state :
Advantages:
It is the best one from other techniques. It is used to solve very complex problems.
It is optimally efficient, i.e. there is no other optimal algorithm guaranteed to expand fewer nodesthan
A*.
Disadvantages:
This algorithm is complete if the branching factor is finite and every action has fixed cost.
The speed execution of A* search is highly dependant on the accuracy of the heuristic algorithmthat is
used to compute h (n).
15
AO* Search: (And-Or) Graph
The Depth first search and Breadth first search given earlier for OR trees or graphs can be easilyadopted
by AND-OR graph. The main difference lies in the way termination conditions are determined, since all
goals following an AND nodes must be realized; where as a single goal node following an OR node will
do. So for this purpose we are using AO* algorithm.
Like A* algorithm here we will use two arrays and one heuristic function.
OPEN:
It contains the nodes that has been traversed but yet not been marked solvable or unsolvable.
CLOSE:
ALGORITH
Step 1: Place the starting node into OPEN.
Step 3: Select a node n that is both on OPEN and a member of T0. Remove it from OPEN andplace it in
CLOSE
Step 4: If n is the terminal goal node then leveled n as solved and leveled all the ancestors of nas solved.
If the starting node is marked as solved then success and exit.
Step 5: If n is not a solvable node, then mark n as unsolvable. If starting node is marked asunsolvable,
then return failure and exit.
Step 6: Expand n. Find all its successors and find their h (n) value, push them into OPEN.
Step 8: Exit.
16
Implementation:
Step 1:
In the above graph, the solvable nodes are A, B, C, D, E, F and the unsolvable nodes are G, H.Take A as
the starting node. So place A into OPEN.
17
18
Advantages:
It is an optimal algorithm.
If traverse according to the ordering of nodes. It can be used for both OR and AND graph.
Disadvantages:
Sometimes for unsolvable nodes, it can’t find the optimal path. Its complexity is than otheralgorithms.
PROBLEM REDUCTION
When a problem can be divided into a set of sub problems, where each sub problem can be solved
separately and a combination of these will be a solution, AND-OR graphs or AND - ORtrees are used
for representing the solution. The decomposition of the problem or problem reduction generates AND
arcs. One AND are may point to any number of successor nodes. All
these must be solved so that the arc will rise to many arcs, indicating several possible solutions. Hence
the graph is known as AND - OR instead of AND. Figure shows an AND - OR graph.
An algorithm to find a solution in an AND - OR graph must handle AND area appropriately. A*algorithm
can not search AND - OR graphs efficiently. This can be understand from the give figure.
In figure (a) the top node A has been expanded producing two area one leading to B and leadingto C-D
. the numbers at each node represent the value of f ' at that node (cost of getting to the goal state from
current state). For simplicity, it is assumed that every operation(i.e. applying a rule) has unit cost, i.e.,
each are with single successor will have a cost of 1 and each of its components. With the available
information till now , it appears that C is the most promising node to expand since its f ' = 3 , the lowest
19
but going through B would be better since to use C we must also use D' and the cost would be
9(3+4+1+1). Through B it would be 6(5+1).
Thus the choice of the next node to expand depends not only n a value but also on whether that node is
part of the current best path form the initial mode. Figure (b) makes this clearer. In figurethe node G
appears to be the most promising node, with the least f ' value. But G is not on the current beat path,
since to use G we must use GH with a cost of 9 and again this demands that arcs be used (with a cost of
27). The path from A through B, E-F is better with a total cost of (17+1=18). Thus we can see that to
search an AND-OR graph, the following three things must be done.
1. traverse the graph starting at the initial node and following the current best path, andaccumulate the
set of nodes that are on the path and have not yet been expanded.
2. Pick one of these unexpanded nodes and expand it. Add its successors to the graph andcomputer f '
(cost of the remaining distance) for each of them.
3. Change the f ' estimate of the newly expanded node to reflect the new information producedby its
successors. Propagate this change backward through the graph. Decide which of the current best path.
The propagation of revised cost estimation backward is in the tree is not necessary in A* algorithm. This
is because in AO* algorithm expanded nodes are re-examined so that the currentbest path can be selected.
The working of AO* algorithm is illustrated in figure as follows:
Referring the figure. The initial node is expanded and D is Marked initially as promising node. D is
expanded producing an AND arc E-F. f ' value of D is updated to 10. Going backwards we can see that
the AND arc B-C is better . it is now marked as current best path. B and C have to be expanded next.
This process continues until a solution is found or all paths have led to dead ends,indicating that there is
no solution. An A* algorithm the path from one node to the other is always that of the lowest cost and it
is independent of the paths through other nodes.
The algorithm for performing a heuristic search of an AND - OR graph is given below. Unlike A*
algorithm which used two lists OPEN and CLOSED, the AO* algorithm uses a single structure G. G
represents the part of the search graph generated so far. Each node in G points down to its immediate
successors and up to its immediate predecessors, and also has with it the value of h' cost of a path from
itself to a set of solution nodes. The cost of getting from the start nodes to the current node "g" is not
stored as in the A* algorithm. This is because it is not possible to compute a single such value since there
20
may be many paths to the same state. In AO* algorithm serves as the estimate of goodness of a node.
Also a there should value called FUTILITY is used. The estimated cost of a solution is greater than
FUTILITY then the search isabandoned as too expansive to be practical.
For representing above graphs AO* algorithm is as follows
AO* ALGORITHM:
1. Let G consists only to the node representing the initial state call this node INTT. Computeh' (INIT).
2. Until INIT is labeled SOLVED or hi (INIT) becomes greater than FUTILITY, repeat thefollowing
procedure.
(I) Trace the marked arcs from INIT and select an unbounded node NODE.
(II) Generate the successors of NODE . if there are no successors then assign FUTILITY ash' (NODE).
This means that NODE is not solvable. If there are successors then for each
o
n called SUCCESSOR, that is not also an ancester of NODE do the following
e
(b) if successor is not a terminal node, mark it solved and assign zero to its h ' value.
(III) propagate the newly discovered information up the graph by doing the following . let S be aset of
nodes that have been marked SOLVED. Initialize S to NODE. Until S is empty
repeat
the following procedure;
(b) compute h' of each of the arcs emerging from CURRENT , Assign minimum h' toCURRENT.
(c) Mark the minimum cost path a s the best out of CURRENT.
(d) Mark CURRENT SOLVED if all of the nodes connected to it through the new markedare have
been labeled SOLVED.
(e) If CURRENT has been marked SOLVED or its h ' has just changed, its new status
m
u be propagate backwards up the graph . hence all the ancestors of CURRENT are addedto S.
s
t
21
(Refered From Artificial Intelligence TMH)AO* Search Procedure.
2. Using the search tree, compute the most promising solution tree TP .
3. Select node n that is both on open and a part of tp, remove n from open and place it no closed.
4. If n is a goal node, label n as solved. If the start node is solved, exit with success where tp isthe solution
tree, remove all nodes from open with a solved ancestor.
5. If n is not solvable node, label n as unsolvable. If the start node is labeled as unsolvable, exitwith
failure. Remove all nodes from open ,with unsolvable ancestors.
6. Otherwise, expand node n generating all of its successor compute the cost of for each newlygenerated
node and place all such nodes on open.
7. Go back to step(2)
CONSTRAINT SATISFACTION:-
Many problems in AI can be considered as problems of constraint satisfaction, in which the goalstate
satisfies a given set of constraint. constraint satisfaction problems can be solved by using any of the
search strategies. The general form of the constraint satisfaction procedure is as follows:
Until a complete solution is found or until all paths have led to lead ends, do
2. Apply the constraint inference rules to the selected node to generate all possible newconstraints.
3. If the set of constraints contains a contradiction, then report that this path is a dead end.
5. If neither a constraint nor a complete solution has been found then apply the rules to generatenew
partial solutions. Insert these partial solutions into the search graph.
SEND
+ MORE
MONEY
22
Assign decimal digit to each of the letters in such a way that the answer to the problem is correctto the
same letter occurs more than once , it must be assign the same digit each time . no two different letters
may be assigned the same digit. Consider the crypt arithmetic problem.
SEND
+ MORE
MONEY
CONSTRAINTS:-
2. Assumption can be made at various levels such that they do not contradict each other.
3. The problem can be decomposed into secured constraints. A constraint satisfaction approachmay be
used.
Goal State: the digits to the letters must be assigned in such a manner so that the sum is satisfied.
Solution Process:
23
1. initial guess m=1 because the sum of two single digits can generate at most a carry '1'.
2. When n=1 o=0 or 1 because the largest single digit number added to m=1 can generate the sum of
either 0 or 1 depend on the carry received from the carry sum. By this we conclude thato=0 because m
is already 1 hence we cannot assign same digit another letter(rule no.)
3. We have m=1 and o=0 to get o=0 we have s=8 or 9, again depending on the carry receivedfrom the
earlier sum.
The same process can be repeated further. The problem has to be composed into various constraints. And
each constraints is to be satisfied by guessing the possible digits that the letterscan be assumed that the
initial guess has been already made . rest of the process is being shownin the form of a tree, using depth-
first search for the clear understandability of the solution process.
D>6(Controduction)
24
25
MEANS - ENDS ANALYSIS:-
Most of the search strategies either reason forward of backward however, often a mixture o the two
directions is appropriate. Such mixed strategy would make it possible to solve the major parts of problem
first and solve the smaller problems the arise when combining them together. Such a technique is called
"Means - Ends Analysis".
The means -ends analysis process centers around finding the difference between current state andgoal
state. The problem space of means - ends analysis has an initial state and one or more goal state, a set of
operate with a set of preconditions their application and difference functions that computes the difference
between two state a(i) and s(j). A problem is solved using means - ends analysis by
1. Computing the current state s1 to a goal state s2 and computing their difference D12.
2. Satisfy the preconditions for some recommended operator op is selected, then to reduce thedifference
D12.
3. The operator OP is applied if possible. If not the current state is solved a goal is created andmeans-
ends analysis is applied recursively to reduce the sub goal.
4. If the sub goal is solved state is restored and work resumed on the original problem.
( the first AI program to use means - ends analysis was the GPS General problem solver)
means- ends analysis I useful for many human planning activities. Consider the example of planing for
an office worker. Suppose we have a different table of three rules:
1. If in out current state we are hungry , and in our goal state we are not hungry , then either the"visit
hotel" or "visit Canteen " operator is recommended.
2. If our current state we do not have money , and if in your goal state we have money, then the"Visit
our bank" operator or the "Visit secretary" operator is recommended.
3. If our current state we do not know where something is , need in our goal state we do know,then either
the "visit office enquiry" , "visit secretary" or "visit co worker " operator is recommended.
KNOWLEDGE REPRESENTATION
KNOWLEDGE REPRESENTATION:-
For the purpose of solving complex problems c\encountered in AI, we need both a large amount of
knowledge and some mechanism for manipulating that knowledge to create solutions to new problems.
A variety of ways of representing knowledge (facts) have been exploited in AI programs. In all variety
of knowledge representations , we deal with two kinds of entities.
A. Facts: Truths in some relevant world. These are the things we want to represent.
26
B. Representations of facts in some chosen formalism . these are things we will actually be able to
manipulate.
One way to think of structuring these entities is at two levels : (a) the knowledge level, at whichfacts are
described, and (b) the symbol level, at which representations of objects at the knowledge level are defined
in terms of symbols that can be manipulated by programs.
The facts and representations are linked with two-way mappings. This link is called representation
mappings. The forward representation mapping maps from facts to representations. The backward
representation mapping goes the other way, from representations
to facts.
One common representation is natural language (particularly English) sentences. Regardless of the
representation for facts we use in a program , we may also need to be concerned with an English
representation of those facts in order to facilitate getting information into and out of the system. We need
mapping functions from English sentences to the representation we actually use and from it back to
sentences.
Representations and Mappings
• In order to solve complex problems encountered in artificial intelligence, one needs both a large
amount of knowledge and some mechanism for manipulating that knowledge to create solutions.
• Knowledge and Representation are two distinct entities. They play central butdistinguishable
roles in the intelligent system.
• Knowledge is a description of the world. It determines a system’s competence by what itknows.
• Moreover, Representation is the way knowledge is encoded. It defines a system’sperformance in
doing something.
• Different types of knowledge require different kinds of representation.
• Given the facts, it is not possible to answer a simple question such as: “Who is the heaviest
player?”
• Also, But if a procedure for finding the heaviest player is provided, then these facts willenable
that procedure to compute an answer.
• Moreover, We can ask things like who “bats – left” and “throws – right”.
Inheritable Knowledge
• Here the knowledge elements inherit attributes from their parents.
• The knowledge embodied in the design hierarchies found in the functional, physical andprocess
domains.
• Within the hierarchy, elements inherit attributes from their parents, but in many cases, notall
29
attributes of the parent elements prescribed to the child elements.
• Also, The inheritance is a powerful form of inference, but not adequate.
• Moreover, The basic KR (Knowledge Representation) needs to augment with inference
mechanism.
• Property inheritance: The objects or elements of specific classes inherit attributes andvalues from
more general classes.
• So, The classes organized in a generalized hierarchy.
31
• Functions, which are a subset of relations where there is only one “value” for any given“input”
First-order Predicate logic (FOPL) provides
• Constants: a, b, dog33. Name a specific object.
• Variables: X, Y. Refer to an object without naming it.
• Functions: Mapping from objects to objects.
• Terms: Refer to objects
• Atomic Sentences: in(dad-of(X), food6) Can be true or false, Correspond to propositional
symbols P, Q.
A well-formed formula (wff) is a sentence containing no “free” variables. So, That is, allvariables are
“bound” by universal or existential quantifiers.
(∀x)P(x, y) has x bound as a universally quantified variable, but y is free.
Quantifiers
Universal quantification
• (∀x)P(x) means that P holds for all values of x in the domain associated with that variable
• E.g., (∀x) dolphin(x) → mammal(x)Existential quantification
• (∃ x)P(x) means that P holds for some value of x in the domain associated with that variable
• E.g., (∃ x) mammal(x) 𝖠 lays-eggs(x)
Also, Consider the following example that shows the use of predicate logic as a way ofrepresenting
knowledge.
1. Marcus was a man.
2. Marcus was a Pompeian.
3. All Pompeians were Romans.
4. Caesar was a ruler.
5. Also, All Pompeians were either loyal to Caesar or hated him.
6. Everyone is loyal to someone.
7. People only try to assassinate rulers they are not loyal to.
8. Marcus tried to assassinate Caesar.
The facts described by these sentences can be represented as a set of well-formed formulas (wffs)as
follows:
1. Marcus was a man.
• man(Marcus)
2. Marcus was a Pompeian.
• Pompeian(Marcus)
3. All Pompeians were Romans.
• ∀x: Pompeian(x) → Roman(x)
4. Caesar was a ruler.
• ruler(Caesar)
5. All Pompeians were either loyal to Caesar or hated him.
• inclusive-or
• ∀x: Roman(x) → loyalto(x, Caesar) ∨ hate(x, Caesar)
• exclusive-or
• ∀x: Roman(x) → (loyalto(x, Caesar) 𝖠¬ hate(x, Caesar)) ∨
• (¬loyalto(x, Caesar) 𝖠 hate(x, Caesar))
32
6. Everyone is loyal to someone.
• ∀x: ∃y: loyalto(x, y)
7. People only try to assassinate rulers they are not loyal to.
• ∀x: ∀y: person(x) 𝖠 ruler(y) 𝖠 tryassassinate(x, y)
• →¬loyalto(x, y)
8. Marcus tried to assassinate Caesar.
• tryassassinate(Marcus, Caesar)
Now suppose if we want to use these statements to answer the question: Was Marcus loyal toCaesar?
Also, Now let’s try to produce a formal proof, reasoning backward from the desired goal: ¬
Ioyalto(Marcus, Caesar)
In order to prove the goal, we need to use the rules of inference to transform it into another goal (or
possibly a set of goals) that can, in turn, transformed, and so on, until there are no unsatisfiedgoals
remaining.
VTUP
Figure: An attempt to prove ¬loyalto(Marcus, Caesar).
• The problem is that, although we know that Marcus was a man, we do not have any way to
conclude from that that Marcus was a person. Also, We need to add the representationof another
fact to our system, namely: ∀ man(x) → person(x)
• Now we can satisfy the last goal and produce a proof that Marcus was not loyal toCaesar.
• Moreover, From this simple example, we see that three important issues must be addressed in the
process of converting English sentences into logical statements and thenusing those statements
to deduce new ones:
1. Many English sentences are ambiguous (for example, 5, 6, and 7 above).Choosing the
correct interpretation may be difficult.
2. Also, There is often a choice of how to represent the knowledge. Simple representations
are desirable, but they may exclude certain kinds of reasoning.
3. Similalry, Even in very simple situations, a set of sentences is unlikely to contain all the
information necessary to reason about the topic at hand. In order to be ableto use a set of
statements effectively. Moreover, It is usually necessary to have access to another set of
statements that represent facts that people consider too obvious to mention.
33
Representing Instance and ISA Relationships
• Specific attributes instance and isa play an important role particularly in a useful form of
reasoning called property inheritance.
• The predicates instance and isa explicitly captured the relationships they used to express,namely
class membership and class inclusion.
• 4.2 shows the first five sentences of the last section represented in logic in three differentways.
• The first part of the figure contains the representations we have already discussed. In these
representations, class membership represented with unary predicates (such as Roman), each of
which corresponds to a class.
• Asserting that P(x) is true is equivalent to asserting that x is an instance (or element) of P.
• The second part of the figure contains representations that use the instance predicateexplicitly.
VTUPulse.
Figure: Three ways of representing class membership: ISA Relationships
• The predicate instance is a binary one, whose first argument is an object and whose second
argument is a class to which the object belongs.
• But these representations do not use an explicit isa predicate.
• Instead, subclass relationships, such as that between Pompeians and Romans, describedas shown
in sentence 3.
• The implication rule states that if an object is an instance of the subclass Pompeian then itis an
instance of the superclass Roman.
• Note that this rule is equivalent to the standard set-theoretic definition of the subclass-superclass
relationship.
• The third part contains representations that use both the instance and isa predicatesexplicitly.
• The use of the isa predicate simplifies the representation of sentence 3, but it requires thatone
additional axiom (shown here as number 6) be provided.
34
Computable Functions and Predicates
• To express simple facts, such as the following greater-than and less-than relationships:gt(1,O)
It(0,1) gt(2,1) It(1,2) gt(3,2) It( 2,3)
• It is often also useful to have computable functions as well as computable predicates.Thus we
might want to be able to evaluate the truth of gt(2 + 3,1)
• To do so requires that we first compute the value of the plus function given the arguments2 and
3, and then send the arguments 5 and 1 to gt.
Consider the following set of facts, again involving Marcus:
1) Marcus was a man.
man(Marcus)
2) Marcus was a Pompeian.
Pompeian(Marcus)
3) Marcus was born in 40 A.D.born(Marcus, 40)
4) All men are mortal.
x: man(x) → mortal(x)
5) All Pompeians died when the volcano erupted in 79 A.D. erupted(volcano, 79) 𝖠 ∀ x : [Pompeian(x)
→ died(x, 79)]
6) No mortal lives longer than 150 years.
x: t1: At2: mortal(x) born(x, t1) gt(t2 – t1,150) → died(x, t2)
7) It is now 1991.
now = 1991
So, Above example shows how these ideas of computable functions and predicates can be useful.It also
makes use of the notion of equality and allows equal objects to be substituted for each other whenever it
appears helpful to do so during a proof.
• So, Now suppose we want to answer the question “Is Marcus alive?”
• The statements suggested here, there may be two ways of deducing an answer.
• Either we can show that Marcus is dead because he was killed by the volcano or we canshow that
he must be dead because he would otherwise be more than 150 years old, which we know is not
possible.
• Also, As soon as we attempt to follow either of those paths rigorously, however, we discover,
just as we did in the last example, that we need some additional knowledge. Forexample, our
statements talk about dying, but they say nothing that relates to being alive, which is what the
question is asking.
So we add the following facts:
8) Alive means not dead.
x: t: [alive(x, t) → ¬ dead(x, t)] [¬ dead(x, t) → alive(x, t)]
9) If someone dies, then he is dead at all later times. x: t1: At2: died(x, t1) gt(t2, t1) → dead(x, t2)
So, Now let’s attempt to answer the question “Is Marcus alive?” by proving: ¬ alive(Marcus,now)
35
Resolution Propositional Resolution
1. Convert all the propositions of F to clause form.
2. Negate P and convert the result to clause form. Add it to the set of clauses obtained instep 1.
3. Repeat until either a contradiction is found or no progress can be made:
1. Select two clauses. Call these the parent clauses.
2. Resolve them together. The resulting clause, called the resolvent, will be the disjunction
of all of the literals of both of the parent clauses with the following exception: If there are
any pairs of literals L and ¬ L such that one of the parentclauses contains L and the other
contains ¬L, then select one such pair and eliminate both L and ¬ L from the resolvent.
3. If the resolvent is the empty clause, then a contradiction has been found. If it is not, then
add it to the set of classes available to the procedure.
The Unification Algorithm
• In propositional logic, it is easy to determine that two literals cannot both be true at thesame time.
• Simply look for L and ¬L in predicate logic, this matching process is more complicatedsince the
arguments of the predicates must be considered.
• For example, man(John) and ¬man(John) is a contradiction, while the man(John) and
¬man(Spot) is not.
• Thus, in order to determine contradictions, we need a matching procedure that compares two
literals and discovers whether there exists a set of substitutions that makes them identical.
• There is a straightforward recursive procedure, called the unification algorithm, that doesit.
Algorithm: Unify(L1, L2)
1. If L1 or L2 are both variables or constants, then:
1. If L1 and L2 are identical, then return NIL.
2. Else if L1 is a variable, then if L1 occurs in L2 then return {FAIL}, else return(L2/L1).
3. Also, Else if L2 is a variable, then if L2 occurs in L1 then return {FAIL}, else return
(L1/L2). d. Else return {FAIL}.
2. If the initial predicate symbols in L1 and L2 are not identical, then return {FAIL}.
3. If LI and L2 have a different number of arguments, then return {FAIL}.
4. Set SUBST to NIL. (At the end of this procedure, SUBST will contain all the substitutions used
to unify L1 and L2.)
5. For I ← 1 to the number of arguments in L1 :
1. Call Unify with the ith argument of L1 and the ith argument of L2, putting theresult in S.
2. If S contains FAIL then return {FAIL}.
3. If S is not equal to NIL then:
2. Apply S to the remainder of both L1 and L2.
3. SUBST: = APPEND(S, SUBST).
6. Return SUBST.
36
Resolution in Predicate Logic
We can now state the resolution algorithm for predicate logic as follows, assuming a set of given
statements F and a statement to be proved P:
Algorithm: Resolution
1. Convert all the statements of F to clause form.
2. Negate P and convert the result to clause form. Add it to the set of clauses obtained in 1.
3. Repeat until a contradiction found, no progress can make, or a predetermined amount ofeffort
has expanded.
1. Select two clauses. Call these the parent clauses.
2. Resolve them together. The resolvent will the disjunction of all the literals of bothparent
clauses with appropriate substitutions performed and with the following exception: If
there is one pair of literals T1 and ¬T2 such that one of the parent clauses contains T2
and the other contains T1 and if T1 and T2 are unifiable, thenneither T1 nor T2 should
appear in the resolvent. We call T1 and T2 Complementary literals. Use the substitution
produced by the unification to create the resolvent. If there is more than one pair of
complementary literals, only one pair should omit from the resolvent.
3. If the resolvent is an empty clause, then a contradiction has found. Moreover, If it is not,
then add it to the set of classes available to the procedure.
Resolution Procedure
• Resolution is a procedure, which gains its efficiency from the fact that it operates onstatements
that have been converted to a very convenient standard form.
• Resolution produces proofs by refutation.
• In other words, to prove a statement (i.e., to show that it is valid), resolution attempts to show
that the negation of the statement produces a contradiction with the known statements (i.e.,
that it is unsatisfiable).
• The resolution procedure is a simple iterative process: at each step, two clauses, called the parent
clauses, are compared (resolved), resulting in a new clause that has inferred from them. The new
clause represents ways that the two parent clauses interact with eachother. Suppose that there are
two clauses in the system:
winter V summer
¬ winter V cold
• Now we observe that precisely one of winter and ¬ winter will be true at any point.
• If winter is true, then cold must be true to guarantee the truth of the second clause. If ¬ winter is
true, then summer must be true to guarantee the truth of the first clause.
• Thus we see that from these two clauses we can deduce summer V cold
• This is the deduction that the resolution procedure will make.
• Resolution operates by taking two clauses that each contains the same literal, in this example,
winter.
• Moreover, The literal must occur in the positive form in one clause and in negative formin the
other. The resolvent obtained by combining all of the literals of the two parent clauses except the
ones that cancel.
• If the clause that produced is the empty clause, then a contradiction has found.
For example, the two clauseswinter
37
¬ winter
will produce the empty clause.
The simplest introduction rule is the one for T. It is called "unit". Because it has no premises, this rule is
an axiom: something that can start a proof.
(unit)
T
Rules for Implication
In natural deduction, to prove an implication of the form P ⇒ Q, we assume P, then reason underthat
assumption to try to derive Q. If we are successful, then we can conclude that P ⇒ Q.
In a proof, we are always allowed to introduce a new assumption P, then reason under that assumption.
We must give the assumption a name; we have used the name x in the examplebelow. Each distinct
assumption must have a different name.
(assum)
[x : P]
38
Because it has no premises, this rule can also start a proof. It can be used as if the proposition Pwere
proved. The name of the assumption is also indicated here.
However, you do not get to make assumptions for free! To get a complete proof, all assumptionsmust be
eventually discharged. This is done in the implication introduction rule. This rule introduces an
implication P ⇒ Q by discharging a prior assumption [x : P]. Intuitively, if Q can be proved under the
assumption P, then the implication P ⇒ Q holds without any assumptions. We write x in the rule name
to show which assumption is discharged. This rule and modus ponens are the introduction and
elimination rules for implications.
[x : P]
⋮ P P⇒Q
(⇒-elim, modus ponens)Q
Q
P ⇒ Q
(
⇒-intro/x)
A proof is valid only if every assumption is eventually discharged. This must happen in the prooftree
below the assumption. The same assumption can be used more than once.
Rules for Disjunction
P Q P∨Q P⇒R Q ⇒ R (∨-
( (
∨-intro- ∨-intro-
P l P right) R elim)
∨ e ∨
Q f Q
t
)
39
Reductio ad absurdum (RAA) is an interesting rule. It embodies proofs by contradiction. It saysthat if by
assuming that P is false we can derive a contradiction, then P must be true. The assumption x is
discharged in the application of this rule. This rule is present in classical logic but not in intuitionistic
(constructive) logic. In intuitionistic logic, a proposition is not considered true simply because its
negation is false.
Excluded Middle
Another classical tautology that is not intuitionistically valid is the the law of the excludedmiddle, P ∨
¬P. We will take it as an axiom in our system. The Latin name for this rule
is tertium non datur, but we will call it magic.
(magic)
P ∨ ¬P
Proofs
A proof of proposition P in natural deduction starts from axioms and assumptions and derives Pwith all
assumptions discharged. Every step in the proof is an instance of an inference rule with metavariables
substituted consistently with expressions of the appropriate syntactic class.
Example
For example, here is a proof of the proposition (A ⇒ B ⇒ C) ⇒ (A 𝖠 B ⇒ C).
The final step in the proof is to derive (A ⇒ B ⇒ C) ⇒ (A 𝖠 B ⇒ C) from (A 𝖠 B ⇒ C), which isdone
using the rule (⇒-intro), discharging the assumption [x : A ⇒ B ⇒ C]. To see how this rule generates the
proof step, substitute for the metavariables P, Q, x in the rule as follows: P = (A ⇒ B ⇒ C), Q = (A 𝖠 B
⇒ C), and x = x. The immediately previous step uses the same rule, but witha different substitution: P =
A 𝖠 B, Q = C, x = y.
The proof tree for this example has the following form, with the proved proposition at the rootand axioms
and assumptions at the leaves.
A proposition that has a complete proof in a deductive system is called a theorem of that system.
Soundness and Completeness
A measure of a deductive system's power is whether it is powerful enough to prove all true statements.
A deductive system is said to be complete if all true statements are theorems (haveproofs in the system).
For propositional logic and natural deduction, this means that all tautologies must have natural deduction
proofs. Conversely, a deductive system is
called sound if all theorems are true. The proof rules we have given above are in fact sound andcomplete
40
for propositional logic: every theorem is a tautology, and every tautology is a theorem.Finding a proof
for a given tautology can be difficult. But once the proof is found, checking that it is indeed a proof is
completely mechanical, requiring no intelligence or insight whatsoever. It is therefore a very strong
argument that the thing proved is in fact true.
We can also make writing proofs less tedious by adding more rules that provide reasoning shortcuts.
These rules are sound if there is a way to convert a proof using them into a proof usingthe original rules.
Such added rules are called admissible.
44