diff --git a/search.ipynb b/search.ipynb index d8629a0ab..d16253be4 100644 --- a/search.ipynb +++ b/search.ipynb @@ -1542,6 +1542,103 @@ " problem=romania_problem)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 7. Depth Limited Search\n", + "\n", + "Let's change all the 'node_colors' to starting position and define a different problem statement. \n", + "Although we have a working implementation, but we need to make changes." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def depth_limited_search(problem, frontier, limit = -1):\n", + " '''\n", + " Perform depth first search of graph g.\n", + " if limit >= 0, that is the maximum depth of the search.\n", + " '''\n", + " # we use these two variables at the time of visualisations\n", + " iterations = 0\n", + " all_node_colors = []\n", + " node_colors = {k : 'white' for k in problem.graph.nodes()}\n", + " \n", + " frontier.append(Node(problem.initial))\n", + " explored = set()\n", + " \n", + " cutoff_occurred = False\n", + " node_colors[Node(problem.initial).state] = \"orange\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " \n", + " while frontier:\n", + " # Popping first node of queue\n", + " node = frontier.pop()\n", + " \n", + " # modify the currently searching node to red\n", + " node_colors[node.state] = \"red\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " \n", + " if problem.goal_test(node.state):\n", + " # modify goal node to green after reaching the goal\n", + " node_colors[node.state] = \"green\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " return(iterations, all_node_colors, node)\n", + "\n", + " elif limit >= 0:\n", + " cutoff_occurred = True\n", + " limit += 1\n", + " all_node_color.pop()\n", + " iterations -= 1\n", + " node_colors[node.state] = \"gray\"\n", + "\n", + " \n", + " explored.add(node.state)\n", + " frontier.extend(child for child in node.expand(problem)\n", + " if child.state not in explored and\n", + " child not in frontier)\n", + " \n", + " for n in frontier:\n", + " limit -= 1\n", + " # modify the color of frontier nodes to orange\n", + " node_colors[n.state] = \"orange\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + "\n", + " # modify the color of explored nodes to gray\n", + " node_colors[node.state] = \"gray\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " \n", + " return 'cutoff' if cutoff_occurred else None\n", + "\n", + "\n", + "def depth_limited_search_for_vis(problem):\n", + " \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n", + " iterations, all_node_colors, node = depth_limited_search(problem, Stack())\n", + " return(iterations, all_node_colors, node) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "all_node_colors = []\n", + "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", + "display_visual(romania_graph_data, user_input=False, \n", + " algorithm=depth_limited_search_for_vis, \n", + " problem=romania_problem)" + ] + }, { "cell_type": "markdown", "metadata": {},