From 604c18e4c7d56b90bba506e7d05d4cb8c281f660 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Thu, 22 Dec 2016 16:57:43 -0800 Subject: [PATCH 01/32] Created new agent for Chapter 2 work. --- adxyz_agents_chap2.ipynb | 369 +++++++++++++++++++++++++++++++++++++++ agents.ipynb | 46 ++--- 2 files changed, 388 insertions(+), 27 deletions(-) create mode 100644 adxyz_agents_chap2.ipynb diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb new file mode 100644 index 000000000..3c0641d14 --- /dev/null +++ b/adxyz_agents_chap2.ipynb @@ -0,0 +1,369 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# AGENT #\n", + "\n", + "An agent, as defined in 2.1 is anything that can perceive its environment through sensors, and act upon that environment through actuators based on its agent program. This can be a dog, robot, or even you. As long as you can perceive the environment and act on it, you are an agent. This notebook will explain how to implement a simple agent, create an environment, and create a program that helps the agent act on the environment based on its percepts.\n", + "\n", + "Before moving on, review the Agent and Environment classes in [agents.py](https://github.com/aimacode/aima-python/blob/master/agents.py).\n", + "\n", + "Let's begin by importing all the functions from the agents.py module and creating our first agent - a blind dog." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "ename": "ImportError", + "evalue": "No module named 'agents'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[1;32mfrom\u001b[0m \u001b[0magents\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mBlindDog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAgent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0meat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mthing\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Dog: Ate food at {}.\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlocation\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[0;31mImportError\u001b[0m: No module named 'agents'" + ] + } + ], + "source": [ + "import sys\n", + "sys.path.append(\"\")\n", + "from agents import *\n", + "\n", + "class BlindDog(Agent):\n", + " def eat(self, thing):\n", + " print(\"Dog: Ate food at {}.\".format(self.location))\n", + " \n", + " def drink(self, thing):\n", + " print(\"Dog: Drank water at {}.\".format( self.location))\n", + "\n", + "dog = BlindDog()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What we have just done is create a dog who can only feel what's in his location (since he's blind), and can eat or drink. Let's see if he's alive..." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(dog.alive)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![Cool dog](https://gifgun.files.wordpress.com/2015/07/wpid-wp-1435860392895.gif)\n", + "This is our dog. How cool is he? Well, he's hungry and needs to go search for food. For him to do this, we need to give him a program. But before that, let's create a park for our dog to play in." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ENVIRONMENT #\n", + "\n", + "A park is an example of an environment because our dog can perceive and act upon it. The Environment class in agents.py is an abstract class, so we will have to create our own subclass from it before we can use it. The abstract class must contain the following methods:\n", + "\n", + "
  • percept(self, agent) - returns what the agent perceives
  • \n", + "
  • execute_action(self, agent, action) - changes the state of the environment based on what the agent does.
  • " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "class Food(Thing):\n", + " pass\n", + "\n", + "class Water(Thing):\n", + " pass\n", + "\n", + "class Park(Environment):\n", + " def percept(self, agent):\n", + " '''prints & return a list of things that are in our agent's location'''\n", + " things = self.list_things_at(agent.location)\n", + " print(things)\n", + " return things\n", + " \n", + " def execute_action(self, agent, action):\n", + " '''changes the state of the environment based on what the agent does.'''\n", + " if action == \"move down\":\n", + " agent.movedown()\n", + " elif action == \"eat\":\n", + " items = self.list_things_at(agent.location, tclass=Food)\n", + " if len(items) != 0:\n", + " if agent.eat(items[0]): #Have the dog pick eat the first item\n", + " self.delete_thing(items[0]) #Delete it from the Park after.\n", + " elif action == \"drink\":\n", + " items = self.list_things_at(agent.location, tclass=Water)\n", + " if len(items) != 0:\n", + " if agent.drink(items[0]): #Have the dog drink the first item\n", + " self.delete_thing(items[0]) #Delete it from the Park after.\n", + " \n", + " def is_done(self):\n", + " '''By default, we're done when we can't find a live agent, \n", + " but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", + " no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", + " dead_agents = not any(agent.is_alive() for agent in self.agents)\n", + " return dead_agents or no_edibles\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Wumpus Environment" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "ImportError", + "evalue": "No module named 'ipythonblocks'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[1;32mfrom\u001b[0m \u001b[0mipythonblocks\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mBlockGrid\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0magents\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m color = {\"Breeze\": (225, 225, 225),\n\u001b[1;32m 5\u001b[0m \u001b[1;34m\"Pit\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[0;31mImportError\u001b[0m: No module named 'ipythonblocks'" + ] + } + ], + "source": [ + "from ipythonblocks import BlockGrid\n", + "from agents import *\n", + "\n", + "color = {\"Breeze\": (225, 225, 225),\n", + " \"Pit\": (0,0,0),\n", + " \"Gold\": (253, 208, 23),\n", + " \"Glitter\": (253, 208, 23),\n", + " \"Wumpus\": (43, 27, 23),\n", + " \"Stench\": (128, 128, 128),\n", + " \"Explorer\": (0, 0, 255),\n", + " \"Wall\": (44, 53, 57)\n", + " }\n", + "\n", + "def program(percepts):\n", + " '''Returns an action based on it's percepts'''\n", + " print(percepts)\n", + " return input()\n", + "\n", + "w = WumpusEnvironment(program, 7, 7) \n", + "grid = BlockGrid(w.width, w.height, fill=(123, 234, 123))\n", + "\n", + "def draw_grid(world):\n", + " global grid\n", + " grid[:] = (123, 234, 123)\n", + " for x in range(0, len(world)):\n", + " for y in range(0, len(world[x])):\n", + " if len(world[x][y]):\n", + " grid[y, x] = color[world[x][y][-1].__class__.__name__]\n", + "\n", + "def step():\n", + " global grid, w\n", + " draw_grid(w.get_world())\n", + " grid.show()\n", + " w.step()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "step()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# PROGRAM #\n", + "Now that we have a Park Class, we need to implement a program module for our dog. A program controls how the dog acts upon it's environment. Our program will be very simple, and is shown in the table below.\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
    Percept: Feel Food Feel WaterFeel Nothing
    Action: eatdrinkmove up
    \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "class BlindDog(Agent):\n", + " location = 1\n", + " \n", + " def movedown(self):\n", + " self.location += 1\n", + " \n", + " def eat(self, thing):\n", + " '''returns True upon success or False otherwise'''\n", + " if isinstance(thing, Food):\n", + " print(\"Dog: Ate food at {}.\".format(self.location))\n", + " return True\n", + " return False\n", + " \n", + " def drink(self, thing):\n", + " ''' returns True upon success or False otherwise'''\n", + " if isinstance(thing, Water):\n", + " print(\"Dog: Drank water at {}.\".format(self.location))\n", + " return True\n", + " return False\n", + " \n", + "def program(percepts):\n", + " '''Returns an action based on it's percepts'''\n", + " for p in percepts:\n", + " if isinstance(p, Food):\n", + " return 'eat'\n", + " elif isinstance(p, Water):\n", + " return 'drink'\n", + " return 'move down'\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "park = Park()\n", + "dog = BlindDog(program)\n", + "dogfood = Food()\n", + "water = Water()\n", + "park.add_thing(dog, 0)\n", + "park.add_thing(dogfood, 5)\n", + "park.add_thing(water, 7)\n", + "\n", + "park.run(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "That's how easy it is to implement an agent, its program, and environment. But that was a very simple case. What if our environment was 2-Dimentional instead of 1? And what if we had multiple agents?\n", + "\n", + "To make our Park 2D, we will need to make it a subclass of XYEnvironment instead of Environment. Also, let's add a person to play fetch with the dog." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Park(XYEnvironment):\n", + " def percept(self, agent):\n", + " '''prints & return a list of things that are in our agent's location'''\n", + " things = self.list_things_at(agent.location)\n", + " print(things)\n", + " return things\n", + " \n", + " def execute_action(self, agent, action):\n", + " '''changes the state of the environment based on what the agent does.'''\n", + " if action == \"move down\":\n", + " agent.movedown()\n", + " elif action == \"eat\":\n", + " items = self.list_things_at(agent.location, tclass=Food)\n", + " if len(items) != 0:\n", + " if agent.eat(items[0]): #Have the dog pick eat the first item\n", + " self.delete_thing(items[0]) #Delete it from the Park after.\n", + " elif action == \"drink\":\n", + " items = self.list_things_at(agent.location, tclass=Water)\n", + " if len(items) != 0:\n", + " if agent.drink(items[0]): #Have the dog drink the first item\n", + " self.delete_thing(items[0]) #Delete it from the Park after.\n", + " \n", + " def is_done(self):\n", + " '''By default, we're done when we can't find a live agent, \n", + " but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", + " no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", + " dead_agents = not any(agent.is_alive() for agent in self.agents)\n", + " return dead_agents or no_edibles" + ] + } + ], + "metadata": { + "anaconda-cloud": {}, + "kernelspec": { + "display_name": "Python [default]", + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/agents.ipynb b/agents.ipynb index db42f8d33..e2e852755 100644 --- a/agents.ipynb +++ b/agents.ipynb @@ -135,9 +135,21 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "No module named 'ipythonblocks'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[1;32mfrom\u001b[0m \u001b[0mipythonblocks\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mBlockGrid\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0magents\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m color = {\"Breeze\": (225, 225, 225),\n\u001b[1;32m 5\u001b[0m \u001b[1;34m\"Pit\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[0;31mImportError\u001b[0m: No module named 'ipythonblocks'" + ] + } + ], "source": [ "from ipythonblocks import BlockGrid\n", "from agents import *\n", @@ -177,32 +189,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/html": [ - "
    " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[], [None], [], [], [None]]\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "step()" ] @@ -340,8 +331,9 @@ } ], "metadata": { + "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python 3", + "display_name": "Python [default]", "language": "python", "name": "python3" }, @@ -355,7 +347,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.2" } }, "nbformat": 4, From 5190b30b42ddfe9a5a08baacc4dbea51bf4eeb2d Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Fri, 23 Dec 2016 15:09:21 -0800 Subject: [PATCH 02/32] Modified chapter 2 agent with start of environment simulator. --- adxyz_agents_chap2.ipynb | 392 ++++++++++++++++++++++----------------- 1 file changed, 218 insertions(+), 174 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 3c0641d14..2ed554ac6 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,37 +15,23 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": { "collapsed": false, "scrolled": true }, - "outputs": [ - { - "ename": "ImportError", - "evalue": "No module named 'agents'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[1;32mfrom\u001b[0m \u001b[0magents\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mBlindDog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAgent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0meat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mthing\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Dog: Ate food at {}.\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlocation\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[0;31mImportError\u001b[0m: No module named 'agents'" - ] - } - ], + "outputs": [], "source": [ - "import sys\n", - "sys.path.append(\"\")\n", - "from agents import *\n", + "#from agents import *\n", "\n", - "class BlindDog(Agent):\n", - " def eat(self, thing):\n", - " print(\"Dog: Ate food at {}.\".format(self.location))\n", - " \n", - " def drink(self, thing):\n", - " print(\"Dog: Drank water at {}.\".format( self.location))\n", - "\n", - "dog = BlindDog()" + "#class BlindDog(Agent):\n", + "# def eat(self, thing):\n", + "# print(\"Dog: Ate food at {}.\".format(self.location))\n", + "# \n", + "# def drink(self, thing):\n", + "# print(\"Dog: Drank water at {}.\".format( self.location))\n", + "#\n", + "#dog = BlindDog()" ] }, { @@ -57,29 +43,22 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], + "outputs": [], "source": [ - "print(dog.alive)" + "#print(dog.alive)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "![Cool dog](https://gifgun.files.wordpress.com/2015/07/wpid-wp-1435860392895.gif)\n", - "This is our dog. How cool is he? Well, he's hungry and needs to go search for food. For him to do this, we need to give him a program. But before that, let's create a park for our dog to play in." + "" ] }, { @@ -96,46 +75,46 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "class Food(Thing):\n", - " pass\n", + "#class Food(Thing):\n", + "# pass\n", "\n", - "class Water(Thing):\n", - " pass\n", + "#class Water(Thing):\n", + "# pass\n", "\n", - "class Park(Environment):\n", - " def percept(self, agent):\n", - " '''prints & return a list of things that are in our agent's location'''\n", - " things = self.list_things_at(agent.location)\n", - " print(things)\n", - " return things\n", + "#class Park(Environment):\n", + "# def percept(self, agent):\n", + "# '''prints & return a list of things that are in our agent's location'''\n", + "# things = self.list_things_at(agent.location)\n", + "# print(things)\n", + "# return things\n", " \n", - " def execute_action(self, agent, action):\n", - " '''changes the state of the environment based on what the agent does.'''\n", - " if action == \"move down\":\n", - " agent.movedown()\n", - " elif action == \"eat\":\n", - " items = self.list_things_at(agent.location, tclass=Food)\n", - " if len(items) != 0:\n", - " if agent.eat(items[0]): #Have the dog pick eat the first item\n", - " self.delete_thing(items[0]) #Delete it from the Park after.\n", - " elif action == \"drink\":\n", - " items = self.list_things_at(agent.location, tclass=Water)\n", - " if len(items) != 0:\n", - " if agent.drink(items[0]): #Have the dog drink the first item\n", - " self.delete_thing(items[0]) #Delete it from the Park after.\n", + "# def execute_action(self, agent, action):\n", + "# '''changes the state of the environment based on what the agent does.'''\n", + "# if action == \"move down\":\n", + "# agent.movedown()\n", + "# elif action == \"eat\":\n", + "# items = self.list_things_at(agent.location, tclass=Food)\n", + "# if len(items) != 0:\n", + "# if agent.eat(items[0]): #Have the dog pick eat the first item\n", + "# self.delete_thing(items[0]) #Delete it from the Park after.\n", + "# elif action == \"drink\":\n", + "# items = self.list_things_at(agent.location, tclass=Water)\n", + "# if len(items) != 0:\n", + "# if agent.drink(items[0]): #Have the dog drink the first item\n", + "# self.delete_thing(items[0]) #Delete it from the Park after.\n", " \n", - " def is_done(self):\n", - " '''By default, we're done when we can't find a live agent, \n", - " but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", - " no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", - " dead_agents = not any(agent.is_alive() for agent in self.agents)\n", - " return dead_agents or no_edibles\n" + "# def is_done(self):\n", + "# '''By default, we're done when we can't find a live agent, \n", + "# but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", + "# no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", + "# dead_agents = not any(agent.is_alive() for agent in self.agents)\n", + "# return dead_agents or no_edibles\n" ] }, { @@ -147,67 +126,67 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "ImportError", - "evalue": "No module named 'ipythonblocks'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[1;32mfrom\u001b[0m \u001b[0mipythonblocks\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mBlockGrid\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0magents\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m color = {\"Breeze\": (225, 225, 225),\n\u001b[1;32m 5\u001b[0m \u001b[1;34m\"Pit\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[0;31mImportError\u001b[0m: No module named 'ipythonblocks'" - ] - } - ], + "outputs": [], "source": [ - "from ipythonblocks import BlockGrid\n", - "from agents import *\n", + "#from ipythonblocks import BlockGrid\n", + "#from agents import *\n", "\n", - "color = {\"Breeze\": (225, 225, 225),\n", - " \"Pit\": (0,0,0),\n", - " \"Gold\": (253, 208, 23),\n", - " \"Glitter\": (253, 208, 23),\n", - " \"Wumpus\": (43, 27, 23),\n", - " \"Stench\": (128, 128, 128),\n", - " \"Explorer\": (0, 0, 255),\n", - " \"Wall\": (44, 53, 57)\n", - " }\n", + "#color = {\"Breeze\": (225, 225, 225),\n", + "# \"Pit\": (0,0,0),\n", + "# \"Gold\": (253, 208, 23),\n", + "# \"Glitter\": (253, 208, 23),\n", + "# \"Wumpus\": (43, 27, 23),\n", + "# \"Stench\": (128, 128, 128),\n", + "# \"Explorer\": (0, 0, 255),\n", + "# \"Wall\": (44, 53, 57)\n", + "# }\n", "\n", - "def program(percepts):\n", - " '''Returns an action based on it's percepts'''\n", - " print(percepts)\n", - " return input()\n", + "#def program(percepts):\n", + "# '''Returns an action based on it's percepts'''\n", + "# print(percepts)\n", + "# return input()\n", "\n", - "w = WumpusEnvironment(program, 7, 7) \n", - "grid = BlockGrid(w.width, w.height, fill=(123, 234, 123))\n", + "#w = WumpusEnvironment(program, 7, 7) \n", + "#grid = BlockGrid(w.width, w.height, fill=(123, 234, 123))\n", "\n", - "def draw_grid(world):\n", - " global grid\n", - " grid[:] = (123, 234, 123)\n", - " for x in range(0, len(world)):\n", - " for y in range(0, len(world[x])):\n", - " if len(world[x][y]):\n", - " grid[y, x] = color[world[x][y][-1].__class__.__name__]\n", + "#def draw_grid(world):\n", + "# global grid\n", + "# grid[:] = (123, 234, 123)\n", + "# for x in range(0, len(world)):\n", + "# for y in range(0, len(world[x])):\n", + "# if len(world[x][y]):\n", + "# grid[y, x] = color[world[x][y][-1].__class__.__name__]\n", "\n", - "def step():\n", - " global grid, w\n", - " draw_grid(w.get_world())\n", - " grid.show()\n", - " w.step()" + "#def step():\n", + "# global grid, w\n", + "# draw_grid(w.get_world())\n", + "# grid.show()\n", + "# w.step()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'step' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'step' is not defined" + ] + } + ], "source": [ "step()" ] @@ -245,36 +224,34 @@ }, "outputs": [], "source": [ - "class BlindDog(Agent):\n", - " location = 1\n", + "#class BlindDog(Agent):\n", + "# location = 1\n", " \n", - " def movedown(self):\n", - " self.location += 1\n", + "# def movedown(self):\n", + "# self.location += 1\n", " \n", - " def eat(self, thing):\n", - " '''returns True upon success or False otherwise'''\n", - " if isinstance(thing, Food):\n", - " print(\"Dog: Ate food at {}.\".format(self.location))\n", - " return True\n", - " return False\n", + "# def eat(self, thing):\n", + "# '''returns True upon success or False otherwise'''\n", + "# if isinstance(thing, Food):\n", + "# print(\"Dog: Ate food at {}.\".format(self.location))\n", + "# return True\n", + "# return False\n", " \n", - " def drink(self, thing):\n", - " ''' returns True upon success or False otherwise'''\n", - " if isinstance(thing, Water):\n", - " print(\"Dog: Drank water at {}.\".format(self.location))\n", - " return True\n", - " return False\n", + "# def drink(self, thing):\n", + "# ''' returns True upon success or False otherwise'''\n", + "# if isinstance(thing, Water):\n", + "# print(\"Dog: Drank water at {}.\".format(self.location))\n", + "# return True\n", + "# return False\n", " \n", - "def program(percepts):\n", - " '''Returns an action based on it's percepts'''\n", - " for p in percepts:\n", - " if isinstance(p, Food):\n", - " return 'eat'\n", - " elif isinstance(p, Water):\n", - " return 'drink'\n", - " return 'move down'\n", - " \n", - " " + "#def program(percepts):\n", + "# '''Returns an action based on it's percepts'''\n", + "# for p in percepts:\n", + "# if isinstance(p, Food):\n", + "# return 'eat'\n", + "# elif isinstance(p, Water):\n", + "# return 'drink'\n", + "# return 'move down' " ] }, { @@ -285,15 +262,15 @@ }, "outputs": [], "source": [ - "park = Park()\n", - "dog = BlindDog(program)\n", - "dogfood = Food()\n", - "water = Water()\n", - "park.add_thing(dog, 0)\n", - "park.add_thing(dogfood, 5)\n", - "park.add_thing(water, 7)\n", + "#park = Park()\n", + "#dog = BlindDog(program)\n", + "#dogfood = Food()\n", + "#water = Water()\n", + "#park.add_thing(dog, 0)\n", + "#park.add_thing(dogfood, 5)\n", + "#park.add_thing(water, 7)\n", "\n", - "park.run(10)" + "#park.run(10)" ] }, { @@ -313,39 +290,106 @@ }, "outputs": [], "source": [ - "class Park(XYEnvironment):\n", - " def percept(self, agent):\n", - " '''prints & return a list of things that are in our agent's location'''\n", - " things = self.list_things_at(agent.location)\n", - " print(things)\n", - " return things\n", + "#class Park(XYEnvironment):\n", + "# def percept(self, agent):\n", + "# '''prints & return a list of things that are in our agent's location'''\n", + "# things = self.list_things_at(agent.location)\n", + "# print(things)\n", + "# return things\n", " \n", - " def execute_action(self, agent, action):\n", - " '''changes the state of the environment based on what the agent does.'''\n", - " if action == \"move down\":\n", - " agent.movedown()\n", - " elif action == \"eat\":\n", - " items = self.list_things_at(agent.location, tclass=Food)\n", - " if len(items) != 0:\n", - " if agent.eat(items[0]): #Have the dog pick eat the first item\n", - " self.delete_thing(items[0]) #Delete it from the Park after.\n", - " elif action == \"drink\":\n", - " items = self.list_things_at(agent.location, tclass=Water)\n", - " if len(items) != 0:\n", - " if agent.drink(items[0]): #Have the dog drink the first item\n", - " self.delete_thing(items[0]) #Delete it from the Park after.\n", + "# def execute_action(self, agent, action):\n", + "# '''changes the state of the environment based on what the agent does.'''\n", + "# if action == \"move down\":\n", + "# agent.movedown()\n", + "# elif action == \"eat\":\n", + "# items = self.list_things_at(agent.location, tclass=Food)\n", + "# if len(items) != 0:\n", + "# if agent.eat(items[0]): #Have the dog pick eat the first item\n", + "# self.delete_thing(items[0]) #Delete it from the Park after.\n", + "# elif action == \"drink\":\n", + "# items = self.list_things_at(agent.location, tclass=Water)\n", + "# if len(items) != 0:\n", + "# if agent.drink(items[0]): #Have the dog drink the first item\n", + "# self.delete_thing(items[0]) #Delete it from the Park after.\n", " \n", - " def is_done(self):\n", - " '''By default, we're done when we can't find a live agent, \n", - " but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", - " no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", - " dead_agents = not any(agent.is_alive() for agent in self.agents)\n", - " return dead_agents or no_edibles" + "# def is_done(self):\n", + "# '''By default, we're done when we can't find a live agent, \n", + "# but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", + "# no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", + "# dead_agents = not any(agent.is_alive() for agent in self.agents)\n", + "# return dead_agents or no_edibles" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 2 Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2.8) Implement a performance-measuring environment simulator for the vacuum-cleaner world depicted in Figure 2.2 and specified on page 38. Your implementation should be modular so that the sensors, actuators, and enviroment characteristics (size, shape, dirt placement, etc.) can be changed easily." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from agents import *\n", + "class adxyz_VacuumEnvironment(XYEnvironment):\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2.9) Implement a simple reflex agent for the vacuum environment in Exercise 2.8. Run the environment with this agent for all possible initial dirt configurations and agent locations. Record the performance score for each consideration and the overall average score." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n", + "[(0, 0)]\n", + "[(0, 1)]\n", + "[(0, 0), (0, 1)]\n" + ] + } + ], + "source": [ + "# Define the initial dirt configurations\n", + "initDirt=[]\n", + "initDirt.append([]) # neither location dirty\n", + "initDirt.append([(0,0)]) # square A dirty, square B clean\n", + "initDirt.append([(0,1)]) # square A clean, square B dirty\n", + "initDirt.append([(0,0),(0,1)]) # sqaure A dirty, square B dirty\n", + "\n", + "print(initDirt[0])\n", + "print(initDirt[1])\n", + "print(initDirt[2])\n", + "print(initDirt[3])" ] } ], "metadata": { "anaconda-cloud": {}, + "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python [default]", "language": "python", From bf344227875748f350a6c8b8cc02ac50cac10fc4 Mon Sep 17 00:00:00 2001 From: td2014 Date: Sat, 24 Dec 2016 08:29:35 -0800 Subject: [PATCH 03/32] Added some classes to Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 55 +++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 2ed554ac6..85f63a0ff 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -336,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -344,7 +344,17 @@ "source": [ "from agents import *\n", "class adxyz_VacuumEnvironment(XYEnvironment):\n", - " pass" + "\n", + "# Need to override the percept method \n", + " def percept(agent):\n", + " if agent.location==thing.location:\n", + " return thing.name\n", + " else:\n", + " return\n", + " \n", + "# Need to override the action method (and update performance measure.)\n", + " def execute_action(agent):\n", + " pass" ] }, { @@ -356,7 +366,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -367,23 +377,50 @@ "text": [ "[]\n", "[(0, 0)]\n", - "[(0, 1)]\n", - "[(0, 0), (0, 1)]\n" + "[(1, 0)]\n", + "[(0, 0), (1, 0)]\n" ] } ], "source": [ "# Define the initial dirt configurations\n", "initDirt=[]\n", - "initDirt.append([]) # neither location dirty\n", + "initDirt.append([]) # neither location dirty - format (X,Y)\n", "initDirt.append([(0,0)]) # square A dirty, square B clean\n", - "initDirt.append([(0,1)]) # square A clean, square B dirty\n", - "initDirt.append([(0,0),(0,1)]) # sqaure A dirty, square B dirty\n", + "initDirt.append([(1,0)]) # square A clean, square B dirty\n", + "initDirt.append([(0,0),(1,0)]) # sqaure A dirty, square B dirty\n", "\n", "print(initDirt[0])\n", "print(initDirt[1])\n", "print(initDirt[2])\n", - "print(initDirt[3])" + "print(initDirt[3])\n", + "\n", + "class dirtClump(Thing):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(, 1)]\n" + ] + } + ], + "source": [ + "# Create a loop over environments to run simulation\n", + "myVacEnv = adxyz_VacuumEnvironment()\n", + "myDirtClump = dirtClump()\n", + "\n", + "myVacEnv.add_thing(myDirtClump)\n", + "print(myVacEnv.things_near((1,1)))" ] } ], From d09d1e16a25227d9fdf346d2d8f6b6901a8750b3 Mon Sep 17 00:00:00 2001 From: td2014 Date: Sun, 25 Dec 2016 09:34:24 -0800 Subject: [PATCH 04/32] Added loops for multiple dirt locations to Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 124 +++++++++++++++++++++++++++++++++++---- 1 file changed, 111 insertions(+), 13 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 85f63a0ff..bb0415956 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -336,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -366,7 +366,20 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Instantiate a simple reflex vacuum agent\n", + "class adxyz_SimpleReflexVacuumAgent(Agent):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 34, "metadata": { "collapsed": false }, @@ -383,9 +396,12 @@ } ], "source": [ + "# Define the dirt clump class\n", + "class DirtClump(Thing):\n", + " pass\n", "# Define the initial dirt configurations\n", "initDirt=[]\n", - "initDirt.append([]) # neither location dirty - format (X,Y)\n", + "initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", "initDirt.append([(0,0)]) # square A dirty, square B clean\n", "initDirt.append([(1,0)]) # square A clean, square B dirty\n", "initDirt.append([(0,0),(1,0)]) # sqaure A dirty, square B dirty\n", @@ -393,15 +409,12 @@ "print(initDirt[0])\n", "print(initDirt[1])\n", "print(initDirt[2])\n", - "print(initDirt[3])\n", - "\n", - "class dirtClump(Thing):\n", - " pass" + "print(initDirt[3])" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 41, "metadata": { "collapsed": false }, @@ -410,17 +423,102 @@ "name": "stdout", "output_type": "stream", "text": [ - "[(, 1)]\n" + "outer loop:\n", + "len of initDirt = 4\n", + "iSimDirtPlacement - outer loop = 0\n", + "\n", + "outer loop:\n", + "len of initDirt = 4\n", + "iSimDirtPlacement - outer loop = 1\n", + "\n", + "inner loop:\n", + "iSimDirtPlacement - inner loop = 1\n", + "iPlace = 0\n", + "1\n", + "[(0, 0)]\n", + "myDirtClumpList = []\n", + "\n", + "See if objects are unique:\n", + "iPlace = 0\n", + "myDirtClumpList[iPlace].performance = 0\n", + "\n", + "outer loop:\n", + "len of initDirt = 4\n", + "iSimDirtPlacement - outer loop = 2\n", + "\n", + "inner loop:\n", + "iSimDirtPlacement - inner loop = 2\n", + "iPlace = 0\n", + "1\n", + "[(1, 0)]\n", + "myDirtClumpList = []\n", + "\n", + "See if objects are unique:\n", + "iPlace = 0\n", + "myDirtClumpList[iPlace].performance = 0\n", + "\n", + "outer loop:\n", + "len of initDirt = 4\n", + "iSimDirtPlacement - outer loop = 3\n", + "\n", + "inner loop:\n", + "iSimDirtPlacement - inner loop = 3\n", + "iPlace = 0\n", + "2\n", + "[(0, 0), (1, 0)]\n", + "myDirtClumpList = []\n", + "\n", + "\n", + "inner loop:\n", + "iSimDirtPlacement - inner loop = 3\n", + "iPlace = 1\n", + "2\n", + "[(0, 0), (1, 0)]\n", + "myDirtClumpList = [, ]\n", + "\n", + "See if objects are unique:\n", + "iPlace = 0\n", + "myDirtClumpList[iPlace].performance = 0\n", + "See if objects are unique:\n", + "iPlace = 1\n", + "myDirtClumpList[iPlace].performance = 10\n", + "\n" ] } ], "source": [ "# Create a loop over environments to run simulation\n", - "myVacEnv = adxyz_VacuumEnvironment()\n", - "myDirtClump = dirtClump()\n", "\n", - "myVacEnv.add_thing(myDirtClump)\n", - "print(myVacEnv.things_near((1,1)))" + "# Loop over agent placements\n", + "##for iSimAgentPlacement in range(len(initAgent)):\n", + "for iSimAgentPlacement in range(1):\n", + "# Loop over dirt placements\n", + " for iSimDirtPlacement in range(len(initDirt)):\n", + " print(\"outer loop:\")\n", + " print(\"len of initDirt = \", len(initDirt))\n", + " print (\"iSimDirtPlacement - outer loop = \" , iSimDirtPlacement)\n", + " \n", + " myVacEnv = adxyz_VacuumEnvironment()\n", + " myDirtClumpList = []\n", + "\n", + " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", + " print(\"\")\n", + " print(\"inner loop:\")\n", + " print (\"iSimDirtPlacement - inner loop = \" , iSimDirtPlacement)\n", + " print (\"iPlace = \" , iPlace)\n", + " print(len(initDirt[iSimDirtPlacement]))\n", + " print(initDirt[iSimDirtPlacement])\n", + " myDirtClumpList.append(DirtClump())\n", + " myDirtClumpList[iPlace].performance=iPlace*10\n", + " print(\"myDirtClumpList = \", myDirtClumpList)\n", + " print(\"\")\n", + "### myVacEnv.add_thing(myDirtClump)\n", + "### print(myVacEnv.things_near((1,1)))\n", + " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", + " print(\"See if objects are unique:\")\n", + " print(\"iPlace = \", iPlace)\n", + " print(\"myDirtClumpList[iPlace].performance = \", myDirtClumpList[iPlace].performance)\n", + " print(\"\")" ] } ], From 1b1602ecfd330cd097547f05ed00112741dfb4d1 Mon Sep 17 00:00:00 2001 From: td2014 Date: Sun, 25 Dec 2016 14:14:42 -0800 Subject: [PATCH 05/32] Added some percept logic to Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 246 ++++++++++++++++++++++----------------- 1 file changed, 140 insertions(+), 106 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index bb0415956..a0d02c82d 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "collapsed": false }, @@ -170,23 +170,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'step' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'step' is not defined" - ] - } - ], + "outputs": [], "source": [ "step()" ] @@ -334,27 +322,84 @@ "2.8) Implement a performance-measuring environment simulator for the vacuum-cleaner world depicted in Figure 2.2 and specified on page 38. Your implementation should be modular so that the sensors, actuators, and enviroment characteristics (size, shape, dirt placement, etc.) can be changed easily." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Agent Name: Vacuum Robot Agent\n", + "-------------------------------\n", + "*Performance Measure:* +1 point for each clean square at each time step, for 1000 time steps\n", + "\n", + "*Environment:* Two squares at positions (0,0) and (1,0). The squares can either be dirty or clean. The agent cannot go outside those two positions.\n", + "\n", + "*Actuators:* The actuators for the agent consist of the ability to move between the squares and the ability to suck up dirt.\n", + "\n", + "*Sensors:* The sensors allow for the agent to know current location and also whether there is dirt or not at the square the currently occupy." + ] + }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from agents import *\n", + "\n", + "# Define the dirt clump class\n", + "class DirtClump(Thing):\n", + " pass\n", + "\n", + "#Define the environment class\n", "class adxyz_VacuumEnvironment(XYEnvironment):\n", "\n", - "# Need to override the percept method \n", - " def percept(agent):\n", - " if agent.location==thing.location:\n", - " return thing.name\n", - " else:\n", - " return\n", - " \n", + "# Need to override the percept method \n", + " def percept(self, agent):\n", + " '''By default, agent perceives things within a default radius.'''\n", + " print (\"In override percept.\")\n", + " print (\"Agent ID = \", agent)\n", + " print (\"Agent location = \", agent.location)\n", + " print (\"Agent performance = \", agent.performance)\n", + " print (\"Self = \", self)\n", + " print (\"Self.things = \", self.things)\n", + " for iThing in range(len(self.things)):\n", + " if self.things[iThing].location==agent.location: #check location\n", + " if self.things[iThing] != agent: # Don't return agent information\n", + " if (isinstance(self.things[iThing], DirtClump)):\n", + " print (\"A thing which is not agent, but dirt clump = \", self.things[iThing] )\n", + " print (\"Location = \", self.things[iThing].location)\n", + " return \"DirtClump\"\n", + " \n", + " return \"CleanSquare\" #Default, if we don't find a dirt clump.\n", + " \n", "# Need to override the action method (and update performance measure.)\n", - " def execute_action(agent):\n", - " pass" + "### def execute_action(self, agent, action):\n", + "### if action==\"Suck\":\n", + "### print(\"Action-Suck\")\n", + "###\n", + "#### Remember to remove the dirt clod at this location.\n", + "###\n", + "### elif action==\"MoveRight\":\n", + "### print(\"Action-MoveRight\")\n", + "### elif action==\"MoveLeft\":\n", + "### print(\"Action-MoveLeft\")\n", + "### else:\n", + "### print(\"Action-None\")\n", + "###\n", + "### Count up number of clean squares (indirectly)\n", + "### and add that to the agent peformance score\n", + "###\n", + "### dirtCount=0\n", + "### for iThings in range(len(self.things)):\n", + "### if isInstance(self.things[iThings], DirtClump):\n", + "### dirtCount = dirtCount+1\n", + "###\n", + "### cleanSquareCount = self.totalSquareCount-dirtCount \n", + "### agent.performance=agent.performance + 2*cleanSquareCount\n", + "### return\n", + "###\n", + " " ] }, { @@ -366,20 +411,36 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ + "#\n", + "# The program for the simple reflex agent is:\n", + "# \n", + "# Percept: Action:\n", + "# -------- -------\n", + "# [(0,0),Clean] -> Right\n", + "# [(0,0),Dirty] -> Suck\n", + "# [(1,0),Clean] -> Left\n", + "# [(1,0),Dirty] -> Suck\n", + "#\n", + "\n", "# Instantiate a simple reflex vacuum agent\n", - "class adxyz_SimpleReflexVacuumAgent(Agent):\n", - " pass" + "class adxyz_SimpleReflexAgentVacuum(Agent):\n", + " def program(percept):\n", + " print(\"Agent-Percept = \", percept)\n", + "### if percept==\"Dirt\":\n", + "### return \"Suck.\"\n", + "### else:\n", + " return \"Do Nothing.\"" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -388,33 +449,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "[]\n", - "[(0, 0)]\n", - "[(1, 0)]\n", - "[(0, 0), (1, 0)]\n" + "[(1, 0)]\n" ] } ], "source": [ - "# Define the dirt clump class\n", - "class DirtClump(Thing):\n", - " pass\n", "# Define the initial dirt configurations\n", "initDirt=[]\n", - "initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", - "initDirt.append([(0,0)]) # square A dirty, square B clean\n", + "##initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", + "##initDirt.append([(0,0)]) # square A dirty, square B clean\n", "initDirt.append([(1,0)]) # square A clean, square B dirty\n", - "initDirt.append([(0,0),(1,0)]) # sqaure A dirty, square B dirty\n", + "##initDirt.append([(0,0),(1,0)]) # sqaure A dirty, square B dirty\n", "\n", "print(initDirt[0])\n", - "print(initDirt[1])\n", - "print(initDirt[2])\n", - "print(initDirt[3])" + "##print(initDirt[1])\n", + "##print(initDirt[2])\n", + "##print(initDirt[3])" ] }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -424,65 +479,29 @@ "output_type": "stream", "text": [ "outer loop:\n", - "len of initDirt = 4\n", + "len of initDirt = 1\n", "iSimDirtPlacement - outer loop = 0\n", "\n", - "outer loop:\n", - "len of initDirt = 4\n", - "iSimDirtPlacement - outer loop = 1\n", - "\n", - "inner loop:\n", - "iSimDirtPlacement - inner loop = 1\n", - "iPlace = 0\n", - "1\n", - "[(0, 0)]\n", - "myDirtClumpList = []\n", - "\n", - "See if objects are unique:\n", - "iPlace = 0\n", - "myDirtClumpList[iPlace].performance = 0\n", - "\n", - "outer loop:\n", - "len of initDirt = 4\n", - "iSimDirtPlacement - outer loop = 2\n", - "\n", "inner loop:\n", - "iSimDirtPlacement - inner loop = 2\n", + "iSimDirtPlacement - inner loop = 0\n", "iPlace = 0\n", "1\n", "[(1, 0)]\n", "myDirtClumpList = []\n", + "currInitDirtLocation = (1, 0)\n", "\n", - "See if objects are unique:\n", - "iPlace = 0\n", - "myDirtClumpList[iPlace].performance = 0\n", + "Environment:\n", + "[(, 0), (, 0)]\n", "\n", - "outer loop:\n", - "len of initDirt = 4\n", - "iSimDirtPlacement - outer loop = 3\n", - "\n", - "inner loop:\n", - "iSimDirtPlacement - inner loop = 3\n", - "iPlace = 0\n", - "2\n", - "[(0, 0), (1, 0)]\n", - "myDirtClumpList = []\n", - "\n", - "\n", - "inner loop:\n", - "iSimDirtPlacement - inner loop = 3\n", - "iPlace = 1\n", - "2\n", - "[(0, 0), (1, 0)]\n", - "myDirtClumpList = [, ]\n", - "\n", - "See if objects are unique:\n", - "iPlace = 0\n", - "myDirtClumpList[iPlace].performance = 0\n", - "See if objects are unique:\n", - "iPlace = 1\n", - "myDirtClumpList[iPlace].performance = 10\n", - "\n" + "In override percept.\n", + "Agent ID = \n", + "Agent location = (1, 0)\n", + "Agent performance = 0\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x104a809b0>\n", + "Self.things = [, ]\n", + "A thing which is not agent, but dirt clump = \n", + "Location = (1, 0)\n", + "Percept=DirtClump; action? None\n" ] } ], @@ -498,7 +517,7 @@ " print(\"len of initDirt = \", len(initDirt))\n", " print (\"iSimDirtPlacement - outer loop = \" , iSimDirtPlacement)\n", " \n", - " myVacEnv = adxyz_VacuumEnvironment()\n", + " myVacEnv = adxyz_VacuumEnvironment() #Create a new environment for each dirt/agent setup\n", " myDirtClumpList = []\n", "\n", " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", @@ -509,16 +528,31 @@ " print(len(initDirt[iSimDirtPlacement]))\n", " print(initDirt[iSimDirtPlacement])\n", " myDirtClumpList.append(DirtClump())\n", - " myDirtClumpList[iPlace].performance=iPlace*10\n", " print(\"myDirtClumpList = \", myDirtClumpList)\n", - " print(\"\")\n", - "### myVacEnv.add_thing(myDirtClump)\n", - "### print(myVacEnv.things_near((1,1)))\n", - " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", - " print(\"See if objects are unique:\")\n", - " print(\"iPlace = \", iPlace)\n", - " print(\"myDirtClumpList[iPlace].performance = \", myDirtClumpList[iPlace].performance)\n", - " print(\"\")" + " currInitDirtLocation = initDirt[iSimDirtPlacement][iPlace]\n", + " print(\"currInitDirtLocation =\", currInitDirtLocation)\n", + " myVacEnv.add_thing(myDirtClumpList[iPlace],location=currInitDirtLocation)\n", + " \n", + "\n", + "#\n", + "# Now place the agent.\n", + "#\n", + " myAgent=adxyz_SimpleReflexAgentVacuum()\n", + " myVacEnv.add_thing(myAgent,location=(1,0))\n", + " print(\"\")\n", + " print(\"Environment:\")\n", + " print(myVacEnv.things_near(location=(0,0)))\n", + " print(\"\")\n", + " \n", + "#\n", + "# Now step the environment clock\n", + "#\n", + "\n", + " myVacEnv.step()\n", + " \n", + "#\n", + "# End of script\n", + "#" ] } ], From ada3954f2025132575175663afb6fdd591ec2fbe Mon Sep 17 00:00:00 2001 From: td2014 Date: Mon, 26 Dec 2016 09:08:08 -0800 Subject: [PATCH 06/32] Added agent program and first cut at environment override classes for Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 132 ++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index a0d02c82d..8da2e339d 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 204, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 205, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 206, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 207, "metadata": { "collapsed": false }, @@ -168,17 +168,6 @@ "# w.step()" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "step()" - ] - }, { "cell_type": "markdown", "metadata": { @@ -206,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 208, "metadata": { "collapsed": false }, @@ -244,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 209, "metadata": { "collapsed": false }, @@ -272,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 210, "metadata": { "collapsed": true }, @@ -339,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 211, "metadata": { "collapsed": false }, @@ -367,25 +356,31 @@ " if self.things[iThing].location==agent.location: #check location\n", " if self.things[iThing] != agent: # Don't return agent information\n", " if (isinstance(self.things[iThing], DirtClump)):\n", - " print (\"A thing which is not agent, but dirt clump = \", self.things[iThing] )\n", + " print (\"A thing which is not agent, but dirt a clump = \", self.things[iThing] )\n", " print (\"Location = \", self.things[iThing].location)\n", - " return \"DirtClump\"\n", + " return agent.location, \"DirtClump\"\n", " \n", - " return \"CleanSquare\" #Default, if we don't find a dirt clump.\n", + " return agent.location, \"CleanSquare\" #Default, if we don't find a dirt clump.\n", " \n", "# Need to override the action method (and update performance measure.)\n", - "### def execute_action(self, agent, action):\n", - "### if action==\"Suck\":\n", - "### print(\"Action-Suck\")\n", - "###\n", - "#### Remember to remove the dirt clod at this location.\n", - "###\n", - "### elif action==\"MoveRight\":\n", - "### print(\"Action-MoveRight\")\n", - "### elif action==\"MoveLeft\":\n", - "### print(\"Action-MoveLeft\")\n", - "### else:\n", - "### print(\"Action-None\")\n", + " def execute_action(self, agent, action):\n", + " print(\"Execute_action:\")\n", + " print(\"self = \", self)\n", + " print(\"agent = \", agent)\n", + " print(\"action = \", action)\n", + " print()\n", + " if action==\"Suck\":\n", + " print(\"Action-Suck\")\n", + " print(\"Need to remove dirt clod\")\n", + " elif action==\"MoveRight\":\n", + " print(\"Action-MoveRight\")\n", + " elif action==\"MoveLeft\":\n", + " print(\"Action-MoveLeft\")\n", + " elif action==\"DoNothing\":\n", + " print(\"Action-DoNothing\")\n", + " else:\n", + " print(\"Action-Not Understood\")\n", + " return\n", "###\n", "### Count up number of clean squares (indirectly)\n", "### and add that to the agent peformance score\n", @@ -411,9 +406,9 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 212, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ @@ -428,19 +423,33 @@ "# [(1,0),Dirty] -> Suck\n", "#\n", "\n", + "#\n", + "# Simple reflex cleaning program\n", + "#\n", + "def SimpleReflexClean(percept):\n", + " print (\"SimpleReflexClean - percept = \", percept)\n", + " print (\"percept[0] = \" , percept[0])\n", + " print (\"percept[1] = \" , percept[1])\n", + " \n", + " if percept[0] == (0,0) and percept[1]==\"DirtClump\":\n", + " return \"Suck\"\n", + " elif percept[0] == (1,0) and percept[1]==\"DirtClump\":\n", + " return \"Suck\"\n", + " elif percept[0] == (0,0) and percept[1]==\"CleanSquare\":\n", + " return \"MoveRight\"\n", + " elif percept[0] == (1,0) and percept[1]==\"CleanSquare\":\n", + " return \"MoveLeft\"\n", + " else:\n", + " return \"DoNothing\" # Not sure how you would get here, but DoNothing to be safe.\n", + "\n", "# Instantiate a simple reflex vacuum agent\n", "class adxyz_SimpleReflexAgentVacuum(Agent):\n", - " def program(percept):\n", - " print(\"Agent-Percept = \", percept)\n", - "### if percept==\"Dirt\":\n", - "### return \"Suck.\"\n", - "### else:\n", - " return \"Do Nothing.\"" + " pass" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 213, "metadata": { "collapsed": false }, @@ -459,7 +468,7 @@ "##initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", "##initDirt.append([(0,0)]) # square A dirty, square B clean\n", "initDirt.append([(1,0)]) # square A clean, square B dirty\n", - "##initDirt.append([(0,0),(1,0)]) # sqaure A dirty, square B dirty\n", + "##initDirt.append([(0,0),(1,0)]) # square A dirty, square B dirty\n", "\n", "print(initDirt[0])\n", "##print(initDirt[1])\n", @@ -469,7 +478,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 214, "metadata": { "collapsed": false }, @@ -491,17 +500,23 @@ "currInitDirtLocation = (1, 0)\n", "\n", "Environment:\n", - "[(, 0), (, 0)]\n", + "[(, 0), (, 1)]\n", "\n", "In override percept.\n", "Agent ID = \n", - "Agent location = (1, 0)\n", + "Agent location = (0, 0)\n", "Agent performance = 0\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x104a809b0>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n", "Self.things = [, ]\n", - "A thing which is not agent, but dirt clump = \n", - "Location = (1, 0)\n", - "Percept=DirtClump; action? None\n" + "SimpleReflexClean - percept = ((0, 0), 'CleanSquare')\n", + "percept[0] = (0, 0)\n", + "percept[1] = CleanSquare\n", + "Execute_action:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n", + "agent = \n", + "action = MoveRight\n", + "\n", + "Action-MoveRight\n" ] } ], @@ -521,7 +536,7 @@ " myDirtClumpList = []\n", "\n", " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", - " print(\"\")\n", + " print()\n", " print(\"inner loop:\")\n", " print (\"iSimDirtPlacement - inner loop = \" , iSimDirtPlacement)\n", " print (\"iPlace = \" , iPlace)\n", @@ -533,22 +548,23 @@ " print(\"currInitDirtLocation =\", currInitDirtLocation)\n", " myVacEnv.add_thing(myDirtClumpList[iPlace],location=currInitDirtLocation)\n", " \n", - "\n", "#\n", "# Now place the agent.\n", "#\n", " myAgent=adxyz_SimpleReflexAgentVacuum()\n", - " myVacEnv.add_thing(myAgent,location=(1,0))\n", - " print(\"\")\n", + " myAgent.program=SimpleReflexClean #Place the agent program here\n", + " myVacEnv.add_thing(myAgent,location=(0,0))\n", + " print()\n", " print(\"Environment:\")\n", " print(myVacEnv.things_near(location=(0,0)))\n", - " print(\"\")\n", + " print()\n", " \n", "#\n", "# Now step the environment clock\n", "#\n", - "\n", - " myVacEnv.step()\n", + " numSteps = 1\n", + " for iStep in range(numSteps):\n", + " myVacEnv.step()\n", " \n", "#\n", "# End of script\n", From 8ee4da2c056ebf44cfdf6f4286b7ccb82fca16c1 Mon Sep 17 00:00:00 2001 From: td2014 Date: Mon, 26 Dec 2016 10:18:18 -0800 Subject: [PATCH 07/32] Added direction class, suck, and move actions for Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 202 +++++++++++++++++++++++++++++++++------ 1 file changed, 172 insertions(+), 30 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 8da2e339d..0f7a51b6d 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 204, + "execution_count": 334, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 205, + "execution_count": 335, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 206, + "execution_count": 336, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 207, + "execution_count": 337, "metadata": { "collapsed": false }, @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 208, + "execution_count": 338, "metadata": { "collapsed": false }, @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 209, + "execution_count": 339, "metadata": { "collapsed": false }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 210, + "execution_count": 340, "metadata": { "collapsed": true }, @@ -328,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 211, + "execution_count": 341, "metadata": { "collapsed": false }, @@ -346,17 +346,19 @@ "# Need to override the percept method \n", " def percept(self, agent):\n", " '''By default, agent perceives things within a default radius.'''\n", - " print (\"In override percept.\")\n", + " print ()\n", + " print (\"In adxyz_VacuumEnvironment - percept override:\")\n", + " print (\"Self = \", self)\n", + " print (\"Self.things = \", self.things)\n", " print (\"Agent ID = \", agent)\n", " print (\"Agent location = \", agent.location)\n", " print (\"Agent performance = \", agent.performance)\n", - " print (\"Self = \", self)\n", - " print (\"Self.things = \", self.things)\n", + " \n", " for iThing in range(len(self.things)):\n", " if self.things[iThing].location==agent.location: #check location\n", " if self.things[iThing] != agent: # Don't return agent information\n", " if (isinstance(self.things[iThing], DirtClump)):\n", - " print (\"A thing which is not agent, but dirt a clump = \", self.things[iThing] )\n", + " print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing] )\n", " print (\"Location = \", self.things[iThing].location)\n", " return agent.location, \"DirtClump\"\n", " \n", @@ -364,18 +366,45 @@ " \n", "# Need to override the action method (and update performance measure.)\n", " def execute_action(self, agent, action):\n", - " print(\"Execute_action:\")\n", + " print ()\n", + " print (\"In adxyz_VacuumEnvironment - execute_action override:\")\n", " print(\"self = \", self)\n", " print(\"agent = \", agent)\n", " print(\"action = \", action)\n", " print()\n", " if action==\"Suck\":\n", " print(\"Action-Suck\")\n", - " print(\"Need to remove dirt clod\")\n", + " print(\"Need to remove dirt clod at correct location\")\n", + " for iThing in range(len(self.things)):\n", + " if self.things[iThing].location==agent.location: #check location\n", + " if (isinstance(self.things[iThing], DirtClump)): # Only clean dirt\n", + " print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing])\n", + " print (\"Location of dirt clod = \", self.things[iThing].location)\n", + " self.delete_thing(self.things[iThing])\n", + " return\n", + " \n", " elif action==\"MoveRight\":\n", " print(\"Action-MoveRight\")\n", + " print(\"agent direction before MoveRight = \", agent.direction)\n", + " print(\"agent location before MoveRight = \", agent.location)\n", + " agent.bump = False\n", + " agent.direction = agent.direction + Direction.R\n", + " agent.direction = agent.direction + Direction.R\n", + " agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))\n", + " print(\"agent direction after MoveRight = \", agent.direction)\n", + " print(\"agent location after MoveRight = \", agent.location)\n", + " print()\n", " elif action==\"MoveLeft\":\n", " print(\"Action-MoveLeft\")\n", + " print(\"agent direction before MoveLeft = \", agent.direction)\n", + " print(\"agent location before MoveLeft = \", agent.location)\n", + " agent.bump = False\n", + " agent.direction = agent.direction + Direction.L\n", + " agent.direction = agent.direction + Direction.L\n", + " agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))\n", + " print(\"agent direction after MoveLeft = \", agent.direction)\n", + " print(\"agent location after MoveLeft = \", agent.location)\n", + " print()\n", " elif action==\"DoNothing\":\n", " print(\"Action-DoNothing\")\n", " else:\n", @@ -406,7 +435,7 @@ }, { "cell_type": "code", - "execution_count": 212, + "execution_count": 342, "metadata": { "collapsed": false }, @@ -423,9 +452,6 @@ "# [(1,0),Dirty] -> Suck\n", "#\n", "\n", - "#\n", - "# Simple reflex cleaning program\n", - "#\n", "def SimpleReflexClean(percept):\n", " print (\"SimpleReflexClean - percept = \", percept)\n", " print (\"percept[0] = \" , percept[0])\n", @@ -444,12 +470,13 @@ "\n", "# Instantiate a simple reflex vacuum agent\n", "class adxyz_SimpleReflexAgentVacuum(Agent):\n", - " pass" + " pass\n", + "\n" ] }, { "cell_type": "code", - "execution_count": 213, + "execution_count": 343, "metadata": { "collapsed": false }, @@ -478,7 +505,7 @@ }, { "cell_type": "code", - "execution_count": 214, + "execution_count": 344, "metadata": { "collapsed": false }, @@ -500,23 +527,128 @@ "currInitDirtLocation = (1, 0)\n", "\n", "Environment:\n", - "[(, 0), (, 1)]\n", + "[(, 0), (, 0)]\n", + "\n", + "step# = 0\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self.things = [, ]\n", + "Agent ID = \n", + "Agent location = (1, 0)\n", + "Agent performance = 0\n", + "A thing which is not agent, but a dirt clump = \n", + "Location = (1, 0)\n", + "SimpleReflexClean - percept = ((1, 0), 'DirtClump')\n", + "percept[0] = (1, 0)\n", + "percept[1] = DirtClump\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "agent = \n", + "action = Suck\n", + "\n", + "Action-Suck\n", + "Need to remove dirt clod at correct location\n", + "A thing which is not agent, but a dirt clump = \n", + "Location of dirt clod = (1, 0)\n", + "------\n", + "------\n", + "\n", + "step# = 1\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (1, 0)\n", + "Agent performance = 0\n", + "SimpleReflexClean - percept = ((1, 0), 'CleanSquare')\n", + "percept[0] = (1, 0)\n", + "percept[1] = CleanSquare\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "agent = \n", + "action = MoveLeft\n", "\n", - "In override percept.\n", + "Action-MoveLeft\n", + "agent direction before MoveLeft = \n", + "agent location before MoveLeft = (1, 0)\n", + "agent direction after MoveLeft = \n", + "agent location after MoveLeft = (0, 0)\n", + "\n", + "------\n", + "------\n", + "\n", + "step# = 2\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 0\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n", - "Self.things = [, ]\n", "SimpleReflexClean - percept = ((0, 0), 'CleanSquare')\n", "percept[0] = (0, 0)\n", "percept[1] = CleanSquare\n", - "Execute_action:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", "agent = \n", "action = MoveRight\n", "\n", - "Action-MoveRight\n" + "Action-MoveRight\n", + "agent direction before MoveRight = \n", + "agent location before MoveRight = (0, 0)\n", + "agent direction after MoveRight = \n", + "agent location after MoveRight = (0, -1)\n", + "\n", + "------\n", + "------\n", + "\n", + "step# = 3\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (0, -1)\n", + "Agent performance = 0\n", + "SimpleReflexClean - percept = ((0, -1), 'CleanSquare')\n", + "percept[0] = (0, -1)\n", + "percept[1] = CleanSquare\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "agent = \n", + "action = DoNothing\n", + "\n", + "Action-DoNothing\n", + "------\n", + "------\n", + "\n", + "step# = 4\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (0, -1)\n", + "Agent performance = 0\n", + "SimpleReflexClean - percept = ((0, -1), 'CleanSquare')\n", + "percept[0] = (0, -1)\n", + "percept[1] = CleanSquare\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "agent = \n", + "action = DoNothing\n", + "\n", + "Action-DoNothing\n", + "------\n", + "------\n", + "\n" ] } ], @@ -553,7 +685,13 @@ "#\n", " myAgent=adxyz_SimpleReflexAgentVacuum()\n", " myAgent.program=SimpleReflexClean #Place the agent program here\n", - " myVacEnv.add_thing(myAgent,location=(0,0))\n", + "\n", + "# Instantiate a direction object for 2D generality\n", + " myAgent.direction = Direction(\"right\")\n", + "### mySimpleReflexAgentVacuumDirection = Direction(myAgent.direction)\n", + " \n", + "# Add agent to environment\n", + " myVacEnv.add_thing(myAgent,location=(1,0))\n", " print()\n", " print(\"Environment:\")\n", " print(myVacEnv.things_near(location=(0,0)))\n", @@ -562,9 +700,13 @@ "#\n", "# Now step the environment clock\n", "#\n", - " numSteps = 1\n", + " numSteps = 5\n", " for iStep in range(numSteps):\n", + " print(\"step# = \", iStep)\n", " myVacEnv.step()\n", + " print(\"------\")\n", + " print(\"------\")\n", + " print()\n", " \n", "#\n", "# End of script\n", From b2ed15bd4f958bcc8ef764da162d1626826e0e92 Mon Sep 17 00:00:00 2001 From: td2014 Date: Tue, 27 Dec 2016 09:38:59 -0800 Subject: [PATCH 08/32] Added performance measure and printing cleanup for Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 291 +++++++++++++++++++++------------------ 1 file changed, 157 insertions(+), 134 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 0f7a51b6d..ba9769909 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 334, + "execution_count": 547, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 335, + "execution_count": 548, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 336, + "execution_count": 549, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 337, + "execution_count": 550, "metadata": { "collapsed": false }, @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 338, + "execution_count": 551, "metadata": { "collapsed": false }, @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 339, + "execution_count": 552, "metadata": { "collapsed": false }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 340, + "execution_count": 553, "metadata": { "collapsed": true }, @@ -328,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 341, + "execution_count": 554, "metadata": { "collapsed": false }, @@ -345,7 +345,6 @@ "\n", "# Need to override the percept method \n", " def percept(self, agent):\n", - " '''By default, agent perceives things within a default radius.'''\n", " print ()\n", " print (\"In adxyz_VacuumEnvironment - percept override:\")\n", " print (\"Self = \", self)\n", @@ -370,19 +369,20 @@ " print (\"In adxyz_VacuumEnvironment - execute_action override:\")\n", " print(\"self = \", self)\n", " print(\"agent = \", agent)\n", - " print(\"action = \", action)\n", + " print(\"current agent action = \", action)\n", " print()\n", " if action==\"Suck\":\n", " print(\"Action-Suck\")\n", - " print(\"Need to remove dirt clod at correct location\")\n", + " print(\"Need to remove dirt clump at correct location\")\n", + " deleteList = []\n", " for iThing in range(len(self.things)):\n", " if self.things[iThing].location==agent.location: #check location\n", " if (isinstance(self.things[iThing], DirtClump)): # Only clean dirt\n", " print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing])\n", " print (\"Location of dirt clod = \", self.things[iThing].location)\n", " self.delete_thing(self.things[iThing])\n", - " return\n", - " \n", + " break # can only do one deletion per action.\n", + " \n", " elif action==\"MoveRight\":\n", " print(\"Action-MoveRight\")\n", " print(\"agent direction before MoveRight = \", agent.direction)\n", @@ -394,6 +394,7 @@ " print(\"agent direction after MoveRight = \", agent.direction)\n", " print(\"agent location after MoveRight = \", agent.location)\n", " print()\n", + " \n", " elif action==\"MoveLeft\":\n", " print(\"Action-MoveLeft\")\n", " print(\"agent direction before MoveLeft = \", agent.direction)\n", @@ -405,25 +406,28 @@ " print(\"agent direction after MoveLeft = \", agent.direction)\n", " print(\"agent location after MoveLeft = \", agent.location)\n", " print()\n", + " \n", " elif action==\"DoNothing\":\n", " print(\"Action-DoNothing\")\n", + " \n", " else:\n", - " print(\"Action-Not Understood\")\n", - " return\n", + " print(\"Action-Not Understood\") #probably error. Don't go to score section.\n", + " return\n", + " \n", "###\n", "### Count up number of clean squares (indirectly)\n", "### and add that to the agent peformance score\n", "###\n", - "### dirtCount=0\n", - "### for iThings in range(len(self.things)):\n", - "### if isInstance(self.things[iThings], DirtClump):\n", - "### dirtCount = dirtCount+1\n", - "###\n", - "### cleanSquareCount = self.totalSquareCount-dirtCount \n", - "### agent.performance=agent.performance + 2*cleanSquareCount\n", - "### return\n", - "###\n", - " " + " print(\"Before dirt count update, agent.performance = \", agent.performance)\n", + " dirtCount=0\n", + " for iThings in range(len(self.things)):\n", + " if isinstance(self.things[iThings], DirtClump):\n", + " dirtCount = dirtCount+1\n", + "\n", + " cleanSquareCount = self.width*self.height-dirtCount \n", + " agent.performance=agent.performance + cleanSquareCount\n", + " print(\"After execute_action, agent.performance = \", agent.performance)\n", + " return " ] }, { @@ -435,7 +439,7 @@ }, { "cell_type": "code", - "execution_count": 342, + "execution_count": 555, "metadata": { "collapsed": false }, @@ -453,9 +457,6 @@ "#\n", "\n", "def SimpleReflexClean(percept):\n", - " print (\"SimpleReflexClean - percept = \", percept)\n", - " print (\"percept[0] = \" , percept[0])\n", - " print (\"percept[1] = \" , percept[1])\n", " \n", " if percept[0] == (0,0) and percept[1]==\"DirtClump\":\n", " return \"Suck\"\n", @@ -470,13 +471,12 @@ "\n", "# Instantiate a simple reflex vacuum agent\n", "class adxyz_SimpleReflexAgentVacuum(Agent):\n", - " pass\n", - "\n" + " pass" ] }, { "cell_type": "code", - "execution_count": 343, + "execution_count": 556, "metadata": { "collapsed": false }, @@ -485,17 +485,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "[(1, 0)]\n" + "[(0, 0), (1, 0)]\n" ] } ], "source": [ "# Define the initial dirt configurations\n", "initDirt=[]\n", - "##initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", + "initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", "##initDirt.append([(0,0)]) # square A dirty, square B clean\n", - "initDirt.append([(1,0)]) # square A clean, square B dirty\n", - "##initDirt.append([(0,0),(1,0)]) # square A dirty, square B dirty\n", + "##initDirt.append([(1,0)]) # square A clean, square B dirty\n", + "###initDirt.append([(0,0),(1,0)]) # square A dirty, square B dirty\n", "\n", "print(initDirt[0])\n", "##print(initDirt[1])\n", @@ -505,7 +505,7 @@ }, { "cell_type": "code", - "execution_count": 344, + "execution_count": 557, "metadata": { "collapsed": false }, @@ -514,140 +514,152 @@ "name": "stdout", "output_type": "stream", "text": [ - "outer loop:\n", - "len of initDirt = 1\n", - "iSimDirtPlacement - outer loop = 0\n", - "\n", - "inner loop:\n", - "iSimDirtPlacement - inner loop = 0\n", - "iPlace = 0\n", - "1\n", - "[(1, 0)]\n", - "myDirtClumpList = []\n", - "currInitDirtLocation = (1, 0)\n", + "Simulation: iSimAgentPlacement = 0\n", + "Simulation: iSimDirtPlacement = 0\n", + "Simulation: iPlace = 0\n", + "Simulation: currInitDirtLocation = (0, 0)\n", + "Simulation: iPlace = 1\n", + "Simulation: currInitDirtLocation = (1, 0)\n", "\n", "Environment:\n", - "[(, 0), (, 0)]\n", + " (0, 0)\n", + " (1, 0)\n", + " (1, 0)\n", "\n", - "step# = 0\n", + "\n", + "<---START--->\n", + "Simulation: step = 0\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", - "Self.things = [, ]\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "Self.things = [, , ]\n", "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 0\n", "A thing which is not agent, but a dirt clump = \n", "Location = (1, 0)\n", - "SimpleReflexClean - percept = ((1, 0), 'DirtClump')\n", - "percept[0] = (1, 0)\n", - "percept[1] = DirtClump\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "agent = \n", - "action = Suck\n", + "current agent action = Suck\n", "\n", "Action-Suck\n", - "Need to remove dirt clod at correct location\n", + "Need to remove dirt clump at correct location\n", "A thing which is not agent, but a dirt clump = \n", "Location of dirt clod = (1, 0)\n", - "------\n", - "------\n", + "Before dirt count update, agent.performance = 0\n", + "After execute_action, agent.performance = 1\n", + "---END---\n", + "---------\n", + "\n", "\n", - "step# = 1\n", + "<---START--->\n", + "Simulation: step = 1\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", - "Self.things = []\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "Self.things = [, ]\n", "Agent ID = \n", "Agent location = (1, 0)\n", - "Agent performance = 0\n", - "SimpleReflexClean - percept = ((1, 0), 'CleanSquare')\n", - "percept[0] = (1, 0)\n", - "percept[1] = CleanSquare\n", + "Agent performance = 1\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "agent = \n", - "action = MoveLeft\n", + "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", - "------\n", - "------\n", + "Before dirt count update, agent.performance = 1\n", + "After execute_action, agent.performance = 2\n", + "---END---\n", + "---------\n", "\n", - "step# = 2\n", + "\n", + "<---START--->\n", + "Simulation: step = 2\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", - "Self.things = []\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "Self.things = [, ]\n", "Agent ID = \n", "Agent location = (0, 0)\n", - "Agent performance = 0\n", - "SimpleReflexClean - percept = ((0, 0), 'CleanSquare')\n", - "percept[0] = (0, 0)\n", - "percept[1] = CleanSquare\n", + "Agent performance = 2\n", + "A thing which is not agent, but a dirt clump = \n", + "Location = (0, 0)\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "agent = \n", - "action = MoveRight\n", + "current agent action = Suck\n", "\n", - "Action-MoveRight\n", - "agent direction before MoveRight = \n", - "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", - "agent location after MoveRight = (0, -1)\n", + "Action-Suck\n", + "Need to remove dirt clump at correct location\n", + "A thing which is not agent, but a dirt clump = \n", + "Location of dirt clod = (0, 0)\n", + "Before dirt count update, agent.performance = 2\n", + "After execute_action, agent.performance = 4\n", + "---END---\n", + "---------\n", "\n", - "------\n", - "------\n", "\n", - "step# = 3\n", + "<---START--->\n", + "Simulation: step = 3\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "Self.things = []\n", "Agent ID = \n", - "Agent location = (0, -1)\n", - "Agent performance = 0\n", - "SimpleReflexClean - percept = ((0, -1), 'CleanSquare')\n", - "percept[0] = (0, -1)\n", - "percept[1] = CleanSquare\n", + "Agent location = (0, 0)\n", + "Agent performance = 4\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "agent = \n", - "action = DoNothing\n", + "current agent action = MoveRight\n", + "\n", + "Action-MoveRight\n", + "agent direction before MoveRight = \n", + "agent location before MoveRight = (0, 0)\n", + "agent direction after MoveRight = \n", + "agent location after MoveRight = (1, 0)\n", "\n", - "Action-DoNothing\n", - "------\n", - "------\n", + "Before dirt count update, agent.performance = 4\n", + "After execute_action, agent.performance = 6\n", + "---END---\n", + "---------\n", "\n", - "step# = 4\n", + "\n", + "<---START--->\n", + "Simulation: step = 4\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "Self.things = []\n", "Agent ID = \n", - "Agent location = (0, -1)\n", - "Agent performance = 0\n", - "SimpleReflexClean - percept = ((0, -1), 'CleanSquare')\n", - "percept[0] = (0, -1)\n", - "percept[1] = CleanSquare\n", + "Agent location = (1, 0)\n", + "Agent performance = 6\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", "agent = \n", - "action = DoNothing\n", + "current agent action = MoveLeft\n", + "\n", + "Action-MoveLeft\n", + "agent direction before MoveLeft = \n", + "agent location before MoveLeft = (1, 0)\n", + "agent direction after MoveLeft = \n", + "agent location after MoveLeft = (0, 0)\n", "\n", - "Action-DoNothing\n", - "------\n", - "------\n", + "Before dirt count update, agent.performance = 6\n", + "After execute_action, agent.performance = 8\n", + "---END---\n", + "---------\n", "\n" ] } @@ -658,43 +670,38 @@ "# Loop over agent placements\n", "##for iSimAgentPlacement in range(len(initAgent)):\n", "for iSimAgentPlacement in range(1):\n", + " print(\"Simulation: iSimAgentPlacement = \", iSimAgentPlacement)\n", + "\n", "# Loop over dirt placements\n", " for iSimDirtPlacement in range(len(initDirt)):\n", - " print(\"outer loop:\")\n", - " print(\"len of initDirt = \", len(initDirt))\n", - " print (\"iSimDirtPlacement - outer loop = \" , iSimDirtPlacement)\n", + " print (\"Simulation: iSimDirtPlacement = \" , iSimDirtPlacement)\n", " \n", " myVacEnv = adxyz_VacuumEnvironment() #Create a new environment for each dirt/agent setup\n", - " myDirtClumpList = []\n", + " myVacEnv.width = 2\n", + " myVacEnv.height = 1\n", "\n", " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", - " print()\n", - " print(\"inner loop:\")\n", - " print (\"iSimDirtPlacement - inner loop = \" , iSimDirtPlacement)\n", - " print (\"iPlace = \" , iPlace)\n", - " print(len(initDirt[iSimDirtPlacement]))\n", - " print(initDirt[iSimDirtPlacement])\n", - " myDirtClumpList.append(DirtClump())\n", - " print(\"myDirtClumpList = \", myDirtClumpList)\n", + " print (\"Simulation: iPlace = \" , iPlace)\n", " currInitDirtLocation = initDirt[iSimDirtPlacement][iPlace]\n", - " print(\"currInitDirtLocation =\", currInitDirtLocation)\n", - " myVacEnv.add_thing(myDirtClumpList[iPlace],location=currInitDirtLocation)\n", + " print(\"Simulation: currInitDirtLocation = \", currInitDirtLocation)\n", + " myVacEnv.add_thing(DirtClump(),location=currInitDirtLocation)\n", " \n", "#\n", - "# Now place the agent.\n", + "# Now setup the agent.\n", "#\n", " myAgent=adxyz_SimpleReflexAgentVacuum()\n", " myAgent.program=SimpleReflexClean #Place the agent program here\n", + " myAgent.performance=0\n", "\n", "# Instantiate a direction object for 2D generality\n", - " myAgent.direction = Direction(\"right\")\n", - "### mySimpleReflexAgentVacuumDirection = Direction(myAgent.direction)\n", + " myAgent.direction = Direction(\"right\") # need to leverage heading mechanism\n", " \n", "# Add agent to environment\n", " myVacEnv.add_thing(myAgent,location=(1,0))\n", " print()\n", " print(\"Environment:\")\n", - " print(myVacEnv.things_near(location=(0,0)))\n", + " for iThings in myVacEnv.things:\n", + " print(iThings, iThings.location)\n", " print()\n", " \n", "#\n", @@ -702,16 +709,32 @@ "#\n", " numSteps = 5\n", " for iStep in range(numSteps):\n", - " print(\"step# = \", iStep)\n", + " print()\n", + " print(\"<---START--->\")\n", + " print(\"Simulation: step =\", iStep)\n", " myVacEnv.step()\n", - " print(\"------\")\n", - " print(\"------\")\n", + " print(\"---END---\")\n", + " print(\"---------\")\n", " print()\n", " \n", "#\n", "# End of script\n", "#" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Todo:\n", + "- Get scoring correct - use internal values, not hardcoded\n", + "- Clean up comments/prints\n", + "- Make processing more generalized\n", + "-- Introduce multiple dirt clods.\n", + "-- Introduce multiple agents.\n", + "-- Add heading sense\n", + "- Move data to cloud" + ] } ], "metadata": { From 32a81823f6ecc67fe47fb441a5ef69f5b32eef71 Mon Sep 17 00:00:00 2001 From: td2014 Date: Tue, 27 Dec 2016 20:25:38 -0800 Subject: [PATCH 09/32] Cleanup of agent direction processing-force left or right heading before moving forward- for Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 364 ++++++++++++++++++++++++++++----------- 1 file changed, 260 insertions(+), 104 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index ba9769909..365c8f671 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 547, + "execution_count": 690, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 548, + "execution_count": 691, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 549, + "execution_count": 692, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 550, + "execution_count": 693, "metadata": { "collapsed": false }, @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 551, + "execution_count": 694, "metadata": { "collapsed": false }, @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 552, + "execution_count": 695, "metadata": { "collapsed": false }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 553, + "execution_count": 696, "metadata": { "collapsed": true }, @@ -328,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 554, + "execution_count": 697, "metadata": { "collapsed": false }, @@ -353,12 +353,12 @@ " print (\"Agent location = \", agent.location)\n", " print (\"Agent performance = \", agent.performance)\n", " \n", - " for iThing in range(len(self.things)):\n", - " if self.things[iThing].location==agent.location: #check location\n", - " if self.things[iThing] != agent: # Don't return agent information\n", - " if (isinstance(self.things[iThing], DirtClump)):\n", - " print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing] )\n", - " print (\"Location = \", self.things[iThing].location)\n", + " for iThing in self.things:\n", + " if iThing.location==agent.location: #check location\n", + " if iThing != agent: # Don't return agent information\n", + " if (isinstance(iThing, DirtClump)):\n", + " print (\"A thing which is not agent, but a dirt clump = \", iThing )\n", + " print (\"Location = \", iThing.location)\n", " return agent.location, \"DirtClump\"\n", " \n", " return agent.location, \"CleanSquare\" #Default, if we don't find a dirt clump.\n", @@ -375,12 +375,12 @@ " print(\"Action-Suck\")\n", " print(\"Need to remove dirt clump at correct location\")\n", " deleteList = []\n", - " for iThing in range(len(self.things)):\n", - " if self.things[iThing].location==agent.location: #check location\n", - " if (isinstance(self.things[iThing], DirtClump)): # Only clean dirt\n", - " print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing])\n", - " print (\"Location of dirt clod = \", self.things[iThing].location)\n", - " self.delete_thing(self.things[iThing])\n", + " for iThing in self.things:\n", + " if iThing.location==agent.location: #check location\n", + " if (isinstance(iThing, DirtClump)): # Only suck dirt\n", + " print (\"A thing which is not agent, but a dirt clump = \", iThing)\n", + " print (\"Location of dirt clod = \", iThing.location)\n", + " self.delete_thing(iThing)\n", " break # can only do one deletion per action.\n", " \n", " elif action==\"MoveRight\":\n", @@ -388,8 +388,7 @@ " print(\"agent direction before MoveRight = \", agent.direction)\n", " print(\"agent location before MoveRight = \", agent.location)\n", " agent.bump = False\n", - " agent.direction = agent.direction + Direction.R\n", - " agent.direction = agent.direction + Direction.R\n", + " agent.direction.direction = \"right\"\n", " agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))\n", " print(\"agent direction after MoveRight = \", agent.direction)\n", " print(\"agent location after MoveRight = \", agent.location)\n", @@ -400,8 +399,7 @@ " print(\"agent direction before MoveLeft = \", agent.direction)\n", " print(\"agent location before MoveLeft = \", agent.location)\n", " agent.bump = False\n", - " agent.direction = agent.direction + Direction.L\n", - " agent.direction = agent.direction + Direction.L\n", + " agent.direction.direction = \"left\"\n", " agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))\n", " print(\"agent direction after MoveLeft = \", agent.direction)\n", " print(\"agent location after MoveLeft = \", agent.location)\n", @@ -420,8 +418,8 @@ "###\n", " print(\"Before dirt count update, agent.performance = \", agent.performance)\n", " dirtCount=0\n", - " for iThings in range(len(self.things)):\n", - " if isinstance(self.things[iThings], DirtClump):\n", + " for iThing in self.things:\n", + " if isinstance(iThing, DirtClump):\n", " dirtCount = dirtCount+1\n", "\n", " cleanSquareCount = self.width*self.height-dirtCount \n", @@ -439,7 +437,7 @@ }, { "cell_type": "code", - "execution_count": 555, + "execution_count": 698, "metadata": { "collapsed": false }, @@ -476,7 +474,7 @@ }, { "cell_type": "code", - "execution_count": 556, + "execution_count": 699, "metadata": { "collapsed": false }, @@ -485,7 +483,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "[(0, 0), (1, 0)]\n" + "[[]]\n", + "initAgent = [(0, 0), (1, 0)]\n" ] } ], @@ -493,19 +492,24 @@ "# Define the initial dirt configurations\n", "initDirt=[]\n", "initDirt.append([]) # neither location dirty - format(X,Y)-locations:A=(0,0), B=(1,0)\n", - "##initDirt.append([(0,0)]) # square A dirty, square B clean\n", + "###initDirt.append([(0,0)]) # square A dirty, square B clean\n", "##initDirt.append([(1,0)]) # square A clean, square B dirty\n", "###initDirt.append([(0,0),(1,0)]) # square A dirty, square B dirty\n", "\n", - "print(initDirt[0])\n", - "##print(initDirt[1])\n", - "##print(initDirt[2])\n", - "##print(initDirt[3])" + "print(\"initDirt = \", initDirt)\n", + "\n", + "#\n", + "# Create agent placements\n", + "#\n", + "initAgent=[]\n", + "initAgent.append((0,0))\n", + "initAgent.append((1,0))\n", + "print(\"initAgent = \", initAgent)" ] }, { "cell_type": "code", - "execution_count": 557, + "execution_count": 700, "metadata": { "collapsed": false }, @@ -516,150 +520,299 @@ "text": [ "Simulation: iSimAgentPlacement = 0\n", "Simulation: iSimDirtPlacement = 0\n", - "Simulation: iPlace = 0\n", - "Simulation: currInitDirtLocation = (0, 0)\n", - "Simulation: iPlace = 1\n", - "Simulation: currInitDirtLocation = (1, 0)\n", "\n", "Environment:\n", - " (0, 0)\n", - " (1, 0)\n", - " (1, 0)\n", + " (0, 0)\n", "\n", "\n", - "<---START--->\n", + "<-START->\n", "Simulation: step = 0\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", - "Self.things = [, , ]\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self.things = []\n", "Agent ID = \n", - "Agent location = (1, 0)\n", + "Agent location = (0, 0)\n", "Agent performance = 0\n", - "A thing which is not agent, but a dirt clump = \n", - "Location = (1, 0)\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", "agent = \n", - "current agent action = Suck\n", + "current agent action = MoveRight\n", + "\n", + "Action-MoveRight\n", + "agent direction before MoveRight = \n", + "agent location before MoveRight = (0, 0)\n", + "agent direction after MoveRight = \n", + "agent location after MoveRight = (1, 0)\n", "\n", - "Action-Suck\n", - "Need to remove dirt clump at correct location\n", - "A thing which is not agent, but a dirt clump = \n", - "Location of dirt clod = (1, 0)\n", "Before dirt count update, agent.performance = 0\n", - "After execute_action, agent.performance = 1\n", + "After execute_action, agent.performance = 2\n", "---END---\n", "---------\n", "\n", "\n", - "<---START--->\n", + "<-START->\n", "Simulation: step = 1\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", - "Self.things = [, ]\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", - "Agent performance = 1\n", + "Agent performance = 2\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", - "Before dirt count update, agent.performance = 1\n", - "After execute_action, agent.performance = 2\n", + "Before dirt count update, agent.performance = 2\n", + "After execute_action, agent.performance = 4\n", "---END---\n", "---------\n", "\n", "\n", - "<---START--->\n", + "<-START->\n", "Simulation: step = 2\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", - "Self.things = [, ]\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (0, 0)\n", + "Agent performance = 4\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "agent = \n", + "current agent action = MoveRight\n", + "\n", + "Action-MoveRight\n", + "agent direction before MoveRight = \n", + "agent location before MoveRight = (0, 0)\n", + "agent direction after MoveRight = \n", + "agent location after MoveRight = (1, 0)\n", + "\n", + "Before dirt count update, agent.performance = 4\n", + "After execute_action, agent.performance = 6\n", + "---END---\n", + "---------\n", + "\n", + "\n", + "<-START->\n", + "Simulation: step = 3\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (1, 0)\n", + "Agent performance = 6\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "agent = \n", + "current agent action = MoveLeft\n", + "\n", + "Action-MoveLeft\n", + "agent direction before MoveLeft = \n", + "agent location before MoveLeft = (1, 0)\n", + "agent direction after MoveLeft = \n", + "agent location after MoveLeft = (0, 0)\n", + "\n", + "Before dirt count update, agent.performance = 6\n", + "After execute_action, agent.performance = 8\n", + "---END---\n", + "---------\n", + "\n", + "\n", + "<-START->\n", + "Simulation: step = 4\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (0, 0)\n", + "Agent performance = 8\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "agent = \n", + "current agent action = MoveRight\n", + "\n", + "Action-MoveRight\n", + "agent direction before MoveRight = \n", + "agent location before MoveRight = (0, 0)\n", + "agent direction after MoveRight = \n", + "agent location after MoveRight = (1, 0)\n", + "\n", + "Before dirt count update, agent.performance = 8\n", + "After execute_action, agent.performance = 10\n", + "---END---\n", + "---------\n", + "\n", + "\n", + "<====>\n", + "<====>\n", + "Final performance measure for Agent = 10\n", + "======\n", + "======\n", + "\n", + "Simulation: iSimAgentPlacement = 1\n", + "Simulation: iSimDirtPlacement = 0\n", + "\n", + "Environment:\n", + " (1, 0)\n", + "\n", + "\n", + "<-START->\n", + "Simulation: step = 0\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (1, 0)\n", + "Agent performance = 0\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "agent = \n", + "current agent action = MoveLeft\n", + "\n", + "Action-MoveLeft\n", + "agent direction before MoveLeft = \n", + "agent location before MoveLeft = (1, 0)\n", + "agent direction after MoveLeft = \n", + "agent location after MoveLeft = (0, 0)\n", + "\n", + "Before dirt count update, agent.performance = 0\n", + "After execute_action, agent.performance = 2\n", + "---END---\n", + "---------\n", + "\n", + "\n", + "<-START->\n", + "Simulation: step = 1\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 2\n", - "A thing which is not agent, but a dirt clump = \n", - "Location = (0, 0)\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", "agent = \n", - "current agent action = Suck\n", + "current agent action = MoveRight\n", + "\n", + "Action-MoveRight\n", + "agent direction before MoveRight = \n", + "agent location before MoveRight = (0, 0)\n", + "agent direction after MoveRight = \n", + "agent location after MoveRight = (1, 0)\n", "\n", - "Action-Suck\n", - "Need to remove dirt clump at correct location\n", - "A thing which is not agent, but a dirt clump = \n", - "Location of dirt clod = (0, 0)\n", "Before dirt count update, agent.performance = 2\n", "After execute_action, agent.performance = 4\n", "---END---\n", "---------\n", "\n", "\n", - "<---START--->\n", + "<-START->\n", + "Simulation: step = 2\n", + "\n", + "In adxyz_VacuumEnvironment - percept override:\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self.things = []\n", + "Agent ID = \n", + "Agent location = (1, 0)\n", + "Agent performance = 4\n", + "\n", + "In adxyz_VacuumEnvironment - execute_action override:\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "agent = \n", + "current agent action = MoveLeft\n", + "\n", + "Action-MoveLeft\n", + "agent direction before MoveLeft = \n", + "agent location before MoveLeft = (1, 0)\n", + "agent direction after MoveLeft = \n", + "agent location after MoveLeft = (0, 0)\n", + "\n", + "Before dirt count update, agent.performance = 4\n", + "After execute_action, agent.performance = 6\n", + "---END---\n", + "---------\n", + "\n", + "\n", + "<-START->\n", "Simulation: step = 3\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", - "Agent performance = 4\n", + "Agent performance = 6\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", - "Before dirt count update, agent.performance = 4\n", - "After execute_action, agent.performance = 6\n", + "Before dirt count update, agent.performance = 6\n", + "After execute_action, agent.performance = 8\n", "---END---\n", "---------\n", "\n", "\n", - "<---START--->\n", + "<-START->\n", "Simulation: step = 4\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", - "Agent performance = 6\n", + "Agent performance = 8\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105498128>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", - "Before dirt count update, agent.performance = 6\n", - "After execute_action, agent.performance = 8\n", + "Before dirt count update, agent.performance = 8\n", + "After execute_action, agent.performance = 10\n", "---END---\n", "---------\n", + "\n", + "\n", + "<====>\n", + "<====>\n", + "Final performance measure for Agent = 10\n", + "======\n", + "======\n", "\n" ] } @@ -668,23 +821,20 @@ "# Create a loop over environments to run simulation\n", "\n", "# Loop over agent placements\n", - "##for iSimAgentPlacement in range(len(initAgent)):\n", - "for iSimAgentPlacement in range(1):\n", + "for iSimAgentPlacement in range(len(initAgent)):\n", + "###for iSimAgentPlacement in range(1):\n", " print(\"Simulation: iSimAgentPlacement = \", iSimAgentPlacement)\n", "\n", "# Loop over dirt placements\n", " for iSimDirtPlacement in range(len(initDirt)):\n", " print (\"Simulation: iSimDirtPlacement = \" , iSimDirtPlacement)\n", - " \n", " myVacEnv = adxyz_VacuumEnvironment() #Create a new environment for each dirt/agent setup\n", " myVacEnv.width = 2\n", " myVacEnv.height = 1\n", "\n", " for iPlace in range(len(initDirt[iSimDirtPlacement])):\n", " print (\"Simulation: iPlace = \" , iPlace)\n", - " currInitDirtLocation = initDirt[iSimDirtPlacement][iPlace]\n", - " print(\"Simulation: currInitDirtLocation = \", currInitDirtLocation)\n", - " myVacEnv.add_thing(DirtClump(),location=currInitDirtLocation)\n", + " myVacEnv.add_thing(DirtClump(),location=initDirt[iSimDirtPlacement][iPlace])\n", " \n", "#\n", "# Now setup the agent.\n", @@ -694,10 +844,10 @@ " myAgent.performance=0\n", "\n", "# Instantiate a direction object for 2D generality\n", - " myAgent.direction = Direction(\"right\") # need to leverage heading mechanism\n", + " myAgent.direction = Direction(\"up\") # need to leverage heading mechanism\n", " \n", "# Add agent to environment\n", - " myVacEnv.add_thing(myAgent,location=(1,0))\n", + " myVacEnv.add_thing(myAgent,location=initAgent[iSimAgentPlacement])\n", " print()\n", " print(\"Environment:\")\n", " for iThings in myVacEnv.things:\n", @@ -710,13 +860,21 @@ " numSteps = 5\n", " for iStep in range(numSteps):\n", " print()\n", - " print(\"<---START--->\")\n", + " print(\"<-START->\")\n", " print(\"Simulation: step =\", iStep)\n", " myVacEnv.step()\n", " print(\"---END---\")\n", " print(\"---------\")\n", " print()\n", - " \n", + " \n", + " print() \n", + " print(\"<====>\")\n", + " print(\"<====>\")\n", + " #need to keep running tally of initial configuration and final performance\n", + " print(\"Final performance measure for Agent = \", myAgent.performance)\n", + " print(\"======\")\n", + " print(\"======\")\n", + " print()\n", "#\n", "# End of script\n", "#" @@ -727,12 +885,10 @@ "metadata": {}, "source": [ "Todo:\n", - "- Get scoring correct - use internal values, not hardcoded\n", - "- Clean up comments/prints\n", + "- Clean up comments/prints (mostly done)\n", "- Make processing more generalized\n", "-- Introduce multiple dirt clods.\n", "-- Introduce multiple agents.\n", - "-- Add heading sense\n", "- Move data to cloud" ] } From fcaec3ef4949aa43771512d1557e18d8693771e8 Mon Sep 17 00:00:00 2001 From: td2014 Date: Wed, 28 Dec 2016 10:15:52 -0800 Subject: [PATCH 10/32] Added some answers to 2.10 of Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 184 ++++++++++++++++++++++++++++----------- 1 file changed, 132 insertions(+), 52 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 365c8f671..4c65bed82 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 690, + "execution_count": 701, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 691, + "execution_count": 702, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 692, + "execution_count": 703, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 693, + "execution_count": 704, "metadata": { "collapsed": false }, @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 694, + "execution_count": 705, "metadata": { "collapsed": false }, @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 695, + "execution_count": 706, "metadata": { "collapsed": false }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 696, + "execution_count": 707, "metadata": { "collapsed": true }, @@ -328,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 697, + "execution_count": 708, "metadata": { "collapsed": false }, @@ -437,7 +437,7 @@ }, { "cell_type": "code", - "execution_count": 698, + "execution_count": 709, "metadata": { "collapsed": false }, @@ -474,7 +474,44 @@ }, { "cell_type": "code", - "execution_count": 699, + "execution_count": 710, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#\n", + "# The program for the simple reflex agent with state is:\n", + "# \n", + "# Percept: Action:\n", + "# -------- -------\n", + "# [(0,0),Clean] -> Right\n", + "# [(0,0),Dirty] -> Suck\n", + "# [(1,0),Clean] -> Left\n", + "# [(1,0),Dirty] -> Suck\n", + "#\n", + "\n", + "def SimpleReflexCleanState(percept):\n", + " \n", + " if percept[0] == (0,0) and percept[1]==\"DirtClump\":\n", + " return \"Suck\"\n", + " elif percept[0] == (1,0) and percept[1]==\"DirtClump\":\n", + " return \"Suck\"\n", + " elif percept[0] == (0,0) and percept[1]==\"CleanSquare\":\n", + " return \"MoveRight\"\n", + " elif percept[0] == (1,0) and percept[1]==\"CleanSquare\":\n", + " return \"MoveLeft\"\n", + " else:\n", + " return \"DoNothing\" # Not sure how you would get here, but DoNothing to be safe.\n", + "\n", + "# Instantiate a simple reflex vacuum agent\n", + "class adxyz_SimpleReflexAgentStateVacuum(Agent):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 711, "metadata": { "collapsed": false }, @@ -483,7 +520,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[]]\n", + "initDirt = [[]]\n", "initAgent = [(0, 0), (1, 0)]\n" ] } @@ -509,7 +546,7 @@ }, { "cell_type": "code", - "execution_count": 700, + "execution_count": 712, "metadata": { "collapsed": false }, @@ -529,21 +566,21 @@ "Simulation: step = 0\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 0\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 0\n", @@ -556,21 +593,21 @@ "Simulation: step = 1\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 2\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 2\n", @@ -583,21 +620,21 @@ "Simulation: step = 2\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 4\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 4\n", @@ -610,21 +647,21 @@ "Simulation: step = 3\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 6\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 6\n", @@ -637,21 +674,21 @@ "Simulation: step = 4\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 8\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca4a8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 8\n", @@ -677,21 +714,21 @@ "Simulation: step = 0\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 0\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 0\n", @@ -704,21 +741,21 @@ "Simulation: step = 1\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 2\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 2\n", @@ -731,21 +768,21 @@ "Simulation: step = 2\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 4\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 4\n", @@ -758,21 +795,21 @@ "Simulation: step = 3\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 6\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 6\n", @@ -785,21 +822,21 @@ "Simulation: step = 4\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "Self.things = []\n", "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 8\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1053f2ac8>\n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 8\n", @@ -891,6 +928,49 @@ "-- Introduce multiple agents.\n", "- Move data to cloud" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2.10) Consider the modified version of the performance metric where the agent is penalized on point for each movement:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "a) Can a simple reflex agent be perfectly rational for this environment?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For this problem, there are 8 cases to consider (8 states of the environment): 4 initial dirt configurations and 2 initial agent configurations.\n", + "\n", + "Case 1a) Clean A, Clean B, agent in square A: The maximum performance score would be 2 points awarded at each step, because there are two clean squares. If we were to design a reflex agent, we could use the following program: [(clean, squareA)-->DoNothing]\n", + "Case 1b) Clean A, Clean B, agent in square B: The maximum performance score would be 2 points awarded at each step, because there are two clean squares. If we were to design a reflex agent, we could use the following program: [(clean, SquareB)-->DoNothing]\n", + "\n", + "Case 2a) Dirt A, Clean B, agent in square A: The maximum performance score would be 2 points, once the dirt is removed from square A. The agent program that could accomplish this is: [(dirt, squareA)-->suck], [(clean, squareA)-->DoNothing]\n", + "Case 2b) Dirt A, Clean B, agent in square B: The maximum performance score would be 1-1 (1 point for clean B, -1 for move to A), then 2 points for each step after that. The agent program that could accomplish this is: [(clean, squareB)-->MoveLeft], [(dirt, squareA)-->suck], [(clean, squareA)-->DoNothing]. However, this is in conflict with the optimum program for Case 1b.\n", + "\n", + "Case 3a) Clean A, Dirt B, agent in squareA: The maximum peformance score would be 1(for clean initial square) -1 (for move to B) = 0 points for step 1. 2 points each step from then on. The agent program that could accomplish this would be: [(clean, squareA)-->MoveRight], [(Dirt, SquareB)-->Suck], [(clean,SquareB)-->doNothing]. However, we can see from this situation that our program for 3a is in conflict with the program for 1a.\n", + "Case 3b) Clean A, Dirt B, agent in squareB: The maximum performance score for this would be 2 per time step: The following agent program could accomplish this [(Dirt, SquareB)-->Suck][(clean,SquareB)-->doNothing.\n", + "\n", + "Case 4a) Dirt A, Dirt B, Agent in Square A: The maximum possible performance points would be 1 for first step, 1-1 for second step, 2 points from that step onwards. An agent function that could accomplish this is: [(dirt,squareA)-->suck], [(clean,squareA)-->moveRight], [(dirt,SquareB)-->suck], [(clean,SquareB)-->doNothing]. However, this includes an instruction which is in conflict with the optimum program in case 1a.\n", + "\n", + "Case 4b) Dirt A, Dirt B, Agent in Square B: The maximum possible performance points would be 1 for the first step, 1-1 for the second step, and 2 points from the step onwards. An agent function that could accomplish this is: [(dirt, squareB)-->suck], [(clean,squareB)-->moveLeft], [(dirt,squareA)-->suck], [(clean, squareA)-->doNothing]. This has instruction which are in conflict with case 1b. \n", + "\n", + "Because we have conflicting instructions in order to achieve optimum performance results, we would have to choose one or the other, which would lead to a suboptimal result in at least one case. Thus a perfectly rational agent cannot be designed. By perfectly rational, I mean one that is optimum in every case, since we must assume all cases are possible to occur." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "b) What about a reflex agent with state? Design such an agent." + ] } ], "metadata": { From dc8ff8f4a01f19db766c0dac1e5bfe0eca90718b Mon Sep 17 00:00:00 2001 From: td2014 Date: Wed, 28 Dec 2016 19:08:15 -0800 Subject: [PATCH 11/32] Added answers to 2.7 - Chap 2 exercises. --- adxyz_agents_chap2.ipynb | 278 ++++++++++++++++++++++----------------- 1 file changed, 154 insertions(+), 124 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 4c65bed82..197a9c033 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 701, + "execution_count": 725, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 702, + "execution_count": 726, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 703, + "execution_count": 727, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 704, + "execution_count": 728, "metadata": { "collapsed": false }, @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 705, + "execution_count": 729, "metadata": { "collapsed": false }, @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 706, + "execution_count": 730, "metadata": { "collapsed": false }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 707, + "execution_count": 731, "metadata": { "collapsed": true }, @@ -304,6 +304,35 @@ "# Chapter 2 Exercises" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2.7) Write pseudocode for the goal-based and utility based agents:\n", + "\n", + "** Goal-based: **\n", + "\n", + " currentDeltaAction=0\n", + " currentBestAction=[]\n", + " While goal==false:\n", + " for iAction in listOfActions:\n", + " if deltaValue(iAction)>currentDeltaAction:\n", + " currentBestAction=iAction\n", + " agent_action(currentBestAction):\n", + " \n", + "\n", + "** Utility-based: **\n", + "\n", + " currentDeltaUtility=0\n", + " currentBestAction=[]\n", + " While true:\n", + " if iAction in listOfActions:\n", + " if deltaUtility(iAction)>currentBestUtility:\n", + " currentBestAction=iAction\n", + " \n", + " agent_action(currentBestAction)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -328,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": 708, + "execution_count": 732, "metadata": { "collapsed": false }, @@ -416,6 +445,7 @@ "### Count up number of clean squares (indirectly)\n", "### and add that to the agent peformance score\n", "###\n", + "\n", " print(\"Before dirt count update, agent.performance = \", agent.performance)\n", " dirtCount=0\n", " for iThing in self.things:\n", @@ -437,7 +467,7 @@ }, { "cell_type": "code", - "execution_count": 709, + "execution_count": 733, "metadata": { "collapsed": false }, @@ -454,7 +484,7 @@ "# [(1,0),Dirty] -> Suck\n", "#\n", "\n", - "def SimpleReflexClean(percept):\n", + "def adxyz_SimpleReflexVacuum(percept):\n", " \n", " if percept[0] == (0,0) and percept[1]==\"DirtClump\":\n", " return \"Suck\"\n", @@ -468,50 +498,13 @@ " return \"DoNothing\" # Not sure how you would get here, but DoNothing to be safe.\n", "\n", "# Instantiate a simple reflex vacuum agent\n", - "class adxyz_SimpleReflexAgentVacuum(Agent):\n", + "class adxyz_SimpleReflexVacuumAgent(Agent):\n", " pass" ] }, { "cell_type": "code", - "execution_count": 710, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "#\n", - "# The program for the simple reflex agent with state is:\n", - "# \n", - "# Percept: Action:\n", - "# -------- -------\n", - "# [(0,0),Clean] -> Right\n", - "# [(0,0),Dirty] -> Suck\n", - "# [(1,0),Clean] -> Left\n", - "# [(1,0),Dirty] -> Suck\n", - "#\n", - "\n", - "def SimpleReflexCleanState(percept):\n", - " \n", - " if percept[0] == (0,0) and percept[1]==\"DirtClump\":\n", - " return \"Suck\"\n", - " elif percept[0] == (1,0) and percept[1]==\"DirtClump\":\n", - " return \"Suck\"\n", - " elif percept[0] == (0,0) and percept[1]==\"CleanSquare\":\n", - " return \"MoveRight\"\n", - " elif percept[0] == (1,0) and percept[1]==\"CleanSquare\":\n", - " return \"MoveLeft\"\n", - " else:\n", - " return \"DoNothing\" # Not sure how you would get here, but DoNothing to be safe.\n", - "\n", - "# Instantiate a simple reflex vacuum agent\n", - "class adxyz_SimpleReflexAgentStateVacuum(Agent):\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 711, + "execution_count": 734, "metadata": { "collapsed": false }, @@ -546,7 +539,7 @@ }, { "cell_type": "code", - "execution_count": 712, + "execution_count": 735, "metadata": { "collapsed": false }, @@ -559,28 +552,28 @@ "Simulation: iSimDirtPlacement = 0\n", "\n", "Environment:\n", - " (0, 0)\n", + " (0, 0)\n", "\n", "\n", "<-START->\n", "Simulation: step = 0\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 0\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 0\n", @@ -593,21 +586,21 @@ "Simulation: step = 1\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 2\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 2\n", @@ -620,21 +613,21 @@ "Simulation: step = 2\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 4\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 4\n", @@ -647,21 +640,21 @@ "Simulation: step = 3\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 6\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 6\n", @@ -674,21 +667,21 @@ "Simulation: step = 4\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 8\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x105332390>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", + "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 8\n", @@ -707,28 +700,28 @@ "Simulation: iSimDirtPlacement = 0\n", "\n", "Environment:\n", - " (1, 0)\n", + " (1, 0)\n", "\n", "\n", "<-START->\n", "Simulation: step = 0\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 0\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 0\n", @@ -741,21 +734,21 @@ "Simulation: step = 1\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 2\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 2\n", @@ -768,21 +761,21 @@ "Simulation: step = 2\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 4\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 4\n", @@ -795,21 +788,21 @@ "Simulation: step = 3\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (0, 0)\n", "Agent performance = 6\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "agent = \n", "current agent action = MoveRight\n", "\n", "Action-MoveRight\n", - "agent direction before MoveRight = \n", + "agent direction before MoveRight = \n", "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", + "agent direction after MoveRight = \n", "agent location after MoveRight = (1, 0)\n", "\n", "Before dirt count update, agent.performance = 6\n", @@ -822,21 +815,21 @@ "Simulation: step = 4\n", "\n", "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "Self.things = []\n", - "Agent ID = \n", + "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "Self.things = []\n", + "Agent ID = \n", "Agent location = (1, 0)\n", "Agent performance = 8\n", "\n", "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x1051ca518>\n", - "agent = \n", + "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", + "agent = \n", "current agent action = MoveLeft\n", "\n", "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", + "agent direction before MoveLeft = \n", "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", + "agent direction after MoveLeft = \n", "agent location after MoveLeft = (0, 0)\n", "\n", "Before dirt count update, agent.performance = 8\n", @@ -876,8 +869,8 @@ "#\n", "# Now setup the agent.\n", "#\n", - " myAgent=adxyz_SimpleReflexAgentVacuum()\n", - " myAgent.program=SimpleReflexClean #Place the agent program here\n", + " myAgent=adxyz_SimpleReflexVacuumAgent()\n", + " myAgent.program=adxyz_SimpleReflexVacuum #Place the agent program here\n", " myAgent.performance=0\n", "\n", "# Instantiate a direction object for 2D generality\n", @@ -971,6 +964,43 @@ "source": [ "b) What about a reflex agent with state? Design such an agent." ] + }, + { + "cell_type": "code", + "execution_count": 736, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#\n", + "# The program for the simple reflex agent with state is:\n", + "# \n", + "# Percept: Action:\n", + "# -------- -------\n", + "# [(0,0),Clean] -> Right\n", + "# [(0,0),Dirty] -> Suck\n", + "# [(1,0),Clean] -> Left\n", + "# [(1,0),Dirty] -> Suck\n", + "#\n", + "\n", + "def adxyz_SimpleReflexStateVacuum(percept):\n", + " \n", + " if percept[0] == (0,0) and percept[1]==\"DirtClump\":\n", + " return \"Suck\"\n", + " elif percept[0] == (1,0) and percept[1]==\"DirtClump\":\n", + " return \"Suck\"\n", + " elif percept[0] == (0,0) and percept[1]==\"CleanSquare\":\n", + " return \"MoveRight\"\n", + " elif percept[0] == (1,0) and percept[1]==\"CleanSquare\":\n", + " return \"MoveLeft\"\n", + " else:\n", + " return \"DoNothing\" # Not sure how you would get here, but DoNothing to be safe.\n", + "\n", + "# Instantiate a simple reflex vacuum agent\n", + "class adxyz_SimpleReflexStateVacuumAgent(Agent):\n", + " pass" + ] } ], "metadata": { From 7b8cd827eade6bb75ac1a865e02160363f71a3b7 Mon Sep 17 00:00:00 2001 From: td2014 Date: Thu, 29 Dec 2016 09:32:13 -0800 Subject: [PATCH 12/32] Added some material from Chapter 3. --- adxyz_agents_chap2.ipynb | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 197a9c033..9f50be967 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1001,6 +1001,127 @@ "class adxyz_SimpleReflexStateVacuumAgent(Agent):\n", " pass" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We are looking to design agents that can solve goal seeking problems.\n", + "Step 1: Define the goal, which is a state of the environment. For example, the desired goal might be \"Car in Bucharest\" or \"Robot in square (10,10) with all squares clean\" \n", + "Step 2: Define the problem. \n", + "- Define the states of the environment (atomic)\n", + "- Define the initial state\n", + "- Define legal actions\n", + "- Define transitions\n", + "- Define goal test\n", + "- Define path/step costs" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "graph-search: A key algorithm for expanding the search space, that avoids redundent paths. The search methods in this chapter are based on graph-search algorithm.\n", + "Each step of the algorithm does this:\n", + "Unexplored state -> frontier states -> explored states.\n", + "A state can only be in one of the three above categories." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Infrastructure for search algorithms:\n", + "Graphs - nodes that include references to \n", + "parent nodes\n", + "state descriptions\n", + "action that got from parent to child node\n", + "path cost (from initial state).\n", + "\n", + "Types of cost:\n", + "Search cost (time to determine solution)\n", + "Path cost (cost of actual solution - for example distance on a roadmap)\n", + "Total cost: Sum of search + path cost (with appropriate scaling to put them in common units)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Types of Search Strategies" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Algorithm evaluation criteria:\n", + "- Completeness (Does the algorithm find a solution - or all solutions)\n", + "- Optimality (Does the algorithm find the best solution)\n", + "- Time complexity (how long does the algorithm take to find solution)\n", + "- Space complexity (how much memory is used)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Uninformed search" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This includes all search algorithms that have no idea whether one choice is \"more promising\" than another non-goal state. These algorithms generate non-goal states and test for goal states." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Breadth-first search: Each node is expanded into the successor nodes one level at a time. Uses a FIFO queue for the frontier." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Pseudo-code for BFS search:\n", + "\n", + " unexploredNodes = {AllStates}-initialState\n", + " exploredNodes = {}\n", + " frontierNodes = initialState\n", + " \n", + " while currentNode != goalState:\n", + " currentNode = frontierNodes.pop\n", + " if currentNode == goalState:\n", + " break\n", + " else:\n", + " exploredNodes.push(currentNode)\n", + " for childNode in currentNode.children:\n", + " frontierNodes.push(childNode)\n", + " unexploredNodes.delete(childNode)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Informed search (heuristics)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] } ], "metadata": { From 1c6a34a424c4dfe815e759b941706d04eafde245 Mon Sep 17 00:00:00 2001 From: td2014 Date: Thu, 29 Dec 2016 19:46:14 -0800 Subject: [PATCH 13/32] Defined some queues for BFS from Chapter 3. --- adxyz_agents_chap2.ipynb | 105 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 9f50be967..2a6eb8185 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1100,7 +1100,6 @@ " exploredNodes = {}\n", " frontierNodes = initialState\n", " \n", - " while currentNode != goalState:\n", " currentNode = frontierNodes.pop\n", " if currentNode == goalState:\n", " break\n", @@ -1111,6 +1110,110 @@ " unexploredNodes.delete(childNode)" ] }, + { + "cell_type": "code", + "execution_count": 750, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "node1 ID = <__main__.searchNode object at 0x105555898>\n", + "node2 ID = <__main__.searchNode object at 0x105555908>\n", + "node3 ID = <__main__.searchNode object at 0x105555940>\n", + "\n", + "node1 children = [<__main__.searchNode object at 0x105555908>, <__main__.searchNode object at 0x105555940>]\n", + "node2 parent = <__main__.searchNode object at 0x105555898>\n", + "node3 parent = <__main__.searchNode object at 0x105555898>\n", + "tmpQueue = <__main__.searchNode object at 0x105555898>\n", + "tmpQueue = <__main__.searchNode object at 0x105555908>\n", + "tmpQueue = <__main__.searchNode object at 0x105555940>\n" + ] + } + ], + "source": [ + "# define a search node class\n", + "class searchNode():\n", + " \n", + " def __init__(self):\n", + " self.parent=None\n", + " self.child=[]\n", + " self.pathCost=0\n", + " self.state=None\n", + " \n", + " def setParent(self, parentNode):\n", + " self.parent=parentNode\n", + " return\n", + "\n", + " def setState(self, state):\n", + " self.state=state\n", + " return\n", + " \n", + " def setChild(self, childNode):\n", + " self.child.append(childNode)\n", + " childNode.setParent(self)\n", + " return\n", + " \n", + " def setPathCost(self, stepCost):\n", + " self.pathCost=self.pathCost+stepCost\n", + " return\n", + " \n", + " def getParent(self):\n", + " return self.parent\n", + "\n", + " def getState(self):\n", + " return self.state\n", + " \n", + " def getChild(self):\n", + " return self.child\n", + " \n", + " def getPathCost(self):\n", + " return self.pathCost\n", + " \n", + "#\n", + "# Instantiate a few nodes\n", + "#\n", + "\n", + "node1 = searchNode()\n", + "node2 = searchNode()\n", + "node3 = searchNode()\n", + "\n", + "node1.setChild(node2)\n", + "node1.setChild(node3)\n", + "\n", + "#\n", + "# Check out hierarchy so far\n", + "#\n", + "print(\"node1 ID = \", node1)\n", + "print(\"node2 ID = \", node2)\n", + "print(\"node3 ID = \", node3)\n", + "print()\n", + "\n", + "print(\"node1 children = \", node1.getChild())\n", + "print(\"node2 parent = \", node2.getParent())\n", + "print(\"node3 parent = \", node3.getParent())\n", + "\n", + "#\n", + "# Instantiate a queue\n", + "#\n", + "\n", + "import queue\n", + "\n", + "frontierQueue = queue.Queue()\n", + "\n", + "frontierQueue.put(node1)\n", + "frontierQueue.put(node2)\n", + "frontierQueue.put(node3)\n", + "\n", + "print(\"Exploring frontier queue:\")\n", + "while not frontierQueue.empty():\n", + " tmp = frontierQueue.get()\n", + " print(\"tmpQueue = \", tmp)" + ] + }, { "cell_type": "markdown", "metadata": {}, From 72548418b25b1db1d52499c4d887eea044cf7b1b Mon Sep 17 00:00:00 2001 From: td2014 Date: Fri, 30 Dec 2016 10:01:42 -0800 Subject: [PATCH 14/32] Added some BFS procssing from Chapter 3. --- adxyz_agents_chap2.ipynb | 716 ++++++++++++++++----------------------- 1 file changed, 295 insertions(+), 421 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 2a6eb8185..5366392ae 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": 725, + "execution_count": null, "metadata": { "collapsed": false, "scrolled": true @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 726, + "execution_count": null, "metadata": { "collapsed": false }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 727, + "execution_count": null, "metadata": { "collapsed": false }, @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 728, + "execution_count": null, "metadata": { "collapsed": false }, @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 729, + "execution_count": null, "metadata": { "collapsed": false }, @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 730, + "execution_count": null, "metadata": { "collapsed": false }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 731, + "execution_count": null, "metadata": { "collapsed": true }, @@ -357,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": 732, + "execution_count": null, "metadata": { "collapsed": false }, @@ -467,7 +467,7 @@ }, { "cell_type": "code", - "execution_count": 733, + "execution_count": null, "metadata": { "collapsed": false }, @@ -504,20 +504,11 @@ }, { "cell_type": "code", - "execution_count": 734, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "initDirt = [[]]\n", - "initAgent = [(0, 0), (1, 0)]\n" - ] - } - ], + "outputs": [], "source": [ "# Define the initial dirt configurations\n", "initDirt=[]\n", @@ -539,314 +530,11 @@ }, { "cell_type": "code", - "execution_count": 735, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Simulation: iSimAgentPlacement = 0\n", - "Simulation: iSimDirtPlacement = 0\n", - "\n", - "Environment:\n", - " (0, 0)\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 0\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (0, 0)\n", - "Agent performance = 0\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "agent = \n", - "current agent action = MoveRight\n", - "\n", - "Action-MoveRight\n", - "agent direction before MoveRight = \n", - "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", - "agent location after MoveRight = (1, 0)\n", - "\n", - "Before dirt count update, agent.performance = 0\n", - "After execute_action, agent.performance = 2\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 1\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (1, 0)\n", - "Agent performance = 2\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "agent = \n", - "current agent action = MoveLeft\n", - "\n", - "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", - "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", - "agent location after MoveLeft = (0, 0)\n", - "\n", - "Before dirt count update, agent.performance = 2\n", - "After execute_action, agent.performance = 4\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 2\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (0, 0)\n", - "Agent performance = 4\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "agent = \n", - "current agent action = MoveRight\n", - "\n", - "Action-MoveRight\n", - "agent direction before MoveRight = \n", - "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", - "agent location after MoveRight = (1, 0)\n", - "\n", - "Before dirt count update, agent.performance = 4\n", - "After execute_action, agent.performance = 6\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 3\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (1, 0)\n", - "Agent performance = 6\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "agent = \n", - "current agent action = MoveLeft\n", - "\n", - "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", - "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", - "agent location after MoveLeft = (0, 0)\n", - "\n", - "Before dirt count update, agent.performance = 6\n", - "After execute_action, agent.performance = 8\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 4\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (0, 0)\n", - "Agent performance = 8\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b0b8>\n", - "agent = \n", - "current agent action = MoveRight\n", - "\n", - "Action-MoveRight\n", - "agent direction before MoveRight = \n", - "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", - "agent location after MoveRight = (1, 0)\n", - "\n", - "Before dirt count update, agent.performance = 8\n", - "After execute_action, agent.performance = 10\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<====>\n", - "<====>\n", - "Final performance measure for Agent = 10\n", - "======\n", - "======\n", - "\n", - "Simulation: iSimAgentPlacement = 1\n", - "Simulation: iSimDirtPlacement = 0\n", - "\n", - "Environment:\n", - " (1, 0)\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 0\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (1, 0)\n", - "Agent performance = 0\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "agent = \n", - "current agent action = MoveLeft\n", - "\n", - "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", - "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", - "agent location after MoveLeft = (0, 0)\n", - "\n", - "Before dirt count update, agent.performance = 0\n", - "After execute_action, agent.performance = 2\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 1\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (0, 0)\n", - "Agent performance = 2\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "agent = \n", - "current agent action = MoveRight\n", - "\n", - "Action-MoveRight\n", - "agent direction before MoveRight = \n", - "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", - "agent location after MoveRight = (1, 0)\n", - "\n", - "Before dirt count update, agent.performance = 2\n", - "After execute_action, agent.performance = 4\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 2\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (1, 0)\n", - "Agent performance = 4\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "agent = \n", - "current agent action = MoveLeft\n", - "\n", - "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", - "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", - "agent location after MoveLeft = (0, 0)\n", - "\n", - "Before dirt count update, agent.performance = 4\n", - "After execute_action, agent.performance = 6\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 3\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (0, 0)\n", - "Agent performance = 6\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "agent = \n", - "current agent action = MoveRight\n", - "\n", - "Action-MoveRight\n", - "agent direction before MoveRight = \n", - "agent location before MoveRight = (0, 0)\n", - "agent direction after MoveRight = \n", - "agent location after MoveRight = (1, 0)\n", - "\n", - "Before dirt count update, agent.performance = 6\n", - "After execute_action, agent.performance = 8\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<-START->\n", - "Simulation: step = 4\n", - "\n", - "In adxyz_VacuumEnvironment - percept override:\n", - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "Self.things = []\n", - "Agent ID = \n", - "Agent location = (1, 0)\n", - "Agent performance = 8\n", - "\n", - "In adxyz_VacuumEnvironment - execute_action override:\n", - "self = <__main__.adxyz_VacuumEnvironment object at 0x10552b940>\n", - "agent = \n", - "current agent action = MoveLeft\n", - "\n", - "Action-MoveLeft\n", - "agent direction before MoveLeft = \n", - "agent location before MoveLeft = (1, 0)\n", - "agent direction after MoveLeft = \n", - "agent location after MoveLeft = (0, 0)\n", - "\n", - "Before dirt count update, agent.performance = 8\n", - "After execute_action, agent.performance = 10\n", - "---END---\n", - "---------\n", - "\n", - "\n", - "<====>\n", - "<====>\n", - "Final performance measure for Agent = 10\n", - "======\n", - "======\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "# Create a loop over environments to run simulation\n", "\n", @@ -967,7 +655,7 @@ }, { "cell_type": "code", - "execution_count": 736, + "execution_count": null, "metadata": { "collapsed": true }, @@ -1096,23 +784,76 @@ "source": [ "Pseudo-code for BFS search:\n", "\n", - " unexploredNodes = {AllStates}-initialState\n", - " exploredNodes = {}\n", + " unexploredNodes = dict()\n", + " exploredNodes = dict()\n", " frontierNodes = initialState\n", + " goalNodeFound = False\n", " \n", + " while not frontierNodes.empty:\n", " currentNode = frontierNodes.pop\n", - " if currentNode == goalState:\n", + " if currentNode.goal == True:\n", + " currentNode.pathCost=currentNode.parent.pathCost+currentNode.stepCost\n", + " goalNodeFound=True\n", " break\n", " else:\n", - " exploredNodes.push(currentNode)\n", - " for childNode in currentNode.children:\n", - " frontierNodes.push(childNode)\n", - " unexploredNodes.delete(childNode)" + " exploredNodes[currentNode]=True # add current node to explored nodes\n", + " for childNode,dummy in currentNode.links: #Any link is a \"child\"\n", + " if (childNode in exploredNodes) or (childNode in frontierNodes):\n", + " continue\n", + " else:\n", + " frontierNodes.push(childNode)\n", + " childNode.stepCost=childNode.link[currentNode] # provide step cost\n", + " childNode.parent=currentNode\n", + " del unexploredNodes[childNode]\n", + " \n", + " If goalNodeFound != True: # goal node was not set\n", + " error\n", + "Need to start at goal node and work back to initial state to provide solution pathway:\n", + "\n", + "pathSequence = queue.LifoQueue()\n", + "\n", + "currentNode = goalNode\n", + "pathSequence.put(currentNode)\n", + "\n", + "while currentNode != currentNode.parent:\n", + " pathSequence.put(currentNode.parent)\n", + " currentNode=currentNode.parent\n", + "\n", + "pathSequence.put(currentNode)\n", + "\n", + "while not pathSequence.empty():\n", + " print(\"Path sequence = \", pathSequence.get())\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We want to create a generic graph that could be undirected in general and search it using BFS and a FIFO frontier queue." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class graphNode():\n", + " def __init__(self):\n", + " self.links=dict() # (name of link:step cost)\n", + " self.parent=None # Is assigned during BFS\n", + " self.goal=False # True if goal state\n", + " self.pathCost=0\n", + " self.stepCost=0\n", + " self.frontier=False # True if node has been added to frontier" ] }, { "cell_type": "code", - "execution_count": 750, + "execution_count": 41, "metadata": { "collapsed": false }, @@ -1121,110 +862,255 @@ "name": "stdout", "output_type": "stream", "text": [ - "node1 ID = <__main__.searchNode object at 0x105555898>\n", - "node2 ID = <__main__.searchNode object at 0x105555908>\n", - "node3 ID = <__main__.searchNode object at 0x105555940>\n", - "\n", - "node1 children = [<__main__.searchNode object at 0x105555908>, <__main__.searchNode object at 0x105555940>]\n", - "node2 parent = <__main__.searchNode object at 0x105555898>\n", - "node3 parent = <__main__.searchNode object at 0x105555898>\n", - "tmpQueue = <__main__.searchNode object at 0x105555898>\n", - "tmpQueue = <__main__.searchNode object at 0x105555908>\n", - "tmpQueue = <__main__.searchNode object at 0x105555940>\n" + "NodeSetup:\n", + "Node1 = <__main__.graphNode object at 0x104acde10>\n", + "Node2 = <__main__.graphNode object at 0x104acd828>\n", + "Node3 = <__main__.graphNode object at 0x104acda90>\n", + "Node4 = <__main__.graphNode object at 0x104acd9b0>\n", + "Node5 = <__main__.graphNode object at 0x104acdda0>\n", + "Node1 links = {<__main__.graphNode object at 0x104acda90>: 15, <__main__.graphNode object at 0x104acd828>: 10}\n", + "Node2 links = {<__main__.graphNode object at 0x104acde10>: 10, <__main__.graphNode object at 0x104acdda0>: 6}\n", + "Node3 links = {<__main__.graphNode object at 0x104acde10>: 15, <__main__.graphNode object at 0x104acdda0>: 8, <__main__.graphNode object at 0x104acd9b0>: 17}\n", + "Node4 links = {<__main__.graphNode object at 0x104acda90>: 17}\n", + "Node5 links = {<__main__.graphNode object at 0x104acda90>: 8, <__main__.graphNode object at 0x104acd828>: 6}\n", + "Node4.goal = True\n", + "\n" ] } ], "source": [ - "# define a search node class\n", - "class searchNode():\n", - " \n", - " def __init__(self):\n", - " self.parent=None\n", - " self.child=[]\n", - " self.pathCost=0\n", - " self.state=None\n", - " \n", - " def setParent(self, parentNode):\n", - " self.parent=parentNode\n", - " return\n", + "#\n", + "# create map\n", + "#\n", + "#\n", + "# Node1 ----- 10 ----- Node2 \n", + "# | |\n", + "# | 6\n", + "# | |\n", + "# 15 Node5\n", + "# | |\n", + "# | =======8======= \n", + "# | |\n", + "# Node3 \n", + "# |\n", + "# |\n", + "# |\n", + "# 17\n", + "# |\n", + "# |\n", + "# |\n", + "# Node4 (goal)\n", + "#\n", + "#\n", + "Node1=graphNode()\n", + "Node2=graphNode()\n", + "Node3=graphNode()\n", + "Node4=graphNode()\n", + "Node5=graphNode()\n", "\n", - " def setState(self, state):\n", - " self.state=state\n", - " return\n", - " \n", - " def setChild(self, childNode):\n", - " self.child.append(childNode)\n", - " childNode.setParent(self)\n", - " return\n", - " \n", - " def setPathCost(self, stepCost):\n", - " self.pathCost=self.pathCost+stepCost\n", - " return\n", - " \n", - " def getParent(self):\n", - " return self.parent\n", + "Node1.links[Node2]=10\n", + "Node1.links[Node3]=15\n", + "Node2.links[Node1]=10\n", + "Node2.links[Node5]=6\n", + "Node3.links[Node1]=15\n", + "Node3.links[Node4]=17\n", + "Node3.links[Node5]=8\n", + "Node4.links[Node3]=17\n", + "Node5.links[Node2]=6\n", + "Node5.links[Node3]=8\n", "\n", - " def getState(self):\n", - " return self.state\n", - " \n", - " def getChild(self):\n", - " return self.child\n", - " \n", - " def getPathCost(self):\n", - " return self.pathCost\n", - " \n", + "print(\"NodeSetup:\")\n", + "print(\"Node1 = \", Node1)\n", + "print(\"Node2 = \", Node2)\n", + "print(\"Node3 = \", Node3)\n", + "print(\"Node4 = \", Node4)\n", + "print(\"Node5 = \", Node5)\n", + "\n", + "print(\"Node1 links = \", Node1.links)\n", + "print(\"Node2 links = \", Node2.links)\n", + "print(\"Node3 links = \", Node3.links)\n", + "print(\"Node4 links = \", Node4.links)\n", + "print(\"Node5 links = \", Node5.links)\n", + "\n", + "Node1.parent=Node1 # node1 is the initial node\n", + "Node1.pathCost=0\n", + "Node1.stepCost=0\n", + "\n", + "Node4.goal=True\n", + "print(\"Node4.goal = \", Node4.goal)\n", + "print()" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Before processing:\n", + "ExploredNodes = {}\n", + "FrontierNodes = \n", + "UnexploredNodes = {<__main__.graphNode object at 0x104acda90>: True, <__main__.graphNode object at 0x104acd828>: True, <__main__.graphNode object at 0x104acd9b0>: True, <__main__.graphNode object at 0x104acdda0>: True}\n", + "\n", + "Exploring frontier nodes:\n", + "CurrentNode = <__main__.graphNode object at 0x104acde10>\n", + "Child Node = <__main__.graphNode object at 0x104acda90>\n", + "Child Node = <__main__.graphNode object at 0x104acd828>\n", + "End of loop:\n", + "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True}\n", + "FrontierNodes = \n", + "UnexploredNodes = {<__main__.graphNode object at 0x104acd9b0>: True, <__main__.graphNode object at 0x104acdda0>: True}\n", + "\n", + "CurrentNode = <__main__.graphNode object at 0x104acda90>\n", + "Child Node = <__main__.graphNode object at 0x104acde10>\n", + "Child Node = <__main__.graphNode object at 0x104acdda0>\n", + "Child Node = <__main__.graphNode object at 0x104acd9b0>\n", + "End of loop:\n", + "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True, <__main__.graphNode object at 0x104acda90>: True}\n", + "FrontierNodes = \n", + "UnexploredNodes = {}\n", + "\n", + "CurrentNode = <__main__.graphNode object at 0x104acd828>\n", + "Child Node = <__main__.graphNode object at 0x104acde10>\n", + "Child Node = <__main__.graphNode object at 0x104acdda0>\n", + "End of loop:\n", + "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True, <__main__.graphNode object at 0x104acd828>: True, <__main__.graphNode object at 0x104acda90>: True}\n", + "FrontierNodes = \n", + "UnexploredNodes = {}\n", + "\n", + "CurrentNode = <__main__.graphNode object at 0x104acdda0>\n", + "Child Node = <__main__.graphNode object at 0x104acda90>\n", + "Child Node = <__main__.graphNode object at 0x104acd828>\n", + "End of loop:\n", + "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True, <__main__.graphNode object at 0x104acd828>: True, <__main__.graphNode object at 0x104acdda0>: True, <__main__.graphNode object at 0x104acda90>: True}\n", + "FrontierNodes = \n", + "UnexploredNodes = {}\n", + "\n", + "Goal node found.\n", + "Current Node = <__main__.graphNode object at 0x104acd9b0>\n", + "Current Node Parent = <__main__.graphNode object at 0x104acda90>\n", + "Current Node Path Cost = 32\n", + "Current Node Step Cost = 17\n" + ] + } + ], + "source": [ "#\n", - "# Instantiate a few nodes\n", + "# Run the BFS process\n", "#\n", "\n", - "node1 = searchNode()\n", - "node2 = searchNode()\n", - "node3 = searchNode()\n", + "import queue\n", "\n", - "node1.setChild(node2)\n", - "node1.setChild(node3)\n", + "unexploredNodes = dict()\n", + "exploredNodes = dict()\n", + "frontierNodes = queue.Queue()\n", + "goalNodeFound = False\n", "\n", "#\n", - "# Check out hierarchy so far\n", + "# Initialize the frontier queue\n", "#\n", - "print(\"node1 ID = \", node1)\n", - "print(\"node2 ID = \", node2)\n", - "print(\"node3 ID = \", node3)\n", - "print()\n", "\n", - "print(\"node1 children = \", node1.getChild())\n", - "print(\"node2 parent = \", node2.getParent())\n", - "print(\"node3 parent = \", node3.getParent())\n", + "frontierNodes.put(Node1)\n", "\n", "#\n", - "# Instantiate a queue\n", + "# Initialize the unexplored nodes\n", "#\n", "\n", - "import queue\n", - "\n", - "frontierQueue = queue.Queue()\n", + "unexploredNodes[Node2]=True\n", + "unexploredNodes[Node3]=True\n", + "unexploredNodes[Node4]=True\n", + "unexploredNodes[Node5]=True\n", "\n", - "frontierQueue.put(node1)\n", - "frontierQueue.put(node2)\n", - "frontierQueue.put(node3)\n", + "print(\"Before processing:\")\n", + "print(\"ExploredNodes = \", exploredNodes)\n", + "print(\"FrontierNodes = \", frontierNodes)\n", + "print(\"UnexploredNodes = \", unexploredNodes)\n", + "print()\n", "\n", - "print(\"Exploring frontier queue:\")\n", - "while not frontierQueue.empty():\n", - " tmp = frontierQueue.get()\n", - " print(\"tmpQueue = \", tmp)" + "# Main loop\n", + "print(\"Exploring frontier nodes:\")\n", + " \n", + "while not frontierNodes.empty():\n", + " currentNode = frontierNodes.get()\n", + " if currentNode.goal == True:\n", + " currentNode.pathCost=currentNode.parent.pathCost+currentNode.stepCost\n", + " goalNodeFound=True\n", + " break\n", + " else:\n", + " exploredNodes[currentNode]=True # add current node to explored nodes\n", + " print(\"CurrentNode = \", currentNode)\n", + " for childNode,dummy in currentNode.links.items(): #Any link is a \"child\"\n", + " print(\"Child Node = \", childNode)\n", + " if (childNode in exploredNodes) or (childNode.frontier==True):\n", + " continue\n", + " else:\n", + " frontierNodes.put(childNode)\n", + " childNode.frontier=True\n", + " childNode.parent=currentNode\n", + " childNode.stepCost=childNode.links[currentNode] # provide step cost\n", + " childNode.pathCost=currentNode.pathCost+childNode.stepCost\n", + " del unexploredNodes[childNode]\n", + " \n", + " print(\"End of loop:\")\n", + " print(\"ExploredNodes = \", exploredNodes)\n", + " print(\"FrontierNodes = \", frontierNodes)\n", + " print(\"UnexploredNodes = \", unexploredNodes)\n", + " print()\n", + " \n", + "if goalNodeFound != True: # goal node was not set\n", + " print (\"Goal node not found.\")\n", + "else:\n", + " print (\"Goal node found.\")\n", + " print (\"Current Node = \", currentNode)\n", + " print (\"Current Node Parent = \", currentNode.parent)\n", + " print (\"Current Node Path Cost = \", currentNode.pathCost)\n", + " print (\"Current Node Step Cost = \", currentNode.stepCost)" ] }, { - "cell_type": "markdown", - "metadata": {}, + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Path sequence = <__main__.graphNode object at 0x104acde10>\n", + "Path sequence = <__main__.graphNode object at 0x104acde10>\n", + "Path sequence = <__main__.graphNode object at 0x104acda90>\n", + "Path sequence = <__main__.graphNode object at 0x104acd9b0>\n" + ] + } + ], "source": [ - "### Informed search (heuristics)" + "pathSequence = queue.LifoQueue()\n", + "pathSequence.put(currentNode)\n", + "\n", + "while currentNode != currentNode.parent:\n", + " pathSequence.put(currentNode.parent)\n", + " currentNode=currentNode.parent\n", + "\n", + "# Add the final node, which is the initial in this case\n", + "# The initial node was specially marked to point to itself as parent\n", + "\n", + "while not pathSequence.empty():\n", + " print(\"Path sequence = \", pathSequence.get())" ] }, { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "### Informed search (heuristics)" + ] } ], "metadata": { @@ -1234,18 +1120,6 @@ "display_name": "Python [default]", "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.5.2" } }, "nbformat": 4, From bbf422002de5ebb4d091adc5b7d9d6a91b3ee23a Mon Sep 17 00:00:00 2001 From: td2014 Date: Fri, 30 Dec 2016 10:34:38 -0800 Subject: [PATCH 15/32] Removed unexploredNodes structure - not necessary - from Chapter 3 code. --- adxyz_agents_chap2.ipynb | 152 +++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 5366392ae..24c8e0ab8 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -797,7 +797,7 @@ " break\n", " else:\n", " exploredNodes[currentNode]=True # add current node to explored nodes\n", - " for childNode,dummy in currentNode.links: #Any link is a \"child\"\n", + " for childNode,dummy in currentNode.links.items(): #Any link is a \"child\"\n", " if (childNode in exploredNodes) or (childNode in frontierNodes):\n", " continue\n", " else:\n", @@ -808,6 +808,7 @@ " \n", " If goalNodeFound != True: # goal node was not set\n", " error\n", + " \n", "Need to start at goal node and work back to initial state to provide solution pathway:\n", "\n", "pathSequence = queue.LifoQueue()\n", @@ -835,7 +836,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 65, "metadata": { "collapsed": true }, @@ -853,7 +854,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 66, "metadata": { "collapsed": false }, @@ -863,17 +864,19 @@ "output_type": "stream", "text": [ "NodeSetup:\n", - "Node1 = <__main__.graphNode object at 0x104acde10>\n", - "Node2 = <__main__.graphNode object at 0x104acd828>\n", - "Node3 = <__main__.graphNode object at 0x104acda90>\n", - "Node4 = <__main__.graphNode object at 0x104acd9b0>\n", - "Node5 = <__main__.graphNode object at 0x104acdda0>\n", - "Node1 links = {<__main__.graphNode object at 0x104acda90>: 15, <__main__.graphNode object at 0x104acd828>: 10}\n", - "Node2 links = {<__main__.graphNode object at 0x104acde10>: 10, <__main__.graphNode object at 0x104acdda0>: 6}\n", - "Node3 links = {<__main__.graphNode object at 0x104acde10>: 15, <__main__.graphNode object at 0x104acdda0>: 8, <__main__.graphNode object at 0x104acd9b0>: 17}\n", - "Node4 links = {<__main__.graphNode object at 0x104acda90>: 17}\n", - "Node5 links = {<__main__.graphNode object at 0x104acda90>: 8, <__main__.graphNode object at 0x104acd828>: 6}\n", - "Node4.goal = True\n", + "Node1 = <__main__.graphNode object at 0x104ab2eb8>\n", + "Node2 = <__main__.graphNode object at 0x104ab2e10>\n", + "Node3 = <__main__.graphNode object at 0x104ab2be0>\n", + "Node4 = <__main__.graphNode object at 0x104ab2c18>\n", + "Node5 = <__main__.graphNode object at 0x104ab2cc0>\n", + "Node6 = <__main__.graphNode object at 0x104ab2cf8>\n", + "Node1 links = {<__main__.graphNode object at 0x104ab2e10>: 10, <__main__.graphNode object at 0x104ab2be0>: 15}\n", + "Node2 links = {<__main__.graphNode object at 0x104ab2eb8>: 10, <__main__.graphNode object at 0x104ab2cc0>: 6, <__main__.graphNode object at 0x104ab2cf8>: 7}\n", + "Node3 links = {<__main__.graphNode object at 0x104ab2c18>: 17, <__main__.graphNode object at 0x104ab2eb8>: 15, <__main__.graphNode object at 0x104ab2cc0>: 8}\n", + "Node4 links = {<__main__.graphNode object at 0x104ab2be0>: 17}\n", + "Node5 links = {<__main__.graphNode object at 0x104ab2e10>: 6, <__main__.graphNode object at 0x104ab2be0>: 8}\n", + "Node6 links = {<__main__.graphNode object at 0x104ab2e10>: 7}\n", + "Node6.goal = True\n", "\n" ] } @@ -883,7 +886,7 @@ "# create map\n", "#\n", "#\n", - "# Node1 ----- 10 ----- Node2 \n", + "# Node1 ----- 10 ----- Node2 ---7--- Node6\n", "# | |\n", "# | 6\n", "# | |\n", @@ -899,7 +902,7 @@ "# |\n", "# |\n", "# |\n", - "# Node4 (goal)\n", + "# Node4\n", "#\n", "#\n", "Node1=graphNode()\n", @@ -907,43 +910,51 @@ "Node3=graphNode()\n", "Node4=graphNode()\n", "Node5=graphNode()\n", + "Node6=graphNode()\n", "\n", "Node1.links[Node2]=10\n", "Node1.links[Node3]=15\n", + "\n", "Node2.links[Node1]=10\n", "Node2.links[Node5]=6\n", + "Node2.links[Node6]=7\n", + "\n", "Node3.links[Node1]=15\n", "Node3.links[Node4]=17\n", "Node3.links[Node5]=8\n", + "\n", "Node4.links[Node3]=17\n", + "\n", "Node5.links[Node2]=6\n", "Node5.links[Node3]=8\n", "\n", + "Node6.links[Node2]=7\n", + "\n", "print(\"NodeSetup:\")\n", "print(\"Node1 = \", Node1)\n", "print(\"Node2 = \", Node2)\n", "print(\"Node3 = \", Node3)\n", "print(\"Node4 = \", Node4)\n", "print(\"Node5 = \", Node5)\n", + "print(\"Node6 = \", Node6)\n", "\n", "print(\"Node1 links = \", Node1.links)\n", "print(\"Node2 links = \", Node2.links)\n", "print(\"Node3 links = \", Node3.links)\n", "print(\"Node4 links = \", Node4.links)\n", "print(\"Node5 links = \", Node5.links)\n", + "print(\"Node6 links = \", Node6.links)\n", "\n", - "Node1.parent=Node1 # node1 is the initial node\n", - "Node1.pathCost=0\n", - "Node1.stepCost=0\n", + "Node1.parent=Node1 # node1 is the initial node - pointing to itself as parent is the flag.\n", "\n", - "Node4.goal=True\n", - "print(\"Node4.goal = \", Node4.goal)\n", + "Node6.goal=True\n", + "print(\"Node6.goal = \", Node6.goal)\n", "print()" ] }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 67, "metadata": { "collapsed": false }, @@ -954,48 +965,49 @@ "text": [ "Before processing:\n", "ExploredNodes = {}\n", - "FrontierNodes = \n", - "UnexploredNodes = {<__main__.graphNode object at 0x104acda90>: True, <__main__.graphNode object at 0x104acd828>: True, <__main__.graphNode object at 0x104acd9b0>: True, <__main__.graphNode object at 0x104acdda0>: True}\n", + "FrontierNodes = \n", + "UnexploredNodes = {}\n", "\n", "Exploring frontier nodes:\n", - "CurrentNode = <__main__.graphNode object at 0x104acde10>\n", - "Child Node = <__main__.graphNode object at 0x104acda90>\n", - "Child Node = <__main__.graphNode object at 0x104acd828>\n", + "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2eb8>\n", + "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2e10>\n", + "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2be0>\n", + "\n", "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True}\n", - "FrontierNodes = \n", - "UnexploredNodes = {<__main__.graphNode object at 0x104acd9b0>: True, <__main__.graphNode object at 0x104acdda0>: True}\n", + "ExploredNodes = {<__main__.graphNode object at 0x104ab2eb8>: True}\n", + "FrontierNodes = \n", + "\n", + "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2e10>\n", + "Child Node has been seen before: <__main__.graphNode object at 0x104ab2eb8>\n", + "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2cc0>\n", + "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2cf8>\n", "\n", - "CurrentNode = <__main__.graphNode object at 0x104acda90>\n", - "Child Node = <__main__.graphNode object at 0x104acde10>\n", - "Child Node = <__main__.graphNode object at 0x104acdda0>\n", - "Child Node = <__main__.graphNode object at 0x104acd9b0>\n", "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True, <__main__.graphNode object at 0x104acda90>: True}\n", - "FrontierNodes = \n", - "UnexploredNodes = {}\n", + "ExploredNodes = {<__main__.graphNode object at 0x104ab2e10>: True, <__main__.graphNode object at 0x104ab2eb8>: True}\n", + "FrontierNodes = \n", + "\n", + "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2be0>\n", + "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2c18>\n", + "Child Node has been seen before: <__main__.graphNode object at 0x104ab2eb8>\n", + "Child Node has been seen before: <__main__.graphNode object at 0x104ab2cc0>\n", "\n", - "CurrentNode = <__main__.graphNode object at 0x104acd828>\n", - "Child Node = <__main__.graphNode object at 0x104acde10>\n", - "Child Node = <__main__.graphNode object at 0x104acdda0>\n", "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True, <__main__.graphNode object at 0x104acd828>: True, <__main__.graphNode object at 0x104acda90>: True}\n", - "FrontierNodes = \n", - "UnexploredNodes = {}\n", + "ExploredNodes = {<__main__.graphNode object at 0x104ab2e10>: True, <__main__.graphNode object at 0x104ab2eb8>: True, <__main__.graphNode object at 0x104ab2be0>: True}\n", + "FrontierNodes = \n", + "\n", + "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2cc0>\n", + "Child Node has been seen before: <__main__.graphNode object at 0x104ab2e10>\n", + "Child Node has been seen before: <__main__.graphNode object at 0x104ab2be0>\n", "\n", - "CurrentNode = <__main__.graphNode object at 0x104acdda0>\n", - "Child Node = <__main__.graphNode object at 0x104acda90>\n", - "Child Node = <__main__.graphNode object at 0x104acd828>\n", "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104acde10>: True, <__main__.graphNode object at 0x104acd828>: True, <__main__.graphNode object at 0x104acdda0>: True, <__main__.graphNode object at 0x104acda90>: True}\n", - "FrontierNodes = \n", - "UnexploredNodes = {}\n", + "ExploredNodes = {<__main__.graphNode object at 0x104ab2e10>: True, <__main__.graphNode object at 0x104ab2eb8>: True, <__main__.graphNode object at 0x104ab2cc0>: True, <__main__.graphNode object at 0x104ab2be0>: True}\n", + "FrontierNodes = \n", "\n", "Goal node found.\n", - "Current Node = <__main__.graphNode object at 0x104acd9b0>\n", - "Current Node Parent = <__main__.graphNode object at 0x104acda90>\n", - "Current Node Path Cost = 32\n", - "Current Node Step Cost = 17\n" + "Current Node = <__main__.graphNode object at 0x104ab2cf8>\n", + "Current Node Parent = <__main__.graphNode object at 0x104ab2e10>\n", + "Current Node Path Cost = 17\n", + "Current Node Step Cost = 7\n" ] } ], @@ -1006,7 +1018,6 @@ "\n", "import queue\n", "\n", - "unexploredNodes = dict()\n", "exploredNodes = dict()\n", "frontierNodes = queue.Queue()\n", "goalNodeFound = False\n", @@ -1017,19 +1028,9 @@ "\n", "frontierNodes.put(Node1)\n", "\n", - "#\n", - "# Initialize the unexplored nodes\n", - "#\n", - "\n", - "unexploredNodes[Node2]=True\n", - "unexploredNodes[Node3]=True\n", - "unexploredNodes[Node4]=True\n", - "unexploredNodes[Node5]=True\n", - "\n", "print(\"Before processing:\")\n", "print(\"ExploredNodes = \", exploredNodes)\n", "print(\"FrontierNodes = \", frontierNodes)\n", - "print(\"UnexploredNodes = \", unexploredNodes)\n", "print()\n", "\n", "# Main loop\n", @@ -1038,28 +1039,28 @@ "while not frontierNodes.empty():\n", " currentNode = frontierNodes.get()\n", " if currentNode.goal == True:\n", - " currentNode.pathCost=currentNode.parent.pathCost+currentNode.stepCost\n", " goalNodeFound=True\n", " break\n", " else:\n", + " print(\"Current Node being added to explored nodes = \", currentNode)\n", " exploredNodes[currentNode]=True # add current node to explored nodes\n", - " print(\"CurrentNode = \", currentNode)\n", - " for childNode,dummy in currentNode.links.items(): #Any link is a \"child\"\n", - " print(\"Child Node = \", childNode)\n", + " \n", + " for childNode,dummy in currentNode.links.items(): #Any link is a potential \"child\"\n", " if (childNode in exploredNodes) or (childNode.frontier==True):\n", + " print(\"Child Node has been seen before: \", childNode)\n", " continue\n", " else:\n", + " print(\"Child Node is being added to frontier: \", childNode)\n", " frontierNodes.put(childNode)\n", " childNode.frontier=True\n", " childNode.parent=currentNode\n", " childNode.stepCost=childNode.links[currentNode] # provide step cost\n", " childNode.pathCost=currentNode.pathCost+childNode.stepCost\n", - " del unexploredNodes[childNode]\n", - " \n", + " \n", + " print()\n", " print(\"End of loop:\")\n", " print(\"ExploredNodes = \", exploredNodes)\n", " print(\"FrontierNodes = \", frontierNodes)\n", - " print(\"UnexploredNodes = \", unexploredNodes)\n", " print()\n", " \n", "if goalNodeFound != True: # goal node was not set\n", @@ -1074,7 +1075,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 68, "metadata": { "collapsed": false }, @@ -1083,10 +1084,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "Path sequence = <__main__.graphNode object at 0x104acde10>\n", - "Path sequence = <__main__.graphNode object at 0x104acde10>\n", - "Path sequence = <__main__.graphNode object at 0x104acda90>\n", - "Path sequence = <__main__.graphNode object at 0x104acd9b0>\n" + "Path sequence = <__main__.graphNode object at 0x104ab2eb8>\n", + "Path sequence = <__main__.graphNode object at 0x104ab2e10>\n", + "Path sequence = <__main__.graphNode object at 0x104ab2cf8>\n" ] } ], From d07344d185129b1b2ae88739e4df0067c5fec8af Mon Sep 17 00:00:00 2001 From: td2014 Date: Fri, 30 Dec 2016 10:45:35 -0800 Subject: [PATCH 16/32] Added some additional commentary to Chapter 3 code. --- adxyz_agents_chap2.ipynb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 24c8e0ab8..ff8e7ff3f 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1091,6 +1091,11 @@ } ], "source": [ + "#\n", + "# Report out the solution path working backwords from the goal node to the\n", + "# initial node (which is flagged by having the parent=node)\n", + "#\n", + "\n", "pathSequence = queue.LifoQueue()\n", "pathSequence.put(currentNode)\n", "\n", From dd050652f5e26caa7e4e10420a3a3ab7d5cd6a6a Mon Sep 17 00:00:00 2001 From: td2014 Date: Sat, 31 Dec 2016 13:27:27 -0800 Subject: [PATCH 17/32] Simplified code by removing explored nodes structure - superfluous - Chapter 3 code. --- adxyz_agents_chap2.ipynb | 223 ++++++++++++++++++++------------------- 1 file changed, 115 insertions(+), 108 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index ff8e7ff3f..87ba3bac9 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -811,19 +811,19 @@ " \n", "Need to start at goal node and work back to initial state to provide solution pathway:\n", "\n", - "pathSequence = queue.LifoQueue()\n", + " pathSequence = queue.LifoQueue()\n", "\n", - "currentNode = goalNode\n", - "pathSequence.put(currentNode)\n", + " currentNode = goalNode\n", + " pathSequence.put(currentNode)\n", "\n", - "while currentNode != currentNode.parent:\n", - " pathSequence.put(currentNode.parent)\n", - " currentNode=currentNode.parent\n", + " while currentNode != currentNode.parent:\n", + " pathSequence.put(currentNode.parent)\n", + " currentNode=currentNode.parent\n", "\n", - "pathSequence.put(currentNode)\n", + " pathSequence.put(currentNode)\n", "\n", - "while not pathSequence.empty():\n", - " print(\"Path sequence = \", pathSequence.get())\n", + " while not pathSequence.empty():\n", + " print(\"Path sequence = \", pathSequence.get())\n", " " ] }, @@ -836,25 +836,26 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "class graphNode():\n", - " def __init__(self):\n", + "class GraphNode():\n", + " def __init__(self, initName):\n", " self.links=dict() # (name of link:step cost)\n", " self.parent=None # Is assigned during BFS\n", " self.goal=False # True if goal state\n", " self.pathCost=0\n", " self.stepCost=0\n", - " self.frontier=False # True if node has been added to frontier" + " self.frontier=False # True if node has been added to frontier\n", + " self.name=initName" ] }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 31, "metadata": { "collapsed": false }, @@ -864,18 +865,18 @@ "output_type": "stream", "text": [ "NodeSetup:\n", - "Node1 = <__main__.graphNode object at 0x104ab2eb8>\n", - "Node2 = <__main__.graphNode object at 0x104ab2e10>\n", - "Node3 = <__main__.graphNode object at 0x104ab2be0>\n", - "Node4 = <__main__.graphNode object at 0x104ab2c18>\n", - "Node5 = <__main__.graphNode object at 0x104ab2cc0>\n", - "Node6 = <__main__.graphNode object at 0x104ab2cf8>\n", - "Node1 links = {<__main__.graphNode object at 0x104ab2e10>: 10, <__main__.graphNode object at 0x104ab2be0>: 15}\n", - "Node2 links = {<__main__.graphNode object at 0x104ab2eb8>: 10, <__main__.graphNode object at 0x104ab2cc0>: 6, <__main__.graphNode object at 0x104ab2cf8>: 7}\n", - "Node3 links = {<__main__.graphNode object at 0x104ab2c18>: 17, <__main__.graphNode object at 0x104ab2eb8>: 15, <__main__.graphNode object at 0x104ab2cc0>: 8}\n", - "Node4 links = {<__main__.graphNode object at 0x104ab2be0>: 17}\n", - "Node5 links = {<__main__.graphNode object at 0x104ab2e10>: 6, <__main__.graphNode object at 0x104ab2be0>: 8}\n", - "Node6 links = {<__main__.graphNode object at 0x104ab2e10>: 7}\n", + "Node1 = <__main__.GraphNode object at 0x103ba8fd0>\n", + "Node2 = <__main__.GraphNode object at 0x103ba8f98>\n", + "Node3 = <__main__.GraphNode object at 0x103bc90b8>\n", + "Node4 = <__main__.GraphNode object at 0x103bc90f0>\n", + "Node5 = <__main__.GraphNode object at 0x103bc9128>\n", + "Node6 = <__main__.GraphNode object at 0x103bc9160>\n", + "Node1 links = {<__main__.GraphNode object at 0x103ba8f98>: 10, <__main__.GraphNode object at 0x103bc90b8>: 15}\n", + "Node2 links = {<__main__.GraphNode object at 0x103bc9128>: 6, <__main__.GraphNode object at 0x103bc90b8>: 28, <__main__.GraphNode object at 0x103ba8fd0>: 10, <__main__.GraphNode object at 0x103bc9160>: 7}\n", + "Node3 links = {<__main__.GraphNode object at 0x103ba8f98>: 28, <__main__.GraphNode object at 0x103bc9128>: 8, <__main__.GraphNode object at 0x103ba8fd0>: 15, <__main__.GraphNode object at 0x103bc90f0>: 17}\n", + "Node4 links = {<__main__.GraphNode object at 0x103bc90b8>: 17}\n", + "Node5 links = {<__main__.GraphNode object at 0x103ba8f98>: 6, <__main__.GraphNode object at 0x103bc90b8>: 8}\n", + "Node6 links = {<__main__.GraphNode object at 0x103ba8f98>: 7}\n", "Node6.goal = True\n", "\n" ] @@ -887,13 +888,13 @@ "#\n", "#\n", "# Node1 ----- 10 ----- Node2 ---7--- Node6\n", - "# | |\n", - "# | 6\n", - "# | |\n", - "# 15 Node5\n", - "# | |\n", - "# | =======8======= \n", - "# | |\n", + "# | 28--------/ |\n", + "# | / 6\n", + "# | / |\n", + "# 15 / Node5\n", + "# | | |\n", + "# | | =======8======= \n", + "# |/ |\n", "# Node3 \n", "# |\n", "# |\n", @@ -905,21 +906,23 @@ "# Node4\n", "#\n", "#\n", - "Node1=graphNode()\n", - "Node2=graphNode()\n", - "Node3=graphNode()\n", - "Node4=graphNode()\n", - "Node5=graphNode()\n", - "Node6=graphNode()\n", + "Node1=GraphNode(\"Node1\")\n", + "Node2=GraphNode(\"Node2\")\n", + "Node3=GraphNode(\"Node3\")\n", + "Node4=GraphNode(\"Node4\")\n", + "Node5=GraphNode(\"Node5\")\n", + "Node6=GraphNode(\"Node6\")\n", "\n", "Node1.links[Node2]=10\n", "Node1.links[Node3]=15\n", "\n", "Node2.links[Node1]=10\n", + "Node2.links[Node3]=28\n", "Node2.links[Node5]=6\n", "Node2.links[Node6]=7\n", "\n", "Node3.links[Node1]=15\n", + "Node3.links[Node2]=28\n", "Node3.links[Node4]=17\n", "Node3.links[Node5]=8\n", "\n", @@ -954,7 +957,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 32, "metadata": { "collapsed": false }, @@ -963,51 +966,44 @@ "name": "stdout", "output_type": "stream", "text": [ - "Before processing:\n", - "ExploredNodes = {}\n", - "FrontierNodes = \n", - "UnexploredNodes = {}\n", - "\n", "Exploring frontier nodes:\n", - "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2eb8>\n", - "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2e10>\n", - "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2be0>\n", + "Expanding current node: Node1\n", + "Child Node is being added to frontier: Node2\n", + "Child Node is being added to frontier: Node3\n", + "End of frontier loop:\n", + "-------\n", "\n", - "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104ab2eb8>: True}\n", - "FrontierNodes = \n", - "\n", - "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2e10>\n", - "Child Node has been seen before: <__main__.graphNode object at 0x104ab2eb8>\n", - "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2cc0>\n", - "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2cf8>\n", - "\n", - "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104ab2e10>: True, <__main__.graphNode object at 0x104ab2eb8>: True}\n", - "FrontierNodes = \n", - "\n", - "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2be0>\n", - "Child Node is being added to frontier: <__main__.graphNode object at 0x104ab2c18>\n", - "Child Node has been seen before: <__main__.graphNode object at 0x104ab2eb8>\n", - "Child Node has been seen before: <__main__.graphNode object at 0x104ab2cc0>\n", - "\n", - "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104ab2e10>: True, <__main__.graphNode object at 0x104ab2eb8>: True, <__main__.graphNode object at 0x104ab2be0>: True}\n", - "FrontierNodes = \n", + "Exploring frontier nodes:\n", + "Expanding current node: Node2\n", + "Child Node is being added to frontier: Node5\n", + "Child Node has been seen before: Node3\n", + "Child Node has been seen before: Node1\n", + "Child Node is being added to frontier: Node6\n", + "End of frontier loop:\n", + "-------\n", "\n", - "Current Node being added to explored nodes = <__main__.graphNode object at 0x104ab2cc0>\n", - "Child Node has been seen before: <__main__.graphNode object at 0x104ab2e10>\n", - "Child Node has been seen before: <__main__.graphNode object at 0x104ab2be0>\n", + "Exploring frontier nodes:\n", + "Expanding current node: Node3\n", + "Child Node has been seen before: Node2\n", + "Child Node has been seen before: Node5\n", + "Child Node has been seen before: Node1\n", + "Child Node is being added to frontier: Node4\n", + "End of frontier loop:\n", + "-------\n", "\n", - "End of loop:\n", - "ExploredNodes = {<__main__.graphNode object at 0x104ab2e10>: True, <__main__.graphNode object at 0x104ab2eb8>: True, <__main__.graphNode object at 0x104ab2cc0>: True, <__main__.graphNode object at 0x104ab2be0>: True}\n", - "FrontierNodes = \n", + "Exploring frontier nodes:\n", + "Expanding current node: Node5\n", + "Child Node has been seen before: Node2\n", + "Child Node has been seen before: Node3\n", + "End of frontier loop:\n", + "-------\n", "\n", + "Exploring frontier nodes:\n", "Goal node found.\n", - "Current Node = <__main__.graphNode object at 0x104ab2cf8>\n", - "Current Node Parent = <__main__.graphNode object at 0x104ab2e10>\n", - "Current Node Path Cost = 17\n", - "Current Node Step Cost = 7\n" + "Current Node = Node6\n", + "Current Node Parent = Node2\n", + "Current Node Step Cost = 7\n", + "Current Node Path Cost = 17\n" ] } ], @@ -1018,7 +1014,7 @@ "\n", "import queue\n", "\n", - "exploredNodes = dict()\n", + "###exploredNodes = dict()\n", "frontierNodes = queue.Queue()\n", "goalNodeFound = False\n", "\n", @@ -1027,55 +1023,47 @@ "#\n", "\n", "frontierNodes.put(Node1)\n", - "\n", - "print(\"Before processing:\")\n", - "print(\"ExploredNodes = \", exploredNodes)\n", - "print(\"FrontierNodes = \", frontierNodes)\n", - "print()\n", + "Node1.frontier=True\n", "\n", "# Main loop\n", - "print(\"Exploring frontier nodes:\")\n", - " \n", + "\n", "while not frontierNodes.empty():\n", + " print(\"Exploring frontier nodes:\")\n", " currentNode = frontierNodes.get()\n", " if currentNode.goal == True:\n", " goalNodeFound=True\n", " break\n", - " else:\n", - " print(\"Current Node being added to explored nodes = \", currentNode)\n", - " exploredNodes[currentNode]=True # add current node to explored nodes\n", - " \n", - " for childNode,dummy in currentNode.links.items(): #Any link is a potential \"child\"\n", - " if (childNode in exploredNodes) or (childNode.frontier==True):\n", - " print(\"Child Node has been seen before: \", childNode)\n", + " else: \n", + " print(\"Expanding current node: \", currentNode.name)\n", + " for childNode,dummy in currentNode.links.items(): #Any link is a potential \"child\" \n", + " if (childNode.frontier==True):\n", + " print(\"Child Node has been seen before: \", childNode.name)\n", " continue\n", " else:\n", - " print(\"Child Node is being added to frontier: \", childNode)\n", + " print(\"Child Node is being added to frontier: \", childNode.name)\n", " frontierNodes.put(childNode)\n", " childNode.frontier=True\n", " childNode.parent=currentNode\n", " childNode.stepCost=childNode.links[currentNode] # provide step cost\n", " childNode.pathCost=currentNode.pathCost+childNode.stepCost\n", " \n", - " print()\n", - " print(\"End of loop:\")\n", - " print(\"ExploredNodes = \", exploredNodes)\n", - " print(\"FrontierNodes = \", frontierNodes)\n", + " print(\"End of frontier loop:\")\n", + " print(\"-------\")\n", " print()\n", " \n", "if goalNodeFound != True: # goal node was not set\n", " print (\"Goal node not found.\")\n", "else:\n", " print (\"Goal node found.\")\n", - " print (\"Current Node = \", currentNode)\n", - " print (\"Current Node Parent = \", currentNode.parent)\n", - " print (\"Current Node Path Cost = \", currentNode.pathCost)\n", - " print (\"Current Node Step Cost = \", currentNode.stepCost)" + " print (\"Current Node = \", currentNode.name)\n", + " print (\"Current Node Parent = \", currentNode.parent.name)\n", + " print (\"Current Node Step Cost = \", currentNode.stepCost)\n", + " print (\"Current Node Path Cost = \", currentNode.pathCost)" ] }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 33, "metadata": { "collapsed": false }, @@ -1084,9 +1072,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "Path sequence = <__main__.graphNode object at 0x104ab2eb8>\n", - "Path sequence = <__main__.graphNode object at 0x104ab2e10>\n", - "Path sequence = <__main__.graphNode object at 0x104ab2cf8>\n" + "Path sequence = Node1\n", + "Path sequence = Node2\n", + "Path sequence = Node6\n" ] } ], @@ -1107,7 +1095,7 @@ "# The initial node was specially marked to point to itself as parent\n", "\n", "while not pathSequence.empty():\n", - " print(\"Path sequence = \", pathSequence.get())" + " print(\"Path sequence = \", pathSequence.get().name)" ] }, { @@ -1116,6 +1104,13 @@ "source": [ "### Informed search (heuristics)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "These approaches for searching the graph tend to produce faster results, but are dependent on information that may or may not be available at all times." + ] } ], "metadata": { @@ -1125,6 +1120,18 @@ "display_name": "Python [default]", "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.5.2" } }, "nbformat": 4, From 6a33fd9e21faefacc31da0d39b531d6042f66263 Mon Sep 17 00:00:00 2001 From: td2014 Date: Sat, 31 Dec 2016 14:16:33 -0800 Subject: [PATCH 18/32] Snapshot: Chapter 3 code. --- adxyz_agents_chap2.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 87ba3bac9..bd467c7ef 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1028,7 +1028,7 @@ "# Main loop\n", "\n", "while not frontierNodes.empty():\n", - " print(\"Exploring frontier nodes:\")\n", + " print(\"Exploring frontier nodes: \")\n", " currentNode = frontierNodes.get()\n", " if currentNode.goal == True:\n", " goalNodeFound=True\n", From 53fc06c3a31b72ce77dc1bff20c855cb8b1192b1 Mon Sep 17 00:00:00 2001 From: td2014 Date: Wed, 4 Jan 2017 17:35:23 -0800 Subject: [PATCH 19/32] Updates to Chap 3. --- adxyz_agents_chap2.ipynb | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index bd467c7ef..66d12de90 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1098,6 +1098,80 @@ " print(\"Path sequence = \", pathSequence.get().name)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Uniform Cost search" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This approach uses a priority queue for the frontier based on the smallest path cost to a given new node (need to check if this is smallest path cost or smallest step cost)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " Example:\n", + "\n", + " Sib-----99-----Fagar\n", + " | |\n", + " | |\n", + " 80 |\n", + " | |\n", + " | |\n", + " RimV 211 \n", + " | /\n", + " | / \n", + " 97 / \n", + " | /\n", + " | /\n", + " Pit /\n", + " | /\n", + " | /\n", + " 101 /\n", + " | /\n", + " | /\n", + " Bucharest\n", + "\n", + "Initialize:\n", + "Frontier <- Sib\n", + "\n", + "Processing steps:\n", + "1. Pop frontier (priority queue, total path cost order), \n", + "2. Goal test\n", + "3. Generate descendent nodes and insert descendent nodes into frontier.\n", + "\n", + "Initialize:\n", + "Frontier <- Sib\n", + "\n", + "Order of examining nodes.\n", + " 1. Frontier(Sib) \n", + " 2. Frontier.pop -> Sib\n", + " 3. GoalTest(Sib) -> False\n", + " 4. Expand(Sib) -> RimV, Fagar\n", + " 5. Frontier(RimV, Fagar)\n", + " 6. Frontier.pop -> RimV\n", + " 7. GoalTest(RimV) -> False\n", + " 8. Expand(RimV) -> Pit\n", + " 9. Frontier(Fagar, Pit)\n", + " 10. Frontier.pop -> Fagar\n", + " 11. GoalTest(Fagar) -> False\n", + " 12. Expand(Fagar) -> Fagar-Bucharest\n", + " 13. Frontier(Pit, Fagar-Bucharest)\n", + " 14. Frontier.pop -> Pit\n", + " 15. GoalTest(Pit) -> False\n", + " 16. Expand(Pit) -> Pit-Bucharest\n", + " 17. Frontier(Pit-Bucharest, Fagar-Bucharest)\n", + " 18. Frontier.pop -> Pit-Bucharest\n", + " 19. GoalTest(Pit-Bucharest) -> True\n", + " 20. STOP" + ] + }, { "cell_type": "markdown", "metadata": {}, From d59769509e5a969956bda100819242b090cdf67e Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Mon, 9 Jan 2017 05:47:52 -0800 Subject: [PATCH 20/32] Additional updates and notes for Chapter 3 --- adxyz_agents_chap2.ipynb | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 66d12de90..e8e658aef 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1172,6 +1172,27 @@ " 20. STOP" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Related proofs" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Prove optimality of uniform cost search (TBD)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Prove that the graph-search version of A-star is optimal if the heuristic function is consistent (TBD)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1183,8 +1204,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "These approaches for searching the graph tend to produce faster results, but are dependent on information that may or may not be available at all times." + "These approaches for searching the graph tend to produce faster results, but are dependent on information that may or may not be available at all times. At each step, an evaluation function is applied to each node in the frontier, and the one that has the optimal evaluation function value will be expanded." ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { From 1231a214d76bc671ea997eabb08b33994deabc98 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Mon, 9 Jan 2017 06:04:04 -0800 Subject: [PATCH 21/32] Integrated notes from word document on dropbox. --- adxyz_agents_chap2.ipynb | 312 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 308 insertions(+), 4 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index e8e658aef..8cbb717f0 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -297,6 +297,104 @@ "# return dead_agents or no_edibles" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Notes and exercises from the book." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Part I Artificial Intelligence  \n", + "          1 Introduction \n", + "The basis of the course is the idea of an intelligent agent (to be described later). The intelligent agent receives input from its environment, is able to do some processing, and then act on the environment to modify it in some desired fashion. \n", + "In some cases, large amounts of training data will make a poor algorithm outperform a good algorithm on a smaller amount of data. (This is very interesting). \n", + "--Yarowsky 1995, Word Sense Disambiguiation. \n", + "--Banko and Brill, 2001, bootstrap \n", + "--Hays and Efros, 2007, filling photos with holes \n", + "Exercises: \n", + "1.1 Define in your own words intelligence, artificial intelligence, agent, rationality, logical reasoning. \n", + "Intelligence is the ability to take information from external sources, and along with internal stored information, take actions which change the external environment in a way that appears to have a structure or pattern that can be observed and recognized by others. For example, if a door is closed and locked, and a person tries to open it by reaching into their pocket and seeking a key that fits, this would be considered an act of intelligence. By contrast, simply kicking the door down would not be considered intelligence, since it is wasteful of resources. \n", + "Artificial Intelligence is simulating the intelligence of living beings by non-living machines, but not necessarily by duplicating the methods (which may be unknown). The pattern of taking external input, processing it in some way to reach a choice, and then acting on that choice externally are the key elements to reproduce. \n", + " \n", + "Notes on Turing (1950): In this paper, Alan Turing describes the \"Imitation Game\" which is what we now think of as the Turing test. In his original version, there is the interregator, the test subject, and also a third player who is human, and tries to provide evidence of human responses to the interogator (probably to simulate a control-test experimental setup). Other parts of the paper describe learning machines, reinforcement learning, and the characterization of \"thinking\" as being related to storage size. At a storage size of 1Gbit or so, Turing believes machines will pass the \"Imitation Game\" test in that an interogator will be unable to distinguish between a human and machine. The human brain, estimated at the time of this paper, was asserted to have approximately 10^10 to 10^15 bits of storage, which is 1GB to 100TB – this is a fairly wide range, but with current day technology, easily achievable either in the cloud or on native hardware." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2 Intelligent Agents  \n", + " \n", + "Rational Agent Design: \n", + "A rational agent will maximize the performance measure given the percept value it has seen so far. \n", + "Four Types of Agents: \n", + "-a- Goal Seeking \n", + "-b- Utility Maximization \n", + "-c- Reflex \n", + "-d- Model based \n", + "-e- Learning agents \n", + " \n", + "Exercises: \n", + "2.1 Suppose that the performance measure is concerned with just the first T steps of the environment and ignores everything thereafter. Show that a rational agent's action may depend not on just the state of the environment but also the time step it has reached: \n", + "In the general case, we would have a situation where you would need to include the time step and compare it to T in order to decide on an action. Once the time step T is reached, any further action cannot change the performance measure, any action is acceptable. However, before that time step is reached, the action taken must be such that the performance measure is maximized. \n", + " \n", + " 2.2: \n", + "A) Show that the simple vacuum-cleaner agent function described in Figure 2.3 is indeed rational under the assumptions listed on page 38: \n", + "Note: A time step consists of observing a percept, taking an action, and having the performance measure updated: \n", + "Definition of a Rational Agent: For each possible percept sequence, a rational agent should select an action that is expected to maximize its performance measure, given the evidence provided by the percept sequence and whatever built-in knowledge the agent has. \n", + "Assumptions: \n", + "-a- The performance measure awards one point for each clean square at each time step, over a \"lifetime\" of 1000 time steps. (Note: The performance measure is assessed from the state of the entire environment, not agent, point of view) \n", + "-b- The geography of the environment is known a priori (Figure 2.2), but the dirt distribution and initial location of the agent are not. Clean squares stay clean and sucking cleans the current square. The Left and Right actions move the agent outside the environment, in which case the agent remains where it is. \n", + "-c- The only available actions are Left, Right, and Suck. \n", + "-d- The agent correctly perceives its location and whether that location contains dirt. \n", + "Solution: \n", + "There are four possible environments that can arise as initial condition (not including the initial location of the agent, which will double the count) \n", + "Case 1: A clean, B clean \n", + "Case 2: A dirty, B clean \n", + "Case 3: A clean, B dirty \n", + "Case 4: A dirty, B dirty \n", + "----> \n", + "For the following analysis, assume that the agent is always initially places in square A. \n", + "For Case 1: The maximum theoretical performance value is 2x1000=2000 points, since at each time step, both squares are clean, so we are awarded 1 point for each panel and the total time history is 1000. The agent will reproduce this theoretical maximum, because it will merely oscillate between squares A and B. \n", + "For Case 2: The maximum theoretical performance value is also 2x1000=2000 points, since we assume that if there is dirt in square A, then it will be cleaned, and at the end of the time step, the evaluation of the measure will be based on two clean squares since we are assuming that our agent is in square A initially. Our agent performs actions that will produce the same result. \n", + "For Case 3: The maximum theoretical performance value is (1+0) + 2*999=1999. During the first time step, we only have one clean square, and the agent will have to move from A to B in order to clean square B. Including and after the second time step, we have two clean squares and gain 2 points for each time step. Our agent reproduces this action and thus will match the maximum possible performance. \n", + "For Case 4: The maximum theoretical performance value is (1+0) + (1+0)+2*998= 1998. On the first time step, we have can only clean the first square A, thus only obtaining 1 points. On the second time step, we move to square B, and only have the clean square A to give a point. On the third and subsequent steps, we have both squares clean, and get a full two points each step. The agent will match this theoretical performance by its actions. \n", + " \n", + "B) Describe a rational agent function for the case where each movement costs one point. Does the corresponding agent program require internal state? \n", + "A key observation is that once a given square is clean you don't want to waste points by moving back to it, so we need to remember whether or not a cell is clean. \n", + "Like in part A, there are four cases that the environment can be in (8 if you include the position of the vacuum). \n", + "Set of rules for agent program: \n", + "[A, clean; A-State:unknown, B-state:unknown] -> [Right, A-State=clean, B-State=unknown] \n", + "[B, clean; A-State:unknown, B-state:unknown] -> [Left, A-State=unknown, B-State=Clean] \n", + "[A, dirty; A-State:unknown, B-state:unknown] -> [Suck, A-State=clean, B-State=unknown] \n", + "[B, dirty; A-State:unknown, B-state:unknown] -> [Suck, A-State=unknown, B-State=clean] \n", + "[A, clean; A-State:clean, B-state:unknown] -> [Right, A-State=clean, B-State=unknown] \n", + "[B, clean; A-State:clean, B-state:unknown] -> [Suck, A-State=clean, B-State=clean] \n", + "XX[A, dirty; A-State:clean, B-state:unknown] -> [Left, A-State=unknown, B-State=unknown] \n", + "[B, dirty; A-State:clean, B-state:unknown] -> [Suck, A-State=clean, B-State=clean] \n", + "[A, clean; A-State:unknown, B-state:clean] -> [Suck, A-State=clean, B-State=clean] \n", + "[B, clean; A-State:unknown, B-state:clean] -> [Left, A-State=unknown, B-State=clean] \n", + "[A, dirty; A-State:unknown, B-state:clean] -> [Suck, A-State=clean, B-State=clean] \n", + "XX[B, dirty; A-State:unknown, B-state:clean] -> [Left, A-State=unknown, B-State=unknown] \n", + "[A, clean; A-State:clean, B-state:clean] -> [Suck, A-State=clean, B-State=clean] \n", + "[B, clean; A-State:clean, B-state:clean] -> [Suck, A-State=clean, B-State=clean] \n", + "XX[A, dirty; A-State:clean, B-state:clean] -> [Left, A-State=unknown, B-State=unknown] \n", + "XX[B, dirty; A-State:clean, B-state:clean] -> [Left, A-State=unknown, B-State=unknown] \n", + " \n", + "2.3) \n", + "An agent that senses only partial information about the state cannot be perfectly rational. \n", + "The definition of a rational agent is: For each possible percept sequence, a rational agent should select an action that is expected to maximize its performance measure, given the evidence provided by the percept sequence and whatever built-in knowledge the agent has. \n", + "By this definition, as long as the agent selects an action that is expected to maximize its performance measure, given the evidence provided by the percept sequence, it is rational. If the percept sequence is something like [A, maybe dirty], there will be an action for this case that will attempt to maximize the known performance measure. \n", + "FALSE: Here is an example where the agent is perfectly rational. The environment is one square and a point is given for each time step where the square is clean. The only action is Suck. Assume the worse case that the agent cannot detect whether the square is clean or not. This becomes irrelevant because each time step, the agent will take the single action Suck and either clean the square if it is dirty, and get the point, or simply clean an already clean square, with no loss. Under the given performance measure (which is awarding points for clean squares) this is optimal. \n", + " \n", + "There exist task environments in which no pure reflex agent can behave rationally. \n", + "A pure reflex agent only uses the current percept to make a decision. It is not allowed to store information about previous percepts. Imagine a situation where a point is deducted for each move on the two square vacuum world, and one point is given for each clean square. Once a square has been cleaned, the agent shouldn't return to it. However, without this knowledge being stored, the agent is destined to repeatedly return to previously clean squares, which is not rational, given the fact that these precepts have already been observed. This assumes that the reflex agent is restricted to observing only the current square it is on. " + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1186,6 +1284,33 @@ "- Prove optimality of uniform cost search (TBD)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Part II Problem Solving  \n", + "          3 Solving Problems by Searching  \n", + "The Chapter introduces solutions to environments that are deterministic, observable, static, and completely known. \n", + "Proof that uniform cost search is optimal (The algorithm will find the path to the goal state with the smallest path cost) \n", + "Algorithm (Frontier is Priority Queue with path cost as the priority – smaller path cost, higher priority: \n", + "Initialize: Frontier <- Initial State \n", + "Frontier.pop -> Ns = Node with smallest path cost in Frontier \n", + "GoalTest(Ns). If True, then stop else expand Ns(and mark as expanded) and place children in Frontier. \n", + "Repeat steps 2 & 3. \n", + "Lemma 1: The path from the starting node to any unexpanded node in the graph must cross the Frontier. This is by the graph separation property. \n", + "Definitions: A graph can be partitioned into three mutually exclusive sets. \n", + "Expanded nodes: A node that is on any path from the initial state node to any frontier node. A node becomes expanded after two steps: 1) it has been added to the frontier and (2) its descendants have been added to the frontier at which point the node itself is marked as \"expanded\" and removed from the frontier set. \n", + "Frontier nodes: A node which is currently in the frontier, but not its descendants. \n", + "Unexpanded nodes: All other nodes in the graph. \n", + "Proof: Base case. The start node is placed in the frontier during initialization, and thus every other node has to be outside the frontier that reaches the initial node. \n", + "Inductive step: Assume a node is a frontier node. We expand all its descendants and make them frontier nodes and remove the original node from the frontier, marking it \"expanded.\" Note that there might be descendent nodes that are already frontier nodes. There are two possible paths to reach the original node from an unexpanded node. \n", + "Path 1: Through a descendent node. Since each descendent node is on the frontier, this would entail crossing the frontier. \n", + "Path 2: Through the parent(s) of the original node. However, since the original node was in the frontier, its parent must have been marked as expanded, meaning that all of its descendants had to be in the frontier thus preventing an unexpanded node from reaching the parent without crossing the frontier. This process can be repeated by induction until the initial node is reached, whereby definition it is already inside of the frontier and cannot be reached by an unexpanded node without crossing the frontier. \n", + "Lemma 2: At each step, the unexpanded node with the smallest path cost will be selected from the Frontier for expansion. \n", + "Base case: The start node is placed in the frontier. The path cost is zero (we assume all path costs to be non-zero positive numbers that require moving to a different node). Thus, the start node will be selected for expansion since the smallest possible path cost is zero. \n", + "Inductive case: The unexpanded node with smallest path cost will be selected from the priority queue frontier for expansion. Additionally, this path cost is optimal (there is no smaller path cost to this node). \n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1208,13 +1333,192 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Exercises in Chapter 3: \n", + "3.1 Explain why problem formulation must follow goal formulation. The goal (state) specifies the structure of the answer being sought by the agent. For example, the goal might be \"have all the tiles in an 8-tile puzzle in the correct location starting from an arbitrary random arrangement.\" This provides the framework that specifies the state space (arrangements tiles in an 8-tile puzzle), and also implies restrictions on how this can be accomplished (namely following the mechanics of how 8-til puzzles work, one-tile can be moved at a time into a blank, or alternatively, a blank can be moved in one of four directions from center, one of three on edge, one of two in a corner position. \n", + "3.2 Your goal is to navigate a robot out of a maze. The robot starts at the center facing north. You can turn the robot to face north, east, south, or west. You can direct the robot to move a certain distance forward, although it will stop before hitting a wall. \n", + "A) How large is the state space: If we consider the state space to be the location of the robot on a discrete x-y grid consisting of N locations, then the state space will be 4*N, for each of the four directions the robot can be facing at each location. \n", + "B) We change the problem so that the only place you can turn is at the intersection of two or more corridors, how big is the state space now?: Let M be the number of intersections of the type just mentioned. Then, we would have a state space of 4*M. Each state would have the form, , which would then yield the next possible state that is reachable in the search tree. \n", + "C) From each point in the maze, we can move in any of the four directions until we reach a turning point, and this is the only action we need to do. Reformulate the problem now. Do we still need to keep track of the robot's orientation? A state could be defined like this: \n", + ", Direction, Evaluation Function. No, you don’t need to keep track of the robots orientation. \n", + "D) List the simplifications to the real world. Simplifications: \n", + "1) Only four directions are possible \n", + "2) You can only turn at intersections \n", + "3) The knowledge of the robots location and heading are exact. \n", + "3.3) \n", + "Suppose two friends live in different cities on a map, such as the Romania one. On every turn we can simultaneously move each friend to a neighboring city on the map. The amount of time to move from one city to another is the road distance d(I,j), but the friend that arrives first must wait for the other to arrive at their city before the next step. We want the two friends to meet as quickly as possible: \n", + "A) Write a detailed formulation of the search problem: \n", + "Initial State: FriendA in their starting location, FriendB in their starting location \n", + "Actions: FriendA & FriendB select next destination that minimizes evaluation function. This action list should also include not moving from the given location (think of the degenerate case of only two cities- In the event of a tie on the goal contour, friendA can arbitrarily be selected to do the travelling. \n", + "Transition Model: (Describing the state changes as a consequence of the actions): New locations for FriendA and FriendB \n", + "Goal Test Function: Are FriendA and FriendB at same map location? \n", + "Path Cost Function: Distance to next town + distance traveled so far. \n", + "If we imagine the state to consist of pairs of cities (with the goal state being the same city), then we can precompute the straight line distance between the city pairs, and this would be an admissible heuristic (it does not over estimate the travel time). At each time step, we would expand the nodes and take the state with the smallest heuristic. \n", + "B) Admissible heuristics \n", + "C) Are there completely connected maps that have no solution? \n", + "D) Are there maps in which all solutions require one friend to visit the same city twice? " + ] + }, + { + "cell_type": "markdown", "metadata": { "collapsed": true }, - "outputs": [], - "source": [] + "source": [ + "   4 Beyond Classical Search  \n", + " \n", + "          5 Adversarial Search  \n", + " \n", + "          6 Constraint Satisfaction Problems  \n", + " \n", + "Part III Knowledge and Reasoning  \n", + "          7 Logical Agents  \n", + " \n", + "          8 First-Order Logic  \n", + " \n", + "          9 Inference in First-Order Logic  \n", + " \n", + "        10 Classical Planning  \n", + " \n", + "        11 Planning and Acting in the Real World  \n", + " \n", + "        12 Knowledge Representation  \n", + " \n", + "Part IV Uncertain Knowledge and Reasoning  \n", + "        13 Quantifying Uncertainty  \n", + " \n", + "        14 Probabilistic Reasoning  \n", + " \n", + "        15 Probabilistic Reasoning over Time  \n", + " \n", + "        16 Making Simple Decisions  \n", + " \n", + "        17 Making Complex Decisions  \n", + " \n", + "Part V Learning  \n", + "        18 Learning from Examples  \n", + " \n", + "        19 Knowledge in Learning  \n", + " \n", + "        20 Learning Probabilistic Models  \n", + " \n", + "        21 Reinforcement Learning  \n", + " \n", + "Part VII Communicating, Perceiving, and Acting  \n", + "        22 Natural Language Processing  \n", + " \n", + "        23 Natural Language for Communication  \n", + " \n", + "        24 Perception  \n", + " \n", + "        25 Robotics  \n", + " \n", + "Part VIII Conclusions  \n", + "        26 Philosophical Foundations  \n", + " \n", + "        27 AI: The Present and Future  " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A Mathematical Background [pdf]  \n", + "         B Notes on Languages and Algorithms [pdf]  \n", + "             Bibliography [pdf and histograms]  \n", + "             Index [html or pdf] " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### OVERALL NOTES: \n", + "Performance Measures: \n", + "We always consider first the performance measure that is evaluated on any given sequence of environment states (not states of the agent). This is critical. \n", + "As a general rule, it is better to design performance measures according to what one actually wants in the environment, rather than according to how one thinks the agent should behave. \n", + "Rational agents maximize expected performance measures. \n", + "The definition of a rational agent is: For each possible percept sequence, a rational agent should select an action that is expected to maximize its performance measure, given the evidence provided by the percept sequence and whatever built-in knowledge the agent has. \n", + "Perfect agents maximize actual performance measures. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "EXAMPLE: PEAS framework – Create this first, before designing the agent. \n", + "Agent Type \n", + "Performance Measure (EXTERNAL TO AGENT) \n", + "Environment (EXTERNAL TO AGENT) \n", + "Actuators \n", + "(AVAILABLE TO AGENT) \n", + "Sensors \n", + "(AVAILABLE TO AGENT) \n", + "Taxi Driver \n", + "Safe, fast, legal, comfortable trip, maximize profits \n", + "Roads, other traffic, pedestrians, customers \n", + "Steering, accelerator, brake, signal, horn, display \n", + "Cameras, sonar, speedometer, GPS, odometer, accelerometer, engine sensors, keyboard \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Types of Rational Agents: \n", + "Simple Reflex \n", + " \n", + "Model Based \n", + " \n", + "Goal Based \n", + "Problem-solving agent (Chap 3): Using atomic representations – states are black boxes. \n", + " \n", + "Planning agents (Chap 7): Using factored or structured state representations. \n", + " \n", + "Utility Based \n", + " \n", + "Learning Agents \n", + "Types of task environments: \n", + "1) Observability: \n", + "Fully observable \n", + "Partially observable \n", + "Totally unobservable \n", + "2) Agents: \n", + "Single \n", + "Multiple \n", + "3) Determinism: \n", + "Deterministic \n", + "Stochastic \n", + "4) Episode: \n", + "Episodic \n", + "Sequential \n", + "5) Dynamic: \n", + "Static \n", + "Semi-Dynamic \n", + "Dynamic \n", + "6) Discreteness: \n", + "Discrete \n", + "Continuous \n", + " \n", + "Types of states of the environment. \n", + "1) Atomic – each state of the environment is a discrete, indivisible state. \n", + "Search & Game Playing (Chapters 3-5) \n", + "Hidden Markov Models (Chapter 15) \n", + "Markov Decision Process (Chapter 17) \n", + "2) Factored -- each state of the environment can be described by internal values such as variables, booleans. \n", + "Constraint satisfaction (Chapter 6) \n", + "Propositional logic (Chapter 7) \n", + "Planning (Chapter 10-11) \n", + "Bayesian networks (13-16) \n", + "Machine Learning (18,20,21) \n", + "3) Structured -- Each state can consists of an internal structure with objects that have relationships to each other. \n", + "Relational Databases and first order logic (Chapter 8,9, 12) \n", + "First order probability models (Chapter 14) \n", + "Knowledge-based learning (Chapter 19) \n", + "Natural language processing (Chapter 22, 23) " + ] } ], "metadata": { From 93ddad8e00f468996131a51616cba8357d762c6a Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Wed, 11 Jan 2017 05:40:44 -0800 Subject: [PATCH 22/32] Some clean up. --- adxyz_agents_chap2.ipynb | 78 ++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 8cbb717f0..f0b6fe112 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -792,7 +792,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Chapter 3" + "# Part II Problem Solving \n", + "\n", + "## Chapter 3 (Solving Problems by Searching" ] }, { @@ -805,7 +807,7 @@ "- Define the states of the environment (atomic)\n", "- Define the initial state\n", "- Define legal actions\n", - "- Define transitions\n", + "- Define transitions (How the states change based on the actions)\n", "- Define goal test\n", "- Define path/step costs" ] @@ -1288,8 +1290,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Part II Problem Solving  \n", - "          3 Solving Problems by Searching  \n", + "### Part II Problem Solving  \n", + "#### 3 Solving Problems by Searching  \n", "The Chapter introduces solutions to environments that are deterministic, observable, static, and completely known. \n", "Proof that uniform cost search is optimal (The algorithm will find the path to the goal state with the smallest path cost) \n", "Algorithm (Frontier is Priority Queue with path cost as the priority – smaller path cost, higher priority: \n", @@ -1297,18 +1299,33 @@ "Frontier.pop -> Ns = Node with smallest path cost in Frontier \n", "GoalTest(Ns). If True, then stop else expand Ns(and mark as expanded) and place children in Frontier. \n", "Repeat steps 2 & 3. \n", + "\n", "Lemma 1: The path from the starting node to any unexpanded node in the graph must cross the Frontier. This is by the graph separation property. \n", + "\n", "Definitions: A graph can be partitioned into three mutually exclusive sets. \n", + "\n", "Expanded nodes: A node that is on any path from the initial state node to any frontier node. A node becomes expanded after two steps: 1) it has been added to the frontier and (2) its descendants have been added to the frontier at which point the node itself is marked as \"expanded\" and removed from the frontier set. \n", + "\n", "Frontier nodes: A node which is currently in the frontier, but not its descendants. \n", + "\n", "Unexpanded nodes: All other nodes in the graph. \n", - "Proof: Base case. The start node is placed in the frontier during initialization, and thus every other node has to be outside the frontier that reaches the initial node. \n", + "\n", + "Proof: \n", + "\n", + "Base case. The start node is placed in the frontier during initialization, and thus every other node has to be outside the frontier that reaches the initial node. \n", + "\n", "Inductive step: Assume a node is a frontier node. We expand all its descendants and make them frontier nodes and remove the original node from the frontier, marking it \"expanded.\" Note that there might be descendent nodes that are already frontier nodes. There are two possible paths to reach the original node from an unexpanded node. \n", + "\n", "Path 1: Through a descendent node. Since each descendent node is on the frontier, this would entail crossing the frontier. \n", + "\n", "Path 2: Through the parent(s) of the original node. However, since the original node was in the frontier, its parent must have been marked as expanded, meaning that all of its descendants had to be in the frontier thus preventing an unexpanded node from reaching the parent without crossing the frontier. This process can be repeated by induction until the initial node is reached, whereby definition it is already inside of the frontier and cannot be reached by an unexpanded node without crossing the frontier. \n", + "\n", "Lemma 2: At each step, the unexpanded node with the smallest path cost will be selected from the Frontier for expansion. \n", + "\n", "Base case: The start node is placed in the frontier. The path cost is zero (we assume all path costs to be non-zero positive numbers that require moving to a different node). Thus, the start node will be selected for expansion since the smallest possible path cost is zero. \n", - "Inductive case: The unexpanded node with smallest path cost will be selected from the priority queue frontier for expansion. Additionally, this path cost is optimal (there is no smaller path cost to this node). \n" + "\n", + "Inductive case: The unexpanded node with smallest path cost will be selected from the priority queue frontier for expansion. \n", + "Additionally, this path cost is optimal (there is no smaller path cost to this node). \n" ] }, { @@ -1338,41 +1355,72 @@ "source": [ "### Exercises in Chapter 3: \n", "3.1 Explain why problem formulation must follow goal formulation. The goal (state) specifies the structure of the answer being sought by the agent. For example, the goal might be \"have all the tiles in an 8-tile puzzle in the correct location starting from an arbitrary random arrangement.\" This provides the framework that specifies the state space (arrangements tiles in an 8-tile puzzle), and also implies restrictions on how this can be accomplished (namely following the mechanics of how 8-til puzzles work, one-tile can be moved at a time into a blank, or alternatively, a blank can be moved in one of four directions from center, one of three on edge, one of two in a corner position. \n", + "\n", "3.2 Your goal is to navigate a robot out of a maze. The robot starts at the center facing north. You can turn the robot to face north, east, south, or west. You can direct the robot to move a certain distance forward, although it will stop before hitting a wall. \n", + "\n", "A) How large is the state space: If we consider the state space to be the location of the robot on a discrete x-y grid consisting of N locations, then the state space will be 4*N, for each of the four directions the robot can be facing at each location. \n", + "\n", "B) We change the problem so that the only place you can turn is at the intersection of two or more corridors, how big is the state space now?: Let M be the number of intersections of the type just mentioned. Then, we would have a state space of 4*M. Each state would have the form, , which would then yield the next possible state that is reachable in the search tree. \n", + "\n", "C) From each point in the maze, we can move in any of the four directions until we reach a turning point, and this is the only action we need to do. Reformulate the problem now. Do we still need to keep track of the robot's orientation? A state could be defined like this: \n", ", Direction, Evaluation Function. No, you don’t need to keep track of the robots orientation. \n", + "\n", "D) List the simplifications to the real world. Simplifications: \n", "1) Only four directions are possible \n", "2) You can only turn at intersections \n", "3) The knowledge of the robots location and heading are exact. \n", - "3.3) \n", - "Suppose two friends live in different cities on a map, such as the Romania one. On every turn we can simultaneously move each friend to a neighboring city on the map. The amount of time to move from one city to another is the road distance d(I,j), but the friend that arrives first must wait for the other to arrive at their city before the next step. We want the two friends to meet as quickly as possible: \n", + "\n", + "3.3) Suppose two friends live in different cities on a map, such as the Romania one. On every turn we can simultaneously move each friend to a neighboring city on the map. The amount of time to move from one city to another is the road distance d(I,j), but the friend that arrives first must wait for the other to arrive at their city before the next step. We want the two friends to meet as quickly as possible: \n", + "\n", "A) Write a detailed formulation of the search problem: \n", + "\n", "Initial State: FriendA in their starting location, FriendB in their starting location \n", + "\n", "Actions: FriendA & FriendB select next destination that minimizes evaluation function. This action list should also include not moving from the given location (think of the degenerate case of only two cities- In the event of a tie on the goal contour, friendA can arbitrarily be selected to do the travelling. \n", + "\n", "Transition Model: (Describing the state changes as a consequence of the actions): New locations for FriendA and FriendB \n", + "\n", "Goal Test Function: Are FriendA and FriendB at same map location? \n", + "\n", "Path Cost Function: Distance to next town + distance traveled so far. \n", "If we imagine the state to consist of pairs of cities (with the goal state being the same city), then we can precompute the straight line distance between the city pairs, and this would be an admissible heuristic (it does not over estimate the travel time). At each time step, we would expand the nodes and take the state with the smallest heuristic. \n", - "B) Admissible heuristics \n", + "\n", + "B) Admissible heuristics?\n", + "\n", "C) Are there completely connected maps that have no solution? \n", + "\n", "D) Are there maps in which all solutions require one friend to visit the same city twice? " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4 Beyond Classical Search  \n", + " \n", + "         " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 5 Adversarial Search\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 6 Constraint Satisfaction Problems\n" + ] + }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ - "   4 Beyond Classical Search  \n", - " \n", - "          5 Adversarial Search  \n", - " \n", - "          6 Constraint Satisfaction Problems  \n", - " \n", "Part III Knowledge and Reasoning  \n", "          7 Logical Agents  \n", " \n", From 055379bec331a2d710fd69dffab1fe0352d56ead Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Wed, 11 Jan 2017 06:04:47 -0800 Subject: [PATCH 23/32] Added answer to chapter 3 questions. --- adxyz_agents_chap2.ipynb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index f0b6fe112..c06c27e17 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1389,6 +1389,8 @@ "\n", "C) Are there completely connected maps that have no solution? \n", "\n", + "One possible case is a map consisting of two nodes that are connected. If the search algorithm doesn't take this into account, then the friends could swap cities, and then no longer be able to either swap back (since the state has been visited) and cannot meet.\n", + "\n", "D) Are there maps in which all solutions require one friend to visit the same city twice? " ] }, From 54239b19622cdbc8ad630fb3ffeecc3031e3cd7e Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Wed, 11 Jan 2017 15:52:30 -0800 Subject: [PATCH 24/32] additional content. --- adxyz_agents_chap2.ipynb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index c06c27e17..7a383a2ad 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1391,7 +1391,10 @@ "\n", "One possible case is a map consisting of two nodes that are connected. If the search algorithm doesn't take this into account, then the friends could swap cities, and then no longer be able to either swap back (since the state has been visited) and cannot meet.\n", "\n", - "D) Are there maps in which all solutions require one friend to visit the same city twice? " + "CityA ------------ CityB\n", + "\n", + "D) Are there maps in which all solutions require one friend to visit the same city twice?\n", + "\n" ] }, { @@ -1399,8 +1402,7 @@ "metadata": {}, "source": [ "## 4 Beyond Classical Search  \n", - " \n", - "         " + " " ] }, { From f629ab22cf708f4e85f5358641c3a858a90e82a7 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Thu, 12 Jan 2017 06:06:28 -0800 Subject: [PATCH 25/32] Worked on 3.3D --- adxyz_agents_chap2.ipynb | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 7a383a2ad..5f83a6133 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1394,7 +1394,26 @@ "CityA ------------ CityB\n", "\n", "D) Are there maps in which all solutions require one friend to visit the same city twice?\n", - "\n" + "\n", + "Consider the following map:\n", + "\n", + "\n", + "CityA--5---CityE\n", + "| |\n", + "| |\n", + "10 15\n", + "| |\n", + "| CityD\n", + "CityB (3) /\n", + " /\n", + "(BC=20) 25\n", + "(AC=30) /\n", + "CityC----\n", + "\n", + "\n", + "FriendA starts in CityA\n", + "FriendB starts in CityC\n", + "This map would require FriendA to visit CityA twice if we used the straightline distance heuristic (which we assert is the same as the road distances shown on the graph)." ] }, { From 5f62837decddda75644fbdcf12360f72d11ee7b1 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Fri, 13 Jan 2017 06:36:53 -0800 Subject: [PATCH 26/32] Started on question 3.4 --- adxyz_agents_chap2.ipynb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 5f83a6133..5001b7520 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1413,7 +1413,17 @@ "\n", "FriendA starts in CityA\n", "FriendB starts in CityC\n", - "This map would require FriendA to visit CityA twice if we used the straightline distance heuristic (which we assert is the same as the road distances shown on the graph)." + "This map would require FriendA to visit CityA twice if we used the straightline distance heuristic (which we assert is the same as the road distances shown on the graph).\n", + "\n", + "3.4) Show that the 8-tile puzzle states are divided into two disjoint sets. You can reach any state from another other state within a given set, but cannot go between sets.\n", + "\n", + "B12 \n", + "345\n", + "678\n", + "\n", + "1B2\n", + "345\n", + "678" ] }, { From 1cb10316b2a8e1800cadb7124275e1ccc1036908 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Fri, 13 Jan 2017 06:41:44 -0800 Subject: [PATCH 27/32] More on question 3.4 --- adxyz_agents_chap2.ipynb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 5001b7520..21ced363d 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1423,6 +1423,10 @@ "\n", "1B2\n", "345\n", + "678\n", + "\n", + "12B\n", + "345\n", "678" ] }, From 64a8d14b5b608f4034044d428f7b94b9a2d1da8c Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Fri, 13 Jan 2017 06:55:02 -0800 Subject: [PATCH 28/32] Additional info in problem 3.4 --- adxyz_agents_chap2.ipynb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 21ced363d..3a654db64 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1427,6 +1427,10 @@ "\n", "12B\n", "345\n", + "678\n", + "\n", + "125\n", + "34B\n", "678" ] }, From 86d85439afae531496066acbcfead324137bc2b1 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Fri, 13 Jan 2017 06:57:44 -0800 Subject: [PATCH 29/32] Some more material for problem 3.4 --- adxyz_agents_chap2.ipynb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 3a654db64..9aed2140f 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1431,7 +1431,11 @@ "\n", "125\n", "34B\n", - "678" + "678\n", + "\n", + "125\n", + "348\n", + "67B" ] }, { From d4cdd723705b57c4eac7c2dc8c4b99456b555de9 Mon Sep 17 00:00:00 2001 From: Anthony Daniell Date: Sun, 15 Jan 2017 08:12:45 -0800 Subject: [PATCH 30/32] worked on problem 3.5 --- adxyz_agents_chap2.ipynb | 48 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index 9aed2140f..ce2430527 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1435,7 +1435,53 @@ "\n", "125\n", "348\n", - "67B" + "67B\n", + "\n", + "3.5). Consider the 8-queens problem with the efficient incremental implementation on page 72. Explain why the state space has at least cube root of n factorial states and estimate the largest n for which exhaustive search is feasible.\n", + "\n", + "A given state is defined as the positions of the n-queens in n separate columns.\n", + "\n", + "In a given column we have the following situation.\n", + "\n", + "From the first column:\n", + "\n", + "In each column, we reduce the potential state space by 3 squares in each remaining column to the right in the worst case. Thus, the next column, i, will have at least N_i-3 new nodes to add to the search tree (although, the exact nodes as they refer to board locations can depend on the selected position of the queens in earlier rounds).\n", + "\n", + "Therefore, the sequence of branching is:\n", + "\n", + "i=0: N\n", + "i=1: N-3(i)\n", + "i=2: N-3(i)\n", + ".\n", + ".\n", + ".\n", + "i=7: N-3(i)\n", + "\n", + "The total number of states then is\n", + "\n", + "Prod(i=0 to N-1) max{N-3(i),1}\n", + "For the case of 8-queens, this is (although after the first three terms, the rest are set to 1): \n", + "\n", + " N(N-3)(N-6) * (1)(1)(1) * (1)(1) \n", + " \n", + "<= N(N-1)(N-2) * (N-3)(N-4)(N-5) * (N-6)(N-7) = N!\n", + "\n", + " X * X * X = N!\n", + "\n", + "where X=cuberoot(N!)\n", + "\n", + "Is the cuberoot of N! <= N(N-3)(N-6) ?\n", + "\n", + "if the cuberoot of N! = N(N-3)(N-6), then N! = N(N-3)(N-6) * N(N-3)(N-6) * N(N-3)(N-6), which it does not. Is N(N-3)(N-6) greater or less than the cuberoot of N! ? \n", + "\n", + "\n", + "N(N-3)(N-6) * N(N-3)(N-6) * N(N-3)(N-6)\n", + "\n", + "N*N*N >= N (N-1)(N-2)\n", + "(N-3)(N-3)(N-3) >= (N-3)(N-4)(N-5)\n", + "(N-6)(N-6)(N-6) >= (N-6)(N-7)(1)\n", + "\n", + "Therefore, cuberoot of N! <= N(N-3)(N-6), which itself was a lower bound on the number of states that would need to be searched, therefore the proof is complete. The number of states that must be searched is at least cuberoot of N!. This proof depends on being able to split up the N! evenly into 3 products, the first of which only includes those terms up to the point where 3i < N, for integer i.\n" ] }, { From 5820d29c31856712fc162cbb7a3af0b5d9b6747b Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Mon, 16 Jan 2017 09:20:39 -0800 Subject: [PATCH 31/32] Added content for exercise 3.6 --- adxyz_agents_chap2.ipynb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index ce2430527..a68374a64 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -1481,7 +1481,24 @@ "(N-3)(N-3)(N-3) >= (N-3)(N-4)(N-5)\n", "(N-6)(N-6)(N-6) >= (N-6)(N-7)(1)\n", "\n", - "Therefore, cuberoot of N! <= N(N-3)(N-6), which itself was a lower bound on the number of states that would need to be searched, therefore the proof is complete. The number of states that must be searched is at least cuberoot of N!. This proof depends on being able to split up the N! evenly into 3 products, the first of which only includes those terms up to the point where 3i < N, for integer i.\n" + "Therefore, cuberoot of N! <= N(N-3)(N-6), which itself was a lower bound on the number of states that would need to be searched, therefore the proof is complete. The number of states that must be searched is at least cuberoot of N!. This proof depends on being able to split up the N! evenly into 3 products, the first of which only includes those terms up to the point where 3i < N, for integer i.\n", + "\n", + "\n", + "3.6)\n", + "a) Using only four colors, you have to color a planar map such that no two adjacent regions have the same color.\n", + "\n", + "i) States:\n", + "A planar map, each region represented by a node, and adjacent regions are connected via links. Each node has a color attribute.\n", + "Choose any region as the initial state, and randomly assign it a color.\n", + "\n", + "ii) Actions: Choose one of the unexplored descendents of the current node, check the list of each descendant of the new node and the parent node and obtain their colors (if they have been assigned). Determine which color(s) have not been assigned. Randomly choose a color from the unassigned list and assign it to the unexplored descendent. Mark this node as explored and repeat. \n", + "\n", + "iii) Transition: An additional node has color assigned to it.\n", + "\n", + "iv) Goal test:\n", + "Search through the entire graph, and verify that no node has a color that matches a connected node to itself.\n", + "\n", + "v) Search/Path cost:" ] }, { From 67b0df9c2b36e446d14475645ae88c839f80a035 Mon Sep 17 00:00:00 2001 From: "U-HOMEOFFICE\\adanie8" Date: Tue, 17 Jan 2017 06:04:44 -0800 Subject: [PATCH 32/32] Added more to exercise 3.6a --- adxyz_agents_chap2.ipynb | 300 +++++++++++++++++++++++++++------------ 1 file changed, 212 insertions(+), 88 deletions(-) diff --git a/adxyz_agents_chap2.ipynb b/adxyz_agents_chap2.ipynb index a68374a64..33569a7ae 100644 --- a/adxyz_agents_chap2.ipynb +++ b/adxyz_agents_chap2.ipynb @@ -936,7 +936,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": true }, @@ -955,33 +955,11 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "NodeSetup:\n", - "Node1 = <__main__.GraphNode object at 0x103ba8fd0>\n", - "Node2 = <__main__.GraphNode object at 0x103ba8f98>\n", - "Node3 = <__main__.GraphNode object at 0x103bc90b8>\n", - "Node4 = <__main__.GraphNode object at 0x103bc90f0>\n", - "Node5 = <__main__.GraphNode object at 0x103bc9128>\n", - "Node6 = <__main__.GraphNode object at 0x103bc9160>\n", - "Node1 links = {<__main__.GraphNode object at 0x103ba8f98>: 10, <__main__.GraphNode object at 0x103bc90b8>: 15}\n", - "Node2 links = {<__main__.GraphNode object at 0x103bc9128>: 6, <__main__.GraphNode object at 0x103bc90b8>: 28, <__main__.GraphNode object at 0x103ba8fd0>: 10, <__main__.GraphNode object at 0x103bc9160>: 7}\n", - "Node3 links = {<__main__.GraphNode object at 0x103ba8f98>: 28, <__main__.GraphNode object at 0x103bc9128>: 8, <__main__.GraphNode object at 0x103ba8fd0>: 15, <__main__.GraphNode object at 0x103bc90f0>: 17}\n", - "Node4 links = {<__main__.GraphNode object at 0x103bc90b8>: 17}\n", - "Node5 links = {<__main__.GraphNode object at 0x103ba8f98>: 6, <__main__.GraphNode object at 0x103bc90b8>: 8}\n", - "Node6 links = {<__main__.GraphNode object at 0x103ba8f98>: 7}\n", - "Node6.goal = True\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "#\n", "# create map\n", @@ -1057,56 +1035,11 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exploring frontier nodes:\n", - "Expanding current node: Node1\n", - "Child Node is being added to frontier: Node2\n", - "Child Node is being added to frontier: Node3\n", - "End of frontier loop:\n", - "-------\n", - "\n", - "Exploring frontier nodes:\n", - "Expanding current node: Node2\n", - "Child Node is being added to frontier: Node5\n", - "Child Node has been seen before: Node3\n", - "Child Node has been seen before: Node1\n", - "Child Node is being added to frontier: Node6\n", - "End of frontier loop:\n", - "-------\n", - "\n", - "Exploring frontier nodes:\n", - "Expanding current node: Node3\n", - "Child Node has been seen before: Node2\n", - "Child Node has been seen before: Node5\n", - "Child Node has been seen before: Node1\n", - "Child Node is being added to frontier: Node4\n", - "End of frontier loop:\n", - "-------\n", - "\n", - "Exploring frontier nodes:\n", - "Expanding current node: Node5\n", - "Child Node has been seen before: Node2\n", - "Child Node has been seen before: Node3\n", - "End of frontier loop:\n", - "-------\n", - "\n", - "Exploring frontier nodes:\n", - "Goal node found.\n", - "Current Node = Node6\n", - "Current Node Parent = Node2\n", - "Current Node Step Cost = 7\n", - "Current Node Path Cost = 17\n" - ] - } - ], + "outputs": [], "source": [ "#\n", "# Run the BFS process\n", @@ -1163,21 +1096,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Path sequence = Node1\n", - "Path sequence = Node2\n", - "Path sequence = Node6\n" - ] - } - ], + "outputs": [], "source": [ "#\n", "# Report out the solution path working backwords from the goal node to the\n", @@ -1489,16 +1412,217 @@ "\n", "i) States:\n", "A planar map, each region represented by a node, and adjacent regions are connected via links. Each node has a color attribute.\n", - "Choose any region as the initial state, and randomly assign it a color.\n", + "Choose any region as the initial state, and place it in frontier.\n", "\n", - "ii) Actions: Choose one of the unexplored descendents of the current node, check the list of each descendant of the new node and the parent node and obtain their colors (if they have been assigned). Determine which color(s) have not been assigned. Randomly choose a color from the unassigned list and assign it to the unexplored descendent. Mark this node as explored and repeat. \n", + "ii) Actions: \n", + "a) While frontier is not empty, pop next node.\n", + "b) Check each linked node of this new node, placing unexplored nodes into the frontier, obtain their colors (if they have been assigned) and determine which color(s) have not been assigned.\n", + "c) Choose a color from the unassigned list and assign it to the current node. Mark this node as explored and return to step (a). \n", "\n", "iii) Transition: An additional node has color assigned to it.\n", "\n", "iv) Goal test:\n", - "Search through the entire graph, and verify that no node has a color that matches a connected node to itself.\n", + "No further regions remain uncolored: (Frontier is empty)\n", + "\n", + "v) Search/Path cost: Search cost will depend on search algorithm. Path cost is not relevant as only the final state matters for correctness and completeness." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "NodeSetup:\n", + "Node1 = <__main__.GraphNode object at 0x0000000004BF8940>\n", + "Node2 = <__main__.GraphNode object at 0x0000000004BF8978>\n", + "Node3 = <__main__.GraphNode object at 0x0000000004BF89B0>\n", + "Node4 = <__main__.GraphNode object at 0x0000000004BF89E8>\n", + "Node5 = <__main__.GraphNode object at 0x0000000004BF8A20>\n", + "Node6 = <__main__.GraphNode object at 0x0000000004BF8A58>\n", + "Node1 links = {<__main__.GraphNode object at 0x0000000004BF89B0>: 1, <__main__.GraphNode object at 0x0000000004BF8978>: 1}\n", + "Node2 links = {<__main__.GraphNode object at 0x0000000004BF8A20>: 1, <__main__.GraphNode object at 0x0000000004BF89B0>: 1, <__main__.GraphNode object at 0x0000000004BF8940>: 1, <__main__.GraphNode object at 0x0000000004BF8A58>: 1}\n", + "Node3 links = {<__main__.GraphNode object at 0x0000000004BF8A20>: 1, <__main__.GraphNode object at 0x0000000004BF8940>: 1, <__main__.GraphNode object at 0x0000000004BF89E8>: 1, <__main__.GraphNode object at 0x0000000004BF8978>: 1}\n", + "Node4 links = {<__main__.GraphNode object at 0x0000000004BF89B0>: 1}\n", + "Node5 links = {<__main__.GraphNode object at 0x0000000004BF89B0>: 1, <__main__.GraphNode object at 0x0000000004BF8978>: 1}\n", + "Node6 links = {<__main__.GraphNode object at 0x0000000004BF8978>: 1}\n" + ] + } + ], + "source": [ + "# Test implementation of 4-color map (Exercise 3.6 a)\n", + "\n", + "#\n", + "# create map\n", + "#\n", + "#\n", + "# Node1 ----- 1 ----- Node2 ---1--- Node6\n", + "# | 1--------/ |\n", + "# | / 1\n", + "# | / |\n", + "# 1 / Node5\n", + "# | | |\n", + "# | | =======1======= \n", + "# |/ |\n", + "# Node3 \n", + "# |\n", + "# |\n", + "# |\n", + "# 1\n", + "# |\n", + "# |\n", + "# |\n", + "# Node4\n", + "#\n", + "#\n", + "\n", + "class GraphNode():\n", + " def __init__(self, initName=None):\n", + " self.links=dict() # (name of link:step cost)\n", + " self.parent=None # Is assigned during BFS\n", + " self.goal=False # True if goal state\n", + " self.pathCost=0\n", + " self.stepCost=0\n", + " self.frontier=False # True if node has been added to frontier\n", + " self.name=initName\n", + " self.color=None\n", + " \n", + "Node1=GraphNode(\"Node1\")\n", + "Node2=GraphNode(\"Node2\")\n", + "Node3=GraphNode(\"Node3\")\n", + "Node4=GraphNode(\"Node4\")\n", + "Node5=GraphNode(\"Node5\")\n", + "Node6=GraphNode(\"Node6\")\n", + "\n", + "Node1.links[Node2]=1\n", + "Node1.links[Node3]=1\n", + "\n", + "Node2.links[Node1]=1\n", + "Node2.links[Node3]=1\n", + "Node2.links[Node5]=1\n", + "Node2.links[Node6]=1\n", + "\n", + "Node3.links[Node1]=1\n", + "Node3.links[Node2]=1\n", + "Node3.links[Node4]=1\n", + "Node3.links[Node5]=1\n", + "\n", + "Node4.links[Node3]=1\n", + "\n", + "Node5.links[Node2]=1\n", + "Node5.links[Node3]=1\n", + "\n", + "Node6.links[Node2]=1\n", + "\n", + "print(\"NodeSetup:\")\n", + "print(\"Node1 = \", Node1)\n", + "print(\"Node2 = \", Node2)\n", + "print(\"Node3 = \", Node3)\n", + "print(\"Node4 = \", Node4)\n", + "print(\"Node5 = \", Node5)\n", + "print(\"Node6 = \", Node6)\n", + "\n", + "print(\"Node1 links = \", Node1.links)\n", + "print(\"Node2 links = \", Node2.links)\n", + "print(\"Node3 links = \", Node3.links)\n", + "print(\"Node4 links = \", Node4.links)\n", + "print(\"Node5 links = \", Node5.links)\n", + "print(\"Node6 links = \", Node6.links)\n", + "\n", + "Node1.parent=Node1 # node1 is the initial node - pointing to itself as parent is the flag." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#\n", + "# Run the BFS process\n", + "#\n", + "\n", + "import queue\n", + "\n", + "###exploredNodes = dict()\n", + "frontierNodes = queue.Queue()\n", + "goalNodeFound = False\n", + "\n", + "#\n", + "# Initialize the frontier queue\n", + "#\n", + "\n", + "frontierNodes.put(Node1)\n", + "Node1.frontier=True\n", + "\n", + "# Main loop\n", + "\n", + "while not frontierNodes.empty():\n", + " print(\"Exploring frontier nodes: \")\n", + " currentNode = frontierNodes.get()\n", + " if currentNode.goal == True:\n", + " goalNodeFound=True\n", + " break\n", + " else: \n", + " print(\"Expanding current node: \", currentNode.name)\n", + " for childNode,dummy in currentNode.links.items(): #Any link is a potential \"child\" \n", + " if (childNode.frontier==True):\n", + " print(\"Child Node has been seen before: \", childNode.name)\n", + " continue\n", + " else:\n", + " print(\"Child Node is being added to frontier: \", childNode.name)\n", + " frontierNodes.put(childNode)\n", + " childNode.frontier=True\n", + " childNode.parent=currentNode\n", + " childNode.stepCost=childNode.links[currentNode] # provide step cost\n", + " childNode.pathCost=currentNode.pathCost+childNode.stepCost\n", + " \n", + " print(\"End of frontier loop:\")\n", + " print(\"-------\")\n", + " print()\n", + " \n", + "if goalNodeFound != True: # goal node was not set\n", + " print (\"Goal node not found.\")\n", + "else:\n", + " print (\"Goal node found.\")\n", + " print (\"Current Node = \", currentNode.name)\n", + " print (\"Current Node Parent = \", currentNode.parent.name)\n", + " print (\"Current Node Step Cost = \", currentNode.stepCost)\n", + " print (\"Current Node Path Cost = \", currentNode.pathCost)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#\n", + "# Report out the solution path working backwords from the goal node to the\n", + "# initial node (which is flagged by having the parent=node)\n", + "#\n", "\n", - "v) Search/Path cost:" + "pathSequence = queue.LifoQueue()\n", + "pathSequence.put(currentNode)\n", + "\n", + "while currentNode != currentNode.parent:\n", + " pathSequence.put(currentNode.parent)\n", + " currentNode=currentNode.parent\n", + "\n", + "# Add the final node, which is the initial in this case\n", + "# The initial node was specially marked to point to itself as parent\n", + "\n", + "while not pathSequence.empty():\n", + " print(\"Path sequence = \", pathSequence.get().name)" ] }, {