Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions aima/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def rule_match(state, rules):
# ______________________________________________________________________________


loc_A, loc_B = (0, 0), (1, 0) # The two locations for the Vacuum world
loc_A, loc_B, loc_C, loc_D = (0, 0), (1, 0), (0, 1), (1, 1) # The four locations for the Vacuum world


def RandomVacuumAgent():
Expand All @@ -203,7 +203,7 @@ def RandomVacuumAgent():
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
return Agent(RandomAgentProgram(['Right', 'Left', 'Suck', 'NoOp']))
return Agent(RandomAgentProgram(['Right', 'Left','Up','Down', 'Suck', 'NoOp']))


def TableDrivenVacuumAgent():
Expand Down Expand Up @@ -807,7 +807,9 @@ class TrivialVacuumEnvironment(Environment):
def __init__(self):
super().__init__()
self.status = {loc_A: random.choice(['Clean', 'Dirty']),
loc_B: random.choice(['Clean', 'Dirty'])}
loc_B: random.choice(['Clean', 'Dirty']),
loc_C: random.choice(['Clean', 'Dirty']),
loc_D: random.choice(['Clean', 'Dirty'])}

def thing_classes(self):
"""Return the Thing/Agent classes that may populate this vacuum world."""
Expand All @@ -820,11 +822,18 @@ def percept(self, agent):
def execute_action(self, agent, action):
"""Change agent's location and/or location's status; track performance.
Score 10 for each dirt cleaned; -1 for each move."""
a, b = agent.location
if action == 'Right':
agent.location = loc_B
agent.location = (a + 1, b)
agent.performance -= 1
elif action == 'Left':
agent.location = loc_A
agent.location = (a - 1, b)
agent.performance -= 1
elif action == 'Up':
agent.location = (a, b + 1)
agent.performance -= 1
elif action == 'Down':
agent.location = (a, b - 1)
agent.performance -= 1
elif action == 'Suck':
if self.status[agent.location] == 'Dirty':
Expand All @@ -833,7 +842,7 @@ def execute_action(self, agent, action):

def default_location(self, thing):
"""Agents start in either location at random."""
return random.choice([loc_A, loc_B])
return random.choice([loc_A, loc_B, loc_C, loc_D])


# ______________________________________________________________________________
Expand Down
10 changes: 9 additions & 1 deletion aima/notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ def psource(*functions):
from pygments.lexers import PythonLexer
from pygments import highlight

display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True))))
# Render an HTML fragment with inline token colors. ``full=True`` emits
# a complete document whose global ``body`` CSS leaks into VS Code's
# shared notebook webview and can make every Markdown cell unreadable.
highlighted = highlight(source_code, PythonLexer(),
HtmlFormatter(noclasses=True, style='dracula',
nobackground=True))
# Give unstyled tokens a readable foreground instead of inheriting
# palette instead of inheriting a color from the notebook theme.
display(HTML('<div style="color: #f8f8f2">{}</div>'.format(highlighted)))

except ImportError:
print(source_code)
Expand Down
Loading