Problem: Assign each state and territory such that no two adjacent states have the same colour
VariablesX={NSW,VIC,QLD,WA,SA,TAS,NT}
DomainsdomX={r,g,b} for each x∈X
for each x∈X describes that each state has the domain {r,g,b}
Constraints(WA=SA)∧(WA=NT)∧(NT=QLD)∧...
VariablesX={NSW,VIC,QLD,WA,SA,TAS,NT}
DomainsdomX={r,g,b} for each x∈X
for each x∈X describes that each state has the domain {r,g,b}
Constraints(WA=SA)∧(WA=NT)∧(NT=QLD)∧...
1.2.2 - Sudoku
Sudoku is one of the most popular puzzle games in the world. The goal of Sudoku is to fill a 9x9 board with numbers so that each, row, column and 3x3 grid section contain all the digits between 1→9. Every sudoku has a unique solution that can be reached logically → Sudoku can be modelled as a CSP
Variables The values in each square on the sudoku board.
The values in each square on the Sudoku board are values; These are the values to be allocated.
Domains Integers 1 to 9
Each square on the Sudoku board can take the values 1 to 9, so the domain of all variables (squares) in Sudoku is the integers 1 to 9
Constraints
For each row, every square in the row must be unique
For each column, every square in the column must be unique
For each 3x3 grid section, every square in the 3x3 grid must be unique
1.2 - Generate-and-Test Algorithm
How do we solve Constraint Satisfaction Problems?
Generate the assignment space (a cartesian product of all domains)
dom=domV1×domV2×domV3×...×domVi
Test each assignment with the constraints
How many assignments need to be tested for n variables with domain size d?
For the graph (map) colour example before, in the worst case, 37=2187 nodes need to be expanded
This approach is basically to brute-force and try every possible combination of assignments
1.3 - Naively Apply DFS to a CSP
States are defined by the value assigned so far (partial assignments)
Initial State: the empty assignment {}
Successor Function: Assign a value to an unassigned variable (possibly left-to-right, top-to-bottom)
Goal Test: The current assignment is complete and satisfies all constraints
What can go wrong?
Can hit the worst case in a DFS approach
Amount of memory required to store all the entries increases exponentially.
DFS on CSPs → When to check constaints
In the parent node (where all the relevant / in-scope assignments are satisfied), we try to expand its children nodes with various assignments
When we try to expand these nodes with new assignments, we check the constaints
If the constraints are satisfied, continue expanding that node
Otherwise, terminate that branch of expansion and move to another child node or another branch of the search tree.
2.0 - Backtracking Algorithms
Systematically explore the dom by instantiating the variables one at a time
Evaluate each constraint predicate as soon as all its variables are bound (within scope)
Any partial assignment that doesn't satisfy the constraint can be pruned (stop expanding that partial solution)
Scheduling Example: Assignment (A=1)∧(B=1) is inconsistent with constraint A=B regardless of the value of other variables.
2.1 - Backtracking Algorithm - Graph Colouring Example
Expand all domain possibilities for WA
Expand all possibilities for the next variable (NT)
→ Not all graph colourings are possible, so prune those from the search tree.
Repeat for QLD
Repeat for SA
→ Does SA have any valid successors?
No, as we have the constraints WA=SA,NT=SA,QLD=SA which mean that there is no possible solution
If implementing this, we could have a dictionary with all of the partial assignments in it (so in the first row, there would only be a single partial assignment with the rest of the values either null or uninitialized. As we continue the search downward, the elements in the dictionary are populated).
2.2 - CSP as Graph Searching
Before performing a search on the problem space, you don't know whether there are:
1. No solutions
2. A single solution
3. Many solutions
You may need to search through the entire problem space to find out
A CSP can be solved by graph-searching with variable-ordering and fail-on-violation
Variable-ordering The order in which we assign values to the variables (given in this course)
Fail-on-violation Stop expanding when constraint is violated.
A node is an assignment of values to some of the variables
Suppose node N is the assignment X1=v1,...,Xk=vk. (A partial assignment)
Select a variable Y that isn't assigned in N (Using your favourite graph searching algorithm or the given variable ordering)
For each value yi∈dom(Y), X1=v1,...,Xk=vk,Y=Yi is a neighbour if it is consistent with the constraints
Does the partial assignment satisfy or violate any constraints?
The start node is the empty assignment
A goal node is the total assignment that satisfies the constraints
In the case where there are multiple solutions, the solution that your search algorithm arrives at first depends on your variable ordering and domain ordering (assuming that you stop when you find the first goal)
2.3 - Recursive Implementation of Backtracking Search
function backtrackingSearch(csp) returns solution/failure
return recursiveBacktracking({}, csp)
function recursiveBacktracking(assignment, csp) returns solution/failure
if assignment is complete thenreturn assignment
var <- selectUnassignedVariable(Variables[csp], assignment, csp)
for each value in orderDomainValues(var, assignment, csp) doif value is consistent with assignment given constraints[csp] then
add {var = value} to assignment
result <- recursiveBacktracking(assignment, csp)
if result != failure thenreturn result
remove {var = value} from assignment # if failurereturn failure
2.4 - Worked Example - Sudoku and Backtracking Search
The partial solution has already assigned variables to location (1,1) → 4, (1,3) → 6 etc.
The next variable to be assigned is (2,5), then (2,7) and (2,8)
For variable (2,5):
The value [1] is already in the 3x3 grid
The value [2] is already in the 3x3 grid
The value [3] is already in the 3x3 grid
The value [4] hasn't been used yet
The value [5] is already in the 3x3 grid
The value [6] is already in the 3x3 grid
The value [7] is already in the 3x3 grid
The value [8] is in the row
The value [9] hasn't been used yet
Expanding variable (2,7) given that (2, 5) = 4
Possible values [1]
Expanding variable (2,8) given that (2,5)=4 and (2,7=4)
No values work (so we backtrack until we have reached a node that has other potential solutions
Go back until (2,5) = 9. We now consider the possible values for (2,7)
[1, 4]
Suppose (2,7) = 1. Then (2,8) = 4 (and then expand the next rows).
Otherwise, if (2,7) = 4, then (2,8) = 4
3.0 - Consistency Algorithms
Algorithms that can use / exploit the additional structure and characteristics that general Search problems don't have, but CSPs have.
The idea with Consistency Algorithms is to prune the domains as much as possible before selecting values from them. A way of doing some pre-processing on the search problems to reduce the domain size.
A variable is domain consistent (or 1-consistent) if no value of the domain of the node is ruled impossible by any of the constraints
Example: Is the scheduling example domain consistent?
VariablesX={A,B,C,D,E} that represent the starting times of various activities
The scheduling example is not domain consistent as we have the constraint B=3 and 3∈domB
We can propagate this information to other unassigned variables.
3.1 - Constraint Network (as a bipartite graph)
Bipartite graph: Has two types of nodes
There is a circle-shaped node for each variable
There is a rectangular node for each constraint
There is a domain of values associated with each variable node
There is an arc form variable X to each constraint that involves X
There is only one constraint on the variable A, hence there is only one edge connected to it.
For example, two binary constraints would be represented as:
Variables {A, B, C}
Domains (not specified)
Constraintsr1(A,B)=(A<B),r2(B,C)=(B<C)
Arcs⟨A,r1(A,B)⟩,⟨B,r1(A,B)⟩,...
The term ⟨A,r1(A,B)⟩ indicates that "Variable A is connected to constraint r1"
Arcs are the edges that connect variables and edges - there are 4 arcs in the graph above.
For the scheduling example from before, the Constraint Network / Graph would be constructed as follows
We want to try to propagate restrictions that we know about in one domain to prune the domains of other variables through constraints (after applying domain consistency)
This graph is not yet arc-consistent
4.0 - Arc-Consistency
4.1 - Forward Checking
Idea Keep track of remaining legal values for unassigned variables
Terminate the search when any variable has no legal values.
This idea is embedded in "vanilla" backtracking search.
Forward checking propagates information from assigned to unassigned variables, but doesn't provide early detection for all failures.
Arc consistency is the simplest form of constraint propagation, which repeatedly enforces constraints locally.
An arc ⟨X,r(X,Yˉ)⟩ is arc consistent if, for each value x∈dom(X), there is some value yˉ∈dom(Yˉ) such that r(x,yˉ) is satisfied.
An arc ⟨X,r(X,Yˉ)⟩ is arc consistent if, for every value x of X, there is some allowed y.
A network is arc consistent if all of its arcs are arc consistent.
What if arc ⟨X,r(X,Yˉ)⟩ is not arc consistent?
All values of X in dom(X) for which there is no corresponding value in dom(Yˉ) can be deleted from dom(X) to make the arc ⟨X,r(X,Yˉ)⟩ consistent
This removal can never rule out any models - all models have arc-consistent network of constraints
This may require other variables' domains to be pruned.
4.2.1 - Arc Consistency Example - Graph Colouring
Suppose we set SA=b. As a result of the constraints defined, NSW=SA which means that NSW=b - as a result of a variable being updated, its neighbours have to be updated.
As a result of this update and the constraints provided, we note that NSW=V which means that another neighbour has to be updated.
Additionally, given that SA=b, NT=b. Since blue is the remaining graph colouring option, this means that this configuration of colours is not a solution.
Therefore, the colouring of WA=r,QLD=g is not arc-consistent.
4.2.2 - Arc Consistency Algorithm
The arcs can be considered in turn making each arc consistent.
When an arc has been made consistent, does it ever need to be checked again?
YES! An arc ⟨X,r(X,Yˉ)⟩ needs to be revisited if the domain of one of the Y's has been reduced.
Regardless of the order in which arcs are considered, we will terminate with the same result: an arc-consistent network.
Three possible outcomes when all arcs are made arc-consistent: (I.e. three answers to the question "Is there a solution?")
One domain is empty → No solution
Each domain has a single value → Unique solution
Some domains have more → There may or may not be a solution
Need to solve this new (usually simpler) CSP - the same constraints, but with reduced domains
4.2.3 - Arc Consistency Algorithm - Worst Case Complexity
The worst-case complexity of this procedure:
Let the maximum size of a variable domain be d
Let the number of constraints be e
The complexity is O(ed3)
Some special cases are faster:
If the constraint graph is a tree, the arc consistency is O(ed)
4.2.4 - Finding a Solution from an Arc-Consistent Network
After performing arc-consistency the network, we have reduced the domain of each variable. If the result is indeterminate, we can perform search
If some variable domains have more than one element, perform search (such has backtracking search)
The combination of arc consistency then backtracking search significantly speeds up the search process
We could even integrate arc-consistency into our search algorithm, either applying it when we expand nodes, or use some heuristic to determine when to use it.
Some other advanced alternatives include
Variable and arc-ordering heuristics speed up arc consistency and search (by an order of magnitude)
Split a domain, then recursively solve each part
Use conditioning (fix a variable and prune its neighbours' domains) or cutset conditioning
Cutset conditioning - instantiate, in all ways, a set of variables such that the remaining constraint graph is a tree
Many other heuristics for exploiting graph structure
4.3 - Final Note on Hard and Soft Constraints
Given a set of variables, assign a value to each variable that either:
Satisfies some set of constraints - satisfiability problems (with "hard constraints")
Minimise some cost function, where each assignment of values to variables has some cost - optimisation problems (with "soft constraints")
For soft constraint optimisation problems, value propagation algorithms are used in the same way that constraint propagation algorithms can be used.
In fact, the abstract algebra underpinning these two methods - the distributive law applied to c-semirings - is identical.
The same belief propagation algorithms use in Bayes-nets, message passing schemes used for turbo-codes and Nash propagation algorithms used in graphical games
Many problems are a mix of hard and soft constraints (called constraint optimisation problems)