0% found this document useful (0 votes)
29 views74 pages

Ai Unit Ii

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 74

ARTIFICIAL INTELLIGENCE

UNIT - II
INDEX
Basic Knowledge Representation and
ADVANCED SEARCH Reasoning
• Constructing Search Trees • Propositional Logic
• Stochastic Search • First-Order Logic
• A* Search Implementation • Forward Chaining and Backward
• Minimax Search Chaining
• Alpha-Beta Pruning • Introduction to Probabilistic
Reasoning
• Bayes Theorem
CONSTRUCTING SEARCH TREES
BINARY SEARCH TREE CONSTRUCTION
• Binary search tree is a data structure that quickly allows us to
maintain a sorted list of numbers.
• It is called a binary tree because each tree node has a maximum of
two children.
• It is called a search tree because it can be used to search for the
presence of a number in O(log(n)) time.
• The properties that separate a binary search tree from a regular binary
tree is
• All nodes of left subtree are less than the root node
• All nodes of right subtree are more than the root node
• Both subtrees of each node are also BSTs i.e. they have the above two
properties
• In a binary search tree (BST), each node contains-
• Only smaller values in its left sub tree
• Only larger values in its right sub tree
• Example-
Example-
Construct a Binary Search Tree (BST) for the following sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100

• When elements are given in a sequence, always consider the first


element as the root node.
• Consider the given elements and insert them in the BST one by one.
STOCHASTIC SEARCH

Local and Stochastic Search


▪ Start somewhere in the search space
▪ Use an evaluation function for each node
▪ Move towards better evaluated nodes
▪ Sometimes, move elsewhere
State-Space Landscape
Plateaus, ridges, local maxima
• Local Maxima and Minima
• ▪ Trap the algorithms in nodes with suboptimal solutions – once in
such a node, all successors have poorer evaluations
• Plateaus cause the algorithm to wander around without any direction
– all nodes have equally good evaluations
• Ridges - A sequence of local maxima not directly connected to each
other
Escape Techniques
• ▪ Tabu Search:
• Add a memory to a local search algorithm to remember certain moves –
keep a list of forbidden (visited) states – avoid moves that lead to
previously explored regions of the search space
• short term memory: do not reverse a previous move – update this list
while search progresses
• Random Restart: Start over when no progress is made – do a random
restart from a randomly generated initial state performing many hill
climbing searches
• theoretically complete, because it will eventually generate the goal state
as an initial state
• Random Walk: “Inject noise” = pick a worse or equal evaluated node
with a certain probability
Success of Escape Strategies
▪ Which strategies and parameters are successful depends on the –
problem class and – the structure of the search space
▪ Few local maxima and plateaus, random restart hillclimbing finds
good solutions very quickly
▪ Most difficult (NP-hard) problems have an exponential number of
local maxima
Hill climbing 8 Queens
▪ State: distribution of all 8 queens, one in each
column
▪ ℎ: number of pairs of queens attacking each
other
▪ Successor: select a column and move the queen
to another square in the same column
ℎ = 17
the best successors have ℎ = 12
hill climbing chooses randomly among the best
successors
• Select a column and move the queen to the square with the fewest
conflicts
UCT: A Stochastic Search Algorithm
• Monte Carlo Algorithms - Perform repeated random sampling to
determine numerical estimations of unknown parameters
• As the number of identically distributed, randomly generated
variables increases , their sample mean ( average) approaches their
theoretical mean.
Estimating PI by Observing Rain Drops on a Board
▪ Area of circle is 𝜋𝑟 2
▪ Area of square is 𝑤𝑖𝑑𝑡ℎ*2 = (2𝑟) 2= 4𝑟 2
▪ If we divide the area of the circle by the area of the square we get
𝜋/4
▪ The same ratio can be used between the number of points within the
square and the number of points within the circle
▪ “Law of large numbers”
Monte Carlo Tree Search (MCTS)
▪ Method for finding optimal decisions in a given domain by taking
random samples in the decision space and building a search tree
according to the results
– statistical anytime algorithm for which more computing power
generally leads to better performance
– can be used with little or no domain knowledge
The Basic MCTS Process
▪ A tree is built in an incremental and asymmetric
manner:
▪ For each iteration of the algorithm, a tree
policy is used to find the next node to be
expanded of the current tree
▪ The tree policy attempts to balance
considerations of exploration (look in areas that
have not been well sampled yet) and exploitation
(look in areas which appear to be promising)
▪ A simulation is run from the selected node and
the search tree is updated according to the result
in the goal state
MIN MAX SEARCH
• Minimax algorithm evaluates decision based on the present status of
the game.
• This algorithm needs deterministic environment with exact
information.
• Minimax algorithm directly implements the defining equation.
• Every time based on the successor state minimax value is calculated
with the help of simple recursive computation.
• In case of minimax algorithm the selected action with the highest
minimax value should be equal to the best possible payoff(outcome)
against best play.
Consider the below example
• In this tree, node A represents current state of any game and nodes B, C and D
represent three possible valid moves from state A.
• Similarly E, F, G represents possible moves from B, H, I from C and J, K, L, from
D. to decide which move to be taken from A, the different possibilities are
explored to two next steps. 0, -3, 3, 4, 5, 6, -5, 0 represent the utility values of
respective move.
• They indicate goodness of a move.
• The utility value is back propagated to ancestor node, according to situation
whether it is max ply or min ply. As it is a two player game, the utility value is
alternatively maximized and minimized.
• Here as the second player’s move is maximizing, so maximum value of all
children of one node will be back propagated to node.
• Thus, the nodes B, C, D, get the values 4, 5, 6 respectively.
• Again as ply 1 is minimizing, so the minimum value out of these i.e. 4 is
propagated to A.
• Then from A move will be taken to B.
1. MOVEGEN (position, player): The move generator which returns a
list of nodes representing the moves that can be made by player in
position. We may have 2 players namely PLAYER-TWO in a chess
problem.
2.STATIC (position, player): The static evaluation function, which
returns a number representing the goodness of position from the
standpoint of player.
• We assume that MIN MAX returns a structure containing both results
and that we have two functions, VALUE and PATH that extract the
separate components.
• A function LAST PLY is taken which is assumed to evaluate all of the
factors and to return TRUE if the search should be stopped at the
current level and FALSE otherwise.
MIN MAX (CURRENT, 0, PLAYER-ONE)
(If player is to move)
MIN MAX procedure takes three parameters like a
board position, a current depth of the search and the Or
players to move. So the initial call to compute the MIN MAX (CURRENT, 0, PLAYER-TWO)
best move from the position CURRENT should be
(If player two is to move)
Algorithm: MINMAX (position, depth, player)

1.     If LAST PLY (position, depth)


 
Then RETURN VALUE = STATIC (position, player) PATH = nil.
 
2. Else, generate one more ply of the tree by calling the function MOVE_GEN (position, player) and set SUCCESORS
to the list it returns.
3.     If SUCESSORS is empty,
THEN no moves to be made
 
RETURN the same structure that would have been returned if LAST_PLY had returned TRUE.
 
4.     If SUCCESORS is not empty,
 
THEN examine each element in turn and keep track of the best one.
 
5.     After examining all the nodes,
 
RETURNVALUE          = BEST- SCORE
PATH         = BEST- PATH
 
When the initial call to MIN MAX returns, the best move from CURRENT is the first element in the PATH.
ALPHA BETA PRUNING
• Alpha beta pruning in artificial intelligence is a way of improving the minimax
algorithm.
• In the minimax search method, the number of game states that it must investigate
grows exponentially with the depth of the tree.
• We cannot remove the exponent, but we can reduce it by half.
• As a result, a technique known as pruning allows us to compute the proper
minimax choice without inspecting each node of the game tree.
• Alpha-beta pruning may be done at any level in a tree, and it can occasionally
trim the entire sub-tree and the tree leaves.
• It is named alpha-beta pruning in artificial intelligence because it involves two
parameters, alpha, and Beta.
Condition for Alpha Beta Pruning
• The two parameters of alpha beta pruning in artificial intelligence are
• Alpha: It is the best highest value choice that we have found in the
path of the maximizer. The starting value of alpha is -∞.
• Beta: It is the best lowest value choice that we have found in the path
of the minimizer. The starting value of beta is +∞.
Working of Alpha Beta
Step 1
The Max player will start by traveling from node A to node B, where α = -∞ and β = +∞, and
delivering these alpha and beta values to node B, where = - and = + once again, and Node B
transmitting the identical value to its offspring D.
Step 2
As Max's turn at Node D approaches, the value of α will be decided. When the value of α is
compared to 2, then 3, the value at node D is max (2, 3) = 3. Hence, the node value is also 3.
Step 3
The algorithm returns to node B, where the value of β will change since this is a turn of Min.
Now β = +∞will be compared to the value of the available subsequent nodes, i.e., min (∞, 3) =
3, resulting in node B now α = -∞, and β = 3. In the next phase, the algorithm will visit the next
successor of Node B, Node E, and pass the values of α = -∞ and β = 3.
Step 4
Max will take over at node E and change alpha's value. The current existing value of alpha will
be compared to 5, resulting in max (-∞, 5) = 5, and at node E, where α>=β, the right successor
of E will be pruned, and the algorithm will not traverse it, resulting in the value at node E being
5.
Step 5
We now traverse the tree backward, from node B to node A. At node A, alpha will be converted to the
greatest feasible value of 3, as max (-∞, 3)= 3, and β = +∞. These two values will now be passed on to
Node C, A's right-hand successor.
At node C, the values and β = +∞ and α =3 will be passed on to node F, and node F will get the
identical values.
Step 6
At node F, the value of α is compared with the left child 0, and max(3,0)= 3. It is then compared with
the right child, which is 1, and max(3,1)= 3.
Step 7
Node F sends the node value 1 to node C. The value of Beta is adjusted at C, α = 3 and β= +∞, and it
is compared to 1, resulting in min (∞, 1) = 1. Now, if α = 3 and β = 1, the condition α>=β is met, the
algorithm will prune the next child of C, which is G, rather than calculating the entire sub-tree G.
Step 8
C now returns the value of 1 to A, with max (3, 1) = 3 being the optimum result for A. The final game
tree is shown here, with nodes that have been calculated and nodes that have never been computed. As
a result, in this case, the ideal value for the maximizer is 3.
Propositional Logic
• The rules of mathematical logic specify methods of reasoning
mathematical statements
• Greek philosopher, Aristotle, was the pioneer of logical reasoning.
• Logical reasoning provides the theoretical base for many areas of
mathematics and consequently computer science.
• Propositional Logic is concerned with statements to which the truth
values, “true” and “false”, can be assigned.
• The purpose is to analyze these statements either individually or in a
composite manner
• A propositional consists of propositional variables and connectives.
• We denote the propositional variables by capital letters (A, B, C etc).
• The connectives connect the propositional variables.
Example:
"Man is Mortal", it returns truth value “TRUE” as T.
"12 + 9 = 3 − 2", it returns truth value “FALSE” as F.
The following is not a Proposition
"A is less than 2".
• It is because unless we give a specific value of A, we cannot say
whether the statement is true or false.
Connectives
• In propositional logic generally we use five connectives which are −
OR (∨)
AND (∧)
Negation/ NOT (¬)
Implication / if-then (→)
If and only if (⇔).
• OR (∨) − The OR operation of two propositions A and B (written as
A∨B) is true if at least any of the propositional variable A or B is true.

A B A∨B
True True True
True False True
False True True
False False False
• AND (∧) − The AND operation of two propositions A and B (written
as A∧BA∧B) is true if both the propositional variable A and B is true.
• The truth table is as follows −

A B A∧B

True True True

True False False

False True False

False False False


• Negation (¬) − The negation of a proposition A (written as ¬A¬A) is
false when A is true and is true when A is false.
• The truth table is as follows −

A ¬A

True False

False True
Implication / if-then (→) − An implication A→BA→B is the proposition
“if A, then B”. It is false if A is true and B is false. The rest cases are
true.
• The truth table is as follows −
A B A→B

True True True

True False False

False True True

False False True


• If and only if (⇔) − A⇔B A⇔B is bi-conditional logical connective
which is true when p and q are same, i.e. both are false or both are
true.
• The truth table is as follows −

A B A⇔B
True True True
True False False
False True False
False False True
Contradictions
• A Contradiction is a formula which is always false for every value of its
propositional variables.
• Example − Prove (A∨B)∧[(¬A)∧(¬B)](A∨B)∧[(¬A)∧(¬B)] is a
contradiction
• The truth table is as follows −

A B A∨B ¬A ¬B (¬ A) ∧ ( ¬ (A ∨ B) ∧
B) [( ¬ A) ∧
(¬ B)]
True True True False False False False

True False True False True False False

False True True True False False False

False False False True True True False


Contingency
• A Contingency is a formula which has both some true and some false
values for every value of its propositional variables.
• Example − Prove (A∨B)∧(¬A)(A∨B)∧(¬A) a contingency
• The truth table is as follows −

A B A∨B ¬A (A ∨ B) ∧ (¬ A)
True True True False False
True False True False False
False True True True True
False False False True False
FIRST ORDER LOGIC
1.FOL is a mode of representation in Artificial Intelligence. It is an
extension of PL.
2.FOL represents natural language statements in a concise way.
3.FOL is also called predicate logic. It is a powerful language used to
develop information about an object and express the relationship
between objects.
4.FOL not only assumes that does the world contains facts (like PL does),
but it also assumes the following:
1. Objects: A, B, people, numbers, colours, wars, theories, squares, pit, etc.
2. Relations: It is unary relation such as red, round, sister of, brother of, etc.
3. Function: father of, best friend, third inning of, end of, etc.
Parts of first-order logic
• FOL also has two parts:
1.Syntax
2.Semantics
• Syntax
• The syntax of FOL decides which
collection of symbols is a logical
expression.
• The basic syntactic elements of FOL
are symbols. We use symbols to write
statements in shorthand notation
Atomic and complex sentences in FOL
1. Atomic Sentence
• This is a basic sentence of FOL formed from a predicate symbol followed by a parenthesis with a sequence of
terms.
• We can represent atomic sentences as a predicate (value1, value2…., value n).
Example
• John and Michael are colleagues → Colleagues (John, Michael)
• German Shepherd is a dog → Dog (German Shepherd)
2. Complex sentence
• Complex sentences are made by combining atomic sentences using connectives.
• FOL is further divided into two parts:
• Subject: the main part of the statement.
• Predicate: defined as a relation that binds two atoms together.
Example
• Colleague (Oliver, Benjamin) ∧ Colleague (Benjamin, Oliver)
• “x is an integer”
It has two parts;
first, x is the subject.
second, “is an integer” is called a predicate.
Propositional logic vs. first-order logic
Propositional logic
• Propositional logic assumes that some facts exist that can either hold
or do not hold.
First-order-logic
• The universe consists of multiple objects with certain relations among
them that can either hold or do not hold.
Predicates and Quantifiers
• Predicate Logic deals with predicates, which are propositions containing variables.
Predicate Logic – Definition
• A predicate is an expression of one or more variables defined on some specific
domain.
• A predicate with variables can be made a proposition by either assigning a value to
the variable or by quantifying the variable.
• The following are some examples of predicates −
Let E(x, y) denote "x = y"
Let X(a, b, c) denote "a + b + c = 0"
Let M(x, y) denote "x is married to y"
Quantifiers
• The variable of predicates is quantified by quantifiers.
• There are two types of quantifier in predicate logic − Universal Quantifier
and Existential Quantifier.
Universal Quantifier
• Universal quantifier states that the statements within its scope are
true for every value of the specific variable. It is denoted by the
symbol ∀∀.
• ∀x P(x)  is read as for every value of x, P(x) is true.
Example − "Man is mortal" can be transformed into the propositional
form ∀xP(x)  where P(x) is the predicate which denotes x is mortal and
the universe of discourse is all men.
Existential Quantifier
• Existential quantifier states that the statements within its scope
are true for some values of the specific variable.
• It is denoted by the symbol ∃.
• ∃ x P(x)  is read as for some values of x, P(x) is true.
• Example − "Some people are dishonest" can be transformed into
the propositional form
•  ∃ x P(x)  where P(x) is the predicate which denotes x is dishonest
and the universe of discourse is some people.
Nested Quantifiers
• If we use a quantifier that appears within the scope of another
quantifier, it is called nested quantifier.
Example
• ∀ a ∃ b P(x,y)  where P(a,b) denotes a+b=0
• ∀ a ∀ b ∀ c P(a,b,c) where P(a,b) denotes a+(b+c)=(a+b)+c
Note − ∀ a ∃ b P(x,y)≠ ∃ a ∀ b P(x,y)
• Backward and forward chaining are methods of reasoning that exist in
the Expert System Domain of artificial intelligence.
• These techniques are used in expert systems such as MYCIN and
DENDRAL to generate solutions to real life problems.
Introduction to the Expert System
• There are three components in an expert system: user interface,
inference engine, and knowledge base.
• The user interface enables users of the system to interact with the
expert system.
• High-quality and domain-specific knowledge is stored in the
knowledge base.
• Backward and forward chaining stem from the inference engine
component.
• This is a component in which logical rules are applied to the
knowledge base to get new information or make a decision.
• The backward and forward chaining techniques are used by the
inference engine as strategies for proposing solutions or deducing
information in the expert system.
Forward chaining
• Forward chaining is a method of reasoning in artificial intelligence in which
inference rules are applied to existing data to extract additional data until an
endpoint (goal) is achieved.
• In this type of chaining, the inference engine starts by evaluating existing
facts, derivations, and conditions before deducing new information.
• An endpoint (goal) is achieved through the manipulation of knowledge that
exists in the knowledge base.
Properties of forward chaining
• The process uses a down-up approach (bottom to top).
• It starts from an initial state and uses facts to make a conclusion.
• This approach is data-driven.
• It’s employed in expert systems and production rule system.
Example of forward chaining
• Tom is running (A)
• If a person is running, he will sweat (A->B)
• Therefore, Tom is sweating. (B)
Advantages
• It can be used to draw multiple conclusions.
• It provides a good basis for arriving at conclusions.
• It’s more flexible than backward chaining because it does not have a
limitation on the data derived from it.
Disadvantages
• The process of forward chaining may be time-consuming. It may take
a lot of time to eliminate and synchronize available data.
• Unlike backward chaining, the explanation of facts or observations for
this type of chaining is not very clear. The former uses a goal-driven
method that arrives at conclusions efficiently.
Backward chaining
• Backward chaining is a concept in artificial intelligence that involves
backtracking from the endpoint or goal to steps that led to the
endpoint. This type of chaining starts from the goal and moves
backward to comprehend the steps that were taken to attain this goal.
• The backtracking process can also enable a person establish logical
steps that can be used to find other important solutions.
Properties of backward chaining
• The process uses an up-down approach (top to bottom).
• It’s a goal-driven method of reasoning.
• The endpoint (goal) is subdivided into sub-goals to prove the truth of
facts.
• A backward chaining algorithm is employed in inference engines,
game theories, and complex database systems.
• The modus ponens rule is used as the basis for the backward chaining
process. This rule states that if both the conditional statement (p->q)
and the antecedent (p) are true, then we can infer the subsequent (q).
Example of backward chaining
• Tom is sweating (B).
• If a person is running, he will sweat (A->B).
• Tom is running (A).
Advantages
• The result is already known, which makes it easy to deduce inferences.
• It’s a quicker method of reasoning than forward chaining because the
endpoint is available.
• In this type of chaining, correct solutions can be derived effectively if
pre-determined rules are met by the inference engine.
Disadvantages
• The process of reasoning can only start if the endpoint is known.
• It doesn’t deduce multiple solutions or answers.
• It only derives data that is needed, which makes it less flexible than
forward chaining.
Introduction to Probabilistic Reasoning
• A→B, which means if A is true then B is true, but consider a situation
where we are not sure about whether A is true or not then we cannot
express this statement, this situation is called uncertainty.
Causes of uncertainty:
• Following are some leading causes of uncertainty to occur in the real
world.
1.Information occurred from unreliable sources.
2.Experimental Errors
3.Equipment fault
4.Temperature variation
5.Climate change.
• We use probability in probabilistic reasoning because it provides a
way to handle the uncertainty that is the result of someone's laziness
and ignorance.
• In the real world, there are lots of scenarios, where the certainty of
something is not confirmed, such as
"It will rain today,"
"behaviour of someone for some situations,"
"A match between two teams or two players."
• These are probable sentences for which we can assume that it will
happen but not sure about it, so here we use probabilistic reasoning.
• In probabilistic reasoning, there are two ways to solve problems with uncertain
knowledge:
Bayes' rule
Bayesian Statistics
Probability: Probability can be defined as a chance that an uncertain event will
occur. It is the numerical measure of the likelihood that an event will occur. The
value of probability always remains between 0 and 1 that represent ideal
uncertainties.

0 ≤ P(A) ≤ 1, where P(A) is the probability of an event A.


P(A) = 0, indicates total uncertainty in an event A.
P(A) =1, indicates total certainty in an event A.
• P(¬A) = probability of a not happening event.
• P(¬A) + P(A) = 1.
Bayes' Theorem
• Theorem − If A and B are two mutually exclusive events, where P(A) is the probability of A and P(B) is
the probability of B, P(A|B) is the probability of A given that B is true.
• P(B|A) is the probability of B given that A is true, then Bayes’ Theorem states −

Application of Bayes' Theorem


• In situations where all the events of sample space are mutually exclusive events.
• In situations where either P(Ai∩B) for each Ai or P(Ai) and P(B|Ai) for each Ai is known.
Problem
Consider three pen-stands. The first pen-stand contains 2 red pens and 3 blue pens; the
second one has 3 red pens and 2 blue pens; and the third one has 4 red pens and 1 blue
pen. There is equal probability of each pen-stand to be selected. If one pen is drawn at
random, what is the probability that it is a red pen?
Solution
Let Ai be the event that ith pen-stand is selected.
Here, i = 1,2,3.
Since probability for choosing a pen-stand is equal, P(Ai)=1/3
Let B be the event that a red pen is drawn.
The probability that a red pen is chosen among the five pens of the first pen-stand,
P(B|A1)=2/5
• The probability that a red pen is chosen among the five pens of the second pen-
stand,
P(B|A2)=3/5
• The probability that a red pen is chosen among the five pens of the third pen-
stand,
P(B|A3)=4/5
According to Bayes' Theorem,
P(B)=
P(A1).P(B|A1)+P(A2).P(B|A2)+P(A3).P(B|A3)=
1/3.2/5+1/3.3/5+1/3.4/5=
3/5

You might also like