Skip to content

Commit

Permalink
Add example for TableDrivenVacuumAgent and FIX: grid not updating in …
Browse files Browse the repository at this point in the history
…GraphicEnvironment (aimacode#1133)

* Add example for TableDrivenVacuumAgent

* Add example of TableDrivenVacuumAgent in agents4e.py

* FIX: grid not updating in GraphicEnvironment

* FIX: grid not updating in GraphicEnvironment in agents4e.py

* FIX: list_things_at to support all iterables
  • Loading branch information
tirthasheshpatel authored and antmarakis committed Dec 10, 2019
1 parent 6fd1428 commit fbdb36d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
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

0 comments on commit fbdb36d

Please sign in to comment.