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

Skip to content

Commit bf5b8dc

Browse files
CharuChhimpanorvig
authored andcommitted
Added Depth Limited Search in search.ipynb (#876)
* Added Depth Limited Search in search.ipynb * Made changes in depth limited search
1 parent ba35aa4 commit bf5b8dc

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

search.ipynb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,103 @@
15421542
" problem=romania_problem)"
15431543
]
15441544
},
1545+
{
1546+
"cell_type": "markdown",
1547+
"metadata": {},
1548+
"source": [
1549+
"## 7. Depth Limited Search\n",
1550+
"\n",
1551+
"Let's change all the 'node_colors' to starting position and define a different problem statement. \n",
1552+
"Although we have a working implementation, but we need to make changes."
1553+
]
1554+
},
1555+
{
1556+
"cell_type": "code",
1557+
"execution_count": 17,
1558+
"metadata": {},
1559+
"outputs": [],
1560+
"source": [
1561+
"def depth_limited_search(problem, frontier, limit = -1):\n",
1562+
" '''\n",
1563+
" Perform depth first search of graph g.\n",
1564+
" if limit >= 0, that is the maximum depth of the search.\n",
1565+
" '''\n",
1566+
" # we use these two variables at the time of visualisations\n",
1567+
" iterations = 0\n",
1568+
" all_node_colors = []\n",
1569+
" node_colors = {k : 'white' for k in problem.graph.nodes()}\n",
1570+
" \n",
1571+
" frontier.append(Node(problem.initial))\n",
1572+
" explored = set()\n",
1573+
" \n",
1574+
" cutoff_occurred = False\n",
1575+
" node_colors[Node(problem.initial).state] = \"orange\"\n",
1576+
" iterations += 1\n",
1577+
" all_node_colors.append(dict(node_colors))\n",
1578+
" \n",
1579+
" while frontier:\n",
1580+
" # Popping first node of queue\n",
1581+
" node = frontier.pop()\n",
1582+
" \n",
1583+
" # modify the currently searching node to red\n",
1584+
" node_colors[node.state] = \"red\"\n",
1585+
" iterations += 1\n",
1586+
" all_node_colors.append(dict(node_colors))\n",
1587+
" \n",
1588+
" if problem.goal_test(node.state):\n",
1589+
" # modify goal node to green after reaching the goal\n",
1590+
" node_colors[node.state] = \"green\"\n",
1591+
" iterations += 1\n",
1592+
" all_node_colors.append(dict(node_colors))\n",
1593+
" return(iterations, all_node_colors, node)\n",
1594+
"\n",
1595+
" elif limit >= 0:\n",
1596+
" cutoff_occurred = True\n",
1597+
" limit += 1\n",
1598+
" all_node_color.pop()\n",
1599+
" iterations -= 1\n",
1600+
" node_colors[node.state] = \"gray\"\n",
1601+
"\n",
1602+
" \n",
1603+
" explored.add(node.state)\n",
1604+
" frontier.extend(child for child in node.expand(problem)\n",
1605+
" if child.state not in explored and\n",
1606+
" child not in frontier)\n",
1607+
" \n",
1608+
" for n in frontier:\n",
1609+
" limit -= 1\n",
1610+
" # modify the color of frontier nodes to orange\n",
1611+
" node_colors[n.state] = \"orange\"\n",
1612+
" iterations += 1\n",
1613+
" all_node_colors.append(dict(node_colors))\n",
1614+
"\n",
1615+
" # modify the color of explored nodes to gray\n",
1616+
" node_colors[node.state] = \"gray\"\n",
1617+
" iterations += 1\n",
1618+
" all_node_colors.append(dict(node_colors))\n",
1619+
" \n",
1620+
" return 'cutoff' if cutoff_occurred else None\n",
1621+
"\n",
1622+
"\n",
1623+
"def depth_limited_search_for_vis(problem):\n",
1624+
" \"\"\"Search the deepest nodes in the search tree first.\"\"\"\n",
1625+
" iterations, all_node_colors, node = depth_limited_search(problem, Stack())\n",
1626+
" return(iterations, all_node_colors, node) "
1627+
]
1628+
},
1629+
{
1630+
"cell_type": "code",
1631+
"execution_count": null,
1632+
"metadata": {},
1633+
"outputs": [],
1634+
"source": [
1635+
"all_node_colors = []\n",
1636+
"romania_problem = GraphProblem('Arad', 'Bucharest', romania_map)\n",
1637+
"display_visual(romania_graph_data, user_input=False, \n",
1638+
" algorithm=depth_limited_search_for_vis, \n",
1639+
" problem=romania_problem)"
1640+
]
1641+
},
15451642
{
15461643
"cell_type": "markdown",
15471644
"metadata": {},

0 commit comments

Comments
 (0)