Ai Unit III Notes
Ai Unit III Notes
Engineering &Technology
V.L.Kartheek
Assistant Professor
Email: [email protected]
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
SYLLABUS
UNIT – III
Constraint Satisfaction problems-
Backtracking search for CSP’s,
Local search for CSP Game Playing:
Adversial search,
Games,
Minimax algorithm,
optimal decisions in multiplayer games,
Alpha-Beta pruning,
Evaluation functions,
cutting of search.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Constraint Satisfaction problems
Constraint satisfaction is a technique where a problem is solved when its values satisfy
certain constraints or rules of the problem.
Formally speaking, a constraint satisfaction problem (or CSP) is defined by a set of
Variables XI, X2,. . . , Xn, and a set of constraints, C1, C2,…..Cm.
Each variable Xi has a nonempty domain Di of possible values.
Each constraint Ci involves some subset of the variables and specifies the allowable
combinations of values for that subset.
A state of the problem is defined by an assignment of values to some or all of the
variables, {Xi = vi, Xj = vj, . . .).
In constraint satisfaction, domains are the spaces where the variables reside, following
the problem specific constraints.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Solving Constraint Satisfaction Problems
Constraint satisfaction includes those problems which contains some constraints while
solving the problem. CSP includes the following problems:
Graph Coloring
Sudoku Playing
n-queen problem
Latin square Problem etc.
1. Graph Coloring Problem:-
We have been given a graph and we are asked to color all vertices with the ‘M’ number
of given colors, in such a way that no two adjacent vertices should have the same
color.
It it is possible to color all the vertices with the given colors then we have to
output the colored result, otherwise output ‘no solution possible’.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
N-queens Problem Solution
STEP-1 STEP-2
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Cryptarithmetic problem
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Backtracking search for CSP’s
The term backtracking search is used for 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. The
algorithm is shown in Figure 5.3.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Propagating information through constraints
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Graph Coloring Problem
We have been given a graph and we are asked to color all vertices with the ‘M’ number of given colors, in
such a way that no two adjacent vertices should have the same color.
It it is possible to color all the vertices with the given colors then we have to output the colored result,
otherwise output ‘no solution possible’.
The least possible value of ‘m’ required to color the graph successfully is known as the chromatic
number of the given graph.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Graph Coloring Using Backtracking Algorithm
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Adversial search
Adversarial search is a search, where we examine the problem which arises when we try to
plan ahead of the world and other agents are planning against us.
Searches in which two or more players with conflicting goals are trying to explore the same
search space for the solution, are called adversarial searches, often known as Games.
Games are modeled as a Search problem and heuristic evaluation function, and these are the
two main factors which help to model and solve games in AI.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Formalization of the problem:
A game can be defined as a type of search in AI which can be formalized of the following
elements:
Initial state: It specifies how the game is set up at the start.
Player(s): It specifies which player has moved in the state space.
Action(s): It returns the set of legal moves in state space.
Result(s, a): It is the transition model, which specifies the result of moves in the state
space.
Terminal-Test(s): Terminal test is true if the game is over, else it is false at any case. The
state where the game ends is called terminal states.
Utility(s, p): A utility function gives the final numeric value for a game that ends in
terminal states s for player p. It is also called payoff function. For Chess, the outcomes are
a win, loss, or draw and its payoff values are +1, 0, ½. And for tic-tac-toe, utility values are
+1, -1, and 0.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Minimax algorithm
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Example for Minmax 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.
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 = +infinity.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
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.
For node D max(-1,4,-∞) = 4
For Node E max(2,6,-∞) = 6
For Node F max(-3,-5,-∞) =-3
For node G max(0,7,-∞) = 7
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
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.
For node B= min(4,6, +∞) = 4
For node C= min (-3, 7, +∞) = -3
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
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.
For node A max(4, -3,- ∞) = 4
That was the complete workflow of the minimax two player game.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
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).
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
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.
This type of games has a huge branching factor, and the player has lots of choices to
decide. This limitation of the minimax algorithm can be improved from alpha-beta
pruning .
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
TIC-TAC-TOE GAME Solution using Minmax
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Optimal decisions in multiplayer games
Many popular games allow more than two players. Let us examine how to extend the minimax idea to
multiplayer games.
First, we need to replace the single value for each nocle with a vector of values. For example, in a three-
player game with players A, B, and C, a vector (vA, vB, vc) is associated with each node.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Alpha-Beta Pruning
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Evaluation functions
An evaluation function improves the minimax and alpha-beta algorithms by cutting
off the search earlier, so that moves in the game can be made in reasonable amount
of time.
The evaluation function converts non terminal nodes into terminal leaves.
It returns an estimate of utility value of a game from a given position.
A well designed evaluation function will improve the performance of the game
playing programs.
A well evaluation function can be learned by neural, networks.
The TD GAMMON program is learned to play Backgammon by fully connected feed
forward neural network. The network outputs a set of estimates q1, q2, q3, q4 that
represents possible outcomes of game. The concluded value is obtained by,
v = q1 + 2 q2 - q3 - 2q4
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
Cutting of search
The next step is to modify ALPHA-BETA-SEARCH so that it will call the heuristic EVAL function
when it is appropriate to cut off the search.
The TERMINAL-TIEST with the following line:
if CUT0FF-TEST(state, depth) then return EVAL(state)
We also must arrange for some bookkeeping so that the current depth is incremented on
each recursive call.
The most straightforward approach to controlling the amount of search is to set a fixed
depth limit, so that CUT0FF-TEST(state, depth) returns true for all depth greater than some
fixed depth d.
(It must also return true for all terminal states, just as TERMINAL-TEST did.)
The depth d is chosen so that the amount of time used will not exceed what the rules of the
game allow.
The horizon effect is more difficult to eliminate. It arises when the program is facing a move
by the opponent that causes serious damage and is ultimately unavoidable.
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad
THANK YOU
V.L.Kartheek, Asst Professor ,Department of Computer Science & Engineering, VNRVJIET, Hyderabad