From bff9d8a5115cd5c3289c4dfa2115f5266e0d4844 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sat, 11 Mar 2017 16:42:07 +0530 Subject: [PATCH 1/4] Improved BlindDog example --- agents.ipynb | 620 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 519 insertions(+), 101 deletions(-) diff --git a/agents.ipynb b/agents.ipynb index 8eba9f07e..1cc12a3c2 100644 --- a/agents.ipynb +++ b/agents.ipynb @@ -98,27 +98,31 @@ " 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", + " 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 pick eat the first item\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", + "\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", + " 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" @@ -126,95 +130,314 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "collapsed": true + }, "source": [ - "## Wumpus Environment" + "# PROGRAM - BlindDog #\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 down
\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": true + "collapsed": false }, "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", + "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", - " print(percepts)\n", - " return input()\n", - "\n", - "w = WumpusEnvironment(program, 7, 7) \n", - "grid = BlockGrid(w.width, w.height, fill=(123, 234, 123))\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": [ + "Lets now run our simulation by creating a park with some food, water, and our dog." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "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", - "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", + "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", - "def step():\n", - " global grid, w\n", - " draw_grid(w.get_world())\n", - " grid.show()\n", - " w.step()" + "Lets continue this simulation for 5 more steps." ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { - "data": { - "text/html": [ - "
" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, + "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. Lets add some more water and see if our dog can reach it." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[], [None], [], [], [None]]\n", - "2\n" + "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": [ - "step()" + "park.add_thing(water, 15)\n", + "park.run(10)" ] }, { "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is how to implement an agent, its program, and environment. However, this was a very simple case. Lets try a 2-Dimentional environment now with multiple agents.\n", + "\n", + "\n", + "# 2D Environment #\n", + "To make our Park 2D, we will need to make it a subclass of XYEnvironment instead of Environment. Please note that our park is indexed in the 4th quadrant of the X-Y plane.\n", + "\n", + "We will also eventually add a person to pet the dog." + ] + }, + { + "cell_type": "code", + "execution_count": 9, "metadata": { "collapsed": true }, + "outputs": [], "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", + "class Park2D(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", + " 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\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'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now lets test this new park with our same dog, food and water" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "BlindDog decided to move down at location: [0, 1]\n", + "BlindDog decided to move down at location: [0, 2]\n", + "BlindDog decided to move down at location: [0, 3]\n", + "BlindDog decided to move down at location: [0, 4]\n", + "BlindDog ate Food at location: [0, 5]\n", + "BlindDog decided to move down at location: [0, 5]\n", + "BlindDog decided to move down at location: [0, 6]\n", + "BlindDog drank Water at location: [0, 7]\n", + "BlindDog decided to move down at location: [0, 7]\n", + "BlindDog decided to move down at location: [0, 8]\n", + "BlindDog decided to move down at location: [0, 9]\n", + "BlindDog decided to move down at location: [0, 10]\n", + "BlindDog decided to move down at location: [0, 11]\n", + "BlindDog decided to move down at location: [0, 12]\n", + "BlindDog decided to move down at location: [0, 13]\n", + "BlindDog decided to move down at location: [0, 14]\n", + "BlindDog drank Water at location: [0, 15]\n" + ] + } + ], + "source": [ + "park = Park2D(5,20) # 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", + "park.run(20)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This 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. We'll also need to make appropriate changes to our environment to be able to handle this extra motion.\n", + "\n", + "# PROGRAM - EnergeticBlindDog #\n", + "\n", + "Lets 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. Our dog is blind, however, so he wouldn't know which way to turn - he'd just have to try arbitrarily.\n", + "\n", "\n", " \n", " \n", @@ -226,117 +449,312 @@ " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", - "
Percept: Action: eatdrinkmove up\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", + "
\n" + "" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "class BlindDog(Agent):\n", - " location = 1\n", + "from random import choice\n", + "\n", + "turn = False# global variable to remember to turn if our dog hits the boundary\n", + "class EnergeticBlindDog(Agent):\n", + " location = [0,1]\n", + " direction = Direction(\"down\")\n", " \n", - " def movedown(self):\n", - " self.location += 1\n", + " def moveforward(self, success=True):\n", + " '''moveforward possible only if success (ie valid destination location)'''\n", + " global turn\n", + " if not success:\n", + " turn = True # if edge has been reached, remember to turn\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", - " print(\"Dog: Ate food at {}.\".format(self.location))\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", + " #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", + " global turn\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", - " 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)" + " if turn: # then recall if you were at an edge and had 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": [ - "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." + "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": null, + "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ - "class Park(XYEnvironment):\n", + "class Park2D(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", + " if action == 'turnright':\n", + " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", + " agent.turn(Direction.R)\n", + " #print('now facing {}'.format(agent.direction.direction))\n", + " elif action == 'turnleft':\n", + " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", + " agent.turn(Direction.L)\n", + " #print('now facing {}'.format(agent.direction.direction))\n", + " elif action == 'moveforward':\n", + " loc = copy.deepcopy(agent.location) # find out the target location\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", + " #print('{} at {} facing {}'.format(agent, loc, agent.direction.direction))\n", + " if self.is_inbounds(loc):# move only if the target is a valid location\n", + " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", + " agent.moveforward()\n", + " else:\n", + " print('{} decided to move {}wards at location: {}, but couldnt'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", + " agent.moveforward(False)\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", + " 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]): #Have the dog drink the first item\n", - " self.delete_thing(items[0]) #Delete it from the Park after.\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 or it with when there is no more food or water'''\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" + " return dead_agents or no_edibles\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dog started at [0,0], facing down. Lets see if he found any food or water!\n", + "EnergeticBlindDog decided to turnright at location: [0, 0]\n", + "EnergeticBlindDog decided to move leftwards at location: [0, 0], but couldnt\n", + "EnergeticBlindDog decided to turnleft at location: [0, 0]\n", + "EnergeticBlindDog decided to move downwards at location: [0, 0]\n", + "EnergeticBlindDog decided to move downwards at location: [0, 1]\n", + "EnergeticBlindDog drank Water at location: [0, 2]\n", + "EnergeticBlindDog decided to move downwards at location: [0, 2], but couldnt\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to move leftwards at location: [0, 2], but couldnt\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to turnright at location: [0, 2]\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to move downwards at location: [0, 2], but couldnt\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to move rightwards at location: [0, 2]\n", + "EnergeticBlindDog ate Food at location: [1, 2]\n", + "EnergeticBlindDog decided to turnleft at location: [1, 2]\n", + "EnergeticBlindDog decided to move upwards at location: [1, 2]\n" + ] + } + ], + "source": [ + "park = Park2D(3,3)\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, [2,1])\n", + "morewater = Water()\n", + "park.add_thing(morewater, [0,2])\n", + "print('dog started at [0,0], facing down. Lets 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": 61, + "metadata": { + "collapsed": true + }, + "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.run(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Death by Pit [-1000].\n" + ] + } + ], + "source": [ + "step()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { @@ -355,7 +773,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.4.3" } }, "nbformat": 4, From 2802f695aab1b33c200f321bd311a9c3da638cea Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sat, 11 Mar 2017 16:53:49 +0530 Subject: [PATCH 2/4] Added 2D GUI IPython capability --- agents.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/agents.py b/agents.py index a5bf376ca..6f6c6ca72 100644 --- a/agents.py +++ b/agents.py @@ -516,6 +516,106 @@ class Obstacle(Thing): class Wall(Obstacle): pass +# ______________________________________________________________________________ + +from ipythonblocks import BlockGrid +from IPython.display import HTML, display +from time import sleep + +class GraphicEnvironment(XYEnvironment): + def __init__(self, width=10, height=10, boundary=True, color={}, display=False): + """define all the usual XYEnvironment characteristics, + but initialise a BlockGrid for GUI too""" + super().__init__(width, height) + self.grid = BlockGrid(width, height, fill=(200,200,200)) + if display: + self.grid.show() + self.visible = True + else: + self.visible = False + self.bounded = boundary + self.colors = color + + #def list_things_at(self, location, tclass=Thing): # need to override because locations + # """Return all things exactly at a given location.""" + # return [thing for thing in self.things + # if thing.location == location and isinstance(thing, tclass)] + + def get_world(self): + """Returns all the items in the world in a format + understandable by the ipythonblocks BlockGrid""" + result = [] + x_start, y_start = (0, 0) + x_end, y_end = self.width, self.height + for x in range(x_start, x_end): + row = [] + for y in range(y_start, y_end): + row.append(self.list_things_at([x, y])) + result.append(row) + return result + + """def run(self, steps=1000, delay=1): + "" "Run the Environment for given number of time steps, + but update the GUI too." "" + for step in range(steps): + sleep(delay) + if self.visible: + self.reveal() + if self.is_done(): + if self.visible: + self.reveal() + return + self.step() + if self.visible: + self.reveal() + """ + def run(self, steps=1000, delay=1): + """Run the Environment for given number of time steps, + but update the GUI too.""" + for step in range(steps): + self.update(delay) + if self.is_done(): + break + self.step() + self.update(delay) + + def update(self, delay=1): + sleep(delay) + if self.visible: + self.conceal() + self.reveal() + else: + self.reveal() + + def reveal(self): + """display the BlockGrid for this world - the last thing to be added + at a location defines the location color""" + #print("Grid={}".format(self.grid)) + self.draw_world() + #if not self.visible == True: + # self.grid.show() + self.grid.show() + self.visible == True + + def draw_world(self): + self.grid[:] = (200, 200, 200) + world = self.get_world() + #print("world {}".format(world)) + for x in range(0, len(world)): + for y in range(0, len(world[x])): + if len(world[x][y]): + self.grid[y, x] = self.colors[world[x][y][-1].__class__.__name__] + #print('location: ({}, {}) got color: {}' + #.format(y, x, self.colors[world[x][y][-1].__class__.__name__])) + + def conceal(self): + """hide the BlockGrid for this world""" + self.visible = False + display(HTML('')) + + + + # ______________________________________________________________________________ From 3e382c7f7740b280b1c6b0cf4e14ea0abfed0592 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sat, 11 Mar 2017 16:54:53 +0530 Subject: [PATCH 3/4] Demonstrated 2D Environment with GUI --- agents.ipynb | 547 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 525 insertions(+), 22 deletions(-) diff --git a/agents.ipynb b/agents.ipynb index 1cc12a3c2..968c8cdc9 100644 --- a/agents.ipynb +++ b/agents.ipynb @@ -200,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -240,7 +240,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -268,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -309,7 +309,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": { "collapsed": true }, @@ -386,7 +386,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -470,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -546,7 +546,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "metadata": { "collapsed": true }, @@ -610,7 +610,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -620,26 +620,26 @@ "output_type": "stream", "text": [ "dog started at [0,0], facing down. Lets see if he found any food or water!\n", - "EnergeticBlindDog decided to turnright at location: [0, 0]\n", - "EnergeticBlindDog decided to move leftwards at location: [0, 0], but couldnt\n", - "EnergeticBlindDog decided to turnleft at location: [0, 0]\n", "EnergeticBlindDog decided to move downwards at location: [0, 0]\n", "EnergeticBlindDog decided to move downwards at location: [0, 1]\n", "EnergeticBlindDog drank Water at location: [0, 2]\n", - "EnergeticBlindDog decided to move downwards at location: [0, 2], but couldnt\n", - "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", + "EnergeticBlindDog decided to turnright at location: [0, 2]\n", + "EnergeticBlindDog decided to move leftwards at location: [0, 2], but couldnt\n", + "EnergeticBlindDog decided to turnright at location: [0, 2]\n", + "EnergeticBlindDog decided to turnright at location: [0, 2]\n", "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", "EnergeticBlindDog decided to move leftwards at location: [0, 2], but couldnt\n", "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", "EnergeticBlindDog decided to turnright at location: [0, 2]\n", + "EnergeticBlindDog decided to move leftwards at location: [0, 2], but couldnt\n", "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", "EnergeticBlindDog decided to move downwards at location: [0, 2], but couldnt\n", + "EnergeticBlindDog decided to turnright at location: [0, 2]\n", + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", "EnergeticBlindDog decided to turnleft at location: [0, 2]\n", "EnergeticBlindDog decided to move rightwards at location: [0, 2]\n", - "EnergeticBlindDog ate Food at location: [1, 2]\n", - "EnergeticBlindDog decided to turnleft at location: [1, 2]\n", - "EnergeticBlindDog decided to move upwards at location: [1, 2]\n" + "EnergeticBlindDog ate Food at location: [1, 2]\n" ] } ], @@ -657,6 +657,508 @@ "park.run(20)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is good, but it still lacks graphics. What if we wanted to visualize our park as it changed? To do that, all we have to do is make our park a subclass of GraphicEnvironment instead of XYEnvironment. Lets see how this looks." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class GraphicPark(GraphicEnvironment):\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", + " 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", + " #print('now facing {}'.format(agent.direction.direction))\n", + " elif action == 'turnleft':\n", + " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", + " agent.turn(Direction.L)\n", + " #print('now facing {}'.format(agent.direction.direction))\n", + " elif action == 'moveforward':\n", + " loc = copy.deepcopy(agent.location) # find out the target location\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", + " #print('{} at {} facing {}'.format(agent, loc, agent.direction.direction))\n", + " if self.is_inbounds(loc):# move only if the target is a valid location\n", + " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", + " agent.moveforward()\n", + " else:\n", + " print('{} decided to move {}wards at location: {}, but couldnt'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", + " agent.moveforward(False)\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": [ + "That is the only change we make. The rest of our code stays the same. There is a slight difference in usage though. Every time we create a GraphicPark, 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." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dog started at [0,0], facing down. Lets 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 move downwards at location: [0, 0]\n" + ] + }, + { + "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" + }, + { + "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" + }, + { + "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" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move downwards at location: [0, 1]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnleft at location: [0, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move rightwards at location: [0, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog ate Food at location: [1, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnleft at location: [1, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnleft at location: [1, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnleft at location: [1, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move downwards at location: [1, 2]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnright at location: [1, 3]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move leftwards at location: [1, 3]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move leftwards at location: [0, 3], but couldnt\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnleft at location: [0, 3]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnright at location: [0, 3]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move leftwards at location: [0, 3], but couldnt\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to turnright at location: [0, 3]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "EnergeticBlindDog decided to move upwards at location: [0, 3]\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "park = GraphicPark(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. Lets see if he found any food or water!')\n", + "park.run(20)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -674,9 +1176,9 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 4, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ @@ -713,12 +1215,12 @@ " global grid, w\n", " draw_grid(w.get_world())\n", " grid.show()\n", - " w.run(20)" + " w.step()" ] }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -726,7 +1228,7 @@ { "data": { "text/html": [ - "
" + "
" ], "text/plain": [ "" @@ -739,7 +1241,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Death by Pit [-1000].\n" + "[[], [], [], [], [, None]]\n", + "Forward\n" ] } ], From dcfbd27e66e7b7233b2113457001b78b80241921 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sat, 11 Mar 2017 17:32:25 +0530 Subject: [PATCH 4/4] allowing import without ipythonblocks installed --- agents.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/agents.py b/agents.py index 6f6c6ca72..df06ea3aa 100644 --- a/agents.py +++ b/agents.py @@ -518,9 +518,12 @@ class Wall(Obstacle): # ______________________________________________________________________________ -from ipythonblocks import BlockGrid -from IPython.display import HTML, display -from time import sleep +try: + from ipythonblocks import BlockGrid + from IPython.display import HTML, display + from time import sleep +except: + pass class GraphicEnvironment(XYEnvironment): def __init__(self, width=10, height=10, boundary=True, color={}, display=False):