Ai Assignment 2
Ai Assignment 2
Related Topics
Assignment 2
1.1 Definition
A CSP is characterized by:
The objective is to find an assignment of values to variables such that all constraints are
satisfied.
1.2 Examples
1.2.1 Sudoku
• Variables: Each cell in the 9x9 grid, represented as xij , where 1 ≤ i, j ≤ 9.
• Constraints:
• Constraints:
1
1.3 Key Features of CSP
• Declarative Representation: CSPs provide a high-level, declarative way to model
problems by focusing on ”what” needs to be satisfied, rather than ”how” to solve it.
• Search and Optimization: Solving CSPs often involves searching through the space
of possible assignments using algorithms such as backtracking, constraint propagation,
or heuristics like minimum remaining values (MRV) and degree heuristics.
• Scalability: CSPs can be adapted to handle both small-scale and large-scale prob-
lems by leveraging advanced techniques like domain reduction and dynamic constraint
satisfaction.
1.4 Applications
• Planning: Generating sequences of actions to achieve a goal while respecting con-
straints (e.g., robot path planning).
• Scheduling: Assigning resources and time slots to tasks (e.g., exam timetabling, airline
crew scheduling).
• Game AI: Solving puzzles and strategic games (e.g., Sudoku, N-Queens problem).
2 Constraint Propagation
Constraint propagation is a fundamental technique in solving Constraint Satisfaction Problems
(CSPs) . It involves using constraints to systematically reduce the domains of variables,
thereby simplifying the problem and reducing the search space. By eliminating values that
cannot participate in any valid solution, constraint propagation helps to focus computational
efforts on feasible solutions.
2.1 Concept
Constraint propagation works by enforcing consistency across variables and their domains
based on the defined constraints. This process is often iterative, repeatedly applying con-
straints to refine the domains of variables until no further reduction is possible or a solution
is found.
2
2.2.1 Formal Definition of Arc Consistency
A binary constraint between two variables x and y is arc-consistent if:
• For every value v ∈ Dx (the domain of x), there exists a value w ∈ Dy (the domain of
y) such that the constraint C(x, y) is satisfied.
If any value in Dx cannot satisfy this condition, it is removed from the domain.
2.2.2 Example
• Initial Problem:
– Variables: x, y
– Domains: Dx = {1, 2}, Dy = {1, 2, 3}
– Constraint: x ̸= y
• Propagation:
After propagation, y’s domain is reduced to Dy = {2, 3}, as these are the only values
that satisfy the constraint.
3
2.5 Applications
Constraint propagation is widely used in AI applications, such as:
• Puzzle Solving: Techniques like arc consistency are commonly used in puzzles like
Sudoku or crosswords.
• Vision and Robotics: Aligning features in images or ensuring consistent sensor read-
ings in robots.
3 Backtracking Search
Backtracking search is a fundamental algorithm for solving Constraint Satisfaction Problems
(CSPs). It operates by exploring possible variable assignments one at a time, systematically
backtracking whenever a constraint violation occurs. This approach ensures that only feasible
solutions are considered, making it both systematic and complete.
3.1 Overview
• Basic Idea: Assign values to variables incrementally, checking constraints at each step.
If a partial assignment violates any constraint, backtrack by undoing the most recent
assignment and trying a different value.
• Characteristics:
4. Check if the value is consistent with the current partial assignment (i.e., does not violate
any constraints).
5. If consistent:
6. If a constraint is violated:
4
• Backtrack by removing the last assignment and trying the next available value.
7. Repeat until all variables are assigned (solution found) or all possibilities are exhausted
(failure).
3.3 Pseudocode
The following pseudocode illustrates the backtracking search algorithm:
BacktrackingSearch(CSP):
if all variables are assigned:
return solution
select an unassigned variable
for each value in variable’s domain:
if value consistent with assignment:
assign value to variable
result = BacktrackingSearch(CSP)
if result is not failure:
return result
remove assignment
return failure
• Constraints: Words must fit within the grid and match overlapping letters.
• Process:
• Value Ordering Heuristics: Try values that are most likely to succeed first (Least
Constraining Value heuristic).
• Constraint Propagation: Use techniques like arc consistency to prune domains before
making assignments.
5
• Forward Checking: Check for constraint violations in future variables after each as-
signment.
3.6 Applications
Backtracking search is used in a variety of AI domains, including:
4 Game Playing
Game playing is a crucial area , focusing on making optimal decisions in competitive or
adversarial environments. These environments often involve multiple agents, each striving to
achieve their goals, which can conflict with those of others. AI systems aim to find strategies
that maximize an agent’s performance, considering possible moves by opponents.
• Deterministic Games: These games have no element of chance. The outcome of any
action is fully determined by the rules of the game.
• Players: Agents participating in the game, often referred to as MAX (the player trying
to maximize the score) and MIN (the opponent trying to minimize the score).
• Actions: The set of valid moves available to a player at any given state.
• Transition Model: Describes how the game state changes in response to a player’s
action.
6
• Terminal Test: Determines whether the game has reached a conclusion (e.g., check-
mate in chess, a full board in Tic-Tac-Toe).
• Utility Function: Assigns a numerical value to terminal states to represent the outcome
(e.g., win, loss, or draw).
• Purpose: To determine the best possible move for the player (MAX) assuming that
the opponent (MIN) is also playing optimally.
• Key Idea: The MAX player aims to maximize the minimum guaranteed payoff, while
the MIN player seeks to minimize the MAX player’s payoff.
• How it works:
– The algorithm explores the entire game tree starting from the root.
– At terminal states, it calculates a utility value (score) based on the game’s outcome.
– Utility values are propagated back up the tree:
∗ MAX nodes take the maximum value of their children.
∗ MIN nodes take the minimum value of their children.
– The optimal move for the current player is determined by selecting the branch with
the best utility value.
• Example: In a game like Tic-Tac-Toe, Minimax evaluates all possible moves to ensure
the MAX player avoids losing, even if it results in a draw.
Pseudocode:
Minimax(node, depth, maximizingPlayer):
if depth == 0 or node is terminal:
return utility(node)
if maximizingPlayer:
value = -
for each child of node:
value = max(value, Minimax(child, depth-1, False))
return value
else:
value =
for each child of node:
value = min(value, Minimax(child, depth-1, True))
return value
7
Components:
• MAX Nodes: Represent the current player trying to maximize their score.
• MIN Nodes: Represent the opponent trying to minimize the score of the MAX player.
• Time Complexity: O(bd ), where b is the branching factor (number of moves per state)
and d is the depth of the game tree.
Advantages:
• Guarantees the best possible move for the MAX player under optimal play.
Limitations:
• Computationally expensive for games with large state spaces or deep trees.
• Assumes perfect rationality from both players, which may not reflect human behavior.
Applications:
• Purpose: To reduce the number of nodes evaluated in the game tree, thereby speeding
up the decision-making process without affecting the result.
• How it works:
– As the algorithm explores the game tree, it keeps track of α and β values.
– If it determines that a branch cannot yield a better outcome than the already
known best value, that branch is pruned (ignored).
– α is updated during the MAX player’s turn to reflect the highest value found so
far.
– β is updated during the MIN player’s turn to reflect the lowest value found so far.
8
• Effectiveness: Pruning can reduce the time complexity of Minimax from O(bd ) to
O(bd/2 ) in the best-case scenario, where b is the branching factor and d is the depth of
the tree.
• Example: In a chess game, Alpha-Beta Pruning avoids exploring moves that are clearly
suboptimal, allowing the algorithm to focus on promising strategies.
Pseudocode:
• Reduces computational cost, allowing deeper search within the same time frame.
• Particularly effective in games with large branching factors (e.g., chess and Go).
Limitations:
• Pruning effectiveness depends on the order of node exploration. A better order can lead
to more significant pruning.
• Not suitable for stochastic games or those with hidden information without further
adaptation.
• Combines random sampling of the game tree with statistical methods to estimate the
best moves.
9
4.3.4 Reinforcement Learning
• Used for games without a clear analytical solution.
• Agents learn optimal strategies through trial and error by interacting with the environ-
ment.
• Definition: Stochastic games are multi-agent systems where transitions between states
depend on the players’ actions and probabilistic outcomes.
• Key Features:
– Chance Nodes: Represent states where the next move is determined by a random
event.
– Mixed Strategies: Players may use probabilistic strategies instead of determin-
istic actions.
– Expected Utility: Decisions are based on the expected payoff considering all
possible outcomes.
• Examples:
10
– Poker: Combines strategy with probabilistic elements such as card shuffling.
– Monopoly: Includes chance cards and dice rolls that influence gameplay.
• Applications:
• Actions (A): The set of all possible moves or decisions for players.
• Transition Function (T ): Defines the probability of moving to a new state given the
current state and actions.
• Reward Function (R): Specifies the payoff for each state and action combination.
• Monte Carlo Tree Search (MCTS): Uses random sampling and exploration to esti-
mate the value of actions in games with large state spaces.
• Reinforcement Learning: Techniques like Q-learning and policy gradient methods are
used to train agents to handle stochastic environments.
11
4.6.3 Challenges in Stochastic Games
• Large state spaces make exact computation infeasible.
• Complex strategies due to interaction between chance events and opponent behavior.
12