|
41 | 41 | "* Breadth-First Search\n",
|
42 | 42 | "* Uniform Cost Search\n",
|
43 | 43 | "* A\\* Search\n",
|
| 44 | + "* Best First Search\n", |
44 | 45 | "* Genetic Algorithm"
|
45 | 46 | ]
|
46 | 47 | },
|
|
447 | 448 | "2. Depth First Tree Search - Implemented\n",
|
448 | 449 | "3. Depth First Graph Search - Implemented\n",
|
449 | 450 | "4. Breadth First Search - Implemented\n",
|
450 |
| - "5. Best First Graph Search\n", |
| 451 | + "5. Best First Graph Search - Implemented\n", |
451 | 452 | "6. Uniform Cost Search - Implemented\n",
|
452 | 453 | "7. Depth Limited Search\n",
|
453 | 454 | "8. Iterative Deepening Search\n",
|
|
1190 | 1191 | ],
|
1191 | 1192 | "source": [
|
1192 | 1193 | "all_node_colors = []\n",
|
1193 |
| - "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", |
| 1194 | + "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", |
1194 | 1195 | "display_visual(user_input = False, algorithm = astar_search, problem = romania_problem)"
|
1195 | 1196 | ]
|
1196 | 1197 | },
|
|
1253 | 1254 | "display_visual(user_input = True)"
|
1254 | 1255 | ]
|
1255 | 1256 | },
|
| 1257 | + { |
| 1258 | + "cell_type": "markdown", |
| 1259 | + "metadata": {}, |
| 1260 | + "source": [ |
| 1261 | + "## BEST FIRST SEARCH\n", |
| 1262 | + "Let's change all the node_colors to starting position and define a different problem statement." |
| 1263 | + ] |
| 1264 | + }, |
| 1265 | + { |
| 1266 | + "cell_type": "code", |
| 1267 | + "execution_count": 23, |
| 1268 | + "metadata": {}, |
| 1269 | + "outputs": [], |
| 1270 | + "source": [ |
| 1271 | + "def best_first_graph_search(problem, f):\n", |
| 1272 | + " \"\"\"Search the nodes with the lowest f scores first.\n", |
| 1273 | + " You specify the function f(node) that you want to minimize; for example,\n", |
| 1274 | + " if f is a heuristic estimate to the goal, then we have greedy best\n", |
| 1275 | + " first search; if f is node.depth then we have breadth-first search.\n", |
| 1276 | + " There is a subtlety: the line \"f = memoize(f, 'f')\" means that the f\n", |
| 1277 | + " values will be cached on the nodes as they are computed. So after doing\n", |
| 1278 | + " a best first search you can examine the f values of the path returned.\"\"\"\n", |
| 1279 | + " \n", |
| 1280 | + " # we use these two variables at the time of visualisations\n", |
| 1281 | + " iterations = 0\n", |
| 1282 | + " all_node_colors = []\n", |
| 1283 | + " node_colors = dict(initial_node_colors)\n", |
| 1284 | + " \n", |
| 1285 | + " f = memoize(f, 'f')\n", |
| 1286 | + " node = Node(problem.initial)\n", |
| 1287 | + " \n", |
| 1288 | + " node_colors[node.state] = \"red\"\n", |
| 1289 | + " iterations += 1\n", |
| 1290 | + " all_node_colors.append(dict(node_colors))\n", |
| 1291 | + " \n", |
| 1292 | + " if problem.goal_test(node.state):\n", |
| 1293 | + " node_colors[node.state] = \"green\"\n", |
| 1294 | + " iterations += 1\n", |
| 1295 | + " all_node_colors.append(dict(node_colors))\n", |
| 1296 | + " return(iterations, all_node_colors, node)\n", |
| 1297 | + " \n", |
| 1298 | + " frontier = PriorityQueue(min, f)\n", |
| 1299 | + " frontier.append(node)\n", |
| 1300 | + " \n", |
| 1301 | + " node_colors[node.state] = \"orange\"\n", |
| 1302 | + " iterations += 1\n", |
| 1303 | + " all_node_colors.append(dict(node_colors))\n", |
| 1304 | + " \n", |
| 1305 | + " explored = set()\n", |
| 1306 | + " while frontier:\n", |
| 1307 | + " node = frontier.pop()\n", |
| 1308 | + " \n", |
| 1309 | + " node_colors[node.state] = \"red\"\n", |
| 1310 | + " iterations += 1\n", |
| 1311 | + " all_node_colors.append(dict(node_colors))\n", |
| 1312 | + " \n", |
| 1313 | + " if problem.goal_test(node.state):\n", |
| 1314 | + " node_colors[node.state] = \"green\"\n", |
| 1315 | + " iterations += 1\n", |
| 1316 | + " all_node_colors.append(dict(node_colors))\n", |
| 1317 | + " return(iterations, all_node_colors, node)\n", |
| 1318 | + " \n", |
| 1319 | + " explored.add(node.state)\n", |
| 1320 | + " for child in node.expand(problem):\n", |
| 1321 | + " if child.state not in explored and child not in frontier:\n", |
| 1322 | + " frontier.append(child)\n", |
| 1323 | + " node_colors[child.state] = \"orange\"\n", |
| 1324 | + " iterations += 1\n", |
| 1325 | + " all_node_colors.append(dict(node_colors))\n", |
| 1326 | + " elif child in frontier:\n", |
| 1327 | + " incumbent = frontier[child]\n", |
| 1328 | + " if f(child) < f(incumbent):\n", |
| 1329 | + " del frontier[incumbent]\n", |
| 1330 | + " frontier.append(child)\n", |
| 1331 | + " node_colors[child.state] = \"orange\"\n", |
| 1332 | + " iterations += 1\n", |
| 1333 | + " all_node_colors.append(dict(node_colors))\n", |
| 1334 | + "\n", |
| 1335 | + " node_colors[node.state] = \"gray\"\n", |
| 1336 | + " iterations += 1\n", |
| 1337 | + " all_node_colors.append(dict(node_colors))\n", |
| 1338 | + " return None\n", |
| 1339 | + "\n", |
| 1340 | + "def best_first_search(problem, h=None):\n", |
| 1341 | + " \"\"\"Best-first graph search is an informative searching algorithm with f(n) = h(n).\n", |
| 1342 | + " You need to specify the h function when you call best_first_search, or\n", |
| 1343 | + " else in your Problem subclass.\"\"\"\n", |
| 1344 | + " h = memoize(h or problem.h, 'h')\n", |
| 1345 | + " iterations, all_node_colors, node = best_first_graph_search(problem, lambda n: h(n))\n", |
| 1346 | + " return(iterations, all_node_colors, node)" |
| 1347 | + ] |
| 1348 | + }, |
| 1349 | + { |
| 1350 | + "cell_type": "code", |
| 1351 | + "execution_count": 39, |
| 1352 | + "metadata": {}, |
| 1353 | + "outputs": [ |
| 1354 | + { |
| 1355 | + "data": { |
| 1356 | + "application/vnd.jupyter.widget-view+json": { |
| 1357 | + "model_id": "5ae2d521b74743afa988c462a851c269" |
| 1358 | + } |
| 1359 | + }, |
| 1360 | + "metadata": {}, |
| 1361 | + "output_type": "display_data" |
| 1362 | + }, |
| 1363 | + { |
| 1364 | + "data": { |
| 1365 | + "application/vnd.jupyter.widget-view+json": { |
| 1366 | + "model_id": "559c20b044a4469db7f0ab8c3fae1022" |
| 1367 | + } |
| 1368 | + }, |
| 1369 | + "metadata": {}, |
| 1370 | + "output_type": "display_data" |
| 1371 | + } |
| 1372 | + ], |
| 1373 | + "source": [ |
| 1374 | + "all_node_colors = []\n", |
| 1375 | + "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", |
| 1376 | + "display_visual(user_input = False, algorithm = best_first_search, problem = romania_problem)" |
| 1377 | + ] |
| 1378 | + }, |
1256 | 1379 | {
|
1257 | 1380 | "cell_type": "markdown",
|
1258 | 1381 | "metadata": {},
|
|
0 commit comments