Constrain Satisfactory Problems (CSPs)
CSPs use a standard structure and a simple representation for states and the goal test. Search algorithms can then exploit that structure and use general-purpose heuristics instead of problem-specific ones
See the algorithm using an example:
Suppose you have to color this map using four colors; Red, Green, Yellow and Blue, such that no two regions sharing a common boundary are colored in the same color.
Formulate the problem
First need to Define variables and Domain for each variable. Domain can be different from one variable to the other depend on constraints.
| Component | General rule | Map example |
|---|---|---|
| Variables | One variable per thing that needs a value | |
| Domains | The allowed values for each variable | |
| Constraints | The legal value combinations for subsets of variables | for every pair sharing a border (14 constraints) |
| Degree - No. of Constraints present for a given Variable |
Constraints
Region Color Neighbour Color
| Region | Neighbours |
|---|---|
| MT | ID, WY, ND, SD |
| ND | MT, SD |
| SD | MT, ND, WY, NE |
| NE | SD, WY, CO |
| WY | MT, ID, UT, CO, SD, NE |
| ID | MT, WY, UT |
| UT | ID, WY, CO |
| CO | UT, WY, NE |
To make things easier draw the constraints graph
- Edges - Constraints
- Nodes - Variables
Judge any assignment
- An assignment is consistent (legal) if it violates no constraint.
- An assignment is complete if every variable has a value.
- A solution is a complete and consistent assignment.
Complete solution:
Choose the variable
- MRV (Minimum Remaining Variable): pick the variable with the fewest legal values left in domain.
- Degree (tie-break): among ties, pick the one with the most constraints on unassigned variables.
| Region | WY | MT | SD | NE | ID | UT | CO | ND |
|---|---|---|---|---|---|---|---|---|
| Degree | 6 | 4 | 4 | 3 | 3 | 3 | 3 | 2 |
In this case, all domains have 4 values at the start. so Degree will have to do the tie breaking.
Choose the Color
LCV (Least Constrained Value): try first the value that rules out the fewest values in neighbouring unassigned variables.
Example, after WY = Blue, assigning MT = Green removes only Green from ID, ND and SD. Any other colour would remove that colour from the same neighbours, so the options tie and any is fine. LCV matters when neighbours' domains already differ.
Infer after each assignment
- Forward checking: after , remove from the domains of 's unassigned neighbours.
- Empty domain? Stop and backtrack now. (If domain is empty for a variable, the solution is not complete)
- AC-3 (stronger): if pruning leaves a neighbour with one value, propagate that removal to its own neighbours.
Backtrack
When a variable has no legal value left, undo the most recent assignment, try its next value, and if none remain, undo the one before it.