|
28 | 28 | "cell_type": "markdown",
|
29 | 29 | "metadata": {},
|
30 | 30 | "source": [
|
31 |
| - "## Review\n", |
| 31 | + "## CONTENTS\n", |
32 | 32 | "\n",
|
33 |
| - "CSPs are a special kind of search problems. Here we don't treat the space as a black box but the state has a particular form and we use that to our advantage to tweak our algorithms to be more suited to the problems. A CSP State is defined by a set of variables which can take values from corresponding domains. These variables can take only certain values in their domains to satisfy the constraints. A set of assignments which satisfies all constraints passes the goal test. Let us start by exploring the CSP class which we will use to model our CSPs. You can keep the popup open and read the main page to get a better idea of the code.\n" |
| 33 | + "* Overview\n", |
| 34 | + "* Graph Coloring\n", |
| 35 | + "* N-Queens\n", |
| 36 | + "* Backtracking Search\n", |
| 37 | + "* Tree CSP Solver\n", |
| 38 | + "* Graph Coloring Visualization\n", |
| 39 | + "* N-Queens Visualization" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "markdown", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "## OVERVIEW\n", |
| 47 | + "\n", |
| 48 | + "CSPs are a special kind of search problems. Here we don't treat the space as a black box but the state has a particular form and we use that to our advantage to tweak our algorithms to be more suited to the problems. A CSP State is defined by a set of variables which can take values from corresponding domains. These variables can take only certain values in their domains to satisfy the constraints. A set of assignments which satisfies all constraints passes the goal test. Let us start by exploring the CSP class which we will use to model our CSPs. You can keep the popup open and read the main page to get a better idea of the code." |
34 | 49 | ]
|
35 | 50 | },
|
36 | 51 | {
|
|
55 | 70 | "cell_type": "markdown",
|
56 | 71 | "metadata": {},
|
57 | 72 | "source": [
|
58 |
| - "## Graph Coloring\n", |
| 73 | + "## GRAPH COLORING\n", |
59 | 74 | "\n",
|
60 | 75 | "We use the graph coloring problem as our running example for demonstrating the different algorithms in the **csp module**. The idea of map coloring problem is that the adjacent nodes (those connected by edges) should not have the same color throughout the graph. The graph can be colored using a fixed number of colors. Here each node is a variable and the values are the colors that can be assigned to them. Given that the domain will be the same for all our nodes we use a custom dict defined by the **UniversalDict** class. The **UniversalDict** Class takes in a parameter which it returns as value for all the keys of the dict. It is very similar to **defaultdict** in Python except that it does not support item assignment."
|
61 | 76 | ]
|
|
161 | 176 | "cell_type": "markdown",
|
162 | 177 | "metadata": {},
|
163 | 178 | "source": [
|
164 |
| - "## NQueens\n", |
| 179 | + "## N-QUEENS\n", |
165 | 180 | "\n",
|
166 | 181 | "The N-queens puzzle is the problem of placing N chess queens on a N×N chessboard so that no two queens threaten each other. Here N is a natural number. Like the graph coloring, problem NQueens is also implemented in the csp module. The **NQueensCSP** class inherits from the **CSP** class. It makes some modifications in the methods to suit the particular problem. The queens are assumed to be placed one per column, from left to right. That means position (x, y) represents (var, val) in the CSP. The constraint that needs to be passed on the CSP is defined in the **queen_constraint** function. The constraint is satisfied (true) if A, B are really the same variable, or if they are not in the same row, down diagonal, or up diagonal. "
|
167 | 182 | ]
|
|
338 | 353 | "cell_type": "markdown",
|
339 | 354 | "metadata": {},
|
340 | 355 | "source": [
|
341 |
| - "# Backtracking Search\n", |
| 356 | + "## BACKTRACKING SEARCH\n", |
342 | 357 | "\n",
|
343 |
| - "For solving a CSP the main issue with Naive search algorithms is that they can continue expanding obviously wrong paths. In backtracking search, we check constraints as we go. Backtracking is just the above idea combined with the fact that we are dealing with one variable at a time. Backtracking Search is implemented in the repository as the function **backtracking_search**. This is the same as **Figure 6.5** in the book. The function takes as input a CSP and few other optional parameters which can be used to further speed it up. The function returns the correct assignment if it satisfies the goal. We will discuss these later. Let us solve our **coloring_problem1** with **backtracking_search**.\n" |
| 358 | + "For solving a CSP the main issue with Naive search algorithms is that they can continue expanding obviously wrong paths. In backtracking search, we check constraints as we go. Backtracking is just the above idea combined with the fact that we are dealing with one variable at a time. Backtracking Search is implemented in the repository as the function **backtracking_search**. This is the same as **Figure 6.5** in the book. The function takes as input a CSP and few other optional parameters which can be used to further speed it up. The function returns the correct assignment if it satisfies the goal. We will discuss these later. Let us solve our **coloring_problem1** with **backtracking_search**." |
344 | 359 | ]
|
345 | 360 | },
|
346 | 361 | {
|
|
647 | 662 | "cell_type": "markdown",
|
648 | 663 | "metadata": {},
|
649 | 664 | "source": [
|
650 |
| - "## Tree CSP Solver\n", |
| 665 | + "## TREE CSP SOLVER\n", |
651 | 666 | "\n",
|
652 | 667 | "The `tree_csp_solver` function (**Figure 6.11** in the book) can be used to solve problems whose constraint graph is a tree. Given a CSP, with `neighbors` forming a tree, it returns an assignement that satisfies the given constraints. The algorithm works as follows:\n",
|
653 | 668 | "\n",
|
|
732 | 747 | "cell_type": "markdown",
|
733 | 748 | "metadata": {},
|
734 | 749 | "source": [
|
735 |
| - "## Graph Coloring Visualization\n", |
| 750 | + "## GRAPH COLORING VISUALIZATION\n", |
736 | 751 | "\n",
|
737 | 752 | "Next, we define some functions to create the visualisation from the assignment_history of **coloring_problem1**. The reader need not concern himself with the code that immediately follows as it is the usage of Matplotib with IPython Widgets. If you are interested in reading more about these visit [ipywidgets.readthedocs.io](http://ipywidgets.readthedocs.io). We will be using the **networkx** library to generate graphs. These graphs can be treated as the graph that needs to be colored or as a constraint graph for this problem. If interested you can read a dead simple tutorial [here](https://www.udacity.com/wiki/creating-network-graphs-with-python). We start by importing the necessary libraries and initializing matplotlib inline.\n"
|
738 | 753 | ]
|
|
911 | 926 | "cell_type": "markdown",
|
912 | 927 | "metadata": {},
|
913 | 928 | "source": [
|
914 |
| - "## NQueens Visualization\n", |
| 929 | + "## N-QUEENS VISUALIZATION\n", |
915 | 930 | "\n",
|
916 | 931 | "Just like the Graph Coloring Problem we will start with defining a few helper functions to help us visualize the assignments as they evolve over time. The **make_plot_board_step_function** behaves similar to the **make_update_step_function** introduced earlier. It initializes a chess board in the form of a 2D grid with alternating 0s and 1s. This is used by **plot_board_step** function which draws the board using matplotlib and adds queens to it. This function also calls the **label_queen_conflicts** which modifies the grid placing 3 in positions in a position where there is a conflict."
|
917 | 932 | ]
|
|
0 commit comments