Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixed errors occurred in search.ipynb due to refactoring #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and
| 3.7 | Graph-Search | `graph_search` | [`search.py`][search] | Done | |
| 3.11 | Breadth-First-Search | `breadth_first_graph_search` | [`search.py`][search] | Done | Included |
| 3.14 | Uniform-Cost-Search | `uniform_cost_search` | [`search.py`][search] | Done | Included |
| 3.17 | Depth-Limited-Search | `depth_limited_search` | [`search.py`][search] | Done | |
| 3.18 | Iterative-Deepening-Search | `iterative_deepening_search` | [`search.py`][search] | Done | |
| 3.17 | Depth-Limited-Search | `depth_limited_search` | [`search.py`][search] | Done | Included |
| 3.18 | Iterative-Deepening-Search | `iterative_deepening_search` | [`search.py`][search] | Done | Included |
| 3.22 | Best-First-Search | `best_first_graph_search` | [`search.py`][search] | Done | Included |
| 3.24 | A\*-Search | `astar_search` | [`search.py`][search] | Done | Included |
| 3.26 | Recursive-Best-First-Search | `recursive_best_first_search` | [`search.py`][search] | Done | |
Expand Down Expand Up @@ -186,4 +186,4 @@ Many thanks for contributions over the years. I got bug reports, corrected code,
[rl]:../master/rl.py
[search]:../master/search.py
[utils]:../master/utils.py
[text]:../master/text.py
[text]:../master/text.py
100 changes: 71 additions & 29 deletions search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@
},
"outputs": [],
"source": [
"def tree_search_for_vis(problem, frontier):\n",
"def tree_breadth_search_for_vis(problem):\n",
" \"\"\"Search through the successors of a problem to find a goal.\n",
" The argument frontier should be an empty queue.\n",
" Don't worry about repeated paths to a state. [Figure 3.7]\"\"\"\n",
Expand All @@ -1143,15 +1143,15 @@
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
" \n",
" #Adding first node to the queue\n",
" frontier.append(Node(problem.initial))\n",
" frontier = deque([Node(problem.initial)])\n",
" \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",
" node = frontier.popleft()\n",
" \n",
" # modify the currently searching node to red\n",
" node_colors[node.state] = \"red\"\n",
Expand Down Expand Up @@ -1179,9 +1179,9 @@
" \n",
" return None\n",
"\n",
"def breadth_first_tree_search_(problem):\n",
"def breadth_first_tree_search(problem):\n",
" \"Search the shallowest nodes in the search tree first.\"\n",
" iterations, all_node_colors, node = tree_search_for_vis(problem, FIFOQueue())\n",
" iterations, all_node_colors, node = tree_breadth_search_for_vis(problem)\n",
" return(iterations, all_node_colors, node)"
]
},
Expand All @@ -1199,10 +1199,10 @@
"outputs": [],
"source": [
"all_node_colors = []\n",
"romania_problem = GraphProblem('Arad', 'Fagaras', romania_map)\n",
"a, b, c = breadth_first_tree_search_(romania_problem)\n",
"romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",
"a, b, c = breadth_first_tree_search(romania_problem)\n",
"display_visual(romania_graph_data, user_input=False, \n",
" algorithm=breadth_first_tree_search_, \n",
" algorithm=breadth_first_tree_search, \n",
" problem=romania_problem)"
]
},
Expand All @@ -1222,11 +1222,56 @@
},
"outputs": [],
"source": [
"def depth_first_tree_search_graph(problem):\n",
"def tree_depth_search_for_vis(problem):\n",
" \"\"\"Search through the successors of a problem to find a goal.\n",
" The argument frontier should be an empty queue.\n",
" Don't worry about repeated paths to a state. [Figure 3.7]\"\"\"\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",
" #Adding first node to the stack\n",
" frontier = [Node(problem.initial)]\n",
" \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 stack\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",
" frontier.extend(node.expand(problem))\n",
" \n",
" for n in node.expand(problem):\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 None\n",
"\n",
"def depth_first_tree_search(problem):\n",
" \"Search the deepest nodes in the search tree first.\"\n",
" # This algorithm might not work in case of repeated paths\n",
" # and may run into an infinite while loop.\n",
" iterations, all_node_colors, node = tree_search_for_vis(problem, Stack())\n",
" iterations, all_node_colors, node = tree_depth_search_for_vis(problem)\n",
" return(iterations, all_node_colors, node)"
]
},
Expand All @@ -1237,9 +1282,9 @@
"outputs": [],
"source": [
"all_node_colors = []\n",
"romania_problem = GraphProblem('Arad', 'Oradea', romania_map)\n",
"romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",
"display_visual(romania_graph_data, user_input=False, \n",
" algorithm=depth_first_tree_search_graph, \n",
" algorithm=depth_first_tree_search, \n",
" problem=romania_problem)"
]
},
Expand All @@ -1262,7 +1307,7 @@
},
"outputs": [],
"source": [
"def breadth_first_search_graph(problem):\n",
"def breadth_first_search_graph(problem):\n",
" \"[Figure 3.11]\"\n",
" \n",
" # we use these two variables at the time of visualisations\n",
Expand All @@ -1282,8 +1327,7 @@
" all_node_colors.append(dict(node_colors))\n",
" return(iterations, all_node_colors, node)\n",
" \n",
" frontier = FIFOQueue()\n",
" frontier.append(node)\n",
" frontier = deque([node])\n",
" \n",
" # modify the color of frontier nodes to blue\n",
" node_colors[node.state] = \"orange\"\n",
Expand All @@ -1292,7 +1336,7 @@
" \n",
" explored = set()\n",
" while frontier:\n",
" node = frontier.pop()\n",
" node = frontier.popleft()\n",
" node_colors[node.state] = \"red\"\n",
" iterations += 1\n",
" all_node_colors.append(dict(node_colors))\n",
Expand All @@ -1315,8 +1359,7 @@
" node_colors[node.state] = \"gray\"\n",
" iterations += 1\n",
" all_node_colors.append(dict(node_colors))\n",
" return None"
]
" return None" ]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -1346,8 +1389,7 @@
"collapsed": true
},
"outputs": [],
"source": [
"def graph_search_for_vis(problem, frontier):\n",
"source": [ "def graph_search_for_vis(problem):\n",
" \"\"\"Search through the successors of a problem to find a goal.\n",
" The argument frontier should be an empty queue.\n",
" If two paths reach a state, only use the first one. [Figure 3.7]\"\"\"\n",
Expand All @@ -1356,7 +1398,7 @@
" all_node_colors = []\n",
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
" \n",
" frontier.append(Node(problem.initial))\n",
" frontier = [(Node(problem.initial))]\n",
" explored = set()\n",
" \n",
" # modify the color of frontier nodes to orange\n",
Expand All @@ -1365,7 +1407,7 @@
" all_node_colors.append(dict(node_colors))\n",
" \n",
" while frontier:\n",
" # Popping first node of queue\n",
" # Popping first node of stack\n",
" node = frontier.pop()\n",
" \n",
" # modify the currently searching node to red\n",
Expand Down Expand Up @@ -1401,7 +1443,7 @@
"\n",
"def depth_first_graph_search(problem):\n",
" \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n",
" iterations, all_node_colors, node = graph_search_for_vis(problem, Stack())\n",
" iterations, all_node_colors, node = graph_search_for_vis(problem)\n",
" return(iterations, all_node_colors, node)"
]
},
Expand Down Expand Up @@ -1462,7 +1504,7 @@
" all_node_colors.append(dict(node_colors))\n",
" return(iterations, all_node_colors, node)\n",
" \n",
" frontier = PriorityQueue(min, f)\n",
" frontier = PriorityQueue('min', f)\n",
" frontier.append(node)\n",
" \n",
" node_colors[node.state] = \"orange\"\n",
Expand Down Expand Up @@ -1558,7 +1600,7 @@
"metadata": {},
"outputs": [],
"source": [
"def depth_limited_search(problem, frontier, limit = -1):\n",
"def depth_limited_search(problem, limit = -1):\n",
" '''\n",
" Perform depth first search of graph g.\n",
" if limit >= 0, that is the maximum depth of the search.\n",
Expand All @@ -1568,7 +1610,7 @@
" all_node_colors = []\n",
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
" \n",
" frontier.append(Node(problem.initial))\n",
" frontier = [Node(problem.initial)]\n",
" explored = set()\n",
" \n",
" cutoff_occurred = False\n",
Expand Down Expand Up @@ -1622,7 +1664,7 @@
"\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",
" iterations, all_node_colors, node = depth_limited_search(problem)\n",
" return(iterations, all_node_colors, node) "
]
},
Expand Down