Lecture 4 - CSPs I

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 55

Announcements

 Homework 2: Search 2 is out


 P1: Search – Deadline Extended
 Monday, 20 March 2023 @ 17h00
 Practical Slot: 22 March – Repurposed***
 Will be a THEORY session
 Venue to be confirmed
 P2: Multi-Agent
 Released on 29 March 2023
 Deadline: Monday, 17 April 2023 @ 17h00
Design A Group Logo
 Design a logo for your group
 Must include: NUST AI 2024
ARI711S: Artificial Intelligence
Constraint Satisfaction Problems I
7-0
3-4
What is Search For?
 Assumptions about the world: a single agent, deterministic actions, fully observed
state, discrete state space

 Planning: sequences of actions


 The path to the goal is the important thing
 Paths have various costs, depths
 Heuristics give problem-specific guidance

 Identification: assignments to variables


 The goal itself is important, not the path
 All paths at the same depth (for some formulations)
 CSPs are specialized for identification problems
Constraint Satisfaction Problems

Constraint Satisfaction Problems (CSPs) are a class of computational problems where


the goal is to find a solution that satisfies a set of constraints.
Constraint Satisfaction Problems
Key components of Constraint Satisfaction Problems
Variables – represent the entities or objects in the problem that need to be assigned values.
Each variable typically has a domain, which is the set of possible values it can take.

Domain - is the set of values that a variable can take. or example, in a scheduling problem, a
variable might represent the time slot for a particular task, and its domain would be the set of
possible time slots.

Constraints - define the relationships between the variables. They specify which combinations
of variable assignments are allowed or disallowed. Constraints can be unary (i.e., involving a
single variable), binary (i.e., involving two variables), or higher-order (involving three or more
variables).

Objective - In some CSPs, there might be an objective function or goal that needs to be
optimized. This could involve finding a solution that minimizes or maximizes some measure of
interest, such as cost, distance, or time.

Solution – Assign a value to each variable such that none of the constraints are broken.
Constraint Satisfaction Problems

Note: Constraints

Explicit constraints in (CSPs) are restrictions or conditions that are directly defined and
provided in the problem statement.
For example: Directly Defined Relationships, Stated Constraints, Constraints on Variable
Assignments, Constraints on Variable Domains, Objective Function Constraints etc..

Implicit constraints in (CSPs) are conditions or limitations that are not explicitly provided in
the problem statement but are inferred from the problem context, domain knowledge, or logical
reasoning.
For example: Domain Restrictions, Feasibility Requirements, Logical Consistency,
Problem-Specific Considerations
Constraint Satisfaction Problems

 Standard search problems:


 State is a “black box”: arbitrary data structure
 Goal test can be any function over states
 Successor function can also be anything

 Constraint satisfaction problems (CSPs):


 A special subset of search problems
 State is defined by variables Xi with values from a
domain D (sometimes D depends on i)
 Goal test is a set of constraints specifying allowable
combinations of values for subsets of variables

 Simple example of a formal representation language

 Allows useful general-purpose algorithms with more


power than standard search algorithms
CSP Examples
Example: Map Coloring
 Variables:

 Domains:

 Constraints: adjacent regions must have different


colors
Implicit:

Explicit:

 Solutions are assignments satisfying all


constraints, e.g.:
Example: Scheduling Classes
Variables:
Classes: Classes = C1, C2, C3, C4, C5
1. Class 1 – [8:00 - 9:00]
2. Class 2 – [8:30 - 9:30] Domain (Values):
3. Class 3 – [9:00 - 10:00] Lecturers : A, B, C
4. Class 4 – [9:00 - 10:00]
5. Class 5 – [10:30 - 11:30] Constraints:
Unary :
Lecturers: Binary :
6. Lect A can teach Classes 1, 2, and 5 Problem: Solution:
7. Lect B can teach Classes 3, 4, and 5 C1 = { A, C} Assign a value to each
8. Lect C can teach Classes 1,3, and 4 C2 = { A }
variable such that none of
the constraints are broken
C3 = { B, C}
C4 = { B, C }
C5 = { A, B }
Example: N-Queens
 Formulation 1:
 Variables:
 Domains:
 Constraints
Example: N-Queens
 Formulation 2:
 Variables:

 Domains:

 Constraints:
Implicit:

Explicit:
Example: Sudoku
 Variables:
 Each (open) square
 Domains:
 {1,2,…,9}
 Constraints:

9-way alldiff for each column


9-way alldiff for each row
9-way alldiff for each region
(or can have a bunch of
pairwise inequality
constraints)
Constraint Graphs
Constraint Graphs

 Binary CSP: each constraint relates (at most) two


variables

 Binary constraint graph: nodes are variables, arcs


show constraints

 General-purpose CSP algorithms use the graph


structure to speed up search. E.g., Tasmania is an
independent subproblem!
Screenshot of Demo N-Queens
Varieties of CSPs and Constraints
Varieties of CSPs
 Discrete Variables
 Finite domains
 Size d means O(dn) complete assignments
 E.g., Boolean CSPs, including Boolean satisfiability (NP-
complete)
 Infinite domains (integers, strings, etc.)
 E.g., job scheduling, variables are start/end times for each job
 Linear constraints solvable, nonlinear undecidable

 Continuous variables
 E.g., start/end times for Hubble Telescope observations
 Linear constraints solvable in polynomial time by LP methods
Varieties of CSPs

 Examples: Imagine a CSP representing a coloring


problem for a map. Each region on the map can be a
variable. The domain for each variable would be a set
of discrete colors (e.g., red, green, blue). Another
example is a Sudoku puzzle, where each cell is a
variable with a domain of numbers 1 to 9.
Varieties of Constraints
 Varieties of Constraints
 Unary constraints involve a single variable (equivalent to
reducing domains), e.g.:

 Binary constraints involve pairs of variables, e.g.:

 Higher-order constraints involve 3 or more variables:


e.g., Sudoku cell constraints

 Preferences (soft constraints):


 E.g., red is better than green
 Often representable by a cost for each variable assignment
 Gives constrained optimization problems
Real-World CSPs
 Assignment problems: e.g., who teaches what class
 Timetabling problems: e.g., which class is offered when and where?
 Hardware configuration
 Transportation scheduling
 Factory scheduling
 Circuit layout
 Fault diagnosis
 … lots more!

 Many real-world problems involve real-valued variables…


Solving CSPs
Standard Search Formulation
 Standard search formulation of CSPs

 States defined by the values assigned


so far (partial assignments)
 Initial state: the empty assignment, {}
 Successor function: assign a value to an
unassigned variable
 Goal test: the current assignment is
complete and satisfies all constraints

 We’ll start with the straightforward,


naïve approach, then improve it
Search Methods
 What would BFS do?

 What would DFS do?

 What problems does naïve search have?


Video of Demo Coloring -- DFS
Backtracking Search
Backtracking Search

 Backtracking search is the basic uninformed algorithm for solving CSPs


 It works by iteratively assigning values to variables and checking if the
current assignment violates any constraints.
 If a constraint is violated, the algorithm backtracks and tries a different
value for the last assigned variable.
Backtracking Search
 Idea 1: One variable at a time
 Variable assignments are commutative, so fix ordering
 I.e., [WA = red then NT = green] same as [NT = green then WA = red]
 Only need to consider assignments to a single variable at each step

 Idea 2: Check constraints as you go


 I.e. consider only values which do not conflict previous assignments
 Might have to do some computation to check the constraints
 “Incremental goal test”

 Depth-first search with these two improvements


is called backtracking search (not the best name)
 Can solve n-queens for n  25
Backtracking Example
Backtracking Search

 Backtracking = DFS + variable-ordering + fail-on-violation


 What are the choice points?
Video of Demo Coloring – Backtracking
Improving Backtracking

 General-purpose ideas give huge gains in speed

 Ordering:
 Which variable should be assigned next?
 In what order should its values be tried?

 Filtering: Can we detect inevitable failure early?

 Structure: Can we exploit the problem structure?


Filtering
Filtering
 Filtering is an optimization technique used in conjunction with backtracking to improve
its efficiency.
 Filtering: Keep track of domains for unassigned variables and cross off bad options
 Filtering works by reducing the domains of unassigned variables based on the current
partial assignment and constraints.
 It essentially prunes out values from domains that are known to lead to dead ends
(assignments violating constraints).
Filtering
Benefits of Filtering

 Filtering significantly reduces the search space for backtracking.


 This leads to faster solution times, especially for complex CSPs.
 It helps backtracking focus on more promising assignments.
Filtering
Examples of Filtering Techniques
 Forward Checking: Removes values from domains of unassigned variables
that would violate constraints with already assigned variables.
 Arc Consistency: A stronger form of filtering that ensures consistency
between all pairs of variables.
 Constraint Propagation: Propagates the effects of assigning a value to one
variable on the domains of other related variables.
Filtering: Forward Checking
Core Idea
 Forward checking focuses on the immediate implications of assigning a
value to a variable.
 It aims to identify and remove values from the domains of unassigned
variables that would lead to constraint violations with the current partial
assignment.
Filtering: Forward Checking
Steps involved:
1. Assign a value to a variable in the CSP.
2. Analyze constraints: Identify all the constraints that involve the newly
assigned variable and other unassigned variables.
3. Prune incompatible values: For each unassigned variable involved in the
constraints, iterate through its domain (set of possible values).
 If a value in the domain, when assigned to the unassigned variable, violates the
constraint with the already assigned variable, remove that value from the domain.
Video of Demo Coloring – Backtracking with Forward Checking
Filtering: Constraint Propagation
 Forward checking propagates information from assigned to unassigned variables, but
doesn't provide early detection for all failures:

NT Q
WA
SA
NSW
V

 NT and SA cannot both be blue!


 Why didn’t we detect this yet?
 Constraint propagation: reason from constraint to constraint
Consistency of A Single Arc
 An arc X  Y is consistent iff for every x in the tail there is some y in the head which
could be assigned without violating a constraint

NT Q
WA
SA
NSW
V

Delete from the tail!


 Forward checking: Enforcing consistency of arcs pointing to each new assignment
Arc Consistency of an Entire CSP
 A simple form of propagation makes sure all arcs are consistent:

NT Q
WA SA
NSW
V

 Important: If X loses a value, neighbors of X need to be rechecked!


 Arc consistency detects failure earlier than forward checking
 Can be run as a preprocessor or after each assignment Remember: Delete
 from the tail!
What’s the downside of enforcing arc consistency?
Enforcing Arc Consistency in a CSP

 Runtime: O(n2d3), can be reduced to O(n2d2)


 … but detecting all possible future problems is NP-hard – why?
Video of Demo Arc Consistency – CSP Applet – n Queens
Limitations of Arc Consistency

 After enforcing arc


consistency:
 Can have one solution left
 Can have multiple solutions left
 Can have no solutions left (and
not know it)

 Arc consistency still runs What went


inside a backtracking search! wrong here?
Video of Demo Coloring – Backtracking with Forward Checking –
Complex Graph
Video of Demo Coloring – Backtracking with Arc Consistency –
Complex Graph
Ordering
Ordering: Minimum Remaining Values
 Variable Ordering: Minimum remaining values (MRV):
 Choose the variable with the fewest legal left values in its domain

 Why min rather than max?


 Also called “most constrained variable”
 “Fail-fast” ordering
Ordering: Least Constraining Value
 Value Ordering: Least Constraining Value
 Given a choice of variable, choose the least
constraining value
 I.e., the one that rules out the fewest values in
the remaining variables
 Note that it may take some computation to
determine this! (E.g., rerunning filtering)

 Why least rather than most?

 Combining these ordering ideas makes


1000 queens feasible
Demo: Coloring -- Backtracking + Forward Checking + Ordering

You might also like