From 54541557e8cf053854f999a28204ee0c1d3cf9d3 Mon Sep 17 00:00:00 2001 From: Charu Date: Wed, 21 Mar 2018 09:30:03 +0530 Subject: [PATCH 1/2] Added Depth Limited Search in search.ipynb --- search.ipynb | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/search.ipynb b/search.ipynb index d8629a0ab..76c283ca5 100644 --- a/search.ipynb +++ b/search.ipynb @@ -1542,6 +1542,73 @@ " 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": 29, + "metadata": {}, + "outputs": [], + "source": [ + "def depth_limited_search_for_vis(problem, limit=50):\n", + " \n", + " iterations = 0\n", + " all_node_colors = []\n", + " node_colors = {k : 'white' for k in problem.graph.nodes()}\n", + " \n", + " def recursive_dls(node, problem, limit, iterations, all_node_colors, node_colors):\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", + " node_colors[node.state] = \"green\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " return (iterations, all_node_colors, node)\n", + " elif limit == 0:\n", + " return 'cutoff'\n", + " else:\n", + " cutoff_occurred = False\n", + " for child in node.expand(problem):\n", + " node_colors[child.state] = \"orange\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " result = recursive_dls(child, problem, limit - 1, iterations, all_node_colors, node_colors)\n", + " if result == 'cutoff':\n", + " cutoff_occurred = True\n", + " elif result is not None:\n", + " return result\n", + " return 'cutoff' if cutoff_occurred else None\n", + " node_colors[node.state] = \"gray\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + "\n", + " # Body of depth_limited_search:\n", + " return recursive_dls(Node(problem.initial), problem, limit, iterations, all_node_colors, node_colors)" + ] + }, + { + "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": {}, From 63abd7806895732ca938991ec16d2ce2aa2afad1 Mon Sep 17 00:00:00 2001 From: Charu Date: Fri, 23 Mar 2018 09:27:15 +0530 Subject: [PATCH 2/2] Made changes in depth limited search --- search.ipynb | 72 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/search.ipynb b/search.ipynb index 76c283ca5..d16253be4 100644 --- a/search.ipynb +++ b/search.ipynb @@ -1554,46 +1554,76 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "def depth_limited_search_for_vis(problem, limit=50):\n", - " \n", + "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", - " def recursive_dls(node, problem, limit, iterations, all_node_colors, node_colors):\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", - " elif limit == 0:\n", - " return 'cutoff'\n", - " else:\n", - " cutoff_occurred = False\n", - " for child in node.expand(problem):\n", - " node_colors[child.state] = \"orange\"\n", - " iterations += 1\n", - " all_node_colors.append(dict(node_colors))\n", - " result = recursive_dls(child, problem, limit - 1, iterations, all_node_colors, node_colors)\n", - " if result == 'cutoff':\n", - " cutoff_occurred = True\n", - " elif result is not None:\n", - " return result\n", - " return 'cutoff' if cutoff_occurred else None\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", - " # Body of depth_limited_search:\n", - " return recursive_dls(Node(problem.initial), problem, limit, iterations, all_node_colors, node_colors)" + "\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) " ] }, {