From 6dd92e0324c02710fffa54bcb3efbcef615bc68f Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Fri, 2 Feb 2018 23:59:58 +0530 Subject: [PATCH 1/3] added Best First search --- search.ipynb | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 1 deletion(-) diff --git a/search.ipynb b/search.ipynb index d537bd6c0..624d7554e 100644 --- a/search.ipynb +++ b/search.ipynb @@ -445,7 +445,7 @@ "2. Depth First Tree Search - Implemented\n", "3. Depth First Graph Search - Implemented\n", "4. Breadth First Search - Implemented\n", - "5. Best First Graph Search\n", + "5. Best First Graph Search - Implemented\n", "6. Uniform Cost Search - Implemented\n", "7. Depth Limited Search\n", "8. Iterative Deepening Search\n", @@ -1251,6 +1251,168 @@ "display_visual(user_input = True)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## BEST FIRST SEARCH\n", + "Let's change all the node_colors to starting position and define a different problem statement." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "def best_first_graph_search(problem, f):\n", + " \"\"\"Search the nodes with the lowest f scores first.\n", + " You specify the function f(node) that you want to minimize; for example,\n", + " if f is a heuristic estimate to the goal, then we have greedy best\n", + " first search; if f is node.depth then we have breadth-first search.\n", + " There is a subtlety: the line \"f = memoize(f, 'f')\" means that the f\n", + " values will be cached on the nodes as they are computed. So after doing\n", + " a best first search you can examine the f values of the path returned.\"\"\"\n", + " \n", + " # we use these two variables at the time of visualisations\n", + " iterations = 0\n", + " all_node_colors = []\n", + " node_colors = dict(initial_node_colors)\n", + " \n", + " f = memoize(f, 'f')\n", + " node = Node(problem.initial)\n", + " \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", + " \n", + " frontier = PriorityQueue(min, f)\n", + " frontier.append(node)\n", + " \n", + " node_colors[node.state] = \"orange\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " \n", + " explored = set()\n", + " while frontier:\n", + " node = frontier.pop()\n", + " \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", + " \n", + " explored.add(node.state)\n", + " for child in node.expand(problem):\n", + " if child.state not in explored and child not in frontier:\n", + " frontier.append(child)\n", + " node_colors[child.state] = \"orange\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " elif child in frontier:\n", + " incumbent = frontier[child]\n", + " if f(child) < f(incumbent):\n", + " del frontier[incumbent]\n", + " frontier.append(child)\n", + " node_colors[child.state] = \"orange\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + "\n", + " node_colors[node.state] = \"gray\"\n", + " iterations += 1\n", + " all_node_colors.append(dict(node_colors))\n", + " return None\n", + "\n", + "def best_first_search(problem, h=None):\n", + " \"\"\"Best-first graph search is an informative searching algorithm with f(n) = h(n).\n", + " You need to specify the h function when you call best_first_search, or\n", + " else in your Problem subclass.\"\"\"\n", + " h = memoize(h or problem.h, 'h')\n", + " iterations, all_node_colors, node = best_first_graph_search(problem, lambda n: h(n))\n", + " return(iterations, all_node_colors, node)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "5ae2d521b74743afa988c462a851c269", + "version_major": 2, + "version_minor": 0 + }, + "text/html": [ + "

Failed to display Jupyter Widget of type interactive.

\n", + "

\n", + " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", + " that the widgets JavaScript is still loading. If this message persists, it\n", + " likely means that the widgets JavaScript library is either not installed or\n", + " not enabled. See the Jupyter\n", + " Widgets Documentation for setup instructions.\n", + "

\n", + "

\n", + " If you're reading this message in another frontend (for example, a static\n", + " rendering on GitHub or NBViewer),\n", + " it may mean that your frontend doesn't currently support widgets.\n", + "

\n" + ], + "text/plain": [ + "interactive(children=(IntSlider(value=0, description='iteration', max=1), Output()), _dom_classes=('widget-interact',))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "559c20b044a4469db7f0ab8c3fae1022", + "version_major": 2, + "version_minor": 0 + }, + "text/html": [ + "

Failed to display Jupyter Widget of type interactive.

\n", + "

\n", + " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", + " that the widgets JavaScript is still loading. If this message persists, it\n", + " likely means that the widgets JavaScript library is either not installed or\n", + " not enabled. See the Jupyter\n", + " Widgets Documentation for setup instructions.\n", + "

\n", + "

\n", + " If you're reading this message in another frontend (for example, a static\n", + " rendering on GitHub or NBViewer),\n", + " it may mean that your frontend doesn't currently support widgets.\n", + "

\n" + ], + "text/plain": [ + "interactive(children=(ToggleButton(value=False, description='Visualize'), Output()), _dom_classes=('widget-interact',))" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "all_node_colors = []\n", + "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", + "display_visual(user_input = False, algorithm = best_first_search, problem = romania_problem)" + ] + }, { "cell_type": "markdown", "metadata": {}, From 26ab258547427e065cd9ebc8966a7e174291c0ec Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Sat, 3 Feb 2018 00:50:00 +0530 Subject: [PATCH 2/3] fixed minor conflicts --- search.ipynb | 48 ++++-------------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/search.ipynb b/search.ipynb index 624d7554e..a19138b4f 100644 --- a/search.ipynb +++ b/search.ipynb @@ -1351,28 +1351,8 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "5ae2d521b74743afa988c462a851c269", - "version_major": 2, - "version_minor": 0 - }, - "text/html": [ - "

Failed to display Jupyter Widget of type interactive.

\n", - "

\n", - " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", - " that the widgets JavaScript is still loading. If this message persists, it\n", - " likely means that the widgets JavaScript library is either not installed or\n", - " not enabled. See the Jupyter\n", - " Widgets Documentation for setup instructions.\n", - "

\n", - "

\n", - " If you're reading this message in another frontend (for example, a static\n", - " rendering on GitHub or NBViewer),\n", - " it may mean that your frontend doesn't currently support widgets.\n", - "

\n" - ], - "text/plain": [ - "interactive(children=(IntSlider(value=0, description='iteration', max=1), Output()), _dom_classes=('widget-interact',))" - ] + "model_id": "5ae2d521b74743afa988c462a851c269" + } }, "metadata": {}, "output_type": "display_data" @@ -1380,28 +1360,8 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "559c20b044a4469db7f0ab8c3fae1022", - "version_major": 2, - "version_minor": 0 - }, - "text/html": [ - "

Failed to display Jupyter Widget of type interactive.

\n", - "

\n", - " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", - " that the widgets JavaScript is still loading. If this message persists, it\n", - " likely means that the widgets JavaScript library is either not installed or\n", - " not enabled. See the Jupyter\n", - " Widgets Documentation for setup instructions.\n", - "

\n", - "

\n", - " If you're reading this message in another frontend (for example, a static\n", - " rendering on GitHub or NBViewer),\n", - " it may mean that your frontend doesn't currently support widgets.\n", - "

\n" - ], - "text/plain": [ - "interactive(children=(ToggleButton(value=False, description='Visualize'), Output()), _dom_classes=('widget-interact',))" - ] + "model_id": "559c20b044a4469db7f0ab8c3fae1022" + } }, "metadata": {}, "output_type": "display_data" From ff7e3c7d2b54aa80a3e4b388ec006b6c4a25f102 Mon Sep 17 00:00:00 2001 From: Vinay Varma Date: Mon, 5 Feb 2018 23:00:37 +0530 Subject: [PATCH 3/3] minor changes --- search.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/search.ipynb b/search.ipynb index a19138b4f..7e53367f3 100644 --- a/search.ipynb +++ b/search.ipynb @@ -39,6 +39,7 @@ "* Breadth-First Search\n", "* Uniform Cost Search\n", "* A\\* Search\n", + "* Best First Search\n", "* Genetic Algorithm" ] }, @@ -1188,7 +1189,7 @@ ], "source": [ "all_node_colors = []\n", - "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", + "romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n", "display_visual(user_input = False, algorithm = astar_search, problem = romania_problem)" ] },