diff --git a/aima/agents.py b/aima/agents.py index d159466cc..28af8d5f6 100644 --- a/aima/agents.py +++ b/aima/agents.py @@ -806,8 +806,7 @@ class TrivialVacuumEnvironment(Environment): def __init__(self): super().__init__() - self.status = {loc_A: random.choice(['Clean', 'Dirty']), - loc_B: random.choice(['Clean', 'Dirty'])} + self.status = {loc: random.choice(['Clean', 'Dirty']) for loc in locations} def thing_classes(self): """Return the Thing/Agent classes that may populate this vacuum world.""" diff --git a/aima/notebook_utils.py b/aima/notebook_utils.py index 7b881d29c..6da04b8a0 100644 --- a/aima/notebook_utils.py +++ b/aima/notebook_utils.py @@ -50,7 +50,7 @@ def psource(*functions): from pygments.lexers import PythonLexer from pygments import highlight - display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True)))) + display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(noclasses=True, style='monokai')))) except ImportError: print(source_code) diff --git a/notebooks/vacuum_world.ipynb b/notebooks/vacuum_world.ipynb index 8fa52dffc..fa3b2af2b 100644 --- a/notebooks/vacuum_world.ipynb +++ b/notebooks/vacuum_world.ipynb @@ -2,9 +2,18 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/iain/.venvs/AIMA/lib/python3.13/site-packages/nbformat/validator.py:434: MissingIDFieldWarning: Cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.\n", + " _validate(nbdict, ref, version, version_minor, relax_add_props)\n" + ] + } + ], "source": [ "%run bootstrap.ipynb" ] @@ -82,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -99,138 +108,49 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "\n", - "\n", - "\n", - "
\n", - "class TrivialVacuumEnvironment(Environment):\n",
+ " \"\"\"This environment has two locations, A and B. Each can be Dirty\n",
+ " or Clean. The agent perceives its location and the location's\n",
+ " status. This serves as an example of how to implement a simple\n",
+ " Environment.\"\"\"\n",
"\n",
- "class TrivialVacuumEnvironment(Environment):\n",
+ " def __init__(self):\n",
+ " super().__init__()\n",
+ " self.status = {loc_A: random.choice(['Clean', 'Dirty']),\n",
+ " loc_B: random.choice(['Clean', 'Dirty'])}\n",
"\n",
- " """This environment has two locations, A and B. Each can be Dirty\n",
- " or Clean. The agent perceives its location and the location's\n",
- " status. This serves as an example of how to implement a simple\n",
- " Environment."""\n",
+ " def thing_classes(self):\n",
+ " \"\"\"Return the Thing/Agent classes that may populate this vacuum world.\"\"\"\n",
+ " return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent, TableDrivenVacuumAgent, ModelBasedVacuumAgent]\n",
"\n",
- " def __init__(self):\n",
- " super().__init__()\n",
- " self.status = {loc_A: random.choice(['Clean', 'Dirty']),\n",
- " loc_B: random.choice(['Clean', 'Dirty'])}\n",
+ " def percept(self, agent):\n",
+ " \"\"\"Returns the agent's location, and the location status (Dirty/Clean).\"\"\"\n",
+ " return agent.location, self.status[agent.location]\n",
"\n",
- " def thing_classes(self):\n",
- " return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,\n",
- " TableDrivenVacuumAgent, ModelBasedVacuumAgent]\n",
+ " def execute_action(self, agent, action):\n",
+ " \"\"\"Change agent's location and/or location's status; track performance.\n",
+ " Score 10 for each dirt cleaned; -1 for each move.\"\"\"\n",
+ " if action == 'Right':\n",
+ " agent.location = loc_B\n",
+ " agent.performance -= 1\n",
+ " elif action == 'Left':\n",
+ " agent.location = loc_A\n",
+ " agent.performance -= 1\n",
+ " elif action == 'Suck':\n",
+ " if self.status[agent.location] == 'Dirty':\n",
+ " agent.performance += 10\n",
+ " self.status[agent.location] = 'Clean'\n",
"\n",
- " def percept(self, agent):\n",
- " """Returns the agent's location, and the location status (Dirty/Clean)."""\n",
- " return (agent.location, self.status[agent.location])\n",
- "\n",
- " def execute_action(self, agent, action):\n",
- " """Change agent's location and/or location's status; track performance.\n",
- " Score 10 for each dirt cleaned; -1 for each move."""\n",
- " if action == 'Right':\n",
- " agent.location = loc_B\n",
- " agent.performance -= 1\n",
- " elif action == 'Left':\n",
- " agent.location = loc_A\n",
- " agent.performance -= 1\n",
- " elif action == 'Suck':\n",
- " if self.status[agent.location] == 'Dirty':\n",
- " agent.performance += 10\n",
- " self.status[agent.location] = 'Clean'\n",
- "\n",
- " def default_location(self, thing):\n",
- " """Agents start in either location at random."""\n",
- " return random.choice([loc_A, loc_B])\n",
- "
\n",
- "\n",
- "\n"
+ " def default_location(self, thing):\n",
+ " \"\"\"Agents start in either location at random.\"\"\"\n",
+ " return random.choice([loc_A, loc_B])\n",
+ "