0% found this document useful (0 votes)
42 views13 pages

Ai Assignment 2

Uploaded by

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

Ai Assignment 2

Uploaded by

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

Constraint Satisfaction Problems &

Related Topics

Assignment 2

Name: SHIVAM MISHRA

Roll Number: 2204281530047

Branch: CSE-AIML (3rd Year)


1 Constraint Satisfaction Problems (CSP)
Constraint Satisfaction Problems (CSPs) provide a framework for solving problems that require
making decisions within defined limits or rules. These problems are widely applicable in fields
such as planning, scheduling, resource allocation, and reasoning.

1.1 Definition
A CSP is characterized by:

• Variables: A finite set of variables X = {x1 , x2 , . . . , xn }, where each variable represents


an entity or component of the problem.

• Domains: A set of domains D = {D1 , D2 , . . . , Dn }, where each Di is the set of


possible values that variable xi can take. In AI, domains are often discrete and finite.

• Constraints: A set of constraints C = {C1 , C2 , . . . , Cm }, where each constraint Cj


specifies the allowable combinations of values for a subset of variables. Constraints are
often expressed as logical formulas or relations.

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.

• Domain: The numbers 1 through 9.

• Constraints:

– Each number appears exactly once in every row.


– Each number appears exactly once in every column.
– Each number appears exactly once in every 3x3 subgrid.

1.2.2 Scheduling Problems


• Variables: Tasks or events to be scheduled (e.g., x1 , x2 , . . . , xn ).

• Domain: Possible time slots or resources available.

• Constraints:

– No two tasks share the same resource simultaneously.


– Tasks must adhere to precedence or dependency requirements.
– Specific time slots may be restricted for certain tasks.

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

• Natural Language Processing (NLP): Parsing sentences by satisfying grammatical


constraints.

• Game AI: Solving puzzles and strategic games (e.g., Sudoku, N-Queens problem).

• Vision and Perception: Matching and alignment tasks in computer vision.

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 Example: Arc Consistency


One of the most common forms of constraint propagation is arc consistency, which ensures
that every value in the domain of a variable satisfies the binary constraints with at least one
value in the domain of the related variable.

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:

– For x = 1, y must be in {2, 3}.


– For x = 2, y must be in {1, 3}.
– Combine results: Dy = {2, 3}.

After propagation, y’s domain is reduced to Dy = {2, 3}, as these are the only values
that satisfy the constraint.

2.3 Techniques in Constraint Propagation


Several techniques are used in constraint propagation, including:
• Node Consistency: Ensures that unary constraints (constraints on a single variable)
are satisfied for all values in the variable’s domain.

• Arc Consistency: Ensures binary constraints are satisfied by reducing incompatible


values between pairs of variables.

• Path Consistency: Extends arc consistency to triples of variables, ensuring compati-


bility along all paths.

• Generalized Consistency: Considers higher-order constraints involving more than two


variables.

2.4 Benefits of Constraint Propagation


• Reduces Search Space: By eliminating invalid values early, it simplifies the problem
and reduces computational complexity.

• Improves Efficiency: Propagation can identify inconsistencies before expensive search


algorithms are applied.

• Enhances Problem Solving: Combined with search strategies, propagation makes


CSP solving more effective by pruning infeasible regions.

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.

• Scheduling: Enforcing constraints like time slots and resource availability.

• Planning and Reasoning: Simplifying planning problems by reducing infeasible action


sequences.

• 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:

– It is a depth-first search (DFS) algorithm.


– It is complete and guarantees finding a solution if one exists.
– It is systematic, ensuring no duplicate assignments are explored.

3.2 Steps in Backtracking Search


1. Start with an empty assignment of variables.

2. Select an unassigned variable.

3. Choose a value for the variable from its domain.

4. Check if the value is consistent with the current partial assignment (i.e., does not violate
any constraints).

5. If consistent:

• Assign the value and proceed to the next variable.


• Recursively apply backtracking search.

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

3.4 Example: Crossword Puzzle


• Problem: Solve a crossword puzzle by filling one word at a time.

• Variables: Each blank in the crossword grid.

• Domain: List of valid words.

• Constraints: Words must fit within the grid and match overlapping letters.

• Process:

– Assign a word to the first blank.


– Check if it satisfies constraints with adjacent blanks.
– If valid, proceed to the next blank.
– If invalid, backtrack and try a different word.

3.5 Improvements to Backtracking Search


Several enhancements can make backtracking search more efficient:
• Variable Ordering Heuristics: Choose variables with the smallest domain first (Min-
imum Remaining Values heuristic) to reduce branching.

• 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.

• Dynamic Backtracking: Allow revisions to earlier assignments rather than simply


undoing the most recent one.

3.6 Applications
Backtracking search is used in a variety of AI domains, including:

• Puzzle Solving: Solving Sudoku, crosswords, and the N-Queens problem.

• Scheduling: Assigning tasks to time slots or resources under constraints.

• Planning: Generating sequences of actions while satisfying preconditions.

• Game AI: Computing valid moves and strategies in board games.

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.

4.1 Categories of Games


Games are classified based on their nature and the information available to players:

• Deterministic Games: These games have no element of chance. The outcome of any
action is fully determined by the rules of the game.

– Examples: Chess, Tic-Tac-Toe, Checkers.

• Stochastic Games: These games involve randomness or uncertainty in the outcome


of actions due to elements like dice rolls or card draws.

– Examples: Backgammon, Poker, Monopoly.

4.2 Key Components of Game Playing


Game playing involves the following components:

• Initial State: The starting configuration 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).

4.3 Techniques in Game Playing


various algorithms and strategies to solve games:

4.3.1 Minimax Algorithm


The Minimax Algorithm is a fundamental decision-making algorithm in AI used for solving
two-player, zero-sum games. It is based on the assumption that both players act optimally to
maximize their own payoff while minimizing their opponent’s.

• 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:

• Utility Function: Assigns a numerical value to terminal states, representing outcomes


such as win, loss, or draw.

• 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 and Space Complexity:

• Time Complexity: O(bd ), where b is the branching factor (number of moves per state)
and d is the depth of the game tree.

• Space Complexity: O(d) for depth-first search.

Advantages:

• Guarantees the best possible move for the MAX player under optimal play.

• Provides a clear framework for evaluating moves and outcomes.

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:

• Board games like Chess, Checkers, and Tic-Tac-Toe.

• Strategy games where all information is available to both players.

4.3.2 Alpha-Beta Pruning


Alpha-Beta Pruning is an enhancement to the Minimax algorithm that significantly improves
its efficiency by pruning branches of the game tree that do not affect the outcome of the
decision. It achieves this by maintaining two values, alpha (α) and beta (β), which represent
the minimum score that the maximizing player is assured of and the maximum score that the
minimizing player is assured of, respectively.

• 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:

AlphaBetaPruning(node, depth, , , maximizingPlayer):


if depth == 0 or node is terminal:
return value of node
if maximizingPlayer:
value = -
for each child of node:
value = max(value, AlphaBetaPruning(child, depth-1, , , False))
= max(, value)
if <= :
break // Beta cutoff
return value
else:
value =
for each child of node:
value = min(value, AlphaBetaPruning(child, depth-1, , , True))
= min(, value)
if <= :
break // Alpha cutoff
return value

Advantages of Alpha-Beta Pruning:

• Reduces computational cost, allowing deeper search within the same time frame.

• Maintains the exact result of the Minimax algorithm.

• 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.

4.3.3 Monte Carlo Tree Search (MCTS)


• A probabilistic approach used in stochastic games.

• 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.

• Examples include DeepMind’s AlphaGo and AlphaZero.

4.4 Applications of Game Playing


• Classic Board Games: Chess, Go, Tic-Tac-Toe.

• Video Games: AI bots for real-time strategy games and simulations.

• Training and Simulation: Military and business strategy simulations.

• Research: Understanding decision-making, learning, and strategic thinking.

4.5 Challenges in Game Playing


• Complexity: Some games, like Go, have an enormous state space that is computation-
ally expensive to explore.

• Uncertainty: Stochastic games involve randomness, making them harder to predict.

• Opponent Modeling: Predicting the strategies of human or AI opponents is challeng-


ing.

4.6 Stochastic Games


Stochastic games are games that incorporate elements of randomness or chance. Unlike
deterministic games where the outcome is solely based on the players’ actions, stochastic
games involve probabilistic events that can influence the game state. These games are widely
used to model real-world scenarios involving uncertainty.

• 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:

– Backgammon: Involves dice rolls that introduce randomness.

10
– Poker: Combines strategy with probabilistic elements such as card shuffling.
– Monopoly: Includes chance cards and dice rolls that influence gameplay.

• Applications:

– Decision-making under uncertainty.


– Multi-agent systems in economics and robotics.
– Modeling real-world scenarios, such as auctions and resource allocation.

4.6.1 Decision Process in Stochastic Games


A stochastic game is typically modeled as a Markov Decision Process (MDP) or a Multi-agent
Markov Decision Process (MMDP).

• States (S): Represent the possible configurations of the game.

• 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.

4.6.2 Solving Stochastic Games


• Expectimax Algorithm: An extension of Minimax for stochastic games. It calculates
the expected utility of moves by considering both players’ actions and probabilistic
outcomes.

• 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.

Pseudocode for Expectimax:


Expectimax(node, depth):
if depth == 0 or node is terminal:
return utility(node)
if node is a MAX node:
value = -
for each child of node:
value = max(value, Expectimax(child, depth-1))
return value
if node is a Chance node:
value = 0
for each child of node:
probability = getProbability(child)
value += probability * Expectimax(child, depth-1)
return value

11
4.6.3 Challenges in Stochastic Games
• Large state spaces make exact computation infeasible.

• Balancing exploration and exploitation in uncertain environments.

• Complex strategies due to interaction between chance events and opponent behavior.

12

You might also like