Unit 2 - AI
Unit 2 - AI
Game theory is about decision-making among rational agents. It’s the study
of strategic interaction where the outcome for each player depends not just
on their own actions, but also on the actions of others.
Games are usually intriguing because they are difficult to solve. Chess, for
example, has an average branching factor of around 35, and games
frequently stretch to 50 moves per player, therefore the search tree has
roughly 35100 or 10154 nodes (despite the search graph having “only”
about 1040 unique nodes). As a result, games, like the real world,
necessitate the ability to make some sort of decision even when
calculating the best option is impossible.
Inefficiency is also heavily punished in games. Whereas a half-efficient
implementation of A search will merely take twice as long to complete, a
chess software that is half as efficient in utilizing its available time will
almost certainly be beaten to death, all other factors being equal. As a
result of this research, a number of intriguing suggestions for making the
most use of time have emerged.
Let us start with games with two players, whom we’ll refer to as MAX and
MIN for obvious reasons. MAX is the first to move, and then they take
turns until the game is finished. At the conclusion of the game, the
victorious player receives points, while the loser receives penalties. A
game can be formalized as a type of search problem that has the
following elements:
S0: The initial state of the game, which describes how it is set up at the
start.
Player (s): Defines which player in a state has the move.
Actions (s): Returns a state’s set of legal moves.
Result (s, a): A transition model that defines a move’s outcome.
Terminal-Test (s): A terminal test that returns true if the game is over
but false otherwise. Terminal states are those in which the game has
come to a conclusion.
Utility (s, p): A utility function (also known as a payout function or
objective function ) determines the final numeric value for a game that
concludes in the terminal state s for player p. The result in chess is a
win, a loss, or a draw, with values of +1, 0, or 1/2.
Various techniques and approaches have been developed to enable AI
agents to make optimal decisions in games. Here are some key concepts
and methods:
Minimax Algorithm:
Alpha-Beta Pruning:
Evolutionary Algorithms:
6. maxEva= -infinity
11.
Initial call .:
Minimax(node, 3, true)
Step-1: In the first step, the algorithm generates the entire game-tree and apply the
utility function to get the utility values for the terminal states. In the below tree
diagram, let's take A is the initial state of the tree. Suppose maximizer takes first
turn which has worst-case initial value =- infinity, and minimizer will take next
turn which has worst-case initial value = +infinite
Step 2: Now, first we find the utilities value for the Maximizer, its initial value is -
∞, so we will compare each value in terminal state with initial value of Maximizer
and determines the higher nodes values. It will find the maximum among the all. o
For node D o For Node E o For Node F o For node G max(-1,- -∞) => max(-1,4)=
4 max(2, -∞) => max(2, 6)= 6 max(-3, -∞) => max(-3,-5) = -3 max(0, -∞) = max(0,
7) = 7
Step 3: In the next step, it's a turn for minimizer, so it will compare all nodes value
with +∞, and will find the 3rd layer node values. o For node B= min(4,6) = 4 o For
node C= min (-3, 7) = -3
Step 4: Now it's a turn for Maximizer, and it will again choose the maximum of all
nodes value and find the maximum value for the root node. In this game tree, there
are only 4 layers, hence we reach immediately to the root node, but in real games,
there will be more than 4 layers. o For node A max(4, -3)= 4
Properties of Mini-Max algorithm:
Complete- Min-Max algorithm is Complete. It will definitely find a solution
(if exist), in the finite search tree.
Optimal- Min-Max algorithm is optimal if both opponents are playing
optimally.
Time complexity- As it performs DFS for the game-tree, so the time
complexity of Min-Max algorithm is O(bm), where b is branching factor of
the game-tree, and m is the maximum depth of the tree.
Space Complexity- Space complexity of Mini-max algorithm is also similar
to DFS which is O(bm).
Limitation of the minimax Algorithm:
The main drawback of the minimax algorithm is that it gets really slow for
complex games such as Chess, go, etc.
At each step of the algorithm, two values, alpha and beta, are used:
Alpha: Represents the best value the maximizing player (the one trying to
win) can guarantee so far.
Beta: Represents the best value the minimizing player (the one trying to
make the opponent lose) can guarantee so far.
As the algorithm explores different branches of the game tree, if it finds that a
particular branch can’t improve the final outcome for either player, it “prunes” that
branch, meaning it stops further evaluation of that part of the tree.
Alpha (α):
Alpha represents the best score that the maximizing player can guarantee
at a given level or above. The maximizing player is always looking for the
highest possible score.
Beta (β):
Beta represents the best score that the minimizing player can guarantee at
a given level or below. The minimizing player aims to minimize the score,
meaning they are looking for the lowest possible score.
The minimizing player finds a value that is lower than or equal to alpha (β ≤
α). In this case, there’s no need to explore further because the minimizing
player will not let the maximizing player choose that branch, so we can
prune it.
Similarly, the maximizing player can prune a branch when they find a value
higher than or equal to beta (α ≥ β), as the maximizing player will never
choose a move that results in a worse score.
Key Steps:
In-Depth Example:
Let’s walk through an example where we have a game tree that represents a series
of moves in a turn-based game (like chess or tic-tac-toe). The goal is for the
maximizing player to find the best possible move while the minimizing player tries
to counter it.
Imagine this tree:
1. Initial Setup:
The maximizing player starts at the root node A and wants to maximize their score.
Each branch represents a different move the player could make.
2. Evaluating Node B:
Now, we look at the next node, G. But before evaluating G, the algorithm
checks the alpha and beta values.
At this point, alpha is 5 (from node E) and beta is 2 (from node F). Since
alpha is greater than beta (5 > 2), the algorithm knows that the maximizing
player will never choose this branch because they can already guarantee a
better score by choosing the path through B.
Therefore, the algorithm prunes node G, meaning it doesn’t bother
evaluating it because it won’t impact the final decision.
working of Alpha-Beta Pruning:
Step 1: At the first step the, Max player will start first move from node A
where α= -∞ and β= +∞, these value of alpha and beta passed down to
node B where again α= -∞ and β= +∞, and Node B passes the same value
to its child D.
Step 2: At Node D, the value of α will be calculated as its turn for Max. The
value of α is compared with firstly 2 and then 3, and the max (2, 3) = 3 will
be the value of α at node D and node value will also 3.
Step 3: Now algorithm backtrack to node B, where the value of β will
change as this is a turn of Min, Now β= +∞, will compare with the available
subsequent nodes value, i.e. min (∞, 3) = 3, hence at node B now α= -∞,
and β= 3.
In the next step, algorithm traverse the next successor of Node B which is
node E, and the values of α= -∞, and β= 3 will also be passed.
Step 4: At node E, Max will take its turn, and the value of alpha will change.
The current value of alpha will be compared with 5, so max (-∞, 5) = 5,
hence at node E α= 5 and β= 3, where α>=β, so the right successor of E will
be pruned, and algorithm will not traverse it, and the value at node E will be
5.
Step 5: At next step, algorithm again backtrack the tree, from node B to
node A. At node A, the value of alpha will be changed the maximum
available value is 3 as max (-∞, 3)= 3, and β= +∞, these two values now
passes to right successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values will be passed on to node
F.
Step 6: At node F, again the value of α will be compared with left child
which is 0, and max(3,0)= 3, and then compared with right child which is 1,
and max(3,1)= 3 still α remains 3, but the node value of F will become 1.
Step 7: Node F returns the node value 1 to node C, at C α= 3 and β= +∞,
here the value of beta will be changed, it will compare with 1 so min (∞, 1) =
1. Now at C, α=3 and β= 1, and again it satisfies the condition α>=β, so the
next child of C which is G will be pruned, and the algorithm will not
compute the entire sub-tree G.
Step 8: C now returns the value of 1 to A here the best value for A is max (3,
1) = 3. Following is the final game tree which is the showing the nodes
which are computed and nodes which has never computed. Hence the
optimal value for the maximizer is 3 for this example.
Move Ordering in Alpha-Beta pruning:
The effectiveness of alpha-beta pruning is highly dependent on the order in
which each node is examined. Move order is an important aspect of alpha-
beta pruning.
1. Backtracking Algorithm
The backtracking algorithm is a depth-first search method used to
systematically explore possible solutions in CSPs. It operates by
assigning values to variables and backtracks if any assignment violates a
constraint.
How it works:
The algorithm selects a variable and assigns it a value.
It recursively assigns values to subsequent variables.
If a conflict arises (i.e., a variable cannot be assigned a valid value), the
algorithm backtracks to the previous variable and tries a different value.
The process continues until either a valid solution is found or all
possibilities have been exhausted.
This method is widely used due to its simplicity but can be inefficient for
large problems with many variables.
2. Forward-Checking Algorithm
The forward-checking algorithm is an enhancement of the backtracking
algorithm that aims to reduce the search space by applying local
consistency checks.
How it works:
For each unassigned variable, the algorithm keeps track of remaining
valid values.
Once a variable is assigned a value, local constraints are applied to
neighboring variables, eliminating inconsistent values from their
domains.
If a neighbor has no valid values left after forward-checking, the
algorithm backtracks.
This method is more efficient than pure backtracking because it prevents
some conflicts before they happen, reducing unnecessary computations.
How it works:
Constraints are propagated between related variables.
Inconsistent values are eliminated from variable domains by leveraging
information gained from other variables.
These algorithms refine the search space by making inferences,
removing values that would lead to conflicts.
Constraint propagation is commonly used in conjunction with other CSP
algorithms, such as backtracking, to increase efficiency by narrowing
down the solution space early in the search process.
Introduction to Constraint Propagation
Constraint propagation is a fundamental concept in constraint satisfaction
problems (CSPs). A CSP involves variables that must be assigned values
from a given domain while satisfying a set of constraints. Constraint
propagation aims to simplify these problems by reducing the domains of
variables, thereby making the search for solutions more efficient.
Key Concepts
Advantages
1. Efficiency: Reduces the search space, making it easier to find
solutions.
2. Scalability: Can handle large problems by breaking them down into
smaller subproblems.
3. Flexibility: Applicable to various types of constraints and domains.
Limitations
1. Computational Cost: Higher levels of consistency can be
computationally expensive.
2. Incomplete Propagation: May not always reduce the domains enough
to find a solution directly.
Backtracking Search
Optimization Techniques
Advantages
Limitations
1. Inefficiency for Large Problems: The algorithm can be slow for large
or highly constrained problems.
2. Redundancy: Without optimization techniques, the search might
redundantly explore many invalid paths.
3. Space Complexity: It requires significant memory for storing the state
of the search tree.
An intelligent agent needs knowledge about the real world for taking
decisions and reasoning to act efficiently.
Knowledge-based agents are those agents who have the capability of
maintaining an internal state of knowledge, reason over that knowledge,
update their knowledge after observations and take actions. These agents can
represent the world with some formal representation and act intelligently.
Knowledge-base and
Inference system.
Knowledge base:
Inference system
Inference means deriving new sentences from old. Inference system allows us to
add a new sentence to the knowledge base. A sentence is a proposition about the
world. Inference system applies logical rules to the KB to deduce new information.
Inference system generates new facts so that an agent can update the KB. An
inference system works mainly in two rules which are given as
Forward chaining
Backward chaining
Following are three operations which are performed by KBA in order to show the
intelligent behavior:
1. TELL: This operation tells the knowledge base what it perceives from the
environment.
2. ASK: This operation asks the knowledge base what action it should perform.
1. function KB-AGENT(percept):
7. t = t + 1
8. return action
The knowledge-based agent takes percept as input and returns an action as output.
The agent maintains the knowledge base, KB, and it initially has some background
knowledge of the real world. It also has a counter to indicate the time for the whole
process, and this counter is initialized with zero
Each time when the function is called, it performs its three operations:
A knowledge-based agent can be viewed at different levels which are given below:
1.Knowledge level
Knowledge level is the first level of knowledge-based agent, and in this level, we
need to specify what the agent knows, and what the agent goals are. With these
specifications, we can fix its behavior. For example, suppose an automated taxi
agent needs to go from a station A to station B, and he knows the way from A to B,
so this comes at the knowledge level
2.Logical level:
3.Implementation level:
Declarative approach:
Procedural approach:
Example:
1. a) It is Sunday.
4. d) 5 is a prime number.
a. Atomic Propositions
b. Compound propositions
a. Atomic Proposition:
Compound proposition:
Example:
1. a) "It is raining today, and street is wet."
2. b) "Ankit is a doctor, and his clinic is in Mumbai."
Logical Connectives:
1. Negation
If p p is a proposition, then the negation of p p is denoted by ¬p ¬p , which
when translated to simple English means- “It is not the case that p” or simply
“not p“. The truth value of -p is the opposite of the truth value of p. The truth
table of -p is:
p ¬p
T F
F T
Example, Negation of “It is raining today”, is “It is not the case that is raining
today” or simply “It is not raining today”.
2. Conjunction
For any two propositions p p and q q , their conjunction is denoted
by p∧q p∧q , which means “p p and q q “. The conjunction p∧q p∧q is True
when both p p and q q are True, otherwise False. The truth table
of p∧q p∧q is:
p q p∧q
T T T
T F F
F T F
F F F
T T T
T F T
F T T
F F F
4. Exclusive Or
For any two propositions p p and q q , their exclusive or is denoted
by p⊕q p⊕q , which means “either p p or q q but not both”. The exclusive
or p⊕q p⊕q is True when either p p or q q is True, and False when both are
true or both are false. The truth table of p⊕q p⊕q is:
p q p⊕q
T T F
T F T
F T T
F F F
Example, Exclusive or of the propositions p p – “Today is Friday” and q q –
“It is raining today”, p⊕q p⊕q is “Either today is Friday or it is raining today,
but not both”. This proposition is true on any day that is a Friday or a rainy
day(not including rainy Fridays) and is false on any day other than Friday when it
does not rain or rainy Fridays.
5 Implication
For any two propositions p p and q q , the statement “if p p then q q ” is
called an implication and it is denoted by p→q p→q .
p q p→q
T T T
T F F
F T T
F F T
T T T
T F F
p q p↔q
F T F
F F T
Example, “It is raining today if and only if it is Friday today.” is a proposition
which is of the form p↔q p↔q . The above proposition is true if it is not Friday
and it is not raining or if it is Friday and it is raining, and it is false when it is not
Friday or it is not raining.
Properties of Operators
2. Associativity:
(P ∧ Q) ∧ R ≡ P ∧ (Q ∧ R)
(P ∨ Q) ∨ R ≡ P ∨ (Q ∨ R)
3. Distributivity:
P ∧ (Q ∨ R) ≡ (P ∧ Q) ∨ (P ∧ R)
P ∨ (Q ∧ R) ≡ (P ∨ Q) ∧ (P ∨ R)
4. Identity:
P ∧ true ≡ P
P ∨ false ≡ P
5. Domination:
P ∨ true ≡ true
P ∧ false ≡ false
6. Double Negation:
¬ (¬P) ≡ P
7. Idempotence:
P∧P≡P
P∨P≡P
Propositional Theorem Proving
It is a different approach to using logic to solve problems is to use logical rules of
inference to generate logical implications
Inference:
In artificial intelligence, we need intelligent computers which can create new logic
from old logic or by evidence, so generating the conclusions from evidence and
facts is termed as Inference.
Inference rules:
Inference rules are the templates for generating valid arguments. Inference rules
are applied to derive proofs in artificial intelligence, and the proof is a sequence of
the conclusion that leads to the desired goal.
In inference rules, the implication among all the connectives plays an important
role. Following are some terminologies related to inference rules:
Implication: It is one of the logical connectives which can be represented
as P → Q. It is a Boolean expression.
Converse: The converse of implication, which means the right-hand side
proposition goes to the left-hand side and vice-versa. It can be written as Q
→ P.
Contrapositive: The negation of converse is termed as contrapositive, and
it can be represented as ¬ Q → ¬ P.
Inverse: The negation of implication is called inverse. It can be represented
as ¬ P → ¬ Q.
From the above term some of the compound statements are equivalent to
each other, which we can prove using truth table:
Types of Inference rules:
1 Modus Ponens (Law of Detachment)
If a conditional statement (“if-then” statement) is true, and its antecedent (the “if”
part) is true, then its consequent (the “then” part) must also be true.
Form: If p → q and p, then q.
Example:
Premise: If it rains, the ground will be wet.
Premise: It is raining.
Conclusion: The ground is wet.
Example:
Premise: If it rains, the ground will be wet.
Premise: The ground is not wet.
Conclusion: It is not raining.
3. Hypothetical Syllogism
If two conditional statements are true, where the consequent of the first is the
antecedent of the second, then a third conditional statement combining the
antecedent of the first and the consequent of the second is also true.
Form: If p → q and q → r, then p → r.
Example:
Premise: If it rains, the ground will be wet.
Premise: If the ground is wet, the plants will grow.
Conclusion: If it rains, the plants will grow.
4. Disjunctive Syllogism
If a disjunction (an “or” statement) is true, and one of the disjuncts (the parts of
the “or” statement) is false, then the other disjunct must be true.
5. Conjunction
If two statements are true, then their conjunction (an “and” statement) is also true.
6. Simplification
If a conjunction (an “and” statement) is true, then each of its conjuncts is also
true.
Form:
If p ∧ q, then p
If p ∧ q, then q
Example:
Premise: It is raining and windy.
Conclusion: It is raining.
7. Addition
If a statement is true, then the disjunction (an “or” statement) of that statement
with any other statement is also true.
Form:
If p, then p ∨ q
Example:
Premise: It is raining.
Conclusion: It is raining or sunny.
First-Order logic:
First-order logic is another way of knowledge representation in artificial
intelligence. It is an extension to propositional logic.
FOL is sufficiently expressive to represent the natural language statements
in a concise way.
First-order logic is also known as Predicate logic or First-order predicate
logic. First-order logic is a powerful language that develops information
about the objects in a more easy way and can also express the relationship
between those objects.
First-order logic (like natural language) does not only assume that the world
contains facts like propositional logic but also assumes the following things
in the world:
Relations: It can be unary relation such as: red, round, is adjacent, or n-any
relation such as: the sister of, brother of, has color, comes between
Function: Father of, best friend, third inning of, end of, .....
These are the symbols that permit to determine or identify the range and scope of
the variable in the logical expression. There are two types of quantifier:
Universal Quantifier:
For all x
For each x
For every x.
Existential Quantifier:
Existential quantifiers are the type of quantifiers, which express that the statement
within its scope is true for at least one instance of something.
Universal Generalization
Universal Instantiation
Existential Instantiation
Existential introduction
Universal Generalization
This rule can be used if we want to show that every element has a similar
property.
In this rule, x must not appear as a free variable.
Universal Instantiation:
Existential Instantiation:
Existential introduction
Proof by resolution
Resolution method is an inference rule which is used in both Propositional as well
as First-order Predicate Logic in different ways. This method is basically used for
proving the satisfiability of a sentence. In resolution method, we use Proof by
Refutation technique to prove the given statement
1. Input: The inputs to the algorithm are the knowledge base (KB) and the query
(α). The knowledge base is a collection of facts in propositional logic, and the
query is the proposition we want to prove.
2. Clause Conversion: The algorithm starts by converting KB ∧ ¬α into a set of
clauses in CNF. This step is crucial because resolution operates only on CNF.
3. Loop and Resolution: In each iteration, the algorithm selects pairs of clauses
and applies the resolution rule. If the resolution of two clauses results in the
empty clause (denoted as False), the algorithm returns true, indicating that the
knowledge base entails the query.
4. Termination: If no new clauses can be added (i.e., new ⊆ clauses), the
algorithm returns false, implying that the query is not entailed by the
knowledge base.
Definite clause: A horn clause with exactly one positive literal in the head.
Horn clause with a head: A horn clause with at most one positive literal in the
head, which may also contain negated literals.
Forms of a Horn Clause :
Definite Clause: A type of Horn clause that has exactly one positive literal. It’s
used in rule-based reasoning systems to represent knowledge.
Horn clauses and definite clauses are fundamental logic structures used in
knowledge representation, especially in forward and backward chaining.
Forward Chaining
Data-Driven: The reasoning starts from available data (facts) and works
toward a goal.
Bottom-Up Approach: It builds knowledge from facts, gradually moving
towards conclusions.
Breadth-First Search Strategy: The inference engine explores multiple
rules simultaneously, applying them step by step.
Possibility of Irrelevant Rules: Forward chaining may explore rules that do
not contribute to the final solution, making it less efficient in some cases
Backward Chaining