{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem-Solving Agents (Section 3.1)\n",
"\n",
"Goal-based agents for which states of the environment are considered\n",
"as atomic representations, ones with no visible internal structure.\n",
"\n",
"Formulating the search problem. We assume\n",
" * environment's state is **observable**\n",
" * environment's state is **discrete**\n",
" * environment is **deterministic**\n",
" * environment is **sequential**\n",
" * environment is **static**\n",
" * **single** agent\n",
"\n",
"Problem definition:\n",
" * set of possible states\n",
" * initial state\n",
" * possible actions available at each state\n",
" * transition model, from state, action pair to next state\n",
" * goal test\n",
" * path cost, in this chapter assumed to be sum of step costs\n",
"\n",
"Definition of state set is critical. Want enough detail to enable\n",
"discovery of useful solution, but want minimal detail to ensure a\n",
"practical search.\n",
"\n",
"Consider the [sliding tile 8-puzzle](http://www.tilepuzzles.com/default.asp?p=12). Example of \n",
" * too much detail\n",
" * a state includes positions of every tile, orientation in three dimensions of puzzle, time of day, what you had for breakfast, the age of your older sister\n",
" * too little detail\n",
" * state is either \"initial\" or \"solved\"\n",
"\n",
"How would you write the problem definition for our simple graph?\n",
" * set of possible states?\n",
" * initial state?\n",
" * possible actions available at each state?\n",
" * goal test?\n",
" * path cost?\n",
"\n",
"How would you write the problem definition for the [8 or 15 puzzle](http://en.wikipedia.org/wiki/Fifteen_puzzle)?\n",
" * set of possible states?\n",
" * initial state?\n",
" * possible actions available at each state?\n",
" * goal test?\n",
" * path cost?\n",
"\n",
"How would you write the problem definition for the [Towers of Hanoi puzzle?](http://en.wikipedia.org/wiki/Tower_of_Hanoi)\n",
" * set of possible states?\n",
" * initial state?\n",
" * possible actions available at each state?\n",
" * goal test?\n",
" * path cost?\n",
"\n",
"How would you write the problem definition for the [Peg Board Puzzle](http://pegboardgame.blogspot.com/)?\n",
" * set of possible states?\n",
" * initial state?\n",
" * possible actions available at each state?\n",
" * goal test?\n",
" * path cost?\n",
"\n",
"How would you write the problem definition for scheduling 10 different observations using the [Hubble Space Telescope](https://arxiv.org/pdf/1810.04815.pdf)?\n",
" * set of possible states?\n",
" * initial state?\n",
" * possible actions available at each state?\n",
" * goal test?\n",
" * path cost?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ways of searching a graph to find path from start to goal node?\n",
"\n",
"Uninformed search means that the choice of action is not \"informed\" by any knowledge of the goal.\n",
"\n",
"Breadth-first search completely explores each level of the search space before proceeding to the next.\n",
"\n",
"Depth-first search completely explores a path until it ends, then backs up a level and tries again."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(\"https://www.cs.colostate.edu/~anderson/cs440/notebooks/simplegraphsteps.pdf\", width=800, height=600)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example does not show the reduced space requirements of\n",
"depth-first search. If the solution path was via the second child of 'a',\n",
"then we would see that the nodes through the first child of 'a' do not\n",
"need to be saved.\n",
"\n",
"Here is an algorithm definition for both breadth-first and depth-first\n",
"search, tailored to fit the python implementation you must complete\n",
"for \n",
"[Assignment 1](http://nbviewer.ipython.org/url/www.cs.colostate.edu/~anderson/cs440/notebooks/A1%20Uninformed%20Search.ipynb). The\n",
"algorithm maintains a local variable named `un_expanded` to be a list\n",
"of nodes whose children have not yet been generated (like the authors'\n",
"`frontier` variable), and a dictionary named `expanded` to keep\n",
"the nodes for which we have generated the children (like the authors'\n",
"`explored` variable). In each a node is stored with its parent,\n",
"allowing a solution path to be generated be stepping backwards from\n",
"the goal node once it is found.\n",
"\n",
"Given the `start_state`, `goal_state`, `successors_f`, and\n",
"`breadth_first` (a boolean variable):\n",
"\n",
" * Initialize `expanded` to be an empty dictionary\n",
" * Initialize `un_expanded` to be a list containing the pair `(start_state, None)`\n",
" * If `start_state` is the `goal_state`, return the list containing just `start_state`\n",
" * Repeat the following steps while `un_expanded` is not empty:\n",
" * Pop from the end of `un_expanded` a (`state`, `parent`) pair.\n",
" * Generate the `children` of `state` using the `successors_f` function.\n",
" * Add `tuple(state): parent` to the `expanded` dictionary\n",
" * For efficiency, remove from `children` any states that are already in `expanded` or `un_expanded`. When looking for states in `expanded`, remember to apply `tuple` to them first.\n",
" * If the goal has been found (in python, `goal_state` is in `children`):\n",
" * Initialize the solution path with the list `[state, goal_state]`. \n",
" * While `parent` exists:\n",
" * Insert `parent` to the front of the solution path.\n",
" * Set `parent` to the parent of `parent`.\n",
" * Return the solution path.\n",
" * Sort and reverse the list of states in `children`, so that we all find the same solution paths.\n",
" * Create a modified `children` list by changing each entry to be a pair (`child`, `parent`), where `parent` is the parent of the child.\n",
" * Insert the modified `children` list into the `un_expanded` list at the front if doing breadth-first search, or at the back if doing depth-first search. Use the boolean variable `breadth_first` provided as the last argument in the call to this function to control inserting at the front the back. Do this insertion with one statement, not a for loop, to preserve the order of the children."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The line that starts with \"For efficiency, remove from children any states ...\" can be tricky, especially if you try to implement this with a for loop that steps through the elements of `children` and remove them.\n",
"\n",
"Here is an example of a result that might surprise you."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 6, 4, 5, 2, 3, 9, 7, 8]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nums = [0, 6, 4, 5, 2, 3, 9, 7, 8]\n",
"nums"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"6\n",
"4\n",
"5\n",
"2\n",
"3\n",
"9\n",
"7\n",
"8\n"
]
}
],
"source": [
"for num in nums:\n",
" print(num)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 5, 3, 9, 7, 8]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"for num in nums:\n",
" if num < 5:\n",
" nums.remove(num)\n",
"\n",
"nums"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why is 3 still in this list?\n",
"\n",
"This is because you are modifying the list `nums` which is controlling the for loop. After 2 is removed, the for loop moves on to the value at 9. (Try printing the value of `nums` at the end of each repetition to see this.)\n",
"\n",
"How can we fix this? There are two ways. The first is to use a copy of the `nums` list to control the for loop, so you are free to modify the original `nums` list."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 5, 9, 7, 8]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import copy\n",
"\n",
"nums = [0, 6, 4, 5, 2, 3, 9, 7, 8]\n",
"for num in copy.copy(nums):\n",
" if num < 5:\n",
" nums.remove(num)\n",
" \n",
"nums"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A second way is to use a list comprehension. This ends up constructing a new list, keeping just the elements you want."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 5, 9, 7, 8]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nums = [0, 6, 4, 5, 2, 3, 9, 7, 8]\n",
"nums = [num for num in nums if num >= 5]\n",
"nums"
]
}
],
"metadata": {
"anaconda-cloud": {},
"jupytext": {
"formats": "ipynb,py:light"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}