Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0e53a8

Browse files
Review & Graph Coloring
1 parent ad73cdb commit d0e53a8

File tree

1 file changed

+113
-3
lines changed

1 file changed

+113
-3
lines changed

csp.ipynb

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,120 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": false
7+
},
8+
"source": [
9+
"# Constraint Satisfaction Problems (CSPs)\n",
10+
"\n",
11+
"This IPy notebook acts as supporting material for topics covered in **Chapter 6 Constraint Satisfaction Problems** of the book* Artificial Intelligence: A Modern Approach*. We make use of the implementations in **csp.py** module. Even though this notebook includes a brief summary of the main topics familiarity with the material present in the book is expected. We will look at some visualizations and solve some of the CSP problems described in the book. Let us import everything from the csp module to get started."
12+
]
13+
},
314
{
415
"cell_type": "code",
5-
"execution_count": null,
16+
"execution_count": 1,
17+
"metadata": {
18+
"collapsed": true
19+
},
20+
"outputs": [],
21+
"source": [
22+
"from csp import *"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"## Review\n",
30+
"\n",
31+
"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"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 2,
37+
"metadata": {
38+
"collapsed": true
39+
},
40+
"outputs": [],
41+
"source": [
42+
"%psource CSP"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"The __ _ _init_ _ __ method parameters specify the CSP. Variable can be passed as a list of strings or integers. Domains are passed as dict where key specify the variables and value specify the domains. The variables are passed as an empty list. Variables are extracted from the keys of the domain dictionary. Neighbor is a dict of variables that essentially describes the constraint graph. Here each variable key has a list its value which are the variables that are constraint along with it. The constraint parameter should be a function **f(A, a, B, b**) that **returns true** if neighbors A, B **satisfy the constraint** when they have values **A=a, B=b**. We have additional parameters like nassings which is incremented each time an assignment is made when calling the assign method. You can read more about the methods and parameters in the class doc string. We will talk more about them as we encounter their use. Let us jump to an example."
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"## Graph Coloring\n",
57+
"\n",
58+
"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."
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 4,
664
"metadata": {
765
"collapsed": false
866
},
67+
"outputs": [
68+
{
69+
"data": {
70+
"text/plain": [
71+
"['R', 'G', 'B']"
72+
]
73+
},
74+
"execution_count": 4,
75+
"metadata": {},
76+
"output_type": "execute_result"
77+
}
78+
],
79+
"source": [
80+
"s = UniversalDict(['R','G','B'])\n",
81+
"s[5]"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"For our CSP we also need to define a constraint function **f(A, a, B, b)**. In this what we need is that the neighbors must not have the same color. This is defined in the function **different_values_constraint** of the module."
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 5,
94+
"metadata": {
95+
"collapsed": true
96+
},
97+
"outputs": [],
98+
"source": [
99+
"%psource different_values_constraint"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"The CSP class takes neighbors in the form of a Dict. The module specifies a simple helper function named **parse_neighbors** which allows to take input in the form of strings and return a Dict of the form compatible with the **CSP Class**."
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 6,
112+
"metadata": {
113+
"collapsed": true
114+
},
9115
"outputs": [],
10116
"source": [
11-
"import csp"
117+
"%pdoc parse_neighbors"
12118
]
13119
},
14120
{
@@ -37,7 +143,11 @@
37143
"name": "python",
38144
"nbconvert_exporter": "python",
39145
"pygments_lexer": "ipython3",
40-
"version": "3.5.1"
146+
"version": "3.4.3"
147+
},
148+
"widgets": {
149+
"state": {},
150+
"version": "1.1.1"
41151
}
42152
},
43153
"nbformat": 4,

0 commit comments

Comments
 (0)