AL Notes
AL Notes
TOPIC NAME:
The solution of a problem generally includes the initial data and facts in order to arrive at the
solution. These unknown facts and information is used to deduce the result. For example,
while diagnosing a patient the doctor first check the symptoms and medical condition of the
body such as temperature, blood pressure, pulse, eye colour, blood, etcetera. After that, the
patient symptoms are analysed and compared against the predetermined symptoms. Then the
doctor is able to provide the medicines according to the symptoms of the patient. So, when a
solution employs this manner of reasoning, it is known as forward reasoning.
The inference engine explores the knowledge base with the provided information for
constraints whose precedence matches the given current state.
• In the first step, the system is given one or more than one constraints.
• Then the rules are searched in the knowledge base for each constraint. The rules that fulfil
the condition are selected(i.e., IF part).
• Now each rule is able to produce new conditions from the conclusion of the invoked one.
As a result, THEN part is again included in the existing one.
• The added conditions are processed again by repeating step 2. The process will end if there
is no new conditions exist.
The backward reasoning is inverse of forward reasoning in which goal is analysed in order to
deduce the rules, initial facts and data. We can understand the concept by the similar example
given in the above definition, where the doctor is trying to diagnose the patient with the help
of the inceptive data such as symptoms. However, in this case, the patient is experiencing a
problem in his body, on the basis of which the doctor is going to prove the symptoms. This
kind of reasoning comes under backward reasoning.
In this type of reasoning, the system chooses a goal state and reasons in the backward
direction. Now, let’s understand how does it happens and what steps are followed.
• Firstly, the goal state and the rules are selected where the goal state reside in the THEN part
as the conclusion.
• From the IF part of the selected rule the subgoals are made to be satisfied for the goal state
to be true.
• Verify whether the provided initial state matches with the established states. If it fulfils the
condition then the goal is the solution otherwise other goal state is selected.
1. The forward reasoning is data-driven approach while backward reasoning is a goal driven.
2. The process starts with new data and facts in the forward reasoning. Conversely, backward
reasoning begins with the results.
3. Forward reasoning aims to determine the result followed by some sequences. On the other
hand, backward reasoning emphasis on the acts that support the conclusion.
5. The flow of the forward reasoning is from the antecedent to consequent while backward
reasoning works in reverse order in which it starts from conclusion to incipient.
Advantages:
1. It is very useful in AI because of it provides a set of all possible states, operations and
goals.
2. If the entire state space is for a problem then it is possible to trace the path from the initial
to the goal state and identify the sequence of operation required for doing it.
Disadvantages:
2. The resources of the computer system are very limited to handle huge combinational state
space.
Search Algorithms in AI
Artificial Intelligence is the study of building agents that act rationally. Most of the time,
these agents perform some kind of search algorithm in the background in order to achieve
their tasks.
• A State Space. Set of all possible states where you can be.
• A Goal Test. A function that looks at the current state returns whether or not it is the goal
state.
• The Solution to a search problem is a sequence of actions, called the plan that transforms
the start state to the goal state.
There are far too many powerful search algorithms out there to fit in a single article. Instead,
this article will discuss six of the fundamental search algorithms, divided into two categories,
as shown below.
UNINFORMED OR BLINED SEARCH ALGORITHMS:
The search algorithms in this section have no additional information on the goal node other
than the one provided in the problem definition. The plans to reach the goal state from the
start state differ only by the order and/or length of actions. Uninformed search is also called
Blind search. These algorithms can only generate the successors and differentiate between the
goal state and non goal state.
• A problem graph, containing the start node S and the goal node G.
• A strategy, describing the manner in which the graph will be traversed to get to G.
• A fringe, which is a data structure used to store all the possible states (nodes) that you can
go from the current states.
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data
structures. The algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and explores as far as possible along each branch before
backtracking. It uses last in- first-out strategy and hence it is implemented using a stack.
Example:
Question. Which solution would DFS find to move from node S to node G if run on the graph
below?
Solution. The equivalent search tree for the above graph is as follows. As DFS traverses the
tree “deepest node first”, it would always pick the deeper branch until it reaches the solution
(or it runs out of nodes, and goes to the next branch). The traversal is shown in blue arrows.
Completeness: DFS is complete if the search tree is finite, meaning for a given finite search
tree, DFS will come up with a solution if it exists.
Optimality: DFS is not optimal, meaning the number of steps in reaching the solution, or the
cost spent in reaching it is high.
Advantages:
Disadvantages:
- It requires lots of memory since each level of the tree must be saved into memory to
expand the next level.
- BFS needs lots of time if the solution is far away from the root node.
Example: In the below tree structure, we have shown the traversing of the tree using BFS
algorithm from the root node S to goal node K. BFS search algorithm traverse in layers, so it
will follow the path which is shown by the dotted arrow, and the traversed path will be:
Time Complexity: Time Complexity of BFS algorithm can be obtained by the number of
nodes traversed in BFS until the shallowest Node. Where the d= depth of shallowest solution
and b is a node at every state. T (b) = 1+b2+b3+.......+ bd= O (bd )
Space Complexity: Space complexity of BFS algorithm is given by the Memory size of
frontier which is O(bd ).
Completeness: BFS is complete, which means if the shallowest goal node is at some finite
depth, then BFS will find a solution.
Optimality: BFS is optimal if path cost is a non-decreasing function of the depth of the node.
Here, the algorithms have information on the goal state, which helps in more efficient
searching. This information is obtained by something called a heuristic.
1. Greedy Search
2. A* Tree Search
3. A* Graph Search
Search Heuristics: In an informed search, a heuristic is a function that estimates how close a
state is to the goal state. For example – Manhattan distance, Euclidean distance, etc. (Lesser
the distance, closer the goal.)
PROBLEM-REDUCTION:
We already know about the divide and conquer strategy, a solution to a problem can be
obtained by decomposing it into smaller sub-problems. Each of this sub problem can then be
solved to get its sub-solution. These sub-solutions can then be recombined to get a solution as
a whole. That is called is Problem Reduction. This method generates arc which is called as
AND arcs. One AND arc may point to any number of successor nodes, all of which must be
solved for an arc to point to a solution.
2. Loop until the starting node is labelled SOLVED or until its cost goes above FUTILITY:
(i) Traverse the graph, starting at the initial node and following the current best path and
accumulate the set of nodes that are on that path and have not yet been expanded.
(ii) Pick one of these unexpanded nodes and expand it. If there are no successors, assign
FUTILITY as the value of this node. Otherwise, add its successors to the graph and for each
of them compute f'(n). If f'(n) of any node is O, mark that node as SOLVED.
(iii) Change the f'(n) estimate of the newly expanded node to reflect the new information
provided by its successors. Propagate this change backwards through the graph. If any node
contains a successor arc whose descendants are all solved, label the node itself as SOLVED.
A* SEARCH:
A* search is the most commonly known form of best-first search. It uses heuristic function
h(n), and cost to reach the node n from the start state g(n). It has combined features of UCS
and greedy best-first search, by which it solve the problem efficiently. A* search algorithm
finds the shortest path through the search space using the heuristic function. This search
algorithm expands less search tree and provides optimal result faster. A* algorithm is similar
to UCS except that it uses g(n)+h(n) instead of g(n).
In A* search algorithm, we use search heuristic as well as the cost to reach the node. Hence
we can combine both costs as following, and this sum is called as a fitness number.
At each point in the search space, only those node is expanded which have the lowest value
of f(n), and the algorithm terminates when the goal node is found.
Algorithm of A* search:
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and
stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation
function (g+h), if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For
each successor n', check whether n' is already in the OPEN or CLOSED list, if not then
compute evaluation function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the
back pointer which reflects the lowest g(n') value.
Advantages:
Disadvantages:
- It does not always produce the shortest path as it mostly based on heuristics and
approximation.
- A* search algorithm has some complexity issues.
- The main drawback of A* is memory requirement as it keeps all generated nodes in
the memory, so it is not practical for various large-scale problems.
Example: In this example, we will traverse the given graph using the A* algorithm. The
heuristic value of all states is given in the below table so we will calculate the f(n) of each
state using the formula f(n)= g(n) + h(n), where g(n) is the cost to reach any node from start
state.
Solution:
Initialization: {(S, 5)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7), (S- ->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it provides the optimal path with
cost 6.
Points to remember:
- A* algorithm returns the path which occurred first, and it does not search for all
remaining paths.
- The efficiency of A* algorithm depends on the quality of heuristic.
- A* algorithm expands all nodes which satisfy the condition f(n)<="" li="">
- Admissible: the first condition requires for optimality is that h(n) should be an
admissible heuristic for A* tree search. An admissible heuristic is optimistic in nature.
- Consistency: Second required condition is consistency for only A* graph search.
If the heuristic function is admissible, then A* tree search will always find the least cost path.
AO* algorithm is a best first search algorithm. AO* algorithm uses the concept of AND-OR
graphs to decompose any complex problem given into smaller set of problems which are
further solved. AND-OR graphs are specialized graphs that are used in problems that can be
broken down into sub problems where AND side of the graph represent a set of task that need
to be done to achieve the main goal , whereas the or side of the graph represent the different
ways of performing task to achieve the same main goal.
In the above figure we can see an example of a simple AND-OR graph wherein, the
acquisition of speakers can be broken into sub problems/tasks that could be performed to
finish the main goal. The sub task is to either steal speakers which will directly helps us
achieve the main goal "or" earn some money "and" buy speakers which helps us achieve the
main goal. The AND part of the graphs are represented by the AND-ARCS, referring that all
the sub problems with the AND-ARCS need to be solved for the predecessor node or
problem to be completed. The edges without AND-ARCS are OR sub problems that can be
done instead of the sub problems with And-arcs. It is to be noted that several edges can come
from a single node as well as the presence of multiple AND arcs and multiple OR sub
problems are possible.
The AO* algorithm is a knowledge-based search technique, meaning the start state and the
goal state is already defined , and the best path is found using heuristics. The time complexity
of the algorithm is significantly reduced due to the informed search technique. Compared to
the A* algorithm , AO* algorithm is very efficient in searching the AND-OR trees very
efficiently.
Working of AO algorithm:
where,
• g(n): The actual cost of traversal from initial state to the current state.
• h(n): The estimated cost of traversal from the current state to the goal state.
• f(n): The actual cost of traversal from the initial state to the goal state.
Now, to get a better idea of the AO* algorithm lets take a look at an example. Example-
Here, in the above example all numbers in brackets are the heuristic value i.e h(n). Each edge
is considered to have a value of 1 by default.
Step-1
Starting from node A, we first calculate the best path. f(A-B) = g(B) + h(B) = 1+4= 5 , where
1 is the default cost value of travelling from A to B and 4 is the estimated cost from B to Goal
state. f(A-C-D) = g(C) + h(C) + g(D) + h(D) = 1+2+1+3 = 7 , here we are calculating the path
cost as both C and D because they have the AND-Arc. The default cost value of travelling
from A-C is 1, and from A-D is 1, but the heuristic value given for C and D are 2 and 3
respectively hence making the cost as 7.
Step-2
Using the same formula as step-1, the path is now calculated from the B node, f(B-E) = 1 + 6
= 7. f(B-F) = 1 + 8 = 9 Hence, the B-E path has lesser cost. Now the heuristics have to be
updated since there is a difference between actual and heuristic value of B. The minimum
cost path is chosen and is updated as the heuristic , in our case the value is 7. And because of
change in heuristic of B there is also change in heuristic of A which is to be calculated again.
f(A-B) = g(B) + updated((h(B)) = 1+7=8
Step-3
Comparing path of f(A-B) and f(A-C-D) it is seen that f(A-C-D) is smaller. Hence f(A-C-D)
needs to be explored. Now the current node becomes C node and the cost of the path is
calculated, f(C-G) = 1+2 = 3 f(C-H-I) = 1+0+1+0 = 2 f(C-H-I) is chosen as minimum cost
path,also there is no change in heuristic since it matches the actual cost. Heuristic of path of
H and I are 0 and hence they are solved, but Path A-D also needs to be calculated , since it
has an AND arc. f(D-J) = 1+0 = 1, hence heuristic of D needs to be updated to 1. And
finally the f(A-C-D) needs to be updated. f(A-C-D) = g(C) + h(C) + g(D) + updated((h(D)) =
1+2+1+1 =5.
• A* algorithm provides with the optimal solution, whereas AO* stops when it finds any
solution.
• AO* algorithm doesn't go into infinite loop whereas the A* algorithm can go into an infinite
loop.
MINIMAX SEARCHING:
Minimax is a kind of backtracking algorithm that is used in decision making and game
theory to find the optimal move for a player, assuming that your opponent also plays
optimally. It is widely used in two player turn-based games such as Tic-Tac-Toe,
Backgammon, Mancala, Chess, etc. In Minimax the two players are called maximizer and
minimizer. The maximizer tries to get the highest score possible while the minimizer tries to
do the opposite and get the lowest score possible. Every board state has a value associated
with it. In a given state if the maximizer has upper hand then, the score of the board will tend
to be some positive value. If the minimizer has the upper hand in that board state then it will
tend to be some negative value. The values of the board are calculated by some heuristics
which are unique for every type of game.
Example: Consider a game which has 4 final states and paths to reach final state are from root
to 4 leaves of a perfect binary tree as shown below. Assume you are the maximizing player
and you get the first chance to move, i.e., you are at the root and your opponent at next level.
Which move you would make as a maximizing player considering that your opponent also
plays optimally?
Since this is a backtracking based algorithm, it tries all possible moves, then backtracks and
makes a decision.
• Maximizer goes LEFT: It is now the minimizers turn. The minimizer now has a choice
between 3 and 5. Being the minimizer it will definitely choose the least among both, that is 3
• Maximizer goes RIGHT: It is now the minimizers turn. The minimizer now has a choice
between 2 and 9. He will choose 2 as it is the least among the two values.
Being the maximizer you would choose the larger value that is 3. Hence the optimal move
for the maximizer is to go LEFT and the optimal value is 3.
Note: Even though there is a value of 9 on the right subtree, the minimizer will never pick
that. We must always assume that our opponent plays optimally.
Time complexity : O(b^d) b is the branching factor and d is count of depth or ply of graph or
tree.
Space Complexity : O(bd) where b is branching factor into d is maximum depth of tree
similar to DFS.
- In the above example, there are only two choices for a player. In general, there can be
more choices. In that case, we need to recur for all possible moves and find the
maximum/minimum. For example, in Tic-Tac-Toe, the first player can make 9
possible moves.
- In the above example, the scores (leaves of Game Tree) are given to us. For a typical
game, we need to derive these values
CONSTRAINT PROPAGATION
The basic step in constraint propagation is called filtering a constraint. As input, filtering
takes a constraint and the current domains for its variables. It then tries to remove all the
values in the domains that do not appear in any solution to that constraint. That is, it tries to
reduce the domains so that all the solutions are preserved. Thus “constraint filtering” in CSPs
corresponds to a “unit clause propagation” step in CNF SAT solvers, but it is usually more
complex because the domains are larger and the constraints are more complex. Filtering on
the global constraint level can also be more effective than unit clause propagation in pruning
the search space.
Constraint propagation then means the process of applying filtering to the constraints in the
CSP instance at hand. As filtering one constraint can reduce the domains of its variables, it
can trigger further reduction when filtering another constraint that also involves the reduced-
domain variables. Thus a constraint can be filtered multiple times during the constraint
propagation process. Usually the process is run to the end, meaning until no more reduction
happens in filtering any of the constraints. Or if this takes too long, then until some resource
usage limit is exceeded.
- Hill climbing algorithm is a local search algorithm which continuously moves in the
direction of increasing elevation/value to find the peak of the mountain or best
solution to the problem. It terminates when it reaches a peak value where no neighbor
has a higher value.
- Hill climbing algorithm is a technique which is used for optimizing the mathematical
problems. One of the widely discussed examples of Hill climbing algorithm is
Traveling-salesman Problem in which we need to minimize the distance traveled by
the salesman.
- It is also called greedy local search as it only looks to its good immediate neighbor
state and not beyond that.
- A node of hill climbing algorithm has two components which are state and value.
- Hill Climbing is mostly used when a good heuristic is available.
- In this algorithm, we don't need to maintain and handle the search tree or graph as it
only keeps a single current state.
- Generate and Test variant: Hill Climbing is the variant of Generate and Test method.
The Generate and Test method produce feedback which helps to decide which
direction to move in the search space.
- Greedy approach: Hill-climbing algorithm search moves in the direction which
optimizes the cost.
- No backtracking: It does not backtrack the search space, as it does not remember the
previous states.
On Y-axis we have taken the function which can be an objective function or cost
function, and state-space on the x-axis. If the function on Y-axis is cost then, the goal of
search is to find the global minimum and local minimum. If the function of Y-axis is
Objective function, then the goal of the search is to find the global maximum and local
maximum.
Local Maximum: Local maximum is a state which is better than its neighbor states, but
there is also another state which is higher than it
Global Maximum: Global maximum is the best possible state of state space landscape. It
has the highest value of objective function.
Flat local maximum: It is a flat space in the landscape where all the neighbor states of
current states have the same value.
Simple hill climbing is the simplest way to implement a hill climbing algorithm. It only
evaluates the neighbor node state at a time and selects the first one which optimizes
current cost and set it as a current state. It only checks it's one successor state, and if it
finds better than the current state, then move else be in the same state. This algorithm has
the following features:
Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
Step 2: Loop Until a solution is found or there is no new operator left to apply.
Step 4: Check new state: 1. If it is goal state, then return success and quit. 2. Else if it is better
than the current state then assign new state as a current state. 3. Else if not better than the
current state, then return to step2.
Step 5: Exit