1.0 - Constraint Satisfaction Problems

1.1 - Constraint Satisfaction Problems - Definition

A constraint satisfaction problem is given by:


There are also constraint optimisation problems, in which there is a function that gives a cost for each assignment of a value to each variable

1.2 - Example - Scheduling Activities

1.2.1 - Graph Colouring

Problem: Assign each state and territory such that no two adjacent states have the same colour

Variables X={NSW,VIC,QLD,WA,SA,TAS,NT}X=\{NSW, VIC, QLD, WA, SA, TAS, NT\}

Domains domX={r,g,b} for each xXdom_X=\{r,g,b\} \text{ for each } x\in X

Constraints (WASA)(WANT)(NTQLD)...(WA\ne SA)\wedge(WA\ne NT)\wedge(NT\ne QLD)\wedge...

Variables X={NSW,VIC,QLD,WA,SA,TAS,NT}X=\{NSW, VIC, QLD, WA, SA, TAS, NT\}

Domains domX={r,g,b} for each xXdom_X=\{r,g,b\} \text{ for each } x\in X

Constraints (WASA)(WANT)(NTQLD)...(WA\ne SA)\wedge(WA\ne NT)\wedge(NT\ne QLD)\wedge...

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?

1.3 - Naively Apply DFS to a CSP

What can go wrong?

DFS on CSPs → When to check constaints

2.0 - Backtracking Algorithms

Scheduling Example: Assignment (A=1)(B=1)(A=1)\wedge(B=1) is inconsistent with constraint ABA\ne B regardless of the value of other variables.

2.1 - Backtracking Algorithm - Graph Colouring Example

  1. Expand all domain possibilities for WA

  2. Expand all possibilities for the next variable (NT) → Not all graph colourings are possible, so prune those from the search tree.

  3. Repeat for QLD

  4. Repeat for SA

    → Does SA have any valid successors?

    No, as we have the constraints WASA,NTSA,QLDSAWA\ne SA, NT\ne SA, QLD\ne SA which mean that there is no possible solution

Figure 1 - Graph Representation of State Colouring Problem
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
function backtrackingSearch(csp) returns solution/failure
	return recursiveBacktracking({}, csp)

function recursiveBacktracking(assignment, csp) returns solution/failure
	if assignment is complete then return assignment
	var <- selectUnassignedVariable(Variables[csp], assignment, csp)
	for each value in orderDomainValues(var, assignment, csp) do
			if value is consistent with assignment given constraints[csp] then
					add {var = value} to assignment
					result <- recursiveBacktracking(assignment, csp)
					if result != failure then return result
					remove {var = value} from assignment # if failure
	return failure
Figure 2 - Sudoku Backtracking Search Example. A partially filled Sudoku board.

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

  1. The value [1] is already in the 3x3 grid
  2. The value [2] is already in the 3x3 grid
  3. The value [3] is already in the 3x3 grid
  4. The value [4] hasn't been used yet
  5. The value [5] is already in the 3x3 grid
  6. The value [6] is already in the 3x3 grid
  7. The value [7] is already in the 3x3 grid
  8. The value [8] is in the row
  9. 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)

Go back until (2,5) = 9. We now consider the possible values for (2,7)

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.

3.1 - Constraint Network (as a bipartite graph)

4.0 - Arc-Consistency

4.1 - Forward Checking

Figure 4 - Arc Consistency Forward Checking visualiastion

4.2 - Arc Consistency

4.2.1 - Arc Consistency Example - Graph Colouring

  1. Suppose we set SA=bSA=b. As a result of the constraints defined, NSWSANSW\ne SA which means that NSWbNSW\ne b - as a result of a variable being updated, its neighbours have to be updated.

  2. As a result of this update and the constraints provided, we note that NSWVNSW\ne V which means that another neighbour has to be updated.

  3. Additionally, given that SA=bSA=b, NTbNT\ne 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=gWA=r, QLD=g is not arc-consistent.

4.2.2 - Arc Consistency Algorithm

4.2.3 - Arc Consistency Algorithm - Worst Case Complexity

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

4.3 - Final Note on Hard and Soft Constraints