{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Intelligent Agents #\n", "\n", "This notebook serves as supporting material for topics covered in **Chapter 2 - Intelligent Agents** from the book *Artificial Intelligence: A Modern Approach.* This notebook uses implementations from [agents.py](https://github.com/aimacode/aima-python/blob/master/agents.py) module. Let's start by importing everything from agents module." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from agents import *\n", "from notebook import psource" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CONTENTS\n", "\n", "* Overview\n", "* Agent\n", "* Environment\n", "* Simple Agent and Environment\n", "* Agents in a 2-D Environment\n", "* Wumpus Environment\n", "\n", "## OVERVIEW\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, a 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 implement a program that helps the agent act on the environment based on its percepts.\n", "\n", "## AGENT\n", "\n", "Let us now see how we define an agent. Run the next cell to see how `Agent` is defined in agents module." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Agent(Thing):\n",
       "    """An Agent is a subclass of Thing with one required slot,\n",
       "    .program, which should hold a function that takes one argument, the\n",
       "    percept, and returns an action. (What counts as a percept or action\n",
       "    will depend on the specific environment in which the agent exists.)\n",
       "    Note that 'program' is a slot, not a method. If it were a method,\n",
       "    then the program could 'cheat' and look at aspects of the agent.\n",
       "    It's not supposed to do that: the program can only look at the\n",
       "    percepts. An agent program that needs a model of the world (and of\n",
       "    the agent itself) will have to build and maintain its own model.\n",
       "    There is an optional slot, .performance, which is a number giving\n",
       "    the performance measure of the agent in its environment."""\n",
       "\n",
       "    def __init__(self, program=None):\n",
       "        self.alive = True\n",
       "        self.bump = False\n",
       "        self.holding = []\n",
       "        self.performance = 0\n",
       "        if program is None or not isinstance(program, collections.Callable):\n",
       "            print("Can't find a valid program for {}, falling back to default.".format(\n",
       "                self.__class__.__name__))\n",
       "\n",
       "            def program(percept):\n",
       "                return eval(input('Percept={}; action? '.format(percept)))\n",
       "\n",
       "        self.program = program\n",
       "\n",
       "    def can_grab(self, thing):\n",
       "        """Return True if this agent can grab this thing.\n",
       "        Override for appropriate subclasses of Agent and Thing."""\n",
       "        return False\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Agent)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `Agent` has two methods.\n", "* `__init__(self, program=None)`: The constructor defines various attributes of the Agent. These include\n", "\n", " * `alive`: which keeps track of whether the agent is alive or not \n", " \n", " * `bump`: which tracks if the agent collides with an edge of the environment (for eg, a wall in a park)\n", " \n", " * `holding`: which is a list containing the `Things` an agent is holding, \n", " \n", " * `performance`: which evaluates the performance metrics of the agent \n", " \n", " * `program`: which is the agent program and maps an agent's percepts to actions in the environment. If no implementation is provided, it defaults to asking the user to provide actions for each percept.\n", " \n", "* `can_grab(self, thing)`: Is used when an environment contains things that an agent can grab and carry. By default, an agent can carry nothing.\n", "\n", "## ENVIRONMENT\n", "Now, let us see how environments are defined. Running the next cell will display an implementation of the abstract `Environment` class." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
class Environment:\n",
       "    """Abstract class representing an Environment. 'Real' Environment classes\n",
       "    inherit from this. Your Environment will typically need to implement:\n",
       "        percept:           Define the percept that an agent sees.\n",
       "        execute_action:    Define the effects of executing an action.\n",
       "                           Also update the agent.performance slot.\n",
       "    The environment keeps a list of .things and .agents (which is a subset\n",
       "    of .things). Each agent has a .performance slot, initialized to 0.\n",
       "    Each thing has a .location slot, even though some environments may not\n",
       "    need this."""\n",
       "\n",
       "    def __init__(self):\n",
       "        self.things = []\n",
       "        self.agents = []\n",
       "\n",
       "    def thing_classes(self):\n",
       "        return []  # List of classes that can go into environment\n",
       "\n",
       "    def percept(self, agent):\n",
       "        """Return the percept that the agent sees at this point. (Implement this.)"""\n",
       "        raise NotImplementedError\n",
       "\n",
       "    def execute_action(self, agent, action):\n",
       "        """Change the world to reflect this action. (Implement this.)"""\n",
       "        raise NotImplementedError\n",
       "\n",
       "    def default_location(self, thing):\n",
       "        """Default location to place a new thing with unspecified location."""\n",
       "        return None\n",
       "\n",
       "    def exogenous_change(self):\n",
       "        """If there is spontaneous change in the world, override this."""\n",
       "        pass\n",
       "\n",
       "    def is_done(self):\n",
       "        """By default, we're done when we can't find a live agent."""\n",
       "        return not any(agent.is_alive() for agent in self.agents)\n",
       "\n",
       "    def step(self):\n",
       "        """Run the environment for one time step. If the\n",
       "        actions and exogenous changes are independent, this method will\n",
       "        do. If there are interactions between them, you'll need to\n",
       "        override this method."""\n",
       "        if not self.is_done():\n",
       "            actions = []\n",
       "            for agent in self.agents:\n",
       "                if agent.alive:\n",
       "                    actions.append(agent.program(self.percept(agent)))\n",
       "                else:\n",
       "                    actions.append("")\n",
       "            for (agent, action) in zip(self.agents, actions):\n",
       "                self.execute_action(agent, action)\n",
       "            self.exogenous_change()\n",
       "\n",
       "    def run(self, steps=1000):\n",
       "        """Run the Environment for given number of time steps."""\n",
       "        for step in range(steps):\n",
       "            if self.is_done():\n",
       "                return\n",
       "            self.step()\n",
       "\n",
       "    def list_things_at(self, location, tclass=Thing):\n",
       "        """Return all things exactly at a given location."""\n",
       "        return [thing for thing in self.things\n",
       "                if thing.location == location and isinstance(thing, tclass)]\n",
       "\n",
       "    def some_things_at(self, location, tclass=Thing):\n",
       "        """Return true if at least one of the things at location\n",
       "        is an instance of class tclass (or a subclass)."""\n",
       "        return self.list_things_at(location, tclass) != []\n",
       "\n",
       "    def add_thing(self, thing, location=None):\n",
       "        """Add a thing to the environment, setting its location. For\n",
       "        convenience, if thing is an agent program we make a new agent\n",
       "        for it. (Shouldn't need to override this.)"""\n",
       "        if not isinstance(thing, Thing):\n",
       "            thing = Agent(thing)\n",
       "        if thing in self.things:\n",
       "            print("Can't add the same thing twice")\n",
       "        else:\n",
       "            thing.location = location if location is not None else self.default_location(thing)\n",
       "            self.things.append(thing)\n",
       "            if isinstance(thing, Agent):\n",
       "                thing.performance = 0\n",
       "                self.agents.append(thing)\n",
       "\n",
       "    def delete_thing(self, thing):\n",
       "        """Remove a thing from the environment."""\n",
       "        try:\n",
       "            self.things.remove(thing)\n",
       "        except ValueError as e:\n",
       "            print(e)\n",
       "            print("  in Environment delete_thing")\n",
       "            print("  Thing to be removed: {} at {}".format(thing, thing.location))\n",
       "            print("  from list: {}".format([(thing, thing.location) for thing in self.things]))\n",
       "        if thing in self.agents:\n",
       "            self.agents.remove(thing)\n",
       "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "psource(Environment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Environment` class has lot of methods! But most of them are incredibly simple, so let's see the ones we'll be using in this notebook.\n", "\n", "* `thing_classes(self)`: Returns a static array of `Thing` sub-classes that determine what things are allowed in the environment and what aren't\n", "\n", "* `add_thing(self, thing, location=None)`: Adds a thing to the environment at location\n", "\n", "* `run(self, steps)`: Runs an environment with the agent in it for a given number of steps.\n", "\n", "* `is_done(self)`: Returns true if the objective of the agent and the environment has been completed\n", "\n", "The next two functions must be implemented by each subclasses of `Environment` for the agent to recieve percepts and execute actions \n", "\n", "* `percept(self, agent)`: Given an agent, this method returns a list of percepts that the agent sees at the current time\n", "\n", "* `execute_action(self, agent, action)`: The environment reacts to an action performed by a given agent. The changes may result in agent experiencing new percepts or other elements reacting to agent input." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SIMPLE AGENT AND ENVIRONMENT\n", "\n", "Let's begin by using the `Agent` class to creating our first agent - a blind dog." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Can't find a valid program for BlindDog, falling back to default.\n" ] } ], "source": [ "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": 5, "metadata": {}, "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 - Park\n", "\n", "A park is an example of an environment because our dog can perceive and act upon it. The Environment class is an abstract class, so we will have to create our own subclass from it before we can use it." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "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", " '''return a list of things that are in our agent's location'''\n", " things = self.list_things_at(agent.location)\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", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\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 eat the first item\n", " print('{} ate {} at location: {}'\n", " .format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))\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", " print('{} drank {} at location: {}'\n", " .format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))\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 stop before itself - 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": [ "### PROGRAM - BlindDog\n", "Now that we have a Park Class, we re-implement our BlindDog to be able to move down and eat food or drink water only if it is present.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "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", " 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", " return True\n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now its time to implement a program module for our dog. A program controls how the dog acts upon its 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 down
" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def program(percepts):\n", " '''Returns an action based on the dog'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'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's now run our simulation by creating a park with some food, water, and our dog." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: 1\n", "BlindDog decided to move down at location: 2\n", "BlindDog decided to move down at location: 3\n", "BlindDog decided to move down at location: 4\n", "BlindDog ate Food at location: 5\n" ] } ], "source": [ "park = Park()\n", "dog = BlindDog(program)\n", "dogfood = Food()\n", "water = Water()\n", "park.add_thing(dog, 1)\n", "park.add_thing(dogfood, 5)\n", "park.add_thing(water, 7)\n", "\n", "park.run(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the dog moved from location 1 to 4, over 4 steps, and ate food at location 5 in the 5th step.\n", "\n", "Let's continue this simulation for 5 more steps." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: 5\n", "BlindDog decided to move down at location: 6\n", "BlindDog drank Water at location: 7\n" ] } ], "source": [ "park.run(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perfect! Note how the simulation stopped after the dog drank the water - exhausting all the food and water ends our simulation, as we had defined before. Let's add some more water and see if our dog can reach it." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: 7\n", "BlindDog decided to move down at location: 8\n", "BlindDog decided to move down at location: 9\n", "BlindDog decided to move down at location: 10\n", "BlindDog decided to move down at location: 11\n", "BlindDog decided to move down at location: 12\n", "BlindDog decided to move down at location: 13\n", "BlindDog decided to move down at location: 14\n", "BlindDog drank Water at location: 15\n" ] } ], "source": [ "park.add_thing(water, 15)\n", "park.run(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Above, we learnt to implement an agent, its program, and an environment on which it acts. However, this was a very simple case. Let's try to add complexity to it by creating a 2-Dimensional environment!\n", "\n", "\n", "## AGENTS IN A 2D ENVIRONMENT\n", "\n", "For us to not read so many logs of what our dog did, we add a bit of graphics while making our Park 2D. To do so, we will need to make it a subclass of GraphicEnvironment instead of Environment. Parks implemented by subclassing GraphicEnvironment class adds these extra properties to it:\n", "\n", " - Our park is indexed in the 4th quadrant of the X-Y plane.\n", " - Every time we create a park subclassing GraphicEnvironment, we need to define the colors of all the things we plan to put into the park. The colors are defined in typical [RGB digital 8-bit format](https://en.wikipedia.org/wiki/RGB_color_model#Numeric_representations), common across the web.\n", " - Fences are added automatically to all parks so that our dog does not go outside the park's boundary - it just isn't safe for blind dogs to be outside the park by themselves! GraphicEnvironment provides `is_inbounds` function to check if our dog tries to leave the park.\n", " \n", "First let us try to upgrade our 1-dimensional `Park` environment by just replacing its superclass by `GraphicEnvironment`. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "class Park2D(GraphicEnvironment):\n", " def percept(self, agent):\n", " '''return a list of things that are in our agent's location'''\n", " things = self.list_things_at(agent.location)\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", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\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 eat the first item\n", " print('{} ate {} at location: {}'\n", " .format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))\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", " print('{} drank {} at location: {}'\n", " .format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))\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 stop before itself - 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", "\n", "class BlindDog(Agent):\n", " location = [0,1] # change location to a 2d value\n", " direction = Direction(\"down\") # variable to store the direction our dog is facing\n", " \n", " def movedown(self):\n", " self.location[1] += 1\n", " \n", " def eat(self, thing):\n", " '''returns True upon success or False otherwise'''\n", " if isinstance(thing, Food):\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", " return True\n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's test this new park with our same dog, food and water. We color our dog with a nice red and mark food and water with orange and blue respectively." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BlindDog starts at (0,1) facing downwards, lets see if he can find any food!\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 2]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 3]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 4]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog ate Food at location: [0, 5]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 5]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 6]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog drank Water at location: [0, 7]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 7]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 8]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 9]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 10]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 11]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 12]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 13]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog decided to move down at location: [0, 14]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "BlindDog drank Water at location: [0, 15]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "park = Park2D(5,20, color={'BlindDog': (200,0,0), 'Water': (0, 200, 200), 'Food': (230, 115, 40)}) # park width is set to 5, and height to 20\n", "dog = BlindDog(program)\n", "dogfood = Food()\n", "water = Water()\n", "park.add_thing(dog, [0,1])\n", "park.add_thing(dogfood, [0,5])\n", "park.add_thing(water, [0,7])\n", "morewater = Water()\n", "park.add_thing(morewater, [0,15])\n", "print(\"BlindDog starts at (0,1) facing downwards, lets see if he can find any food!\")\n", "park.run(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding some graphics was a good idea! We immediately see that the code works, but our blind dog doesn't make any use of the 2 dimensional space available to him. Let's make our dog more energetic so that he turns and moves forward, instead of always moving down. In doing so, we'll also need to make some changes to our environment to be able to handle this extra motion.\n", "\n", "### PROGRAM - EnergeticBlindDog\n", "\n", "Let's make our dog turn or move forwards at random - except when he's at the edge of our park - in which case we make him change his direction explicitly by turning to avoid trying to leave the park. However, our dog is blind so he wouldn't know which way to turn - he'd just have to try arbitrarily.\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Percept: Feel Food Feel WaterFeel Nothing
Action: eatdrink\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Remember being at Edge : At EdgeNot at Edge
Action : Turn Left / Turn Right
( 50% - 50% chance )
Turn Left / Turn Right / Move Forward
( 25% - 25% - 50% chance )
\n", "
" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "from random import choice\n", "\n", "class EnergeticBlindDog(Agent):\n", " location = [0,1]\n", " direction = Direction(\"down\")\n", " \n", " def moveforward(self, success=True):\n", " '''moveforward possible only if success (i.e. valid destination location)'''\n", " if not success:\n", " return\n", " if self.direction.direction == Direction.R:\n", " self.location[0] += 1\n", " elif self.direction.direction == Direction.L:\n", " self.location[0] -= 1\n", " elif self.direction.direction == Direction.D:\n", " self.location[1] += 1\n", " elif self.direction.direction == Direction.U:\n", " self.location[1] -= 1\n", " \n", " def turn(self, d):\n", " self.direction = self.direction + d\n", " \n", " def eat(self, thing):\n", " '''returns True upon success or False otherwise'''\n", " if isinstance(thing, Food):\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", " return True\n", " return False\n", " \n", "def program(percepts):\n", " '''Returns an action based on it's percepts'''\n", " \n", " for p in percepts: # first eat or drink - you're a dog!\n", " if isinstance(p, Food):\n", " return 'eat'\n", " elif isinstance(p, Water):\n", " return 'drink'\n", " if isinstance(p,Bump): # then check if you are at an edge and have to turn\n", " turn = False\n", " choice = random.choice((1,2));\n", " else:\n", " choice = random.choice((1,2,3,4)) # 1-right, 2-left, others-forward\n", " if choice == 1:\n", " return 'turnright'\n", " elif choice == 2:\n", " return 'turnleft'\n", " else:\n", " return 'moveforward'\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ENVIRONMENT - Park2D\n", "\n", "We also need to modify our park accordingly, in order to be able to handle all the new actions our dog wishes to execute. Additionally, we'll need to prevent our dog from moving to locations beyond our park boundary - it just isn't safe for blind dogs to be outside the park by themselves." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "\n", "class Park2D(GraphicEnvironment):\n", " def percept(self, agent):\n", " '''return a list of things that are in our agent's location'''\n", " things = self.list_things_at(agent.location)\n", " loc = copy.deepcopy(agent.location) # find out the target location\n", " #Check if agent is about to bump into a wall\n", " if agent.direction.direction == Direction.R:\n", " loc[0] += 1\n", " elif agent.direction.direction == Direction.L:\n", " loc[0] -= 1\n", " elif agent.direction.direction == Direction.D:\n", " loc[1] += 1\n", " elif agent.direction.direction == Direction.U:\n", " loc[1] -= 1\n", " if not self.is_inbounds(loc):\n", " things.append(Bump())\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 == 'turnright':\n", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", " agent.turn(Direction.R)\n", " elif action == 'turnleft':\n", " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", " agent.turn(Direction.L)\n", " elif action == 'moveforward':\n", " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", " agent.moveforward()\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]):\n", " print('{} ate {} at location: {}'\n", " .format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))\n", " self.delete_thing(items[0])\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]):\n", " print('{} drank {} at location: {}'\n", " .format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))\n", " self.delete_thing(items[0])\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 stop before itself - 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": [ "Now that our park is ready for the 2D motion of our energetic dog, lets test it!" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dog started at [0,0], facing down. Let's see if he found any food or water!\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to move downwards at location: [0, 0]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog drank Water at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnleft at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "EnergeticBlindDog decided to turnright at location: [0, 1]\n" ] }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "park = Park2D(5,5, color={'EnergeticBlindDog': (200,0,0), 'Water': (0, 200, 200), 'Food': (230, 115, 40)})\n", "dog = EnergeticBlindDog(program)\n", "dogfood = Food()\n", "water = Water()\n", "park.add_thing(dog, [0,0])\n", "park.add_thing(dogfood, [1,2])\n", "park.add_thing(water, [0,1])\n", "morewater = Water()\n", "morefood = Food()\n", "park.add_thing(morewater, [2,4])\n", "park.add_thing(morefood, [4,3])\n", "print(\"dog started at [0,0], facing down. Let's see if he found any food or water!\")\n", "park.run(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Wumpus Environment" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "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": 1, "metadata": {}, "outputs": [], "source": [ "from hw1a import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hours\n", "# is it greater than 0?\n", " OK got: 2 expected: . at 0x7f007468c8c0>\n", "NewVacuumEnvironment - Walls\n", " OK got: '[[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]' expected: '[[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]'\n", "NewVacuumEnvironment - Dirt\n", " OK got: 159 expected: . at 0x7f007468cdd0>\n", "NewVacuumEnvironment - Very Clean\n", " OK got: 0 expected: 0\n", "NewVacuumEnvironment - Very Dirty\n", " OK got: 324 expected: 324\n", "TraceAgent and BetterReflexVacuumAgent\n", " perceives ('Dirty', 'None') and does Suck at (1, 1)\n", " OK got: (1, 1) expected: (1, 1)\n", " OK got: 10 expected: 10\n", " perceives ('Clean', 'None') and does Left at (1, 1)\n", " perceives ('Clean', 'None') and does Up at (1, 1)\n", " perceives ('Clean', 'None') and does Up at (1, 1)\n", " perceives ('Clean', 'None') and does Up at (1, 1)\n", " perceives ('Clean', 'None') and does Down at (1, 1)\n", " perceives ('Dirty', 'None') and does Suck at (1, 2)\n", " perceives ('Clean', 'None') and does Right at (1, 2)\n", " perceives ('Dirty', 'None') and does Suck at (2, 2)\n", " perceives ('Clean', 'None') and does Left at (2, 2)\n", " perceives ('Clean', 'None') and does Right at (1, 2)\n", " perceives ('Clean', 'None') and does Down at (2, 2)\n", " perceives ('Dirty', 'None') and does Suck at (2, 3)\n", " perceives ('Clean', 'None') and does Left at (2, 3)\n", " perceives ('Dirty', 'None') and does Suck at (1, 3)\n", " perceives ('Clean', 'None') and does Right at (1, 3)\n", " perceives ('Clean', 'None') and does Down at (2, 3)\n", " perceives ('Dirty', 'None') and does Suck at (2, 4)\n", " perceives ('Clean', 'None') and does Down at (2, 4)\n", " perceives ('Dirty', 'None') and does Suck at (2, 5)\n", " perceives ('Clean', 'None') and does Right at (2, 5)\n", " OK got: 56 expected: . at 0x7f006c38edd0>\n" ] } ], "source": [ "main()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }