Class Scheduling Troubles
15-381: Artificial Intelligence
Behrang Mohit and Davide Fossati
Constraint Satisfaction
Problems
Slides adapted from Emma Brunskill, Stuart Russell, Andrew Moore, Dan Klein, Manuela Veloso
§ 4 more required classes to graduate
§ A: Algorithms
§ C: Computer Programming
B: Bayesian Learning
D: Distributed Computing
§ A few restrictions
§ Algorithms must be taken same semester as distributed
computing
§ Computer programming is a prereq for distributed
computing and Bayesian learning, so it must be taken in
an earlier semester
§ Advanced algorithms and Bayesian Learning are always
offered at the same time, so cannot be taken the same
semester
§ 3 semesters (semester 1,2,3) when can take classes
Search Problem Formulation?
§ 4 more required classes to graduate: A, B, C, D
§ A few restrictions
§ A must be taken same semester as D
§ C is a prereq for D and B so must take C earlier than D & B
§ A & B are always offered at the same time, so they cannot be
taken the same semester
§ 3 semesters (semester 1,2,3) when can take classes
Search Problem Formulation
§ 4 more required classes to graduate: A, B, C, D
§ A few restrictions
§ A must be taken same semester as D
§ C is a prereq for D and B so must take C earlier than D & B
§ A & B are always offered at the same time, so they cannot be
taken the same semester
§ 3 semesters (semester 1,2,3) when can take classes
§ Start state: A={},B={},C={},D={}
§ Actions: Take unassigned class & assign to any semester (1,2,3)
§ Goal test: All states assigned and all restrictions met
Let’s Try Tree BFS
Exercise: Try Tree DFS starting with A
A={},B={},C={},D={}
A=1,B={},C={},D={}
A=1,B=1,C={},D={}
A=2,B={},C={},D={}
… A=1,B={},C={},D=3
…
…
A={},B={},C={},D=3
A=1,B={},C={},D=3
…
§ 4 more required classes to graduate: A, B, C, D
§ A few restrictions
§ A must be taken same semester as D
§ C is a prereq for D and B so must take C earlier than D & B
§ A & B are always offered at the same time, so they cannot be
taken the same semester
§ 3 semesters (semester 1,2,3) when can take classes
§ Start state: A={},B={},C={},D={}
§ Actions: Take unassigned class & assign to any semester (1,2,3)
§ Goal test: All states assigned and all restrictions met
A must be taken same semester as D
C is a prereq for D and B so must take C earlier than D & B
A & B are cannot be taken the same semester
A must be taken same semester as D
C is a prereq for D and B so must take C earlier than D & B
A & B are cannot be taken the same semester
A={},B={},C={},D={}
A={},B={},C={},D={}
A=1,B={},C={},D={}
A=1,B=1,C={},D={}
…
A=1,B={},C={},D={}
A=1,B=1,C={},D={}
A=1,B=1,C=1,D={}
A=1,B=1,C=1,D={} … A=1,B=1,C={},D=1
A=1,B=1,C=1,D=1
A=1,B=1,C=1,D=1
Fails Goal Test
Fails Goal Test
A=1,B=1,C={},D=2
A=1,B=1,C=1,D=2 A=1,B=1,C=1,D=1
Fails Goal Test
Fails Goal Test
A=1,B=1,C=3,D=1
Fails Goal Test
…
A must be taken same semester as D
C is a prereq for D and B so must take C earlier than D & B
A & B are cannot be taken the same semester
A must be taken same semester as D
C is a prereq for D and B so must take C earlier than D & B
A & B are cannot be taken the same semester
A={},B={},C={},D={}
A={},B={},C={},D={}
A=1,B={},C={},D={}
A=1,B={},C={},D={}
A=1,B=1,C={},D={}
A=1,B=1,C={},D={}
A=1,B=1,C=1,D={}… A=1,B=1,C={},D=1
A=1,B=1,C=1,D=1
Fails Goal Test
A=1,B=1,C=1,D=2 A=1,B=1,C=1,D=1
Fails Goal Test
A=1,B=1,C=1,D={}… A=1,B=1,C={},D=1
A=1,B=1,C={},D=2
Fails Goal Test
A=1,B=1,C=3,D=1
Fails Goal Test
…
Constraint Satisfaction Problems
Special subset of search problems
Variables: V = {V1,..,VN}
Domain: Set of d possible values for each variable
Constraints: C = {C1,..,CK}
A constraint consists of
§ variable tuple
§ list of possible values for tuple (ex. [(V2,V3),{(R,B),(R,G)] )
§ Or function that describes possible values (ex. V2 ≠ V3)
§ Allows useful general-purpose algorithms with more power
than standard search algorithms
§
§
§
§
§
A=1,B=1,C=1,D=1
A=1,B=1,C={},D=2
A=1,B=1,C=1,D=2 A=1,B=1,C=1,D=1
A=1,B=1,C=3,D=1
3number of unassigned classes
Branching factor is
Tree search size 34 x 33 x 32 x 3 = 310
But only 34 assignments!
Example: Map-Coloring
§ Variables:
§ Domain:
§ Constraints: adjacent regions must have
different colors
§ Solutions are assignments satisfying all
constraints, e.g.:
14
…
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
Very Important CSPs: Scheduling
§ Many industries. Many multi-million $ decisions. Used
extensively for space mission planning. Military uses.
§ People really care about improving scheduling
algorithms! Problems with phenomenally huge state
spaces. But for which solutions are needed very quickly
§ Many kinds of scheduling problems e.g.:
§ Job shop: Discrete time; weird ordering of operations
possible; set of separate jobs.
§ Batch shop: Discrete or continuous time; restricted
operation of ordering; grouping is important.
§ Manufacturing cell: Discrete, automated version of
open job shop.
Scheduling
§ A set of N jobs, J1,…, Jn.
§ Each job j is composed of a sequence of
operations Oj1,..., OjLj
§ Each operation may use resource R, and has a
specific duration in time.
§ A resource must be used by a single operation
at a time.
§ All jobs must be completed by a due time.
§ Problem: assign a start time to each job.
• 4 jobs
• 4 resources
• 10 operations
Precedence Constraints:
S11 + T11 ≤ S12
S12 + T12 ≤ S13
…
Deliver Constraints:
S13 + T13 ≤ Due
S22 + T22 ≤ Due
S33 + T33 ≤ Due
Resource constraints
Operations (1,1), (2,1), and (3,2) share the same
resource R1
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 Linear
programming methods
Varieties of Constraints
§ Varieties of Constraints
§ Unary constraints involve a single variable (equiv. to shrinking domains):
§ Binary constraints involve pairs of variables:
§ Higher-order constraints involve 3 or more variables:
e.g., cryptarithmetic column constraints
§ Preferences (soft constraints):
§
§
§
§
E.g., red is better than green
Often representable by a cost for each variable assignment
Gives constrained optimization problems
(We’ll ignore these today)
22
Exercise: Define CSP & Constraint Graph
Constraint Graphs
§ Binary CSP: each constraint relates (at most) two
variables
§ Binary constraint graph: nodes are variables, arcs show
constraints
Exercise: Define CSP & Constraint Graph
§ 4 more required classes to graduate: A, B, C, D
§ A few restrictions
§ A must be taken same semester as D
§ C is a prereq for D and B so must take C earlier than D & B
§ A & B are always offered at the same time, so they cannot be
taken the same semester
§ 3 semesters (semester 1,2,3) when can take classes
Improve DFS: Backtracking Search
§ 4 more required classes to graduate: A, B, C, D
§ A few restrictions
§ A must be taken same semester as D
§ C is a prereq for D and B so must take C earlier than D & B
§ A & B are always offered at the same time, so they cannot be
taken the same semester
§ 3 semesters (semester 1,2,3) when can take classes
§ Only consider a single variable at each point
§ Don’t care about path!
§ Order of variable assignment doesn’t matter, so
fix ordering
§ How many leaves are there?
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ Depth-first search for CSPs with these two
improvements is called backtracking search
B
A
C
D
§ Only consider only values which do not conflict with
assignment made so far
§ A ≠ B, A=D, C < B, C < D
26
Backtracking
§ Function Backtracking(csp) returns soln or fail
§ Return Backtrack({},csp)
§ Function Backtrack(assignment,csp) returns soln or fail
§ If assignment is complete, return assignment
§ Viß select_unassigned_var(csp)
§ For each val in order-domain-values(var,csp,assign)
§ If value is consistent with assignment
§ Add [Vi = val] to assignment
§ Result ß Backtrack(assignment,csp)
§ If Result ≠ fail, return result
§ Remove [Vi = val] from assignments
§ Return fail
Variables: A,B,C,D
Domain: {1,2,3}
Constraints: A ≠ B, A=D, C < B, C < D
Variable order: alphabetical
Value order: ascending (1,…)
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
(A=1)
(A=1,B=1) inconsistent with A ≠ B
(A=1,B=2)
(A=1,B=2,C=1)
(A=1,B=2,C=1,D=1) inconsistent with C < D
(A=1,B=2,C=1,D=2) inconsistent with A=D
(A=1,B=2,C=1,D=3) inconsistent with A=D
No valid assignment for D, return result = fail
§ Backtrack to (A=1,B=2,C=)
Try (A=1,B=2,C=2) but inconsistent with C < B
Try (A=1,B=2,C=3) but inconsistent with C < B
No other assignments for C, return result= fail
§ Backtrack to (A=1,B=)
(A=1,B=3)
(A=1,B=3,C=1)
(A=1,B=3,C=1,D=1) inconsistent with C < D
(A=1,B=3,C=1,D=2) inconsistent with A = D
(A=1,B=3,C=1,D=3) inconsistent with A = D
Return result = fail
§ Backtrack to (A=1,B=3,C=)
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
(A=1,B=3,C=2) inconsistent with C < B
(A=1,B=3,C=3) inconsistent with C < B
No remaining assignments for C, return fail
§ Backtrack to (A=1,B=)
No remaining assignments for B, return fail
§ Backtrack to A
(A=2)
(A=2,B=1)
(A=2,B=1,C=1) inconsistent with C < B
(A=2,B=1,C=2) inconsistent with C < B
(A=2,B=1,C=3) inconsistent with C < B
No remaining assignments for C, return fail
§ Backtrack to (A=2,B=?)
(A=2,B=2) inconsistent with A ≠ B
(A=2,B=3)
(A=2,B=3,C=1)
(A=2,B=3,C=1,D=1) inconsistent with C < D
(A=2,B=3,C=1,D=2) ALL VALID
Exercise: Backtracking
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D
§ Variable order: alphabetical
§ Value order: ascending (1,…)
How Could We Improve?
§ Function Backtracking(csp) returns soln or fail
§ Return Backtrack({},csp)
§ Function Backtrack(assignment,csp) returns soln or fail
§ If assignment is complete, return assignment
§ Viß select_unassigned_var(csp)
§ For each val in order-domain-values(var,csp,assign)
§ If value is consistent with assignment
§ Add [Vi = val] to assignment
§ Result ß Backtrack(assignment,csp)
§ If Result ≠ fail, return result
§ Remove [Vi = val] from assignments
§ Return fail
Ordering
§ Function Backtracking(csp) returns soln or fail
§ Return Backtrack({},csp)
§ Function Backtrack(assignment,csp) returns soln or fail
§ If assignment is complete, return assignment
§ Viß select_unassigned_var(csp)
§ For each val in order-domain-values(var,csp,assign)
§ If value is consistent with assignment
§ Add [Vi = val] to assignment
§ Result ß Backtrack(assignment,csp)
§ If Result ≠ fail, return result
Minimum Remaining Values
§ Minimum remaining values (MRV):
§ Choose the variable with the fewest legal values
§ Why min rather than max?
§ Also called most constrained variable
§ Fail-fast ordering
§ Remove [Vi = val] from assignments
§ Return fail
Least Constraining Value
§ Given a choice of variable:
§ Choose the least constraining value
§ The one that rules out the fewest values in the remaining
variables
§ Note that it may take some computation to determine this!
§ Why least rather than most?
Inference: Propagating Information
Inference: Detecting Failure Early
Consistency of An Arc
WA
NT
SA
Q
NSW
V
§ 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
§ Function Backtracking(csp) returns soln or fail
§ Return Backtrack({},csp)
§ Function Backtrack(assignment,csp) returns soln or fail
§ If assignment is complete, return assignment
§ Viß select_unassigned_var(csp)
§ For each val in order-domain-values(var,csp,assign)
Delete
from tail!
§ If value is consistent with assignment
§ Add [Vi = val] to assignment
§ Result ß Backtrack(assignment,csp)
§ If Result ≠ fail, return result
How does this change possible
remaining variable values?
§ Remove [Vi = val] from assignments
§ Return fail
§ Forward checking = Enforcing consistency of each arc
pointing to the new assignment
36
Arc Consistency of a CSP
WA
NT
SA
Q
NSW
Arc Consistency
V
Function AC-3 (csp) returns CSP, possibly with reduced domains, or inconsistent
Inputs: csp, binary CSP with components (X, D, C)
Local variables: queue, initially queue of all arcs (binary constraints in csp)
§ A simple form of propagation makes sure all arcs are consistent:
Delete
from tail!
§
§
§
§
If X loses a value, neighbors of X need to be rechecked!
Arc consistency detects failure earlier than forward checking
What s the downside of enforcing arc consistency?
Can be run as a preprocessor or after each assignment
37
Have to add in arc
While queue is not empty
for (Xi,Xj) and (Xj,Xi)
(Xi,Xj) = Remove-First(queue)
for i,j constraint
[domainXi, anyChangeToDomainXi] = Revise(csp,Xi,Xj)
if anyChangeToDomainXi == true
if size(domainXi) = 0, return inconsistent
else
for each Xk in Neighbors(Xi) except Xj
add (Xk,Xi) to queue
Return csp
------------------------------------------------------------------------------------------------------------------Function Revise(csp,Xi,Xj) returns DomainXi and anyChangeToDomainXi
anyChangeToDomainXi = false
for each x in Domain(Xi)
if no value y in Domain(Xj) allows (x,y) to satisfy constraint between (Xi,Xj)
delete x from Domain(Xi)
anyChangeToDomainXi = true
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D
§ What are the remaining domains after
enforcing arc consistency on the initial
CSP with no assignments?
§ Run AC-3 where keep queue always in
alphabetical order
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop AB
§ For any value of A (1,2,3) is there a value of B (2,3,1) that
satisfies constraint A ≠ B?
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop AB
§ For any value of A (1,2,3) is there a value of B (2,3,1) that
satisfies constraint A ≠ B? Yes, so no change to possible values
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop AD
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop AD.
§ For every value of A=(1,2,3) there is a value of D that satisfies
A=D. So no change to variable domains
Exercise: Arc Consistency
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop BA
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop BC (constraint B > C)
§ If B = 1 what values can C take on?
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop BC (constraint B > C)
§ If B = 1 what values can C take on? None. So B can only = {2,3}
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop BC (constraint B > C)
§ If B = 1 what values can C take on? None. So B can only = {2,3}
§ Also have to add neighbors of B back to queue: AB
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Pop BC (constraint B > C)
§ If B = 1 what values can C take on? None. So B can only = {2,3}
§ Also have to add neighbors of B (except C) back to queue: AB
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Pop AB (Constraint A ≠ B). Any change? No.
Exercise: Arc Consistency
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Pop CB (C < B constraint)
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Pop CB (C < B constraint)
§ C = (1,2)
§ Any additions to queue? (No, DC already in queue)
Exercise: Arc Consistency
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CD,DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Pop CD
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CD,DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Pop DA
Exercise: Arc Consistency
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CD,DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Pop DC, change D’s domain to (2,3). Anything else?
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CD,DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: AD
A= (1,2,3) B=(2,3), C = (1,2) D = (2,3)
Exercise: Arc Consistency
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D , D > C, B > C, D = A, B ≠ A
§ Queue: AB,AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AD,BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BA,BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: BC,CB,CD,DA,DC and all domains (1,2,3)
§ Queue: AB, CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CB,CD,DA,DC
A,C,D= (1,2,3) B=(2,3)
§ Queue: CD,DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DA,DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: DC
A,D= (1,2,3) B=(2,3), C = (1,2)
§ Queue: AD
A= (1,2,3) B=(2,3), C = (1,2) D = (2,3)
§ Final: A=(1,2) B=(2,3) C = (1,2) D = (2,3)
Forward Checking
§ When assign a variable, make all of its
neighbors arc-consistent
Backtracking with Forward Checking
§ Function Backtracking(csp) returns soln or fail
§ Return Backtrack({},csp)
§ Function Backtrack(assignment,csp) returns soln or fail
§ If assignment is complete, return assignment
§ Viß select_unassigned_var(csp)
§ For each val in order-domain-values(var,csp,assign)
§ If value is consistent with assignment
§ Add [Vi = val] to assignment
§ Make domains of all neighbors of Vi arc-consistent
with [Vi = val]
§ Result ß Backtrack(assignment,csp)
§ If Result ≠ fail, return result
§ Remove [Vi = val] from assignments
§ Return fail
Exercise: 1 Step of MAC
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D
§ Remaining domains after enforcing arc consistency on the initial
CSP with no assignments:
§ A = {2, 3}, B = {2, 3}, C = {1, 2}, D = {2, 3}
§ Assign A=2
§ Initialize arc consistency queue with arcs (Xj,Vi) of neighbors of
Vithat are unassigned variables, but can add other arcs if these
variables change domains. Vi is the variable that just got assigned,
here A=2.
Maintaining Arc Consistency (MAC)
§ Function Backtracking(csp) returns soln or fail
§ Run AC-3 (arc-consistency over all variables)
§ Return Backtrack({},csp)
§ Function Backtrack(assignment,csp) returns soln or fail
§ If assignment is complete, return assignment
§ Viß select_unassigned_var(csp)
§ For each val in order-domain-values(var,csp,assign)
§ If value is consistent with assignment
§ Add [Vi = val] to assignment
§ Run AC-3 to make all variables arc-consistent with [Vi =
val]. Initial queue is arcs (Xj,Vi) of neighbors of Vi that are
unassigned variables, but can add other arcs if these
variables change domains.
§ Result ß Backtrack(assignment,csp)
§ If Result ≠ fail, return result
§ Remove [Vi = val] from assignments
§ Return fail
Variables: A,B,C,D
Domain: {1,2,3}
Constraints: A ≠ B, A=D, C < B, C < D
Initial Domains A = {2, 3}, B = {2, 3}, C = {1, 2}, D = {2, 3}
§ Assign (A=2)
§ Queue: BA, DA
A=2, B=(2,3), C=(1,2), D=(2,3)
§ Pop BA
§ Change B’s domain to {3}, add CB to queue because B’s domain changed
§ Queue: CB, DA
A=2, B=3, C=(1,2), D=(2,3)
§ Pop CB
§ C can be either 1 or 2 and satisfies C < B, so no change to C’s domain
§ Queue: DA
A=2, B=3, C=(1,2), D=(2,3)
§ Pop DA
§ D can only be = 2 to satisfy constraint D=A, So D’s domain = 2. Add CD to
queue
§ Queue: CD
A=2, B=3, C=(1,2), D=2
§ Pop CD
§ If C = 2 cannot satisfy C < D for D’s domain 2. So C’s domain is revised to
C=1
§ Queue is empty, so done.
§ And found a complete assignment that satisfies all constraints!
Is Arc-consistency Enough?
Is Arc-consistency Enough?
§ True / False: With arc consistency, we will
never have to backtrack while solving a
CSP.
§ False: With arc consistency, we will never
have to backtrack while solving a CSP.
Limitations of Arc Consistency
Local Search for CSPs
§ After running arc
consistency:
§ So far considered search methods that
incrementally build a full valid assignment
§ Local search methods instead:
§ Can have one solution
left
§ Can have multiple
solutions left
§ Can have no solutions
left (and not know it)
§ Start with invalid full assignment
§ Make local adjustments to try to satisfy all the
constraints
What went
wrong here?
66
Local Search for CSPs
Local Search for CSPs
§ Select initial full assignment
§ Repeat until no conflicts in assignment
§ Select a variable
§ Change its value
Min Conflicts Algorithm
§ Start with an initial assignment
§ Repeat until a solution is found or
maximum number of iterations is reached:
§ Select a variable Vi randomly among the
variables in conflict
§ Set Vi to the value that minimizes the
number of constraints violated
Exercise: Min Conflicts
§ Variables: A,B,C,D
§ Domain: {1,2,3}
§ Constraints:
§ A ≠ B, A=D, C < B, C < D
§ Initial assignment: A=1, B=2, C=3, D=3
§ Run min conflicts except instead of randomly choosing
among variables with conflict, use lexicographic order.
Break ties by starting with lowest value
Exercise: Min Conflicts
§
§
§
§
§
Variables: A,B,C,D
Domain: {1,2,3}
Constraints: A ≠ B, A=D, C < B, C < D
Initial assignment: A=1, B=2, C=3, D=3
Run min conflicts except instead of randomly choosing
among variables with conflict, use lexicographic order
§ A is in conflict with D. Change to avoid conflict with D. Set
(A=3,B=2,C=3,D=3)
Exercise: Min Conflicts
§
§
§
§
§
Variables: A,B,C,D
Domain: {1,2,3}
Constraints: A ≠ B, A=D, C < B, C < D
Initial assignment: A=1, B=2, C=3, D=3
Run min conflicts except instead of randomly choosing
among variables with conflict, use lexicographic order
§ A is in conflict with D. Change to avoid conflict with D. Set
(A=3,B=2,C=3,D=3)
§ B is in conflict with C (B should be > C). But setting B = 3 makes B
in conflict with A, and does not solve conflict with C. So leave B’s
value as 2.
§ C is in conflict with B and D. Set C = 1. (A=3,B=2,C=1,D=3)
§ No more conflicts, found solution
© Copyright 2026 Paperzz