UNIT-2-AI-Notes
UNIT-2-AI-Notes
Game : -The situations where more than one agent opponent to each other ,
searching for the solution or goal in the same search spaceis called as Game
Playing.
The two main factors to help to model and solve games in AI.
• Search problem
• Heuristic Evaluation function
• Minmax Algorithm
• Alpha-beta Pruning.
Mini-Max Algorithm in Artificial Intelligence:-
• Mini-max algorithm is a recursive or backtracking algorithm which is used in
decision-making and game theory. It provides an optimal move for the player
assuming that opponent is also playing optimally.
• Both the players are opponent of each other , fight it as the opponent player
gets the minimum benefit while they get the maximum benefit.
where MAX will select the maximized value and MIN will select the
minimized value.
• The minimax algorithm performs a Depth-First Search algorithm for the
exploration of the complete game tree Alternatively Level By Level.
• The minimax algorithm proceeds all the way down to the terminal node of the
tree, then backtrack the tree as the recursion.
Min-Max Algorithm:
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. let's Node A is the initial
state of the tree. first initial value of MAX = - ∞ and MINI = +∞.
Step 2: compare Maximizer initial value is -∞, with each value in terminal state and
determines the higher nodes values. It will find the maximum among the all.
• For node D max(-1,- -∞) => max(-1,4)= 4
• For Node E max(2, -∞) => max(2, 6)= 6
• For Node F max(-3, -∞) => max(-3,-5) = -3
• For node G max(0, -∞) = max(0, 7) = 7
Step 3: Compare Minimizer initial value +∞ with node B, C and will find the 3rd layer
node values.
• For node B= MIN(4,6) = 4
• For node C= MIN (-3, 7) = -3
Step 4: Maximizer again choose the maximum of value of B node, C node and find the
maximum value for the root node A.
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.
• For node A Max(4, -3)= 4
That was the complete workflow of the Mini-Max two player game.
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).
This limitation of the minimax algorithm can be improved from alpha-beta pruning.
Alpha-Beta Pruning:
• Alpha-beta pruning is a modified version of the minimax algorithm. It is an
optimization technique for the minimax algorithm by cut it to half sub tree
without checking each node of game tree to compute the correct minimax
decision is called Alpha-beta pruning.
The technique by which without checking each node of the game tree we
can compute the correct minimax decision, and this technique is
called pruning.
This involves two threshold parameter Alpha and Beta for future
expansion, so it is called alpha-beta pruning.
Alpha-beta pruning can be applied at any depth of a tree, and sometimes
it not only prune the tree leaves but also entire sub-tree.
The Alpha-beta pruning to a standard minimax algorithm returns the same move as the
standard algorithm does, but it removes all the nodes which are not really affecting the
final decision but making algorithm slow. Hence by pruning these nodes, it makes the
algorithm fast.
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.
It is not feasible to consider the whole game tree (even with alpha-beta), so
programs should cut the search off at some point earlier and apply a heuristic
evaluation function to states in the search, effectively turning nonterminal nodes into
terminal leaves.
i.e. Alter minimax or alpha-beta in 2 ways:
1) replace the utility function by a heuristic evaluation function EVAL, which estimates the
position’s utility.
2) replace the terminal test by a cutoff test that decides when to apply EVAL.
Evaluation function
An evaluation function returns an estimate of the expected utility of the game
from a given position.
1) The evaluation function should order the terminal states in the same way as the true
utility function.
2) The computation must not take too long.
3) For nonterminal states, the evaluation function should be strongly correlated with the
actual chances of winning.
Features of the state: Most evaluation function work by calculating various features of
the state,
Example . in chess, number of white pawns, black pawns, white queens, black queens,
etc.
Categories: The features, taken together, define various categories (a.k.a. equivalence
classes) of states, the states in each category have the same values for all the features.
Any given category will contain some states that lead to win, draws or losses, the
evaluation function can return a single value that reflects the proportion of states with
each outcome.
a. Expected value: (requires too many categories and hence too much experience to
estimate) Example:
72% of the states encountered in the two-pawns vs. one-pawn category lead to a
win(utility+!); 20% to a loss(0) and 8% to a draw(1/2).
Then a reasonable evaluation for states in the category is the expected value:
(0.72*1)+(0.20*0)+(0.08*1/2)=0/76.
As with terminal states, the evaluation function need not return actual expected
values as long as the ordering of the sates is the same.
Each wi is a weight and each fi is a feature of the position. For chess, the fi could
be the numbers of each kind of piece on the board (i.e. feature), and wi could be the
values of the pieces (1 for pawn, 3 for bishop, etc.).
Adding up the values of features in fact involves a strong assumption (that the
contribution of each feature is independent of the values of the other features), thus
current programs for games also use nonlinear combinations of features.
UNIT-2 :-Constraint Satisfaction Problems : Defining Constraint Satisfaction
Problems, Constraint Propagation, Backtracking Search for CSPs, Local Search for
CSPs, The Structure of Problems.
• Discrete Domain: It is an infinite domain which can have one state for multiple
variables. For example, a start state space can be allocated infinite times for
each variable.
• Finite Domain: It is a finite domain which can have continuous states describing
one domain for one specific variable. It is also called a continuous domain.
Constraint Propagation:-
Constraint propagation is a special type of inference or permission which helps in
Reducing the Legal Number Of Values For The Variables. The idea behind constraint
propagation is Local Consistency.
In local consistency, variables are treated as nodes, and each binary constraint is
treated as an arc in the given problem.
are following local consistencies which are discussed below:
• Node Consistency: A single variable is said to be node consistent if all the
values in the variable’s domain satisfy the unary constraints on the variables.
• Arc Consistency: A variable is arc consistent if every value in its domain
satisfies the binary constraints of the variables.
• Path Consistency: When the evaluation of a set of two variable with respect to a
third variable can be extended over another variable, satisfying all the binary
constraints. It is similar to arc consistency.
• K-consistency: This type of consistency is used to define the notion of stronger
forms of propagation. Here, we examine the k-consistency of the variableas.
CSP Problems:
Constraint satisfaction includes those problems which contains some constraints while
solving the problem. CSP includes the following problems: Example
• Sudoku Playing: The gameplay where the constraint is that no number from 0-9
can be repeated in the same row or column.
Backtracking search: A depth-first search that chooses values for one variable
at a time and backtracks when a variable has no legal values left to assign.
Variable selection—fail-first:
Minimum-remaining-values (MRV) heuristic:
The idea of choosing the variable with the fewest “legal” value. A.k.a. “most
constrained variable” or “fail-first” heuristic, it picks a variable that is most likely to cause
a failure soon thereby pruning the search tree. If some variable X has no legal values
left, the MRV heuristic will select X and failure will be detected immediately—avoiding
pointless searches through other variables.
E.g. After the assignment for WA=red and NT=green, there is only one possible value
for SA, so it makes sense to assign SA=blue next rather than assigning Q.
[Powerful guide]
Degree heuristic: The degree heuristic attempts to reduce the branching factor on
future choices by selecting the variable that is involved in the largest number of
constraints on other unassigned variables. [useful tie-breaker]
e.g. SA is the variable with highest degree 5; the other variables have degree 2 or 3; T
has degree 0.
Least-constraining-value heuristic: prefers the value that rules out the fewest choice
for the neighboring variables in the constraint graph. (Try to leave the maximum
flexibility for subsequent variable assignments.)
e.g. We have generated the partial assignment with WA=red and NT=green and that
our next choice is for Q. Blue would be a bad choice because it eliminates the last legal
value left for Q’s neighbor, SA, therefore prefers red to blue.
Intelligent backtracking: Backtrack to a variable that was responsible for making one
of the possible values of the next variable (e.g. SA) impossible.
Conflict set for a variable: A set of assignments that are in conflict with some value for
that variable.
(e.g. The set {Q=red, NSW=green, V=blue} is the conflict set for SA.)
backjumping method: Backtracks to the most recent assignment in the conflict set.
(e.g. backjumping would jump over T and try a new value for V.)
Constraint weighting: a technique that can help concentrate the search on the
important constraints.
Each constraint is given a numeric weight Wi, initially all 1.
At each step, the algorithm chooses a variable/value pair to change that will result in the
lowest total weight of all violated constraints.
The weights are then adjusted by incrementing the weight of each constraint that is
violated by the current assignment.
Local search can be used in an online setting when the problem changes, this is
particularly important in scheduling problems.
The structure of problem:
Tree: A constraint graph is a tree when any two varyiable are connected by only one
path.
Directed arc consistency (DAC): A CSP is defined to be directed arc-consistent under
an ordering of variables X1, X2, … , Xn if and only if every Xi is arc-consistent with
each Xj for j>i.
By using DAC, any tree-structured CSP can be solved in time linear in the number of
variables.
How to solve a tree-structure CSP:
Logic : Logic is the key behind any knowledge to filter the necessary information from the
bulk and draw a conclusion. In artificial intelligence, the representation of knowledge is
done via logics.
Knowledge-based agents:- So far we have studied about intelligent agents which acquire
knowledge about the world to make better decisions in the real world. Knowledge-based
agent uses some task-specific knowledge to solve a problem efficiently.
Inference Engine: It is the engine of a knowledge-based system which allows to infer new
knowledge in the system.
Actions Of An Agent:-
When there is a need to add/ update some new information or sentences in
the knowledge-based system, we require an inference system. Also, to know what
information is already known to the agent, we require the inference system. The technical
words used for describing the mechanism of the inference system are: TELL and ASK.
When the agent solves a problem, it calls the agent program each time. The agent
For example, Consider an automated air conditioner. The inbuilt knowledge stored in its
system is that “ It would adjust its temperature according to the weather.” This represents
the knowledge level of the agent. The actual working and its adjustment define the
implementation level of the knowledge-based agent.
Approaches used to build a Knowledge-based agent:-
There are following two approaches used to design the behavior of a knowledge-
based system:
Propositional logic :-
Propositional logic (PL) is the simplest form of logic where all the statements are made
by propositions. A proposition is a declarative statement which is either true or false. It is a
technique of knowledge representation in logical and mathematical form.
Example:
1. a) It is Sunday.
2. b) The Sun rises from West (False proposition)
3. c) 3+3= 7(False proposition)
4. d) 5 is a prime number.
Following are some basic facts about propositional logic:
• Propositional logic is also called Boolean logic , can be either true or false, but it cannot
be both. as it works on 0 and 1.
• In propositional logic, we use symbolic variables to represent the logic, and we can use
any symbol for a representing a proposition, such A, B, C, P, Q, R, etc.
• Propositional logic consists of an object, relations or function, and logical connectives.
These connectives are also called logical operators which connects two sentences.
• The propositions and connectives are the basic elements of the propositional logic.
• A proposition formula which is always true is called tautology, and it is also called a
valid sentence.
• A proposition formula which is always false is called Contradiction.
• Statements which , what , why, where ,who how are questions, commands, or opinions
are not propositions such as "Where is Rohini", "How are you", "What is your name",
are not propositions.
Logical Connectives:
Logical connectives are used to connect two simpler propositions or representing a
sentence logically. We can create compound propositions with the help of logical connectives.
There are mainly five connectives, which are given as follows:
1. Negation: A sentence such as ¬ P is called negation of P. A literal can be either
Positive literal or negative literal.
2. Conjunction: A sentence which has ∧ connective such as, P ∧ Q is called a
conjunction.
Example: Rohan is intelligent and hardworking. It can be written as,
P= Rohan is intelligent,
Q= Rohan is hardworking. → P∧ Q.
3. Disjunction: A sentence which has ∨ connective, such as P ∨ Q. is called
disjunction, where P and Q are the propositions.
Example: "Ritika is a doctor or Engineer",
Here P= Ritika is Doctor. Q= Ritika is Doctor, so we can write it as P ∨ Q.
4. Implication: A sentence such as P → Q, is called an implication. Implications are
also known as if-then rules. It can be represented as
If it is raining, then the street is wet.
Let P= It is raining, and Q= Street is wet, so it is represented as P → Q
5. Biconditional: A sentence such as P⇔ Q is a Biconditional sentence, example If
I am breathing, then I am alive
P= I am breathing, Q= I am alive, it can be represented as P ⇔ Q.
Precedence Operators
First Precedence Parenthesis
Second Precedence Negation
Third Precedence Conjunction(AND)
Fourth Precedence Disjunction(OR)
Fifth Precedence Implication
Six Precedence Biconditional
Note: For better understanding use parenthesis to make sure of the correct interpretations.
Such as ¬R∨ Q, It can be interpreted as (¬R) ∨ Q.
Logical equivalence:
Logical equivalence is one of the features of propositional logic. Two propositions are said
to be logically equivalent if and only if the columns in the truth table are identical to each
other.
Let's take two propositions A and B, so for logical equivalence, we can write it as A⇔B. In
below truth table we can see that column for ¬A∨ B and A→B, are identical hence A is
Equivalent to B,
A B ~A ~AVB A→B
T T F T T
T F F F F
F T T T T
F F T T T
Properties of Operators:
• Commutativity:
• P∧ Q= Q ∧ P, or
• P ∨ Q = Q ∨ P.
• Associativity:
• (P ∧ Q) ∧ R= P ∧ (Q ∧ R),
• (P ∨ Q) ∨ R= P ∨ (Q ∨ R)
• Identity element:
• P ∧ True = P,
• P ∨ True= True.
• Distributive:
• P∧ (Q ∨ R) = (P ∧ Q) ∨ (P ∧ R).
• P ∨ (Q ∧ R) = (P ∨ Q) ∧ (P ∨ R).
• DE Morgan's Law:
• ¬ (P ∧ Q) = (¬P) ∨ (¬Q)
• ¬ (P ∨ Q) = (¬ P) ∧ (¬Q).
• Double-negation elimination:
• ¬ (¬P) = P.
Limitations of Propositional logic:
• We cannot represent relations like ALL, some, or none with propositional logic.
Example:
a. All the girls are intelligent.
b. Some apples are sweet.
Propositional logic has limited expressive power.
In propositional logic, we cannot describe statements in terms of their properties or logical
relationships.