Skip to content
Open
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
49 changes: 27 additions & 22 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Thing:
.__name__ slot (used for output only)."""

def __repr__(self):
return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__))
return f"<{getattr(self, '__name__', self.__class__.__name__)}>"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Thing.__repr__ refactored with the following changes:


def is_alive(self):
"""Things that are 'alive' should return true."""
Expand Down Expand Up @@ -338,7 +338,7 @@ def step(self):

def run(self, steps=1000):
"""Run the Environment for given number of time steps."""
for step in range(steps):
for _ in range(steps):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Environment.run refactored with the following changes:

if self.is_done():
return
self.step()
Expand Down Expand Up @@ -378,8 +378,8 @@ def delete_thing(self, thing):
except ValueError as e:
print(e)
print(" in Environment delete_thing")
print(" Thing to be removed: {} at {}".format(thing, thing.location))
print(" from list: {}".format([(thing, thing.location) for thing in self.things]))
print(f" Thing to be removed: {thing} at {thing.location}")
print(f" from list: {[(thing, thing.location) for thing in self.things]}")
Comment on lines -381 to +382
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Environment.delete_thing refactored with the following changes:

if thing in self.agents:
self.agents.remove(thing)

Expand Down Expand Up @@ -506,8 +506,11 @@ def execute_action(self, agent, action):
elif action == 'Forward':
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
elif action == 'Grab':
things = [thing for thing in self.list_things_at(agent.location) if agent.can_grab(thing)]
if things:
if things := [
thing
for thing in self.list_things_at(agent.location)
if agent.can_grab(thing)
]:
Comment on lines -509 to +513
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function XYEnvironment.execute_action refactored with the following changes:

agent.holding.append(things[0])
print("Grabbing ", things[0].__class__.__name__)
self.delete_thing(things[0])
Expand Down Expand Up @@ -552,7 +555,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False):
def is_inbounds(self, location):
"""Checks to make sure that the location is inbounds (within walls if we have walls)"""
x, y = location
return not (x < self.x_start or x > self.x_end or y < self.y_start or y > self.y_end)
return (
x >= self.x_start
and x <= self.x_end
and y >= self.y_start
and y <= self.y_end
)
Comment on lines -555 to +563
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function XYEnvironment.is_inbounds refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)


def random_location_inbounds(self, exclude=None):
"""Returns a random location that is inbounds (within walls if we have walls)"""
Expand Down Expand Up @@ -634,9 +642,7 @@ def get_world(self):
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)))
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
Comment on lines -637 to +645
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphicEnvironment.get_world refactored with the following changes:

result.append(row)
return result

Expand All @@ -660,7 +666,7 @@ def run(self, steps=1000, delay=1):
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):
for _ in range(steps):
Comment on lines -663 to +669
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphicEnvironment.run refactored with the following changes:

self.update(delay)
if self.is_done():
break
Expand All @@ -685,8 +691,8 @@ def reveal(self):
def draw_world(self):
self.grid[:] = (200, 200, 200)
world = self.get_world()
for x in range(0, len(world)):
for y in range(0, len(world[x])):
for x in range(len(world)):
for y in range(len(world[x])):
Comment on lines -688 to +695
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphicEnvironment.draw_world refactored with the following changes:

if len(world[x][y]):
self.grid[y, x] = self.colors[world[x][y][-1].__class__.__name__]

Expand Down Expand Up @@ -908,9 +914,7 @@ def get_world(self, show_walls=True):
x_end, y_end = self.width - 1, self.height - 1

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)))
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
Comment on lines -911 to +917
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.get_world refactored with the following changes:

result.append(row)
return result

Expand Down Expand Up @@ -938,8 +942,7 @@ def percept(self, agent):
"""Return things in adjacent (not diagonal) cells of the agent.
Result format: [Left, Right, Up, Down, Center / Current location]"""
x, y = agent.location
result = []
result.append(self.percepts_from(agent, (x - 1, y)))
result = [self.percepts_from(agent, (x - 1, y))]
Comment on lines -941 to +945
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.percept refactored with the following changes:

result.append(self.percepts_from(agent, (x + 1, y)))
result.append(self.percepts_from(agent, (x, y - 1)))
result.append(self.percepts_from(agent, (x, y + 1)))
Expand Down Expand Up @@ -999,10 +1002,12 @@ def is_done(self):
if explorer[0].alive:
return False
else:
print("Death by {} [-1000].".format(explorer[0].killed_by))
print(f"Death by {explorer[0].killed_by} [-1000].")
else:
print("Explorer climbed out {}."
.format("with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
print(
f'Explorer climbed out {"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"}.'
)

Comment on lines -1002 to +1010
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.is_done refactored with the following changes:

return True

# TODO: Arrow needs to be implemented
Expand All @@ -1024,7 +1029,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
>>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent
True
"""
envs = [EnvFactory() for i in range(n)]
envs = [EnvFactory() for _ in range(n)]
Comment on lines -1027 to +1032
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function compare_agents refactored with the following changes:

return [(A, test_agent(A, steps, copy.deepcopy(envs)))
for A in AgentFactories]

Expand Down
46 changes: 24 additions & 22 deletions agents4e.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Thing:
.__name__ slot (used for output only)."""

def __repr__(self):
return '<{}>'.format(getattr(self, '__name__', self.__class__.__name__))
return f"<{getattr(self, '__name__', self.__class__.__name__)}>"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Thing.__repr__ refactored with the following changes:


def is_alive(self):
"""Things that are 'alive' should return true."""
Expand Down Expand Up @@ -343,7 +343,7 @@ def step(self):

def run(self, steps=1000):
"""Run the Environment for given number of time steps."""
for step in range(steps):
for _ in range(steps):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Environment.run refactored with the following changes:

if self.is_done():
return
self.step()
Expand Down Expand Up @@ -383,8 +383,8 @@ def delete_thing(self, thing):
except ValueError as e:
print(e)
print(" in Environment delete_thing")
print(" Thing to be removed: {} at {}".format(thing, thing.location))
print(" from list: {}".format([(thing, thing.location) for thing in self.things]))
print(f" Thing to be removed: {thing} at {thing.location}")
print(f" from list: {[(thing, thing.location) for thing in self.things]}")
Comment on lines -386 to +387
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Environment.delete_thing refactored with the following changes:

if thing in self.agents:
self.agents.remove(thing)

Expand Down Expand Up @@ -554,7 +554,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False):
def is_inbounds(self, location):
"""Checks to make sure that the location is inbounds (within walls if we have walls)"""
x, y = location
return not (x < self.x_start or x > self.x_end or y < self.y_start or y > self.y_end)
return (
x >= self.x_start
and x <= self.x_end
and y >= self.y_start
and y <= self.y_end
)
Comment on lines -557 to +562
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function XYEnvironment.is_inbounds refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)


def random_location_inbounds(self, exclude=None):
"""Returns a random location that is inbounds (within walls if we have walls)"""
Expand Down Expand Up @@ -639,9 +644,7 @@ def get_world(self):
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)))
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
Comment on lines -642 to +647
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphicEnvironment.get_world refactored with the following changes:

result.append(row)
return result

Expand All @@ -665,7 +668,7 @@ def run(self, steps=1000, delay=1):
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):
for _ in range(steps):
Comment on lines -668 to +671
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphicEnvironment.run refactored with the following changes:

self.update(delay)
if self.is_done():
break
Expand All @@ -690,8 +693,8 @@ def reveal(self):
def draw_world(self):
self.grid[:] = (200, 200, 200)
world = self.get_world()
for x in range(0, len(world)):
for y in range(0, len(world[x])):
for x in range(len(world)):
for y in range(len(world[x])):
Comment on lines -693 to +697
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphicEnvironment.draw_world refactored with the following changes:

if len(world[x][y]):
self.grid[y, x] = self.colors[world[x][y][-1].__class__.__name__]

Expand Down Expand Up @@ -913,9 +916,7 @@ def get_world(self, show_walls=True):
x_end, y_end = self.width - 1, self.height - 1

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)))
row = [self.list_things_at((x, y)) for y in range(y_start, y_end)]
Comment on lines -916 to +919
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.get_world refactored with the following changes:

result.append(row)
return result

Expand Down Expand Up @@ -943,8 +944,7 @@ def percept(self, agent):
"""Return things in adjacent (not diagonal) cells of the agent.
Result format: [Left, Right, Up, Down, Center / Current location]"""
x, y = agent.location
result = []
result.append(self.percepts_from(agent, (x - 1, y)))
result = [self.percepts_from(agent, (x - 1, y))]
Comment on lines -946 to +947
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.percept refactored with the following changes:

result.append(self.percepts_from(agent, (x + 1, y)))
result.append(self.percepts_from(agent, (x, y - 1)))
result.append(self.percepts_from(agent, (x, y + 1)))
Expand Down Expand Up @@ -980,8 +980,8 @@ def execute_action(self, agent, action):
if agent.can_grab(thing)]
if len(things):
print("Grabbing", things[0].__class__.__name__)
if len(things):
agent.holding.append(things[0])
if len(things):
agent.holding.append(things[0])
Comment on lines -983 to +984
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.execute_action refactored with the following changes:

agent.performance -= 1
elif action == 'Climb':
if agent.location == (1, 1): # Agent can only climb out of (1,1)
Expand Down Expand Up @@ -1018,10 +1018,12 @@ def is_done(self):
if explorer[0].alive:
return False
else:
print("Death by {} [-1000].".format(explorer[0].killed_by))
print(f"Death by {explorer[0].killed_by} [-1000].")
else:
print("Explorer climbed out {}."
.format("with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"))
print(
f'Explorer climbed out {"with Gold [+1000]!" if Gold() not in self.things else "without Gold [+0]"}.'
)

Comment on lines -1021 to +1026
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WumpusEnvironment.is_done refactored with the following changes:

return True

# TODO: Arrow needs to be implemented
Expand All @@ -1043,7 +1045,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
>>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent
True
"""
envs = [EnvFactory() for i in range(n)]
envs = [EnvFactory() for _ in range(n)]
Comment on lines -1046 to +1048
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function compare_agents refactored with the following changes:

return [(A, test_agent(A, steps, copy.deepcopy(envs)))
for A in AgentFactories]

Expand Down
Loading