Skip to content

Commit 23e64aa

Browse files
nvinayvarma189norvig
authored andcommitted
Fixed errors occurred in search.ipynb due to refactoring (#902)
* refactored changes * added DLS and IDS to readme
1 parent dbcc989 commit 23e64aa

File tree

2 files changed

+74
-32
lines changed

2 files changed

+74
-32
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
7474
| 3.7 | Graph-Search | `graph_search` | [`search.py`][search] | Done | |
7575
| 3.11 | Breadth-First-Search | `breadth_first_graph_search` | [`search.py`][search] | Done | Included |
7676
| 3.14 | Uniform-Cost-Search | `uniform_cost_search` | [`search.py`][search] | Done | Included |
77-
| 3.17 | Depth-Limited-Search | `depth_limited_search` | [`search.py`][search] | Done | |
78-
| 3.18 | Iterative-Deepening-Search | `iterative_deepening_search` | [`search.py`][search] | Done | |
77+
| 3.17 | Depth-Limited-Search | `depth_limited_search` | [`search.py`][search] | Done | Included |
78+
| 3.18 | Iterative-Deepening-Search | `iterative_deepening_search` | [`search.py`][search] | Done | Included |
7979
| 3.22 | Best-First-Search | `best_first_graph_search` | [`search.py`][search] | Done | Included |
8080
| 3.24 | A\*-Search | `astar_search` | [`search.py`][search] | Done | Included |
8181
| 3.26 | Recursive-Best-First-Search | `recursive_best_first_search` | [`search.py`][search] | Done | |
@@ -186,4 +186,4 @@ Many thanks for contributions over the years. I got bug reports, corrected code,
186186
[rl]:../master/rl.py
187187
[search]:../master/search.py
188188
[utils]:../master/utils.py
189-
[text]:../master/text.py
189+
[text]:../master/text.py

search.ipynb

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@
11321132
},
11331133
"outputs": [],
11341134
"source": [
1135-
"def tree_search_for_vis(problem, frontier):\n",
1135+
"def tree_breadth_search_for_vis(problem):\n",
11361136
" \"\"\"Search through the successors of a problem to find a goal.\n",
11371137
" The argument frontier should be an empty queue.\n",
11381138
" Don't worry about repeated paths to a state. [Figure 3.7]\"\"\"\n",
@@ -1143,15 +1143,15 @@
11431143
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
11441144
" \n",
11451145
" #Adding first node to the queue\n",
1146-
" frontier.append(Node(problem.initial))\n",
1146+
" frontier = deque([Node(problem.initial)])\n",
11471147
" \n",
11481148
" node_colors[Node(problem.initial).state] = \"orange\"\n",
11491149
" iterations += 1\n",
11501150
" all_node_colors.append(dict(node_colors))\n",
11511151
" \n",
11521152
" while frontier:\n",
11531153
" #Popping first node of queue\n",
1154-
" node = frontier.pop()\n",
1154+
" node = frontier.popleft()\n",
11551155
" \n",
11561156
" # modify the currently searching node to red\n",
11571157
" node_colors[node.state] = \"red\"\n",
@@ -1179,9 +1179,9 @@
11791179
" \n",
11801180
" return None\n",
11811181
"\n",
1182-
"def breadth_first_tree_search_(problem):\n",
1182+
"def breadth_first_tree_search(problem):\n",
11831183
" \"Search the shallowest nodes in the search tree first.\"\n",
1184-
" iterations, all_node_colors, node = tree_search_for_vis(problem, FIFOQueue())\n",
1184+
" iterations, all_node_colors, node = tree_breadth_search_for_vis(problem)\n",
11851185
" return(iterations, all_node_colors, node)"
11861186
]
11871187
},
@@ -1199,10 +1199,10 @@
11991199
"outputs": [],
12001200
"source": [
12011201
"all_node_colors = []\n",
1202-
"romania_problem = GraphProblem('Arad', 'Fagaras', romania_map)\n",
1203-
"a, b, c = breadth_first_tree_search_(romania_problem)\n",
1202+
"romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",
1203+
"a, b, c = breadth_first_tree_search(romania_problem)\n",
12041204
"display_visual(romania_graph_data, user_input=False, \n",
1205-
" algorithm=breadth_first_tree_search_, \n",
1205+
" algorithm=breadth_first_tree_search, \n",
12061206
" problem=romania_problem)"
12071207
]
12081208
},
@@ -1222,11 +1222,56 @@
12221222
},
12231223
"outputs": [],
12241224
"source": [
1225-
"def depth_first_tree_search_graph(problem):\n",
1225+
"def tree_depth_search_for_vis(problem):\n",
1226+
" \"\"\"Search through the successors of a problem to find a goal.\n",
1227+
" The argument frontier should be an empty queue.\n",
1228+
" Don't worry about repeated paths to a state. [Figure 3.7]\"\"\"\n",
1229+
" \n",
1230+
" # we use these two variables at the time of visualisations\n",
1231+
" iterations = 0\n",
1232+
" all_node_colors = []\n",
1233+
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
1234+
" \n",
1235+
" #Adding first node to the stack\n",
1236+
" frontier = [Node(problem.initial)]\n",
1237+
" \n",
1238+
" node_colors[Node(problem.initial).state] = \"orange\"\n",
1239+
" iterations += 1\n",
1240+
" all_node_colors.append(dict(node_colors))\n",
1241+
" \n",
1242+
" while frontier:\n",
1243+
" #Popping first node of stack\n",
1244+
" node = frontier.pop()\n",
1245+
" \n",
1246+
" # modify the currently searching node to red\n",
1247+
" node_colors[node.state] = \"red\"\n",
1248+
" iterations += 1\n",
1249+
" all_node_colors.append(dict(node_colors))\n",
1250+
" \n",
1251+
" if problem.goal_test(node.state):\n",
1252+
" # modify goal node to green after reaching the goal\n",
1253+
" node_colors[node.state] = \"green\"\n",
1254+
" iterations += 1\n",
1255+
" all_node_colors.append(dict(node_colors))\n",
1256+
" return(iterations, all_node_colors, node)\n",
1257+
" \n",
1258+
" frontier.extend(node.expand(problem))\n",
1259+
" \n",
1260+
" for n in node.expand(problem):\n",
1261+
" node_colors[n.state] = \"orange\"\n",
1262+
" iterations += 1\n",
1263+
" all_node_colors.append(dict(node_colors))\n",
1264+
"\n",
1265+
" # modify the color of explored nodes to gray\n",
1266+
" node_colors[node.state] = \"gray\"\n",
1267+
" iterations += 1\n",
1268+
" all_node_colors.append(dict(node_colors))\n",
1269+
" \n",
1270+
" return None\n",
1271+
"\n",
1272+
"def depth_first_tree_search(problem):\n",
12261273
" \"Search the deepest nodes in the search tree first.\"\n",
1227-
" # This algorithm might not work in case of repeated paths\n",
1228-
" # and may run into an infinite while loop.\n",
1229-
" iterations, all_node_colors, node = tree_search_for_vis(problem, Stack())\n",
1274+
" iterations, all_node_colors, node = tree_depth_search_for_vis(problem)\n",
12301275
" return(iterations, all_node_colors, node)"
12311276
]
12321277
},
@@ -1237,9 +1282,9 @@
12371282
"outputs": [],
12381283
"source": [
12391284
"all_node_colors = []\n",
1240-
"romania_problem = GraphProblem('Arad', 'Oradea', romania_map)\n",
1285+
"romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",
12411286
"display_visual(romania_graph_data, user_input=False, \n",
1242-
" algorithm=depth_first_tree_search_graph, \n",
1287+
" algorithm=depth_first_tree_search, \n",
12431288
" problem=romania_problem)"
12441289
]
12451290
},
@@ -1262,7 +1307,7 @@
12621307
},
12631308
"outputs": [],
12641309
"source": [
1265-
"def breadth_first_search_graph(problem):\n",
1310+
"def breadth_first_search_graph(problem):\n",
12661311
" \"[Figure 3.11]\"\n",
12671312
" \n",
12681313
" # we use these two variables at the time of visualisations\n",
@@ -1282,8 +1327,7 @@
12821327
" all_node_colors.append(dict(node_colors))\n",
12831328
" return(iterations, all_node_colors, node)\n",
12841329
" \n",
1285-
" frontier = FIFOQueue()\n",
1286-
" frontier.append(node)\n",
1330+
" frontier = deque([node])\n",
12871331
" \n",
12881332
" # modify the color of frontier nodes to blue\n",
12891333
" node_colors[node.state] = \"orange\"\n",
@@ -1292,7 +1336,7 @@
12921336
" \n",
12931337
" explored = set()\n",
12941338
" while frontier:\n",
1295-
" node = frontier.pop()\n",
1339+
" node = frontier.popleft()\n",
12961340
" node_colors[node.state] = \"red\"\n",
12971341
" iterations += 1\n",
12981342
" all_node_colors.append(dict(node_colors))\n",
@@ -1315,8 +1359,7 @@
13151359
" node_colors[node.state] = \"gray\"\n",
13161360
" iterations += 1\n",
13171361
" all_node_colors.append(dict(node_colors))\n",
1318-
" return None"
1319-
]
1362+
" return None" ]
13201363
},
13211364
{
13221365
"cell_type": "code",
@@ -1346,8 +1389,7 @@
13461389
"collapsed": true
13471390
},
13481391
"outputs": [],
1349-
"source": [
1350-
"def graph_search_for_vis(problem, frontier):\n",
1392+
"source": [ "def graph_search_for_vis(problem):\n",
13511393
" \"\"\"Search through the successors of a problem to find a goal.\n",
13521394
" The argument frontier should be an empty queue.\n",
13531395
" If two paths reach a state, only use the first one. [Figure 3.7]\"\"\"\n",
@@ -1356,7 +1398,7 @@
13561398
" all_node_colors = []\n",
13571399
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
13581400
" \n",
1359-
" frontier.append(Node(problem.initial))\n",
1401+
" frontier = [(Node(problem.initial))]\n",
13601402
" explored = set()\n",
13611403
" \n",
13621404
" # modify the color of frontier nodes to orange\n",
@@ -1365,7 +1407,7 @@
13651407
" all_node_colors.append(dict(node_colors))\n",
13661408
" \n",
13671409
" while frontier:\n",
1368-
" # Popping first node of queue\n",
1410+
" # Popping first node of stack\n",
13691411
" node = frontier.pop()\n",
13701412
" \n",
13711413
" # modify the currently searching node to red\n",
@@ -1401,7 +1443,7 @@
14011443
"\n",
14021444
"def depth_first_graph_search(problem):\n",
14031445
" \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n",
1404-
" iterations, all_node_colors, node = graph_search_for_vis(problem, Stack())\n",
1446+
" iterations, all_node_colors, node = graph_search_for_vis(problem)\n",
14051447
" return(iterations, all_node_colors, node)"
14061448
]
14071449
},
@@ -1462,7 +1504,7 @@
14621504
" all_node_colors.append(dict(node_colors))\n",
14631505
" return(iterations, all_node_colors, node)\n",
14641506
" \n",
1465-
" frontier = PriorityQueue(min, f)\n",
1507+
" frontier = PriorityQueue('min', f)\n",
14661508
" frontier.append(node)\n",
14671509
" \n",
14681510
" node_colors[node.state] = \"orange\"\n",
@@ -1558,7 +1600,7 @@
15581600
"metadata": {},
15591601
"outputs": [],
15601602
"source": [
1561-
"def depth_limited_search(problem, frontier, limit = -1):\n",
1603+
"def depth_limited_search(problem, limit = -1):\n",
15621604
" '''\n",
15631605
" Perform depth first search of graph g.\n",
15641606
" if limit >= 0, that is the maximum depth of the search.\n",
@@ -1568,7 +1610,7 @@
15681610
" all_node_colors = []\n",
15691611
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
15701612
" \n",
1571-
" frontier.append(Node(problem.initial))\n",
1613+
" frontier = [Node(problem.initial)]\n",
15721614
" explored = set()\n",
15731615
" \n",
15741616
" cutoff_occurred = False\n",
@@ -1622,7 +1664,7 @@
16221664
"\n",
16231665
"def depth_limited_search_for_vis(problem):\n",
16241666
" \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n",
1625-
" iterations, all_node_colors, node = depth_limited_search(problem, Stack())\n",
1667+
" iterations, all_node_colors, node = depth_limited_search(problem)\n",
16261668
" return(iterations, all_node_colors, node) "
16271669
]
16281670
},

0 commit comments

Comments
 (0)