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
35 changes: 23 additions & 12 deletions aima/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ 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
locations = [loc_A, loc_B, loc_C, loc_D]

def RandomVacuumAgent():
"""Randomly choose one of the actions from the vacuum environment.
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 @@ -261,20 +261,25 @@ def ModelBasedVacuumAgent():
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
model = {loc_A: None, loc_B: None}
model = {loc_A: None, loc_B: None, loc_C: None, loc_D: None}

def program(percept):
"""Same as ReflexVacuumAgent, except if everything is clean, do NoOp."""
location, status = percept
model[location] = status # Update the model here
if model[loc_A] == model[loc_B] == 'Clean':
# location, status = percept
# model[location] = status # Update the model here
state = update_state(state, action, percept, model)
if model[loc_A] == model[loc_B] == model[loc_C] == model[loc_D] == 'Clean':
return 'NoOp'
elif status == 'Dirty':
return 'Suck'
elif location == loc_A:
return 'Right'
elif location == loc_B:
return 'Left'
elif location == loc_C:
return 'Down'
elif location == loc_D:
return 'Up'

return Agent(program)

Expand Down Expand Up @@ -806,8 +811,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."""
Expand All @@ -820,11 +824,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 +844,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
2 changes: 1 addition & 1 deletion aima/notebook_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading