{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Constraint Satisfaction Problems (CSP) and Methods\n",
"\n",
"## What is a Constraint Satisfaction Problem?\n",
"\n",
"Defined by\n",
" * variables to which you must assign values,\n",
" * valid values to assign to each variable,\n",
" * constraints, or restrictions, on assigned values involving one or more variables\n",
" * preferences among possible assignments, making it a Constraint Optimization Problem (COP).\n",
"\n",
"Many kinds of problems can be expressed as CSPs:\n",
" * map coloring\n",
" * job-shop scheduling\n",
" * cryptarithmetic puzzles\n",
" * puzzles like Sudoku, and eight-queens\n",
" \n",
"or COPs:\n",
" * scheduling\n",
" * packet-routing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What is a Constraint Satisfaction Problem Solution Method?\n",
"\n",
"Well, it is a search!! :)\n",
"\n",
"Three approaches are described by our authors:\n",
"\n",
"- constraint propagation\n",
" - Each state is list of valid values for each variable.\n",
" - Next states are ones with different list of valid values.\n",
"\n",
"\n",
"- backtracking search\n",
" - Each state is a partial assignment---some variables have assigned values.\n",
" - Next states are ones with an additional variable assigned.\n",
"\n",
"\n",
"- local search\n",
" - Each state is a complete assignment---all variables are assigned values.\n",
" - Next states are complete assignments with value of one variable changed. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Constraint Propagation\n",
"\n",
"Map coloring problem is a nice example.\n",
"\n",
"\n",
"\n",
"What are the variables? What are the domains of values for each\n",
"variable?\n",
"\n",
"What are the constraints? No neighboring regions can have the same\n",
"color. Can be expressed in a constraint graph with\n",
" * nodes representing variables\n",
" * edges representing pairwise (binary) constraints\n",
"\n",
"\n",
"\n",
"A popular constraint propagation algorithm is AC-3. \"AC\" is for\n",
"\"arc-consistency\". 3 is for third version. It proceeds by removing\n",
"values from the domains of variables at either end of an arc to\n",
"maintain the constraints.\n",
"\n",
"A stronger version of this approach is called path-consistency (PC-2). It considers triplets of variables at a time.\n",
"\n",
"| | | WA | NT | SA | Q | NSW | V | T |\n",
"| :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | \n",
"| 1 | | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) | \n",
"| 2 | WA-NT considering SA | (r) | (g) | (b) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| | | (g) | (r) | (b) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| | | (b) | (g) | (r) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| | | (b) | (r) | (g) | (r,g,b) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| 3 | NT-SA considering Q | (r) | (g) | (b) | (r) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| | | (g) | (r) | (b) | (g) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| | | (b) | (g) | (r) | (b) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| | | (b) | (r) | (g) | (b) | (r,g,b) | (r,g,b) | (r,g,b) |\n",
"| 4 | and so on ... |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Backtracking Search\n",
"\n",
"Assign value to one variable at a time. Do depth-first search with backtracking.\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"Many ideas exist for guiding the depth-first search:\n",
" * selecting next variable to assign\n",
" * predefined order (not so good)\n",
" * MRV heuristic: variable with the minimum number of remaining values\n",
" * degree heuristic: variable with largest number of constraints with other unassigned variables\n",
" \n",
" \n",
" * selecting the next value to assign to the chosen variable\n",
" * least-constraining value heuristic: value that rules out the fewest remaining values in other variables\n",
" \n",
" \n",
" * propagate effect of an assignment, or inference\n",
" * forward checking heuristic: remove values from other variables that break constraints with the assignment just made,\n",
" * MAC heuristic: continue forward checking through other constraints affected by each new reduction in remaining values\n",
" \n",
" \n",
" * more informed backtracking\n",
" * chronological backtracking: the usual depth-first backtracking, most recent\n",
" * backjumping: backtrack up further, to variable assignment that reduced the set of values available to the variable being assigned at the node where we fail"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Local Search\n",
"\n",
"Here is the procedure, from Figure 6.8, in python and pseudo-code:\n",
"\n",
"\n",
" def min_conflicts(csp, max_steps):\n",
" assignment = dictionary indexed by variables with values randomly assigned\n",
" for i in range(max_steps):\n",
" if assignment is a solution for csp:\n",
" return assignment\n",
" conflicted_variables = set of variables involved in broken constraints\n",
" variable = one chosen randomly from conflicted_variables\n",
" value = value for variable with minimum number of conflicts\n",
" assignment[variable] = value\n",
" return 'failure'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Applications of Min-Conflicts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Map Coloring Problem\n",
"\n",
"Let's try the map-coloring problem. What is the initial state? Must\n",
"be a complete assignment. Let's just pick colors at random.\n",
"\n",
"\n",
"\n",
"Now what are the one-step neighbors of this starting state? Local\n",
"search for CSP proceeds by randomly picking a variable from ones with\n",
"assigned values that break a constraint. The new value assigned to\n",
"that variable is one that has a minimum of conflicts with other\n",
"variable assignments in this state.\n",
"\n",
"Five variables have conflicts. \n",
"Let's pick New South Wales, at random, among set of conflicted variables. Blue conflicts with two variables, red\n",
"conflicts with none, and green conflicts with one. So, set color of New South Wales to red.\n",
"\n",
"\n",
"\n",
"Now three variables have conflicts. Let's pick Victoria. For\n",
"Victoria, green has no conflicts, red and blue each have one, so pick green.\n",
"\n",
"\n",
"\n",
"Now two variables have conflicts. Pick Northern Territory. For\n",
"Northern Territory, red, green and blue each conflict with one.\n",
"Pick green randomly.\n",
"\n",
"\n",
"\n",
"Again, only two variables have conflicts. Pick Queensland. For Queensland,\n",
"red, green and blue each have one conflict. Pick red randomly.\n",
"\n",
"\n",
"Again, two variables. Pick New South Wales. For New South Wales, green,\n",
"red and blue \n",
"conflict with one. Pick green randomly.\n",
"\n",
"\n",
"\n",
"Again, two variables. Pick Victoria. For Victoria, red has no\n",
"conflicts, blue and green have one. So pick red.\n",
"\n",
"\n",
"\n",
"And, TA-DA, no conflicts remain. We have found a solution."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### N-Queens Problem Solved\n",
"\n",
"Place $n$ queens on a chess board such that no queen attacks another.\n",
"\n",
"In figure below, $h$ is the number of pairs of queens that attack each other. We want to find an arrangement of queens for which $h=0$.\n",
"\n",
"\n",
"\n",
"Can solve 8-queens problem quickly, but for large $n$,$n$-queens problems take a very long time....right?\n",
"\n",
"\n",
"Wrong! Even when $n=1,000,000$ solutions can be found in an average of 50 steps!\n",
"\n",
"Following figures are from Minton, et al.'s, paper linked below.\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given a random initial state, min-conflicts can solve an n-queens problem in almost constant time for arbitrary $n$ with high probability (such as $n=10,000,000$).\n",
"\n",
"A similar statement seems to be true for any randomly-generated CSP except in a narrow range of the ratio $R$:\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Scheduling Experiments on Hubble Space Telescope\n",
"\n",
"Scheduling a week of tasks for the Hubble Space Telescope takes about 10\n",
"minutes with local search. Previously it had taken three weeks.\n",
"\n",
"\n",
"\n",
"from [this site](https://frontierfields.org/2014/06/23/how-hubble-observations-are-scheduled/). And check out Hubble's [amazing photos and videos](https://hubblesite.org/).\n",
"\n",
"[The min-conflicts heuristic: Experimental and theoretical results](https://www.researchgate.net/publication/24322715_The_min-conflicts_heuristic_Experimental_and_theoretical_results), by Steven Minton, Andrew Philips, Mark Johnston, and Philip Laird, September 1991."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}