0% found this document useful (0 votes)
59 views88 pages

Ai Unit 2

The document covers adversarial search in artificial intelligence, focusing on game theory concepts such as the minimax algorithm and alpha-beta pruning. It explains types of games, optimal decision-making, and the challenges faced in multiplayer environments. Additionally, it details the workings of the minimax algorithm and its limitations, as well as how alpha-beta pruning optimizes the decision-making process in game trees.

Uploaded by

madhurics247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views88 pages

Ai Unit 2

The document covers adversarial search in artificial intelligence, focusing on game theory concepts such as the minimax algorithm and alpha-beta pruning. It explains types of games, optimal decision-making, and the challenges faced in multiplayer environments. Additionally, it details the workings of the minimax algorithm and its limitations, as well as how alpha-beta pruning optimizes the decision-making process in game trees.

Uploaded by

madhurics247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 88

Unit 2 - Adversarial

Search

MODULE-II
Adversarial Search : Games, Optimal Decisions in Games, The minimax algorithm, Alpha–Beta
Pruning, Defining Constraint Satisfaction Problems, Constraint Propagation, Backtracking Search
for CSPs, Knowledge-Based Agents, The wumpus world.
Adversarial 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.

• Mostly the search strategies which are only associated with a single agent
that aims to find the solution which often expressed in the form of a
sequence of actions. But, there might be some situations where more than
one agent is searching for the solution in the same search space, and this
situation usually occurs in game playing.

• The environment with more than one agent is termed as multi-agent


environment, in which each agent is an opponent of other agent and playing
against each other. Each agent needs to consider the action of other agent
and effect of that action on their performance.
Adversarial Search

• So, 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 modelled as a


• Search problem
• Heuristic evaluation function,
These are the two main factors which help to model and solve games in AI.
Types of Games in AI

• Perfect information:
A game with the perfect information is that in which agents can look into the
complete board. Agents have all the information about the game, and they can see each
other moves also. Examples are Chess, Checkers, Go, etc.

• Imperfect information:
If in a game agents do not have all information about the game and not aware
with what's going on, such type of games are called the game with imperfect information,
such as tic-tac-toe, Battleship, blind, Bridge, etc.

• Deterministic games:
Deterministic games are those games which follow a strict pattern and set of
rules for the games, and there is no randomness associated with them. Examples are chess,
Checkers, Go, tic-tac-toe, etc.

• Non-deterministic games:
Non-deterministic are those games which have various unpredictable events and
has a factor of chance or luck. This factor of chance or luck is introduced by either dice or
cards. These are random, and each action response is not fixed. Such games are also called
as stochastic games.
Example: Backgammon, Monopoly, Poker, etc.
Games
• Game Playing is an important domain of artificial intelligence. Games
don’t require much knowledge; the only knowledge we need to provide
is the rules, legal moves and the conditions of winning or losing
the game.

• Both players try to win the game. So, both of them try to make the
best move possible at each turn.

• Searching techniques like BFS(Breadth First Search) are not accurate


for this as the branching factor is very high, so searching will take a lot
of time. So, we need another search procedures that improve
• Generate procedure so that only good moves are generated.
• Test procedure so that the best move can be explored first.
Games

There are two main approaches to game playing in AI, rule-based


systems and machine learning-based systems.

1.Rule-based systems
use a set of fixed rules to play the game.
2.Machine learning-based systems
use algorithms to learn from experience and make decisions based on
that experience.
Zero-Sum Game

• Zero-sum games are adversarial search which involves pure


competition.
• In Zero-sum game each agent's gain or loss of utility is exactly
balanced by the losses or gains of utility of another agent.
• One player of the game try to maximize one single value, while
other player tries to minimize it.
• Each move by one player in the game is called as ply.
• Chess and tic-tac-toe are examples of a Zero-sum game.
Zero-sum game: Embedded thinking

• The Zero-sum game involved embedded thinking in which one agent or player is
trying to figure out:
• What to do.
• How to decide the move
• Needs to think about his opponent as well
• The opponent also thinks what to do
• Each of the players is trying to find out the response of his opponent to their
actions. This requires embedded thinking or backward reasoning to solve the
game problems in AI.
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.
Optimal Decision

Making the greatest decision feasible in a circumstance while considering a


variety of variables, including the game mechanics, the resources at your disposal,
potential risks and rewards, and the activities of other players, is known as optimal
decision making in a player game.
Challenges
While making the best decisions might help players win multiplayer games, doing
so can also provide a number of difficulties. Among the difficulties are −
• Limited Knowledge
• Stress and Pressure
• Environmentally Dynamic
• Coordination and communication
The MiniMax algorithm
• 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.
• Mini-Max algorithm uses recursion to search through the game-tree.
• Min-Max algorithm is mostly used for game playing in AI. Such as Chess, Checkers, tic-tac-toe, go, and
various tow-players game. This Algorithm computes the minimax decision for the current state.
• In this algorithm two players play the game, one is called MAX and other is called MIN.
• Both the players fight it as the opponent player gets the minimum benefit while they get the maximum
benefit.
• Both Players of the game are opponent of each other, 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.
• The minimax algorithm proceeds all the way down to the terminal node of the tree, then backtrack the
tree as the recursion.
Working of Min-Max Algorithm:
• The working of the minimax algorithm can be easily described using an
example. Below we have taken an example of game-tree which is
representing the two-player game.
• In this example, there are two players one is called Maximizer and other is
called Minimizer.
• Maximizer will try to get the Maximum possible score, and Minimizer will
try to get the minimum possible score.
• This algorithm applies DFS, so in this game-tree, we have to go all the way
through the leaves to reach the terminal nodes.
• At the terminal node, the terminal values are given so we will compare
those value and backtrack the tree until the initial state occurs. Following
are the main steps involved in solving the two-player game tree:
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.
Step 2: Now, first we find the utilities value for the Step 3: In the next step, it's a turn for
Maximizer, its initial value is -∞, so we will compare each minimizer, so it will compare all nodes
value in terminal state with initial value of Maximizer
and determines the higher nodes values. It will find the value with +∞, and will find the 3rd layer
maximum among the all. node values.
• For node D max(-1,- -∞) => max(-1,4)= 4 • For node B= min(4,6) = 4
• For Node E max(2, -∞) => max(2, 6)= 6 • For node C= min (-3, 7) = -3
• For Node F max(-3, -∞) => max(-3,-5) = -3
• For node G max(0, -∞) = max(0, 7) = 7
• 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
The minimax algorithm
• Perfect play for deterministic environments with perfect information

• Basic idea: choose move with highest minimax value= best achievable payoff against best play

• Algorithm:
1. Generate game tree completely
2. Determine utility of each terminal state
3. Propagate the utility values upward in the tree by applying MIN and MAX operators on the nodes in
the current level
4. At the root node use minimax decision to select the move with the max (of the min) utility value

• Steps 2 and 3 in the algorithm assume that the opponent will play perfectly.
ALGORITHM

function minimax(node, depth, maximizingPlayer) is


if depth ==0 or node is a terminal node then
return static evaluation of node

if MaximizingPlayer then // for Maximizer Player


maxEva= -infinity
for each child of node do
eva= minimax(child, depth-1, false)
maxEva= max(maxEva,eva) //gives Maximum of the values
return maxEva

else // for Minimizer player


minEva= +infinity
for each child of node do
eva= minimax(child, depth-1, true)
minEva= min(minEva, eva) //gives minimum of the values
return minEva
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( the number of children
that each node has) 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.
• 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 .
Min Max algorithm examples
Alpha-Beta Pruning
•Alpha-beta pruning is a modified version of the minimax algorithm. It is an optimization
technique for the minimax algorithm.
• there is a 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. It is also called as Alpha-Beta Algorithm.
•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 two-parameter can be defined as:
• Alpha: The best (highest-value) choice we have found so far at any point along the path of
Maximizer. The initial value of alpha is -∞.
• Beta: The best (lowest-value) choice we have found so far at any point along the path of
Minimizer. The initial value of beta is +∞.
• 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
Condition for Alpha-beta pruning:
• The main condition which required for alpha-beta pruning is:
α>=β

Key points about alpha-beta pruning:


• The Max player will only update the value of alpha.
• The Min player will only update the value of beta.
• While backtracking the tree, the node values will be passed to
upper nodes instead of values of alpha and beta.
• We will only pass the alpha, beta values to the child nodes.
Working of Alpha-Beta
Pruning:
• Let's take an example of two-
player search tree to understand
the 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.
Pseudo-code for Alpha-beta Pruning:

function minimax(node, depth, alpha, beta, ma


ximizingPlayer) is else // for Minimizer pl
if depth ==0 or node is a terminal node then ayer
return static evaluation of node minEva= +infinity
for each child of node do
if MaximizingPlayer then // for Maximizer Pla eva= minimax(child, depth-
yer
1, alpha, beta, true)
maxEva= -infinity
for each child of node do minEva= min(minEva, eva)

eva= minimax(child, depth- beta= min(beta, eva)


1, alpha, beta, False) if beta<=alpha
maxEva= max(maxEva, eva)
break
alpha= max(alpha, maxEva)
return minEva
if beta<=alpha
break
return maxEva
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.
It can be of two types:
• Worst ordering: In some cases, alpha-beta pruning algorithm
does not prune any of the leaves of the tree, and works exactly as
minimax algorithm. In this case, it also consumes more time
because of alpha-beta factors, such a move of pruning is called
worst ordering. In this case, the best move occurs on the right side
of the tree. The time complexity for such an order is O(bm).
• Ideal ordering: The ideal ordering for alpha-beta pruning occurs
when lots of pruning happens in the tree, and best moves occur at
the left side of the tree. We apply DFS hence it first search left of
the tree and go deep twice as minimax algorithm in the same
amount of time. Complexity in ideal ordering is O(bm/2).
Rules to find good ordering:

Following are some rules to find good ordering in alpha-


beta pruning:
• Occur the best move from the lowest node.
• Order the nodes in the tree such that the best nodes
are checked first.
• Use domain knowledge while finding the best move.
Ex: for Chess, try order: captures first, then threats,
then forward moves, backward moves.
α-β pruning
• Alpha-beta search updates the values of α and β as it goes along and
prunes the remaining branches at a node as soon as the value of the
current node is known to be worse than the current α or β value for
MAX or MIN, respectively.
• The effectiveness of alpha-beta pruning is highly dependent on the
order in which the successors are examined.
Properties of α-β
Pruning does not affect final result
Good move ordering improves effectiveness of pruning
• With "perfect ordering," time complexity = O(bm/2)
 doubles depth of search
Alpha Beta Pruning Example
1 4 7 2 3 0 6 5
-1 4 2 6 -3 -5 0 7

4 3 6 2 2 1 9 5
Constrain Satisfaction Problem
• Finding a solution that meets a set of constraints is the goal of constraint
satisfaction problems (CSPs).
• Finding values for a group of variables that fulfill a set of restrictions or rules is
the aim of constraint satisfaction problems.
• These constraints impose restrictions on the values or assignments of variables
in such a way that the variables must be assigned values from their respective
domains while meeting all specified conditions.
• For tasks including resource allocation, planning, scheduling, and decision-
making, CSPs are frequently employed in AI.
Significance of Constraint Satisfaction Problem in AI

• CSPs are highly significant in artificial intelligence for several reasons:


• They model a wide range of real-world problems where decision-making is
subject to certain conditions and limitations.
• CSPs offer a structured and general framework for representing and solving
problems, making them versatile in problem-solving applications.
• Many AI applications, such as scheduling, planning, and configuration, can be
mapped to CSPs, allowing AI systems to find optimal solutions efficiently.
Basic Components of CSP

There are mainly three basic components in the constraint satisfaction problem:

Variables: The things that need to be determined are variables. Variables in a CSP are the

objects that must have values assigned to them in order to satisfy a particular set of

constraints. Boolean, integer, and categorical variables are just a few examples of the
• Domains: The range of potential values that a
variable can have is represented by domains. Depending
on the issue, a domain may be finite or limitless. For
instance, in Sudoku, the set of numbers from 1 to 9 can
serve as the domain of a variable representing a problem
cell.

• Constraints: The guidelines that control how variables


relate to one another are known as constraints.
Constraints in a CSP define the ranges of possible values
for variables. Unary constraints, binary constraints,
and higher-order constraints are only a few examples
of the various sorts of constraints. For instance, in a
sudoku problem, the restrictions might be that each row,
column, and 3×3 box can only have one instance of each
number from 1 to 9.
Constraint Satisfaction Problems (CSP)
representation:

• The finite set of variables V1, V2, V3 ……………..Vn.


• Non-empty domain for every single variable D1, D2,
D3 …………..Dn.
• The finite set of constraints C1, C2 …….…, Cm.
• where each constraint Ci restricts the possible values for
variables,
• e.g., V1 ≠ V2
• Each constraint Ci is a pair <scope, relation>
• Example: <(V1, V2), V1 not equal to V2>
• Scope = set of variables that participate in constraint.
• Relation = list of valid variable value combinations.
Types of Constraints in CSP

In constraint Satisfaction Problems (CSPs), constraints are used to specify


relationships between variables and limit the possible combinations of values
that can be assigned to those variables. There are several types of constraints
commonly used in CSPs:
• Basically, there are three different categories of limitations in regard towards
the parameters:
• Unary restrictions are the easiest kind of restrictions because they only
limit the value of one variable.
• Binary resource limits: These restrictions connect two parameters. A value
between x1 and x3 can be found in a variable named x2.
• Global Resource limits: This kind of restriction includes a unrestricted
amount of variables.

Examples
• Unary Constraints: Unary constraints limit the possible values of a single
variable without considering the values of other variables. It is the easiest
constraint to find, as it has only one parameter. Example: The expression X1
≠ 7 says that the variable X1 cannot have the value 7.

• Binary Constraints: Binary constraints describe the relationship between


two variables and consist of only two variables. Example: X1< X2 indicates
that X1 must be less than X2 in order to be true.
•Global Constraints: In contrast to unary or binary constraints, global constraints involve
multiple variables and impose a more complex relationship or restriction between them.
Global constraints are often used in CSP problems to capture higher-level patterns, structures, or
rules. These restrictions can apply to any number of variables at once and are not limited to
pairwise interactions. It is further divided into two main categories:
•Alldifferent Constraint: The All different constraint (AllDiff) requires that each variable in a
set of variables has a unique value. You commonly apply all different constraints, when you
want to be sure that no two variables in a set can take the same value. Example: The expression
alldifferent(X1, X2, X3) ensures that the values of X1, X2, and X3 must be unique.
•Sum Constraint: The Sum Constraint requires that the sum of the values assigned to a group
of variables meet a particular requirement. It is useful for expressing restrictions like “the
sum of these variables should equal a certain value.” Example: The expression Sum(X1, X2, X3)
= 15 demands that the sum of the values for X1, X2, and X3 be 15.
Real-World Examples of CSPs

To illustrate CSPs, consider the following examples:


• Sudoku Puzzles: In Sudoku, the variables are the empty cells,
the domains are numbers from 1 to 9, and the constraints ensure
that no number is repeated in a row, column, or 3x3 subgrid.
• Scheduling Problems: In university course scheduling,
variables might represent classes, domains represent time slots,
and constraints ensure that classes with overlapping students or
instructors cannot be scheduled simultaneously.
• Map Coloring: In the map coloring problem, variables represent
regions or countries, domains represent available colors, and
constraints ensure that adjacent regions must have different
colors.
• Resource Allocation: In resource allocation problems,
variables represent tasks or jobs, domains represent
resource assignments, and constraints ensure that resource
limits are not exceeded. Soft constraints may be used to
optimize resource usage while considering costs.
• Game Playing: In game playing, CSPs can represent game
states, and constraints define the rules of the game. Global
constraints ensure that game moves are legal, and
optimization may aim to find the best move based on a
scoring function

Algorithms in CSP
Constraint Satisfaction Problems (CSPs) are typically solved
using various algorithms designed to find a consistent
assignment of values to variables that satisfies all the
constraints. Some of the common algorithms used for solving
CSPs include:
• The Backtracking Algorithm: The backtracking algorithm is a
popular method for resolving CSPs. It looks for the search
space by picking a variable, setting a value for it, and
then recursively scanning through the other variables. In
the event of a conflict, it goes back and tries a different value
for the preceding variable. The backtracking algorithm’s
essential elements are:
• Variable Ordering: The order in which variables are chosen is known
as variable ordering.
• Value Ordering: The sequence in which values are assigned to
variables is known as value ordering.
• Constraint Propagation: Reducing the domain of variables based on
constraint compliance is known as constraint propagation.
• Forward Checking: The backtracking technique has been
improved using forward checking. It tracks the remaining
accurate values of the unassigned variables after each
assignment and reduces the domains of variables whose
values don’t match the assigned ones. As a result, the search
space is smaller, and constraint propagation is more
effectively accomplished.
• Constraint Propagation: Constraint propagation techniques
reduce the search space by removing values inconsistent
with current assignments through local consistency checks.
To do this, techniques like generalized arc consistency and
path consistency are applied
Domain Categories in CSP
• In Constraint Satisfaction Problems (CSPs), domain categories
refer to the set of possible values that can be assigned to each
variable in the problem. The specific categories or domains can
vary depending on the nature of the CSP, but here are some
common domain categories:
• Finite Domain: Variables in many CSPs have finite domains
that are made up of discrete values. Examples comprise:
• Binary Domains: Domains that only have two values (for binary
CSPs, this would be 0 and 1).
• Integer Domains: Domains made up of a limited number of integer
values, such as 1, 2, 3, and 4, are known as integer domains.
• Enumeration Domains: Domains containing a limited number of
distinct values, such as “red, green, and blue” in an issue involving
color assignment.
• Continuous Domains: Some CSPs contain variables
whose domains are continuous, i.e., they can accept
any real number falling within a given range. Examples
comprise:
• Real-valued Domains: Variables may accept any real
number that falls within a given range (for example, X [0, 1]).
• Interval Domains: Variables are limited to a specific range of
real values in the interval domain. e.g., X ∈ [−π, π])
Applications OF CSP -The map coloring problem.
• We are given the task of coloring each region red, green, or blue in such a way that the neighboring regions
must not have the same color.
• To formulate this as CSP, we define the variable to be the regions: WA, NT, Q, NSW, V, SA, and T.
• The domain of each variable is the set {red, green, blue}.
• The constraints require neighboring regions to have distinct colors: for example, the allowable combinations
for WA and NT are the pairs {(red,green),(red,blue),(green,red),(green,blue),(blue,red),(blue,green)}.
• (The constraint can also be represented as the inequality WA ≠ NT). There are many possible solutions, such as
{WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = red}.
• Map of Australia showing each of its states and territories
• With the map colouring algorithm, a graph G and the colours to be added to the graph
are taken as an input and a coloured graph with no two adjacent vertices having the
same colour is achieved
Algorithm
•Initiate all the vertices in the graph.
•Select the node with the highest degree to colour it with any colour.
•Choose the colour to be used on the graph with the help of the selection colour function so that no
adjacent vertex is having the same colour.
•Check if the colour can be added and if it does, add it to the solution set.
•Repeat the process from step 2 until the output set is ready.
The map-coloring problem represented as a constraint graph.
CSP can be viewed as a standard search problem as follows:
• Initial state : the empty assignment {},in which all variables are unassigned.
• Successor function: a value can be assigned to any unassigned variable, provided
that it does not conflict with previously assigned variables.
• Goal test: the current assignment is complete.
• Path cost: a constant cost(E.g.,1) for every step.

Solution:
WA: red
NT: green
SA: blue
Q: red
NSW: green
V: red
T: green
Map Coloring Examples
A Simple example
Variables
A,B,C
Domains
D = {1, 2, 3} for all A, B, C
Constraints
C1: A > B
C2: B ≠ C
C3: A ≠ C
One Solution [A Complete
Assignment]
Using backtracking Method: A simplest Algo. For CSP

A = 1 (Partial assignment), ok
A = 1, B = 1 [C1 is broken, backtrack on B]
A = 1, B = 2 [C1 is broken, backtrack on B]
A = 1, B = 3 [C1 is broken, backtrack on A]
A = 2 (Partial assignment), ok
A = 2, B = 1 (Partial assignment), ok
A = 2, B = 1, c = 1 [C2 is broken, backtrack on C]
A = 2, B = 1, c = 2 [C3 is broken, backtrack on C]
A = 2, B = 1, c = 3 [ALL CONSTRAINTS SATISFIED]'
One Solution (Complete assignment) = { A = 2, B = 1, C = 3}
Constraint Propagation
Constraint propagation is the process of using the constraints to reduce the
domain of possible values for each variable and to infer new constraints from the
existing ones.
For example, if you have a variable X that can take values from 1 to 10, and a
constraint that X must be even, then you can reduce the domain of X to 2, 4, 6, 8,
and 10. Similarly, if you have a constraint that X + Y = 12, and you know that X = 4,
then you can infer that Y = 8. By applying constraint propagation iteratively, you
can eliminate inconsistent values and simplify the problem.
One of the main advantages of constraint propagation is that it can reduce the
search space and prune branches that lead to dead ends. This can make the
problem easier to solve and improve the performance of your algorithm.
For example, if you use constraint propagation to assign colors to a map, you
might find that some regions have only one possible color left, and you can assign
it without further exploration.
Solving Constraint Satisfaction Problems in Artificial Intelligence:

2. Constraint Propagation:
• Constraint propagation is a powerful technique that enforces constraints throughout
the CSP solving process. It narrows down the domains of variables by iteratively
applying constraints. It's often used in conjunction with backtracking to improve
efficiency. The concept of constraint propagation can be illustrated as follows:
• Step 1: Start with an initial CSP problem in ai with variables, domains, and
constraints.
• Step 2: Apply constraints that have been specified in the problem to narrow down
the domains of variables. For instance, if two variables have a binary constraint that
one must be double the other, this constraint will eliminate many inconsistent
assignments.
• Step 3: After constraint propagation, some variables may have their domains reduced
to only a few possibilities, making it easier to find valid assignments.
• Step 4: If a variable's domain becomes empty during propagation, it indicates that
the current assignment is inconsistent, and backtracking is needed.
Illustration with a Simple CSP Example:

Let's consider a simplified Sudoku puzzle to illustrate the problem-solving process step by step:
Variables: 9x9 grid cells
Domains: Numbers from 1 to 9
Constraints: No number can repeat in the same row, column, or 3x3 subgrid.
Step 1: Start with an empty Sudoku grid.
Step 2: Apply the initial constraints for the given numbers, reducing the domains of variables
based on the puzzle's clues.
Step 3: Use constraint propagation to narrow down the domains further. For example, if a row
has two cells with domains {2, 5}, and the constraint specifies that these two cells cannot have
the same number, we can eliminate the possibility of 5 for one of them.
Step 4: Continue applying constraints and propagating until the domains of variables are either
empty or filled with single values. If they are all filled, you have a valid solution. If any variable's
domain is empty, you backtrack to the previous step and try an alternative assignment.
This simple example demonstrates how backtracking and constraint propagation work together
to efficiently find a solution to a CSP. The combination of systematic search and constraint
enforcement allows for solving complex problems in various domains.
The process of constraint propagation typically involves three main
steps:
• Propagation: During this step, the constraints propagate information
throughout the problem domain. This propagation usually involves
iteratively applying inference rules based on the constraints to
reduce the domains of variables.
• Detection: After propagation, the algorithm checks for any
inconsistencies or conflicts that may have arisen due to the constraint
propagation. If such conflicts are detected, the current assignment is
not feasible.
• Resolution: If conflicts are detected, the algorithm must backtrack
and revise the previous decisions made. This could involve undoing
variable assignments or making choices to resolve the conflicts.
Techniques of Constraint Propagation:
• Arc Consistency: Arc consistency is a basic form of constraint propagation
where each variable is made consistent with its neighboring variables based
on the constraints. This involves checking for consistency between each pair
of variables connected by a constraint.
• Path Consistency: Path consistency extends arc consistency by considering
longer paths of variables and constraints. It ensures that the constraints are
consistent along any path of variables in the CSP.
• Forward Checking: Forward checking is a more efficient form of constraint
propagation that is used during the search process. After each variable
assignment, forward checking updates the domains of neighboring variables,
eliminating any values that are inconsistent with the current assignment.
• Constraint Propagation Algorithms: There are specific algorithms designed
for constraint propagation, such as AC-3 (Arc Consistency 3) and AC-4, which
efficiently enforce arc consistency in a CSP.
Arc Consistency Arc consistency between B and C: We
Consider a CSP with three variables: A, B, and C. check each value in the domain of B
Each variable has a domain of {1, 2, 3}. against each value in the domain of C to
Additionally, we have the following constraints: ensure no conflicts with the constraint
A≠B B≠C B ≠ C.
Step 1: Initial Setup For B = 1, we check if there is any
• Initially, each variable has a domain of {1, 2, 3}. value in C's domain that satisfies C ≠
Step 2: Arc Consistency Enforcement 1. Since C's domain is {1, 2, 3}, and C
• Arc consistency between A and B: We check each ≠ 1 holds for all values in C's
value in the domain of A against each value in the domain, B = 1 remains consistent.
domain of B to ensure no conflicts with the
constraint A ≠ B. Similarly, we check for B = 2 and B =
• For A = 1, we check if there is any value in B's 3.
domain that satisfies B ≠ 1. Since B's domain is After this step, no values are eliminated
{1, 2, 3}, and B ≠ 1 holds for all values in B's from B's domain, and the arc
domain, A = 1 remains consistent. consistency between B and C is
• Similarly, we check for A = 2 and A = 3. maintained.
After this step, no values are eliminated from A's
domain, and the arc consistency between A and B is
Path Consistency
Let's consider a slightly more complex CSP with four variables: A, B, C, and D. Each variable has a domain of
{1, 2, 3}.
The constraints for this CSP are as follows:
• A≠B
• B≠C
• C≠D
We'll demonstrate the process of enforcing path consistency step by step:
Step 1: Initial Setup
• Initially, each variable has a domain of {1, 2, 3}.
Step 2: Path Consistency Enforcement
• Path consistency between A and B: Since there's a direct constraint A ≠ B, we enforce arc consistency
between A and B. As we've seen in the previous example, this doesn't eliminate any values from A or B's
domain.
• Path consistency between B and C: Similarly, since there's a direct constraint B ≠ C, we enforce arc
consistency between B and C. Again, no values are eliminated from B or C's domain.
• Path consistency between C and D: There's a direct constraint C ≠ D. We enforce arc consistency between
C and D. As before, no values are eliminated from C or D's domain.
AC-3:
Consider a CSP with three variables: A, B, and C. Each variable has a domain of
{1, 2, 3}.
Additionally, we have the following constraints:
A≠B
B≠C
Step 1: Initial Setup
• Initialize a queue with all the arcs (binary constraints) in the CSP.
Initial domains:
• Domain(A) = {1, 2, 3}
• Domain(B) = {1, 2, 3}
• Domain(C) = {1, 2, 3}
• Initial queue: {(A, B), (B, C)}
Step 2: AC-3 Algorithm
The AC-3 algorithm processes each arc in the queue, checking and possibly reducing the
domains of variables based on arc consistency.
• Processing (A, B):
• For each value a in Domain(A), check if there exists any value b in Domain(B) such that a ≠ b holds.
• If there's no value b in Domain(B) satisfying a ≠ b, remove a from Domain(A).
• Since A ≠ B, all values in Domain(A) remain consistent.
• Processing (B, C):
• For each value b in Domain(B), check if there exists any value c in Domain(C) such that b ≠ c holds.
• If there's no value c in Domain(C) satisfying b ≠ c, remove b from Domain(B).
• Since B ≠ C, all values in Domain(B) remain consistent.
Step 3: Result
After processing all arcs, the domains are:
• Domain(A) = {1, 2, 3}
• Domain(B) = {1, 2, 3}
• Domain(C) = {1, 2, 3}
No values were removed from any domain, indicating that the CSP remains consistent with
the given constraints.
Forward CheckingStep 2: Forward Checking Algorithm
Consider a CSP with three
variables: A, B, and C. Each variable
Assignment: Suppose we assign a value
has a domain of {1, 2, 3}. to variable A, let's say A = 1.
Additionally, we have the following Forward Checking:
constraints: Since A is assigned the value 1, we check the
constraints involving A.
• A≠B For A ≠ B, we remove 1 from the domain of B.
• B≠C For B ≠ C, no action is needed since B is not assigned
yet.
Step 1: Initial Setup Updated domains:
Initialize the domains of variables: Domain(A) = {1}
• Domain(A) = {1, 2, 3} Domain(B) = {2, 3}
Domain(C) = {1, 2, 3}
• Domain(B) = {1, 2, 3}
• Domain(C) = {1, 2, 3}
Assignment: Now, let's assign a value to variable B, let's say B = 2.
• Forward Checking:
• Since B is assigned the value 2, we check the constraints involving B.
• For A ≠ B, no action is needed since A is already assigned.
• For B ≠ C, we remove 2 from the domain of C.
• Updated domains:
• Domain(A) = {1}
• Domain(B) = {2}
• Domain(C) = {1, 3}
Assignment: Finally, let's assign a value to variable C, let's say C = 3.
• Forward Checking:
• Since C is assigned the value 3, we check the constraints involving C.
• For A ≠ B, no action is needed since neither A nor B are assigned.
• For B ≠ C, no action is needed since B is already assigned.
• Updated domains remain unchanged:
• Domain(A) = {1}
• Domain(B) = {2}
• Domain(C) = {3}
Cryptarithmetic Problem
Cryptarithmetic Problem is a type of constraint satisfaction problem where
the game is about digits and its unique replacement either with alphabets
or other symbols. In cryptarithmetic problem, the digits (0-9) get
substituted by some possible alphabets or symbols. The task in
cryptarithmetic problem is to substitute each digit with an alphabet
to get the result arithmetically correct.
The rules or constraints on a cryptarithmetic problem are as
follows:
•There should be a unique digit to be replaced with a unique alphabet.
•The result should satisfy the predefined arithmetic rules, i.e., 2+2 =4,
nothing else.
•Digits should be from 0-9 only.
•There should be only one carry forward, while performing the addition
operation on a problem.
•The problem can be solved from both sides, i.e., lefthand side (L.H.S),
or righthand side (R.H.S)
Let’s understand the cryptarithmetic problem as well its constraints
better with the help of an example:
1) here result is 3 digit no so leftmost digit must be carry now if we add
any 2 no between 0 to 9 we can get max 18 so O=1.
2) so T becomes 2 and we need to add 2+ G=U+10. The G value must
produce carry so we can take G=8 hence U=0.
3)We cant take G=9 bcoz 2+9=11 it means U=1 which is not possible.

T O letter digit
+G O T 2 2 1
------------- O 1 8 1
OU T G 8 -----------------------
U 0 1 0 2
S E ND • S+M,if m=1,S+M>=10,S=9
+MORE • E+O=N but O=zero if C2=0 then we can take any
___________ • value from 0 to 9
MONEY
• for E but in that case Both E and N will be same
C3(1) C2(1) C1(1) • which is not possible.
S(9) E (5) N(6) D(7) • Hence our assumption of C2=0 is wrong.
+ M(1) O(0) R (8) E(5) • So now C2=1
-------------------------------------
• Let E=5 so N=C2+E+O=1+5+0=6.
1 0 6 5 2
• Let R=9 so N+R=6+9=15 as E is already 5 so 15
means 5 and 1 carry.
• But already we assigned S=9 so we cant assign R=9.
TWO + TWO = FOUR • So we can take C1=1 and R=8 so that 1+6+8=15.
GERALD + DONALD = ROBERT
CROSS + ROADS = DANGER
• Now D+E=Y and it should produce carry C1=1
POTATO + TOMATO = PUMPKIN • which is possible only when D>=5.
(P-1,O-6,T-8,A-4,M-3,U-0,K-9,I-7,N-2)
• So D+5=Y and D can be between 5 to 9.
Wumpus world:
• The Wumpus world is a simple world example to illustrate the worth of a knowledge-based agent and to represent

knowledge representation. It was inspired by a video game Hunt the Wumpus by Gregory Yob in 1973.

• The Wumpus world is a cave which has 4/4 rooms connected with passageways. So there are total 16 rooms which are

connected with each other. We have a knowledge-based agent who will go forward in this world.

• The cave has a room with a beast which is called Wumpus, who eats anyone who enters the room. The Wumpus can be

shot by the agent, but the agent has a single arrow.

• In the Wumpus world, there are some Pits rooms which are bottomless, and if agent falls in Pits, then he will be stuck

there forever. The exciting thing with this cave is that in one room there is a possibility of finding a heap of gold.

• So the agent goal is to find the gold and climb out the cave without fallen into Pits or eaten by Wumpus. The agent

will get a reward if he comes out with gold, and he will get a penalty if eaten by Wumpus or falls in the pit.
Components of Wumpus WORLD:
1. The rooms adjacent to the Wumpus room are smelly, so that it would have some stench.
2. The room adjacent to PITs has a breeze, so if the agent reaches near to PIT, then he will perceive the breeze.
3. There will be glitter in the room if and only if the room has gold.
4. The Wumpus can be killed by the agent if the agent is facing to it, and Wumpus will emit a horrible scream which can be
heard anywhere in the cave.
PEAS description of Wumpus world:
Performance measure:
• +1000 reward points if the agent comes out of the cave with the gold.
• -1000 points penalty for being eaten by the Wumpus or falling into the pit.
• -1 for each action, and -10 for using an arrow.
• The game ends if either agent dies or came out of the cave.
Environment:
• A 4*4 grid of rooms.
• The agent initially in room square [1, 1], facing toward the right.
• Location of Wumpus and gold are chosen randomly except the first square [1,1].
• Each square of the cave can be a pit with probability 0.2 except the first square.
Actuators:
• Left turn,
• Right turn
• Move forward
• Grab
• Release
• Shoot.
Sensors:
• The agent will perceive the stench if he is in the room adjacent to the Wumpus. (Not diagonally).
• The agent will perceive breeze if he is in the room directly adjacent to the Pit.
• The agent will perceive the glitter in the room where the gold is present.
• The agent will perceive the bump if he walks into a wall.
• When the Wumpus is shot, it emits a horrible scream which can be perceived anywhere in the cave.
• These percepts can be represented as five element list, in which we will have different indicators for each
sensor.
• Example if agent perceives stench, breeze, but no glitter, no bump, and no scream then it can be represented
as:
[Stench, Breeze, None, None, None].
Wumpus World

INTRODUCTION

Wumpus, Strenchy, Agent, Pit, Breeze, Gold, Arrow


Environment
 Agent enters in [1,1]
 16 rooms
 Empty Rooms
 Room with Wumpus: A deadly beast who kills anyone entering his
room.
 Stenchy rooms
 Breezy rooms
 Room with gold
 Arrow
 Cave of 4×4
 Pits: Bottomless pits that will
 trap you forever.

Agents Sensors:
 Odor sensor -Stench next to Wumpus
 Odor sensor -Breeze next to pit
 Camera-Glitter in square with gold
 Bump when agent moves
 into a wall
 Audio Sensor-Scream from wumpus when killed

Agents actions
 Agent can move forward, turn left or turn right
 Shoot, one shot from arrow
 Grab Gold
Wumpus world


Awards and punishments
 +1000 for picking up gold
 -1000 got falling into pit
 -1 for each move
 -10 for using arrow

Performance measure
- +1000 for picking up gold
 -1000 got falling into pit
 -1 for each move
 -10 for using arrow

percept is [Stench = none, Breeze =


none,Glitter = none,Bump = none, Scream =
none],
Exploring the Wumpus World
• Initially agent is at [1,1]. The KB initially contains the rules of the
environment.
• The first percept is [Stench = none, Breeze = none,Glitter =
none,Bump = none, Scream = none],
• move to safe cell e.g. [2,1]
Exploring the Wumpus World

• [2,1] = breeze
• indicates that there is a pit in [2,2] or [3,1],
• return to [1,1] to try next safe cell
Exploring the Wumpus World
• Exploring the Wumpus World
• [1,2] Stench in cell which means that wumpus is in [1,3] or [2,2]
• YET … not in [1,1]
• YET … not in [2,2] or stench would have been detected in [2,1]
• (this is relatively sophisticated reasoning!)
Exploring the Wumpus World
• THUS … wumpus is in [1,3]
• THUS [2,2] is safe because of lack of breeze in [1,2]
• THUS pit in [3,1] (again a clever inference)
• move to next safe cell [2,2]
Exploring the Wumpus World
• [2,2] move to [2,3]
• [2,3] detect glitter , stench, breeze
• THUS pick up gold
• THUS pit in [3,3] or [2,4]
Knowledge-Based Agent in Artificial intelligence
• An intelligent agent needs knowledge about the real The learning element of KBA regularly updates the
world for taking decisions and reasoning to act
efficiently. KB by learning new knowledge .
• 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-based agents are composed of two main
parts:
• Knowledge-base and
• Inference system.
• The is showing how knowledge-based agent (KBA)
take input from the environment by perceiving the
environment. The input is taken by the inference
engine of the agent and which also communicate
with KB to decide as per the knowledge store in KB.
Knowledge base: Knowledge-base is a central component of a knowledge-based agent, it is also
known as KB. It is a collection of sentences. These sentences are expressed in a language which is
called a knowledge representation language. The Knowledge-base of KBA stores fact about the world.
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 ( In AI, a proposition refers to a
declarative sentence that is either true or false.)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
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.
3. Perform: It performs the selected action.
And the structure of a generic knowledge-based agents program:
function KB-AGENT(percept):
persistent: KB, a knowledge base
t, a counter, initially 0, indicating time
TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
Action = ASK(KB, MAKE-ACTION-QUERY(t))
TELL(KB, MAKE-ACTION-SENTENCE(action, t))
t=t+1
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:
• Firstly it TELLs the KB what it perceives.
• Secondly, it asks KB what action it should take
• Third agent program TELLS the KB that which action was chosen.
The MAKE-PERCEPT-SENTENCE generates a sentence as setting that the agent
perceived the given percept at the given time.
The MAKE-ACTION-QUERY generates a sentence to ask which action should be
done at the current time.
MAKE-ACTION-SENTENCE generates a sentence which asserts that the chosen
action was executed.
levels of knowledge-based agent:
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: At this level, we understand that how the knowledge representation of knowledge is
stored. At this level, sentences are encoded into different logics. At the logical level, an encoding of
knowledge into logical sentences occurs. At the logical level we can expect to the automated taxi agent
to reach to the destination B.
3. Implementation level: This is the physical representation of logic and knowledge. At the
implementation level agent perform actions as per logical and knowledge level. At this level, an
automated taxi agent actually implement his knowledge and logic so that he can reach to the
destination.
Approaches to designing a knowledge-based agent:
Declarative approach: We can create a knowledge-based agent by initializing with an empty
knowledge base and telling the agent all the sentences with which we want to start with. This approach
is called Declarative approach.
Procedural approach: In the procedural approach, we directly encode desired behavior as a program
code. Which means we just need to write a program that already encodes the desired behavior or agent.
Procedural Knowledge vs Declarative Knowledge

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy