0% found this document useful (0 votes)
18 views43 pages

Unit 2

Adversarial search algorithms are essential for strategic decision-making in competitive environments, particularly in game-playing scenarios where agents have opposing objectives. Key algorithms include Minimax and Alpha-Beta Pruning, which help AI systems evaluate future consequences and optimize moves against intelligent opponents. These algorithms are widely applied in various fields, including board games, economic models, and autonomous decision systems.

Uploaded by

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

Unit 2

Adversarial search algorithms are essential for strategic decision-making in competitive environments, particularly in game-playing scenarios where agents have opposing objectives. Key algorithms include Minimax and Alpha-Beta Pruning, which help AI systems evaluate future consequences and optimize moves against intelligent opponents. These algorithms are widely applied in various fields, including board games, economic models, and autonomous decision systems.

Uploaded by

demonincarnate77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT - 2

Adversarial Search Algorithms in Artificial Intelligence (AI)


Adversarial search algorithms form the foundation of strategic decision-making in Artificial
Intelligence, especially in environments where multiple intelligent agents compete against each
other. These algorithms enable AI systems to anticipate opponent actions, evaluate future
consequences, and select the best possible move under competitive conditions.

Adversarial search is widely used in game-playing, strategic planning, and competitive decision-
making, where the success of one agent often comes at the expense of another. This discussion
explains the core ideas, major algorithms, and real-world applications of adversarial search in a clear
and structured manner.

What is Adversarial Search?

Adversarial search is a problem-solving approach designed for competitive environments, where


two or more agents have opposing objectives. It is most applied to two-player zero-sum games,
meaning:

 A gain for one player results in an equal loss for the other.

 There is no possibility of a win–win outcome.

In such environments, each agent must consider not only its own actions but also the possible
responses of the opponent. The goal of adversarial search is to determine an optimal strategy by
examining all feasible moves and counter-moves.

In Artificial Intelligence, adversarial search plays a crucial role in enabling agents to make rational
and optimal decisions while assuming that the opponent is also acting intelligently and strategically.

Key Characteristics of Adversarial Environments

Adversarial search is particularly useful in environments where:

 Each agent attempts to maximize its own utility or minimize its loss.

 The action of one agent directly affects the outcome for other agents.

 Strategic uncertainty exists because agents may not fully know the opponent’s future actions
or strategies.

 Decisions must be made by anticipating worst-case scenarios.

Role of Adversarial Search in AI

1. Game Playing

Adversarial search is extensively used in classic and modern games such as Chess, Go, Checkers, Tic-
Tac-Toe, and Poker. These games provide a structured environment where:

 The state of the game is clearly defined.

 Players take turns making moves.


 The effects of actions are governed by strict rules.

AI agents use adversarial search to evaluate future game states and select moves that lead to the
best possible outcome against an optimal opponent.

2. Decision-Making

In competitive decision-making scenarios, adversarial search helps AI systems:

 Evaluate multiple possible strategies.

 Predict opponent behavior.

 Adapt decisions dynamically based on evolving situations.

This makes adversarial search valuable not only in games but also in economic models, security
systems, and strategic planning problems.

Adversarial Search Algorithms

Traditional search algorithms such as Depth-First Search (DFS), Breadth-First Search (BFS), and A*
are well-suited for single-agent problems, where the environment does not actively oppose the
agent’s goal.

However, in zero-sum games and competitive environments, these algorithms are insufficient
because:

 The outcome depends on both players’ actions.

 The opponent deliberately tries to reduce the agent’s success.

To handle such situations, specialized adversarial search algorithms are used, primarily:

 Minimax Algorithm

 Alpha–Beta Pruning

Minimax Algorithm

The Minimax algorithm is the fundamental adversarial search technique used in two-player games.

 One player is called MAX, who tries to maximize the utility value.

 The opponent is called MIN, who tries to minimize the utility value.

The algorithm assumes that:

 Both players play optimally.

 MAX selects the move with the highest minimum payoff, while MIN selects the move with
the lowest maximum loss.

Minimax systematically explores the game tree and ensures optimal decision-making, but it can be
computationally expensive for large games.

Alpha–Beta Pruning

Alpha–Beta pruning is an optimization technique for the minimax algorithm.


 It eliminates branches of the game tree that cannot influence the final decision.

 Two values are maintained:

o Alpha (α): Best value MAX can guarantee.

o Beta (β): Best value MIN can guarantee.

 When α becomes greater than or equal to β, further exploration of that branch is


unnecessary.

Alpha–Beta pruning produces the same result as minimax while significantly reducing the number of
nodes evaluated, making it practical for complex games like chess.

Adversarial Search Implementation Using Connect-4

Connect-4 is a popular example used to demonstrate adversarial search:

 Two players alternate turns.

 Each move affects future possibilities.

 The game can be modelled using a game tree.

 Minimax with Alpha–Beta pruning is commonly applied to compute optimal moves


efficiently.

This implementation highlights how adversarial search works in real-world, turn-based games.

Applications of Adversarial Search Algorithms


Adversarial search algorithms are applied in:

 Board games (Chess, Go, Checkers)

 Video games and simulations

 Autonomous decision systems

 Cybersecurity and intrusion detection

 Economic and negotiation models

 Competitive multi-agent systems

Optimal Decisions in Games


In adversarial environments like competitive games, simply finding a sequence of actions leading to a
goal (as in normal search problems) isn’t sufficient — because the opponent’s moves also influence
the outcome.

Adversarial Search Framework

Games in AI are treated as structured search problems where:

 Every game position is a state.

 Legal moves are actions that transfer from one state to another.
 A game ends at terminal states with a numerical utility value.

A game tree represents all possible sequences of moves:

 The root is the current game state.

 Levels alternate between players:

o MAX — the player trying to maximize utility.

o MIN — the opponent trying to minimize MAX’s utility.

What Makes Decisions “Optimal”?

In a normal search problem, you look for the best sequence of moves to reach a goal. But in an
adversarial (competitive) setting:

 MIN also influences outcomes, so MAX must anticipate MIN’s replies.

 An optimal strategy must specify:

o MAX’s move at the start.

o Then MAX’s move for every possible reply by MIN.

o Then MAX’s move after every possible counter-reply by MIN, and so on.

This ensures that the strategy performs at least as well as any other even when the opponent plays
perfectly.

 The minimax rule chooses the action for MAX that ensures the best worst-case outcome.

Mini-Max Algorithm in Artificial Intelligence


o 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.
o Mini-Max algorithm uses recursion to search through the game-tree.
o 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.
o In this algorithm two players play the game, one is called MAX and other is
called MIN.
o Both the players fight it as the opponent player gets the minimum benefit
while they get the maximum benefit.
o Both Players of the game are opponent of each other, where MAX will select
the maximized value and MIN will select the minimized value.
o The minimax algorithm performs a depth-first search algorithm for the
exploration of the complete game tree.
o The minimax algorithm proceeds all the way down to the terminal node of the
tree, then backtrack the tree as the recursion.

Pseudo-code for MinMax Algorithm:

1. function minimax(node, depth, maximizingPlayer) is


2. if depth ==0 or node is a terminal node then
3. return static evaluation of
node 4.
5. if MaximizingPlayer then // for Maximizer Player
6. maxEva= -infinity
7. for each child of node do
8. eva= minimax(child, depth-1, false)
9. maxEva= max(maxEva,eva) //gives Maximum of the values
10. return maxEva
11.
12. else // for Minimizer player
13. minEva= +infinity
14. for each child of node do
15. eva= minimax(child, depth-1, true)
16. minEva= min(minEva, eva) //gives minimum of the values
17. return minEva

Initial call:

Minimax(node, 3, true)

Working of Min-Max Algorithm:

o 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.
o In this example, there are two players one is called Maximizer and other is
called Minimizer.
o Maximizer will try to get the Maximum possible score, and Minimizer will try
to get the minimum possible score.
o This algorithm applies DFS, so in this game-tree, we have to go all the way
through the leaves to reach the terminal nodes.
o 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 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 max(-1,- -∞) => max(-1,4)= 4


o For Node E max(2, -∞) => max(2, 6)= 6
o For Node F max(-3, -∞) => max(-3,-5) = -3
o For node G 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

That was the complete workflow of the minimax two player game.

Properties of Mini-Max algorithm:

o Complete- Min-Max algorithm is Complete. It will definitely find a solution (if


exist), in the finite search tree.
o Optimal- Min-Max algorithm is optimal if both opponents are playing
optimally.
o 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.
o 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 which we have discussed in the next topic.
Alpha-Beta Pruning
o Alpha-beta pruning is a modified version of the minimax algorithm. It is an
optimization technique for the minimax algorithm.
o As we have seen in the minimax search algorithm that the number of game
states it has to examine are exponential in depth of the tree. Since we cannot
eliminate the exponent, but we can cut it to half. Hence 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.
o 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.
o The two-parameter can be defined as:
a. 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 -∞.
b. 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 +∞.
o 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.

Note: To better understand this topic, kindly study the minimax algorithm.

Condition for Alpha-beta pruning:

The main condition which required for alpha-beta pruning is:

1. α>=β

Key points about alpha-beta pruning:

o The Max player will only update the value of alpha.


o The Min player will only update the value of beta.
o While backtracking the tree, the node values will be passed to upper nodes
instead of values of alpha and beta.

o We will only pass the alpha, beta values to the child nodes.
Pseudo-code for Alpha-beta Pruning:

1. function minimax(node, depth, alpha, beta, maximizingPlayer) is


2. if depth ==0 or node is a terminal node then
3. return static evaluation of
node 4.
5. if MaximizingPlayer then // for Maximizer Player
6. maxEva= -infinity
7. for each child of node do
8. eva= minimax(child, depth-1, alpha, beta, False)
9. maxEva= max(maxEva, eva)
10. alpha= max(alpha, maxEva)
11. if beta<=alpha
12. break
13. return maxEva
14.
15. else // for Minimizer player
16. minEva= +infinity
17. for each child of node do
18. eva= minimax(child, depth-1, alpha, beta, true)
19. minEva= min(minEva, eva)
20. beta= min(beta, eva)
21. if beta<=alpha
22. break
23. return minEva

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.

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:

o 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).
o 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:

o Occur the best move from the shallowest node.


o Order the nodes in the tree such that the best nodes are checked first.
o Use domain knowledge while finding the best move. Ex: for Chess, try order:
captures first, then threats, then forward moves, backward moves.
o We can bookkeep the states, as there is a possibility that states may repeat.
Imperfect Real-Time Decisions
Introduction
In adversarial search and game-playing systems, the objective of an intelligent agent is to
select the best possible move by anticipating future actions of the opponent. Classical
approaches such as the Minimax algorithm assume that the agent has sufficient time and
computational power to explore the entire game tree until terminal states are reached.
Under such assumptions, the agent can guarantee an optimal decision.
In real-world scenarios, however, these assumptions rarely hold true. Many games and
competitive environments demand immediate responses, and the underlying search space
is often extremely large. As a result, AI agents are forced to make decisions before complete
analysis is possible. Such decisions are referred to as imperfect real-time decisions.
An imperfect real-time decision is not necessarily the best possible decision in theory, but it
is the best achievable decision within the available time, memory, and computational
constraints.
Need for Imperfect Real-Time Decision Making
1. Exponential Growth of Game Trees
One of the primary challenges in game-playing AI is the exponential growth of the game
tree.
 The number of nodes grows as

where:
o ( b ) = branching factor
o ( d ) = depth of the tree
For example:
 In Chess, the average branching factor is about 35
 A typical game depth is around 80 moves
This makes complete exploration of the game tree practically impossible.
2. Real-Time Constraints
In many competitive environments:
 The agent must respond within strict time limits
 Delayed decisions can lead to immediate failure
Examples include:
 Chess engines operating under clock constraints
 Online multiplayer games
 Real-time simulations and competitions
Thus, the agent cannot afford to wait for a perfectly computed solution.
3. Limited Resources
Even powerful systems face:
 Memory limitations
 CPU time restrictions
 Energy constraints
As the search depth increases, resource consumption grows rapidly, forcing the AI to
terminate search early.
4. Uncertainty and Dynamic Play
In practice:
 Opponents may behave unpredictably
 Game situations evolve rapidly
 New information may become available after each move
These factors further reduce the feasibility of perfect decision-making.
Nature of Imperfect Decisions
Imperfect real-time decisions exhibit the following properties:
 They are bounded by time
 They are heuristic-driven
 They are context-dependent
 They prioritize responsiveness over optimality
Such decisions aim to be good enough rather than theoretically perfect.
Search Cutoffs and Their Role
Depth Cutoff
A depth cutoff limits the search to a predefined depth ( d ).
When the cutoff is reached:
 The algorithm stops expanding further nodes
 A heuristic evaluation is applied to the current state
This avoids the cost of exploring deeper levels of the game tree.
Time Cutoff
In time-critical systems:
 Search continues only until the time limit expires
 The best move identified so far is selected
Time cutoffs are essential in real-time environments.
Evaluation Functions in Real-Time Decisions
When terminal states are not reached, the agent must estimate the desirability of a game
state.
An evaluation function:
 Assigns a numerical value to non-terminal states
 Approximates the true utility of a state
 Is designed using domain knowledge
Characteristics of a Good Evaluation Function
1. Computationally efficient
2. Correlated with actual winning chances
3. Sensitive to strategic advantages
Example: Chess Evaluation Function
A chess evaluation function may consider:
 Material balance
 King safety
 Piece mobility
 Pawn structure
 Control of key squares
Each factor is weighted and combined to produce a final score.
Iterative Deepening in Real-Time Decision Making
Iterative deepening allows the agent to:
 Perform multiple depth-limited searches
 Gradually increase search depth
 Always maintain a valid decision
If time runs out during deeper search, the agent still has:
 A reliable move from a previous iteration
This makes iterative deepening especially suitable for real-time systems.
Alpha–Beta Pruning for Efficiency
Alpha–beta pruning significantly reduces the number of nodes evaluated by:
 Discarding branches that cannot influence the final decision
 Allowing deeper exploration within the same time frame
This is critical for improving decision quality under real-time constraints.

Real-World Examples
1. Chess and Board Game Engines
Modern chess engines:
 Combine depth-limited minimax
 Use advanced evaluation functions
 Employ alpha–beta pruning and iterative deepening
Despite imperfect decisions, they consistently outperform human players.
2. Real-Time Strategy (RTS) Games
In RTS games:
 Decisions must be made continuously
 Long-term planning is combined with immediate reactions
 Perfect optimization is impossible
Thus, fast heuristic decisions are preferred.
3. Autonomous and Interactive Agents
Robots and AI agents:
 Must act immediately in uncertain environments
 Rely on approximate reasoning
 Adjust decisions dynamically
Advantages of Imperfect Real-Time Decisions
 Enables real-time interaction
 Scales to complex environments
 Balances speed and intelligence
 Makes AI systems practical
Limitations
 No guarantee of optimal outcomes
 Strong dependence on heuristic quality
 Risk of strategic errors in complex situations

Constraint Satisfaction Problems (CSP) in Artificial


Intelligence

A Constraint Satisfaction Problem is a mathematical problem where the solution must meet
a number of constraints. In CSP the objective is to assign values to variables such that all the
constraints are satisfied. Many AI applications use CSPs to solve decision-making problems
that involve managing or arranging resources under strict guidelines. Common applications
of CSPs include:
 Scheduling: It assigns resources like employees or equipment while respecting time
and availability constraints.
 Planning: Organize tasks with specific deadlines or sequences.
 Resource Allocation: Distributing resources efficiently without overuse
Components of Constraint Satisfaction Problems
CSPs are composed of three key elements:
1. Variables: These are the things we need to find values for. Each variable represents
something that needs to be decided. For example, in a Sudoku puzzle each empty cell is a
variable that needs a number. Variables can be of different types like yes/no choices
(Boolean), whole numbers (integers) or categories like colors or names.
2. Domains: This is the set of possible values that a variable can have. The domain tells us
what values we can choose for each variable. In Sudoku the domain for each cell is the
numbers 1 to 9 because each cell must contain one of these numbers. Some domains are
small and limited while others can be very large or even infinite.
3. Constraints: These are the rules that restrict how variables can be assigned values.
Constraints define which combinations of values are allowed. There are different types of
constraints:
 Unary constraints apply to a single variable like "this cell cannot be 5".
 Binary constraints involve two variables like "these two cells cannot have the same
number".
 Higher-order constraints involve three or more variables like "each row in Sudoku
must have all numbers from 1 to 9 without repetition".
Types of Constraint Satisfaction Problems
CSPs can be classified into different types based on their constraints and problem
characteristics:
1. Binary CSPs: In these problems each constraint involves only two variables. Like in a
scheduling problem the constraint could specify that task A must be completed
before task B.
2. Non-Binary CSPs: These problems have constraints that involve more than two
variables. For instance in a seating arrangement problem a constraint could state that
three people cannot sit next to each other.
3. Hard and Soft Constraints: Hard constraints must be strictly satisfied while soft
constraints can be violated but at a certain cost. This is often used in real-world
applications where not all constraints are equally important.
Representation of Constraint Satisfaction Problems (CSP)
In CSP it involves the interaction of variables, domains and constraints. Below is a structured
representation of how CSP is formulated:
1. Finite Set of Variables (V1,V2,...,Vn)(V1,V2,...,Vn): The problem consists of a set of
variables each of which needs to be assigned a value that satisfies the given
constraints.
2. Non-Empty Domain for Each Variable (D1,D2,...,Dn)(D1,D2,...,Dn): Each variable has
a domain a set of possible values that it can take. For example, in a Sudoku puzzle the
domain could be the numbers 1 to 9 for each cell.
3. Finite Set of Constraints (C1,C2,...,Cm)(C1,C2,...,Cm): Constraints restrict the possible
values that variables can take. Each constraint defines a rule or relationship between
variables.
4. Constraint Representation: Each constraint CiCi is represented as a pair of (scope,
relation) where:
Scope: The set of variables involved in the constraint.
Relation: A list of valid combinations of variable values that satisfy the
constraint.
Example: Let’s say you have two variables V1V1 and V2V2. A possible constraint could
be V1≠V2V1=V2, which means the values assigned to these variables must not be equal.
There:
 Scope: The variables V1V1 and V2V2.
 Relation: A list of valid value combinations where V1V1 is not equal to V2V2.
Some relations might include explicit combinations while others may rely on abstract
relations that are tested for validity dynamically.
Solving Constraint Satisfaction Problems Efficiently
CSP use various algorithms to explore and optimize the search space ensuring that solutions
meet the specified constraints. Here’s a breakdown of the most commonly used CSP
algorithms:
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 then 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 and eliminate 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.
3. Constraint Propagation Algorithms
Constraint propagation algorithms further reduce the search space by enforcing local
consistency across all variables.
How it works:
 Constraints are propagated between related variables.
 Inconsistent values are eliminated from variable domains by using information
gained from other variables.
 These algorithms filter the search space by making inferences and by remove values
that would led to conflicts.
Constraint propagation is used along with other CSP methods like backtracking to make the
search faster.
Solving Sudoku with Constraint Satisfaction Problem (CSP) Algorithms
Step 1: Define the Problem (Sudoku Puzzle Setup)
The first step is to define the Sudoku puzzle as a 9x9 grid where 0 represents an empty cell.
We also define a function print_sudoku to display the puzzle in a human readable format.
puzzle = [[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]]

def print_sudoku(puzzle):
for i in range(9):
if i % 3 == 0 and i != 0:
print("- - - - - - - - - - - ")
for j in range(9):
if j % 3 == 0 and j != 0:
print(" | ", end="")
print(puzzle[i][j], end=" ")
print()

print("Initial Sudoku Puzzle:\n")


print_sudoku(puzzle)
Output:

Initial Sudoku Puzzle


Step 2: Create the CSP Solver Class
We define a class CSP to handle the logic of the CSP algorithm. This includes functions for
selecting variables, assigning values and checking consistency between variables and
constraints.
class CSP:
def __init__(self, variables, domains, constraints):
[Link] = variables
[Link] = domains
[Link] = constraints
[Link] = None
def solve(self):
assignment = {}
[Link] = [Link](assignment)
return [Link]

def backtrack(self, assignment):


if len(assignment) == len([Link]):
return assignment

var = self.select_unassigned_variable(assignment)
for value in self.order_domain_values(var, assignment):
if self.is_consistent(var, value, assignment):
assignment[var] = value
result = [Link](assignment)
if result is not None:
return result
del assignment[var]
return None

def select_unassigned_variable(self, assignment):


unassigned_vars = [var for var in [Link] if var not in assignment]
return min(unassigned_vars, key=lambda var: len([Link][var]))

def order_domain_values(self, var, assignment):


return [Link][var]

def is_consistent(self, var, value, assignment):


for constraint_var in [Link][var]:
if constraint_var in assignment and assignment[constraint_var] == value:
return False
return True
Step 3: Implement Helper Functions for Backtracking
We add helper methods for selecting unassigned variables, ordering domain values and
checking consistency with constraints. These methods ensure that the backtracking
algorithm is efficient.
def select_unassigned_variable(self, assignment):
unassigned_vars = [var for var in [Link] if var not in assignment]
return min(unassigned_vars, key=lambda var: len([Link][var]))
def order_domain_values(self, var, assignment):
return [Link][var]

def is_consistent(self, var, value, assignment):


for constraint_var in [Link][var]:
if constraint_var in assignment and assignment[constraint_var] == value:
return False
return True
Step 4: Define Variables, Domains and Constraints
Next we define the set of variables, their possible domains and the constraints for the
Sudoku puzzle. Variables represent the cells and domains represent possible values.
Constraints should ensure that each number only appears once per row, column and 3x3
subgrid.
variables = [(i, j) for i in range(9) for j in range(9)]

domains = {
var: set(range(1, 10)) if puzzle[var[0]][var[1]] == 0 else {puzzle[var[0]][var[1]]}
for var in variables
}

constraints = {}

def add_constraint(var):
constraints[var] = []
for i in range(9):
if i != var[0]:
constraints[var].append((i, var[1]))
if i != var[1]:
constraints[var].append((var[0], i))
sub_i, sub_j = var[0] // 3, var[1] // 3
for i in range(sub_i * 3, (sub_i + 1) * 3):
for j in range(sub_j * 3, (sub_j + 1) * 3):
if (i, j) != var:
constraints[var].append((i, j))

for var in variables:


add_constraint(var)
Step 5: Solve the Sudoku Puzzle Using CSP
We create an instance of CSP class and call the solve method to find the solution to the
Sudoku puzzle. The final puzzle with the solution is then printed.
csp = CSP(variables, domains, constraints)
sol = [Link]()

solution = [[0 for _ in range(9)] for _ in range(9)]


for (i, j), val in [Link]():
solution[i][j] = val

print("\n******* Solution *******\n")


print_sudoku(solution)
Output:

Output

Applications of CSPs in AI
CSPs are used in many fields because they are flexible and can solve real-world problems
efficiently. Here are some common applications:
1. Scheduling: They help in planning things like employee shifts, flight schedules and
university timetables. The goal is to assign tasks while following rules like time limits,
availability and priorities.
2. Puzzle Solving: Many logic puzzles such as Sudoku, crosswords and the N-Queens
problem can be solved using CSPs. The constraints make sure that the puzzle rules
are followed.
3. Configuration Problems: They help in selecting the right components for a product
or system. For example when building a computer it ensure that all selected parts are
compatible with each other.
4. Robotics and Planning: Robots use CSPs to plan their movements, avoid obstacles
and complete task efficiently. For example a robot navigating a warehouse must
avoid crashes and minimize energy use.
5. Natural Language Processing (NLP): In NLP they help with tasks like breaking
sentences into correct grammatical structures based on language rules.
Benefits of CSPs in AI
1. Standardized Representation: They provide a clear and structured way to define
problems using variables, possible values and rules.
2. Efficiency: Smart search techniques like backtracking and forward-checking help to
reduce the time needed to find solutions.
3. Flexibility: The same CSP methods can be used in different areas without needing
expert knowledge in each field.
Challenges in Solving CSPs
1. Scalability: When there are too many variables and rules the problem becomes very
complex and finding a solution can take too long.
2. Changing Problems (Dynamic CSPs): In real life conditions and constraints can
change over time requiring the CSP solution to be updated.
3. Impossible or Overly Strict Problems: Sometimes a CSP may not have a solution
because the rules are too strict. In such cases adjustments or compromises may be
needed to find an acceptable solution.

Propositional Logic

Knowledge-Based Agents
KB = knowledge base
A set of sentences or facts
e.g., a set of statements in a logic language
Inference
Deriving new sentences from old
e.g., using a set of logical statements to infer new ones
A simple model for reasoning
Agent is told or perceives new evidence
E.g., A is true
Agent then infers new facts to add to the KB
E.g., KB = { A -> (B OR C) }, then given A and not C we can infer that B is true
B is now added to the KB even though it was not explicitly asserted, i.e., the agent inferred B

In Artificial Intelligence, intelligent behavior requires more than just reacting to immediate
inputs. Many AI systems must store knowledge, reason logically, and make decisions based
on past and present information. Such systems are known as Knowledge-Based Agents.
A Knowledge-Based Agent is an agent that maintains an internal knowledge base (KB)
containing information about the world and uses logical inference to derive new knowledge
and decide appropriate actions.
Knowledge Base (KB)
Definition
A Knowledge Base (KB) is a structured collection of facts, rules, and assertions about the
environment. These are represented in a formal language, usually a logic-based language.
Formally:
KB = a set of sentences expressed in a logical representation language
These sentences describe:
 What is known to be true about the world
 Relationships between objects
 Conditional rules that describe cause and effect
Nature of Knowledge in KB
The knowledge stored in a KB may include:
 Facts (e.g., “A is true”)
 Rules (e.g., “If A is true, then B or C is true”)
 Constraints about the environment
This explicit storage of knowledge allows the agent to reason instead of merely reacting.
Inference in Knowledge-Based Agents
What is Inference?
Inference is the process of deriving new facts from existing knowledge using logical
reasoning.
In simple terms:
Inference enables an agent to reach conclusions that are not directly stated but logically
follow from known facts.
Inference is performed using predefined logical inference rules, such as:
 Modus Ponens
 Resolution
 Forward and backward chaining
Reasoning Process of a Knowledge-Based Agent
The reasoning process of a KB agent follows a structured cycle:
1. Perception of new information
The agent receives new evidence from the environment through sensors.
2. Updating the Knowledge Base
The perceived information is converted into logical statements and added to the KB.
3. Inference
The agent applies inference rules to derive new facts from existing knowledge.
4. Decision Making
Based on inferred knowledge, the agent decides the best possible action.
5. Action Execution
The agent performs the selected action using actuators.
This cycle continues throughout the agent’s operation.
Example of Knowledge-Based Reasoning
Consider the following rule stored in the knowledge base:
KB = { A → (B OR C) }
Now suppose the agent perceives:
A is true
C is false
Step-by-Step Reasoning
 The rule states that if A is true, then either B or C must be true
 The agent already knows that C is false
 Therefore, by logical reasoning, B must be true
Important Insight
 The fact B was not explicitly provided
 The agent inferred B logically
 The inferred fact B is added to the knowledge base
This demonstrates how a Knowledge-Based Agent extends its knowledge through
reasoning.
Significance of Knowledge-Based Agents
Knowledge-Based Agents are important because they:
 Can reason about complex and dynamic environments
 Can infer hidden or indirect information
 Can update knowledge as new evidence appears
 Can justify their actions logically
Unlike simple agents, KB agents do not rely only on current perception.
Key Components of a Knowledge-Based Agent
1. Knowledge Base
Stores facts, rules, and assertions.
2. Inference Mechanism
Derives new knowledge from existing information.
3. Perception Module
Accepts new evidence from the environment.
4. Action Module
Executes actions based on logical conclusions.
Features of Knowledge-Based Agents
 Explicit representation of knowledge
 Logical reasoning capability
 Ability to infer new information
 Goal-directed behavior
 Adaptability to changing environments
Advantages
 Intelligent and flexible decision-making
 Easy to update or modify knowledge
 Suitable for complex problem domains
 Decisions can be explained logically

The Wumpus World


The Wumpus World is a classic example of a knowledge-based agent in AI. It involves
reasoning, knowledge representation and planning. In this agent uses its knowledge of the
world to make decisions and navigate safely in the given environment. In this article, we will
learn more about it.
Understanding Wumpus World Problem
Wumpus World is a 4x4 grid consisting of 16 rooms. Agent starts at Room[1,1] facing right
and its goal is to retrieve treasure while avoiding hazards such as pits and the Wumpus.
Agent must navigate through grid using its limited sensory input to make decisions that will
keep it safe and allow it to successfully collect the treasure and exit the cave.

The Cave
Key Elements:
 Pits: If the agent steps into a pit it falls and dies. A breeze in adjacent rooms suggests
nearby pits.
 Wumpus: A creature that kills agent if it enters its room. Rooms next to the Wumpus
have a stench. Agent can use an arrow to kill the Wumpus.
 Treasure: Agent’s main objective is to collect the treasure (gold) which is located in
one room.
 Breeze: Indicates a pit is nearby.
 Stench: Indicates the Wumpus is nearby.
Agent must navigate carefully avoiding dangers to collect treasure and exit safely.
PEAS Description
PEAS stands for Performance Measures, Environment, Actuators and Sensors which
describe agent’s capabilities and environment.
1. Performance measures: Rewards or Punishments
 Agent gets gold and return back safe = +1000 points
 Agent dies (pit or Wumpus)= -1000 points
 Each move of the agent = -1 point
 Agent uses the arrow = -10 points
2. Environment: A setting where everything will take place.
 A cave with 16(4x4) rooms.
 Rooms adjacent (not diagonally) to the Wumpus are stinking.
 Rooms adjacent (not diagonally) to the pit are breezy.
 Room with gold glitters.
 Agent's initial position - Room[1, 1] and facing right side.
 Location of Wumpus, gold and 3 pits can be anywhere except in Room[1, 1].
3. Actuators: Devices that allow agent to perform following actions in the environment.
 Move forward: Move to next room.
 Turn right/left: Rotate agent 90 degrees.
 Shoot: Kill Wumpus with arrow.
 Grab: Take treasure.
 Release: Drop treasure
4. Sensors: Devices help the agent in sensing following from the environment.
 Breeze: Detected near a pit.
 Stench: Detected near the Wumpus.
 Glitter: Detected when treasure is in the room.
 Scream: Triggered when Wumpus is killed.
 Bump: Occurs when hitting a wall.
How the Agent Operates with PEAS
1. Perception: Agent uses sensory inputs (breeze, stench, glitter) to detect its
surroundings and understand the environment.
2. Inference: Agent applies logical reasoning to find location of hazards. For example if
it detects a breeze, it warns that a pit is nearby or if there’s a stench it suspects the
Wumpus is in an adjacent room.
3. Planning: Based on its deductions agent plans its next move avoiding risky areas like
rooms with suspected pits or the Wumpus.
4. Action: Agent performs planned action such as moving to a new room, shooting
arrow at the Wumpus or taking the treasure.
This process repeats till the agent finds the cave using its sensory inputs, reasoning and
planning to achieve its goal safely.
By using PEAS framework agent’s interactions with its environment are clearly defined,
providing a structured approach to modeling intelligent behavior.
Propositional Logic
Propositional logic works with statements called propositions that can be true or
false. These propositions represent facts or conditions about a situation. We use symbols to
represent the propositions and logical operations to connect those propositions. It help us
understand how different facts are related to each other in complex statements or problem.
Proposition operators like conjunction (∧), disjunction (∨), negation (¬), implication( →) and
biconditional (↔) helps combine various proposition to represent logical relations.
Example of Propositions Logic
 P: "The sky is blue." (This statement can be either true or false.)
 Q: "It is raining right now." (This can also be true or false.)
 R: "The ground is wet." (This is either true or false.)
These can be combined using logical operations to create more complex statements. For
example:
P ∧ Q: "The sky is blue AND it is raining." (This is true only if both P and Q are true.)
 P ∨ Q: "The sky is blue OR it is raining." (This is true if at least one of P or Q is true.)

 ¬P: "It is NOT true that the sky is blue." (This is true if P is false means the sky is not
blue.)
Logical Equivalence
Two statements are logically equivalent if they always have the same truth values in every
possible situation. For example:
 The statement "S → T" (if S then T) is equivalent to "¬S ∨ T" (not S or T). This means
"if S is true, then T must be true" is the same as "either S is false or T is true."
 The biconditional "P ↔ Q" (P if and only if Q) is equivalent to "(P → Q) ∧ (Q → P)" (P
implies Q and Q implies P).
These equivalences show that different logical expressions can have the same meaning. You
can verify them using truth tables or by simplifying the statements with logical rules.
Basic Concepts of Propositional Logic
1. Propositions
A proposition is a statement that can either be true or false. It does not matter how
complicated statement is if it can be classified as true or false then it is a proposition. For
example:
 "The sky is blue." (True)
 "It is raining." (False)
2. Logical Connectives
Logical connectives are used to combine simple propositions into more complex ones. The
main connectives are:

Example: "It is sunny ∧ it is warm" is true only if both "It is sunny" and "It is warm"
 AND (∧): This operation is true if both propositions are true.

are true.

Example: "It is sunny ∨ it is raining" is true if either "It is sunny" or "It is raining" is
 OR (∨): This operation is true if at least one of the propositions is true.

true.
 NOT (¬): This operation reverses the truth value of a proposition.
Example: "¬It is raining" is true if "It is raining" is false.
 IMPLIES (→): This operation is true if the first proposition leads to the second.
Example: "If it rains then the ground is wet" (It rains → The ground is wet) is true
unless it rains and the ground is not wet.
 IF AND ONLY IF (↔): This operation is true if both propositions are either true or
false together.
Example: "It is raining ↔ The ground is wet" is true if both "It is raining" and "The
ground is wet" are either true or both false.
3. Truth Tables
They are used to find the truth value of complex propositions by checking all possible
combinations of truth values for their components. They systematically list every possible
combinations which helps in making it easy to find how different logical operators affect the
overall outcome. This approach ensures that no combination is given extra importance
which provides a clear and complete picture of the logic at work.
4. Tautologies, Contradictions and Contingencies
 Tautology: A proposition that is always true no matter the truth values of the

Example: "P ∨ ¬P" (This is always true because either P is true or P is false).
individual components.

Example: "P ∧ ¬P" (This is always false because P can't be both true and false at the
 Contradiction: A proposition that is always false.

same time).
 Contingency: A proposition that can be true or false depending on the truth values of

Example: "P ∧ Q" (This is true only if both P and Q are true).
its components.

Properties of Operators
Logical operators in propositional logic have various important properties that help to
simplify and analyze complex statements:
1. Commutativity: Order of propositions doesn’t matter when using AND (∧) or OR (∨).
 P∧Q≡Q∧P
 P∨Q≡Q∨P
2. Associativity: Grouping of propositions doesn’t matter when using multiple ANDs or ORs.
 (P ∧ Q) ∧ R ≡ P ∧ (Q ∧ R)
 (P ∨ Q) ∨ R ≡ P ∨ (Q ∨ R)
3. Distributivity: AND (∧) and OR (∨) can distribute over each other which is similar to
multiplication and addition in math.
 P ∧ (Q ∨ R) ≡ (P ∧ Q) ∨ (P ∧ R)
 P ∨ (Q ∧ R) ≡ (P ∨ Q) ∧ (P ∨ R)
4. Identity: A proposition combined with "True" or "False" behaves predictably.
 P ∧ true ≡ P
 P ∨ false ≡ P
5. Domination: When combined with "True" or "False" some outcomes are always fixed.
P ∨ true ≡ true
 P ∧ false ≡ false

6. Double Negation: Negating a proposition twice cancels out the negation.


¬ (¬P) ≡ P
7. Idempotence: Repeating same proposition with AND or OR doesn’t change its value.
 P∧P≡P
 P∨P≡P
Applications of Propositional Logic in AI
1. Knowledge Representation: Propositional logic is used to represent knowledge in a
structured way. It allows AI systems to store and manipulate facts about the world. For
example in expert systems knowledge is encoded as a set of propositions and logical rules.
2. Automated Reasoning: AI uses logical rules such as Modus Ponens and Modus Tollens
which help systems to find new conclusions from existing fact and to "think" logically. For
example:
 Modus Ponens: If "P → Q" and "P" are true then "Q" must be true.
 Modus Tollens: If "P → Q" and "¬Q" are true then "¬P" must be true.
3. Problem Solving and Planning: It allows AI planners to solve problems and to create
action sequences by representing goals. For example the STRIPS planning system helps
propositional logic to represent preconditions and effects of actions.
4. Decision Making: It helps to evaluate various options and find the best course of action.
Logical rules can encode decision criteria and truth tables can be used to assess the
outcomes of different choices.
5. Natural Language Processing (NLP): It is applied in NLP for tasks like semantic parsing
where natural language sentences are converted into logical representations. This helps in
understanding and reasoning about the meaning of sentences.
Limitations of Propositional Logic
Despite of having many advantages it has various limitations:
1. Lack of Expressiveness: It cannot handle relationships like "All humans are mortal."
2. Scalability: Truth tables expands exponentially with the number of propositions.
3. Limited Inference: It only handles binary truth values (true/false) and cannot
represent probabilities.

“for all” (denoted by ∀) or “there exists” (denoted by ∃).


4. No Quantifiers: Unlike predicate logic it does not allow the use of quantifiers such as

5. Inability to Handle Uncertainty: It cannot represent probabilities or partial truths


which helps in making it unsuitable for uncertain situations.
6. Lack of Context Awareness: It ignores meaning or context of statements which limits
its ability to interpret nuanced scenarios.
Propositional logic is a simple but efficient way to teach machines how to think and make
decisions based on facts and knowledge base.
Propositional Theorem Proving

Proofs and Inferences in Proving Propositional Theorem


This article discusses how to use inference rules to create proof—a series of conclusions that
leads to the desired result. The most well-known rule is known as Modus Ponens (Latin for
affirming mode) and is expressed as

Inferences in Proving Propositional Theorem

type are supplied. If ( WumpusAhead ∧ WumpusAlive) ⇒ Shoot


The notation signifies that the sentence may be deduced whenever any sentences of the

and (WumpusAhead ∧ WumpusAlive) are both supplied, Shoot may be deduced.


And-Elimination is another helpful inference rule, which states that any of the conjuncts can
be inferred from conjunction:

WumpusAlive can be deduced from (WumpusAhead ∧ WumpusAlive), for example. One


may readily demonstrate that Modus Ponens and And-Elimination are sound once and for all
by evaluating the potential truth values of α and β. These principles may then be applied to
each situation in which they apply, resulting in good conclusions without the necessity of
enumerating models.

The equations above show all the logical equivalences that can be utilized as inference rules.
The equivalence for biconditional elimination, for example, produces the two inference
rules.

Some inference rules do not function in both directions in the same way. We cannot, for
example, run Modus Ponens in the reverse direction to get α⇒β and α from β.
Let's look at how these equivalences and inference rules may be applied in the wumpus
environment. We begin with the knowledge base including R1 through R5 and demonstrate
how to establish ¬P1,2 i.e. that [1,2] does not include any pits. To generate R6, we first apply
biconditional elimination to R2:

After that, we apply And-Elimination on R6 to get

For contrapositives, logical equivalence yields

With R8 and the percept R4 (i.e., ¬B1,1 ), we can now apply Modus Ponens to get

Finally, we use De Morgan's rule to arrive at the following conclusion:

That is to say,neither [1,2] nor [2,1] have a pit in them.


We found this proof by hand, but any of the search techniques may be used to produce a
proof-like sequence of steps. All we have to do now is define a proof problem:
 Initial State: the starting point for knowledge.
 Actions: the set of actions is made up of all the inference rules that have been
applied to all the sentences that fit the inference rule's upper half.
 Consequences: Adding the statement to the bottom part of the inference rule is the
result of an action.
 Objective: The objective is to arrive at a state that contains the phrase we are
attempting to verify.
As a result, looking for proofs is a viable alternative to counting models.

Horn Clauses and Definite Clauses


In Artificial Intelligence, logical reasoning must balance expressive power with
computational efficiency. While full propositional and first-order logic are expressive,
inference in them can be computationally expensive. To overcome this, AI systems often
restrict knowledge representation to special forms of logic sentences that allow efficient
inference.
Horn clauses and definite clauses are such restricted logical forms. They are widely used in
knowledge-based agents, rule-based systems, logic programming, and expert systems
because they support sound, complete, and efficient reasoning mechanisms.
Clausal Form in Logic
Before understanding Horn clauses, it is important to recall that:
 A clause is a disjunction of literals
 A literal is either:
o A propositional symbol (positive literal), or
o The negation of a propositional symbol (negative literal)
Example:
¬P ∨ ¬Q ∨ R
This clause contains:
 Two negative literals (¬P, ¬Q)
 One positive literal (R)
Horn Clauses
A Horn clause is a clause that contains at most one positive literal.
Formally:
A clause is a Horn clause if the number of positive literals ≤ 1
This restriction may appear small, but it dramatically reduces the complexity of inference.
General Form of a Horn Clause
¬P₁ ∨ ¬P₂ ∨ ... ∨ ¬Pₙ ∨ Q
Where:
 (P₁, P₂, ..., Pₙ) are negative literals
 (Q) is the single positive literal (optional)
Implication Representation
Horn clauses are usually written as implications, which are easier to interpret:
(P₁ ∧ P₂ ∧ ... ∧ Pₙ) → Q
Meaning:
If all conditions (P₁, P₂, ..., Pₙ) are true, then Q must be true.
Types of Horn Clauses
Horn clauses can be classified into three important categories:
1. Definite Clauses
 Contain exactly one positive literal
 Represent rules or facts
 Most commonly used in AI systems
Example:
Bird ∧ CanFly → Flies
2. Facts
 A special case of definite clauses
 Contain only one positive literal
 No conditions
Example:
Human(Socrates)
Logical form:
SocratesIsHuman
3. Goal (or Query) Clauses
 Contain no positive literals
 Used to ask questions or express goals
Example:
¬Mortal(Socrates)
This asks whether Mortal(Socrates) can be proven false.
Definite Clauses
A definite clause is a Horn clause that contains:
 Exactly one positive literal
 Zero or more negative literals
Thus:
Every definite clause is a Horn clause, but not every Horn clause is a definite clause.
Structure of a Definite Clause
P₁ ∧ P₂ ∧ ... ∧ Pₙ → Q
Where:
 Body (antecedent): (P₁, P₂, ..., Pₙ)
 Head (consequent): (Q)
Interpretation
A definite clause represents a logical rule:
Q is true whenever all P₁, P₂, ..., Pₙ are true.
This structure makes definite clauses ideal for rule-based reasoning.

Examples of Definite Clauses


1. Single-condition rule
Human(x) → Mortal(x)
2. Multiple-condition rule
HasKey ∧ DoorUnlocked → CanEnter
3. Fact
HaveArrow
All the above are valid definite clauses.
Inference with Horn and Definite Clauses
One of the greatest advantages of Horn and definite clauses is that they allow efficient
inference algorithms.
Computational Properties
Horn clauses have highly desirable computational properties:
 Sound: All inferred conclusions are correct
 Complete: All valid conclusions can be derived
 Efficient: Entailment can be decided in linear time
This makes Horn clauses far more practical than general propositional logic for large
knowledge bases.
Applications
 Logic programming (Prolog)
 Knowledge-based agents
 Expert systems
 Diagnostic and planning systems
 Intelligent tutoring systems

Forward Chaining and Backward Chaining inference in Rule-Based


Systems
Rule-based systems are a cornerstone of artificial intelligence, enabling machines to make
decisions based on a set of predefined rules. These systems utilize logical inferences to
derive conclusions from given data. Two primary methods of inference in rule-based systems
are forward chaining and backward chaining.
What is Forward Chaining?
Forward chaining is a data-driven inference technique. It starts with the available data and
applies rules to infer new data until a goal is reached. This method is commonly used in
situations where the initial data set is extensive, and the goal is to derive conclusions from it.
How Forward Chaining Works
1. Start with Known Facts: The inference engine begins with the known facts in the
knowledge base.
2. Apply Rules: It looks for rules whose conditions are satisfied by the known facts.
3. Infer New Facts: When a rule is applied, new facts are inferred and added to the
knowledge base.
4. Repeat: This process is repeated until no more rules can be applied or a specified
goal is achieved.
Example of Forward Chaining
Consider a medical diagnosis system where rules are used to diagnose diseases based on
symptoms:
 Fact: The patient has a fever.
 Rule: If a patient has a fever and a rash, they might have measles.
Starting with the known fact (fever), the system checks for other symptoms (rash). If the
patient also has a rash, the system infers the possibility of measles.
Advantages of Forward Chaining
1. Simplicity: Forward chaining is straightforward and easy to implement.
2. Automatic Data Processing: It processes data as it arrives, making it suitable for
dynamic environments where new data continuously becomes available.
3. Comprehensive: It explores all possible inferences, ensuring that all relevant
conclusions are reached.
4. Efficiency in Certain Scenarios: It can be efficient when all possible inferences need
to be made from a set of data.
Disadvantages of Forward Chaining
1. Inefficiency in Goal-Oriented Tasks: It can be inefficient if only a specific goal needs
to be achieved, as it may generate many irrelevant inferences.
2. Memory Intensive: It can consume significant memory, storing a large number of
intermediate facts.
3. Complexity with Large Rule Sets: As the number of rules increases, the system may
become slow due to the need to check many conditions.
What is Backward Chaining?
Backward chaining is a goal-driven inference technique. It starts with the goal and works
backward to determine which facts must be true to achieve that goal. This method is ideal
for situations where the goal is clearly defined, and the path to reach it needs to be
established.
How Backward Chaining Works
1. Start with a Goal: The inference engine begins with the goal or hypothesis it wants
to prove.
2. Identify Rules: It looks for rules that could conclude the goal.
3. Check Conditions: For each rule, it checks if the conditions are met, which may
involve proving additional sub-goals.
4. Recursive Process: This process is recursive, working backward through the rule set
until the initial facts are reached or the goal is deemed unattainable.
Example of Backward Chaining
In a troubleshooting system for network issues:
 Goal: Determine why the network is down.
 Rule: If the router is malfunctioning, the network will be down.
The system starts with the goal (network down) and works backward to check if the router is
malfunctioning, verifying the necessary conditions to confirm the hypothesis.
Advantages of Backward Chaining
1. Goal-Oriented: It is efficient for goal-specific tasks as it only generates the facts
needed to achieve the goal.
2. Resource Efficient: It typically requires less memory, as it focuses on specific goals
rather than exploring all possible inferences.
3. Interactive: It is well-suited for interactive applications where the system needs to
answer specific queries or solve particular problems.
4. Suitable for Diagnostic Systems: It is particularly effective in diagnostic systems
where the goal is to determine the cause of a problem based on symptoms.
Disadvantages of Backward Chaining
1. Complex Implementation: It can be more complex to implement, requiring
sophisticated strategies to manage the recursive nature of the inference process.
2. Requires Known Goals: It requires predefined goals, which may not always be
feasible in dynamic environments where the goals are not known in advance.
3. Inefficiency with Multiple Goals: If multiple goals need to be achieved, backward
chaining may need to be repeated for each goal, potentially leading to inefficiencies.
4. Difficulty with Large Rule Sets: As the number of rules increases, managing the
backward chaining process can become increasingly complex.
Comparison of Forward and Backward Chaining

Feature Forward Chaining Backward Chaining

Approach Data-driven Goal-driven

Starting Point Known facts Specific goals

Efficient for exploring all possible Efficient for achieving specific


Efficiency inferences goals

Can be memory intensive due to Typically requires less


Memory Usage storing intermediate facts memory

Implementation More complex to implement


Simple to implement
Complexity due to recursive nature

Suitable for dynamic Suitable for diagnostic


environments with continuously systems and interactive
Suitability arriving data applications

Handling Large Rule May become slow with large rule Can be complex to manage
Sets sets with large rule sets
Propositional Logic based Agent
In this article, we will use our understanding to make wumpus world agents that use propositional
logic. The first stage is to enable the agent to deduce the state of the world from its percept history
to the greatest extent possible. This necessitates the creation of a thorough logical model of the
consequences of actions. We also demonstrate how the agent may keep track of the world without
having to return to the percept history for each inference. Finally, we demonstrate how the agent
may develop plans that are guaranteed to meet its objectives using logical inference.

Wumpus World's Current State

A logical agent works by deducing what to do given a knowledge base of words about the world.
Axioms are the general information about how the universe works combine with percept sentences
gleaned from the agent's experience in a specific reality to form the knowledge base.

Understanding Axioms

We will start with the immutable aspects of the Wumpus world and move on to the mutable aspects
later. For the time being, we will need the following symbols for each [x,y] coordinate:

 If there is a pit in [x,y], Px,y is true.

 If there is a Wumpus in [x,y], whether dead or living, Wx,y is true.

 If the agent perceives a breeze in [x,y], Bx,y is true.

 If the agent detects a smell in [x,y], Sx,y is true.

The sentences we write will be adequate to infer P1,2 (there is no pit in [1,2] labelled). Each sentence
is labeled Ri so that we can refer to it:

 In [1,1] , there is no pit: R1: ¬P1,1

 A square is breezy if and only if one of its neighbours has a pit. This must be stated for each
square; for the time being, we will only add the relevant squares:

 In all Wumpus universes, the previous sentences are correct. The breeze percepts for the
first two squares visited in the specific environment the agent is in are now included.

R4:¬B1,1

R5:B2,1.

The agent is aware that there are no pits (¬P1,1) or Wumpus (¬W1,1) in the starting square. It also
understands that a square is windy if and only if a surrounding square has a pit, and that a square is
stinky if and only if a neighbouring square has a Wumpus. As a result, we include a huge number of
sentences of the following type:

B1,1⇔(P1,2∨P2,1)

S1,1⇔(W1,2∨W2,1)

The agent is also aware that there is only one wumpus on the planet. This is split into two sections.
First and foremost, we must state that there is at least one wumpus:

W1,1∨W1,2∨⋯∨W4,3∨W4,4

Then we must conclude that there is only one wumpus. We add a statement to each pair of places
stating that at least one of them must be wumpus-free:

¬W1,1∨¬W1,2

¬W1,1∨¬W1,3

¬W4,3∨¬W4,4

So far, the agent’s reasoning works correctly. Now consider how the agent handles percepts over
time. Suppose the agent perceives a stench at the current moment. We should add this information
to the knowledge base. However, if at the previous time step there was no stench, then ¬Stench
would already exist in the KB, leading to a contradiction.

This issue is resolved by recognizing that percepts describe only the current time step. Therefore,
instead of adding a general proposition like Stench, we associate it with time. For example, at time
step 4, we add Stench₄ rather than Stench. This avoids any conflict with Stench₃. The same idea
applies to other percepts such as Breeze, Bump, Glitter, and Scream.

Fluents and Atemporal Symbols

Any property that changes over time is represented using time-indexed propositions. For example:

 L₁,₁⁰ means the agent is at square [1,1] at time 0

 FacingEast⁰, HaveArrow⁰, and WumpusAlive⁰ are also part of the initial knowledge base

Such time-varying properties are called fluents (or state variables).


In contrast, atemporal symbols represent permanent facts of the world and do not need time
indices.

Linking Percepts to Locations

Using the location fluent, percepts can be connected to properties of specific squares. For every
square [x, y] and time t, we assert:

This allows the agent to relate what it perceives to where it is.

Actions and Effect Axioms

To model how the world changes, we introduce time-indexed action symbols such as Forward⁰,
TurnLeft⁰, etc. The standard order is:
1. Percept at time t

2. Action at time t

3. State transition to time t+1

An effect axiom specifies the result of an action. For example:

This means that moving forward from [1,1] while facing east places the agent in [2,1] at the next time
step.

Using such axioms, the agent can infer its new position. For example:

The Frame Problem

However, problems arise when asking about facts that should remain unchanged. For instance, after
moving forward, the agent should still have its arrow. But without additional axioms:

This happens because effect axioms describe what changes, but not what stays the same. This issue
is known as the frame problem.

A direct solution is to add frame axioms, such as:

However, this approach is inefficient. With m actions and n fluents, the number of frame axioms
becomes O(mn). This is called the representational frame problem.

Successor-State Axioms: A Better Solution

Since each action usually affects only a few fluents, a better approach is to write axioms for each
fluent, rather than for each action. These are called successor-state axioms.

General form:

Example: HaveArrow Fluent

Because the arrow can only be lost by shooting and never regained, its successor-state axiom
becomes:
This compactly captures both change and persistence, solving the frame problem efficiently.

You might also like