Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for TableDrivenVacuumAgent and FIX: grid not updating in GraphicEnvironment #1133

Merged
merged 6 commits into from
Dec 10, 2019
Merged
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
19 changes: 16 additions & 3 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import random
import copy
import collections
import numbers


# ______________________________________________________________________________
Expand Down Expand Up @@ -211,7 +212,14 @@ def RandomVacuumAgent():


def TableDrivenVacuumAgent():
"""[Figure 2.3]"""
"""Tabular approach towards vacuum world as mentioned in [Figure 2.3]
>>> agent = TableDrivenVacuumAgent()
>>> environment = TrivialVacuumEnvironment()
>>> environment.add_thing(agent)
>>> environment.run()
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
table = {((loc_A, 'Clean'),): 'Right',
((loc_A, 'Dirty'),): 'Suck',
((loc_B, 'Clean'),): 'Left',
Expand Down Expand Up @@ -342,7 +350,12 @@ def run(self, steps=1000):

def list_things_at(self, location, tclass=Thing):
"""Return all things exactly at a given location."""
return [thing for thing in self.things if thing.location == location and isinstance(thing, tclass)]
if isinstance(location, numbers.Number):
return [thing for thing in self.things
if thing.location == location and isinstance(thing, tclass)]
return [thing for thing in self.things
if all(x==y for x,y in zip(thing.location, location))
and isinstance(thing, tclass)]

def some_things_at(self, location, tclass=Thing):
"""Return true if at least one of the things at location
Expand Down Expand Up @@ -621,7 +634,7 @@ def get_world(self):
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.append(self.list_things_at((x, y)))
result.append(row)
return result

Expand Down
19 changes: 16 additions & 3 deletions agents4e.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import random
import copy
import collections
import numbers


# ______________________________________________________________________________
Expand Down Expand Up @@ -211,7 +212,14 @@ def RandomVacuumAgent():


def TableDrivenVacuumAgent():
"""[Figure 2.3]"""
"""Tabular approach towards vacuum world as mentioned in [Figure 2.3]
>>> agent = TableDrivenVacuumAgent()
>>> environment = TrivialVacuumEnvironment()
>>> environment.add_thing(agent)
>>> environment.run()
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
True
"""
table = {((loc_A, 'Clean'),): 'Right',
((loc_A, 'Dirty'),): 'Suck',
((loc_B, 'Clean'),): 'Left',
Expand Down Expand Up @@ -342,7 +350,12 @@ def run(self, steps=1000):

def list_things_at(self, location, tclass=Thing):
"""Return all things exactly at a given location."""
return [thing for thing in self.things if thing.location == location and isinstance(thing, tclass)]
if isinstance(location, numbers.Number):
return [thing for thing in self.things
if thing.location == location and isinstance(thing, tclass)]
return [thing for thing in self.things
if all(x==y for x,y in zip(thing.location, location))
and isinstance(thing, tclass)]

def some_things_at(self, location, tclass=Thing):
"""Return true if at least one of the things at location
Expand Down Expand Up @@ -621,7 +634,7 @@ def get_world(self):
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.append(self.list_things_at((x, y)))
result.append(row)
return result

Expand Down