Commit 11e326bf authored by Chris Dancy's avatar Chris Dancy

Commiting some previously fixed issues

parent 6cb34e0b
......@@ -37,6 +37,10 @@ class Agent:
return self.get_cell_on_left()
elif key == 'right_cell':
return self.get_cell_on_right()
elif key == 'left90_cell':
return self.get_cell_on_left90()
elif key == 'right90_cell':
return self.get_cell_on_right90()
elif key == 'ahead_cell':
return self.get_cell_ahead()
raise AttributeError(key)
......@@ -80,6 +84,12 @@ class Agent:
def get_cell_on_right(self):
return self.cell.neighbour[(self.dir + 1) % self.world.directions]
def get_cell_on_left90(self):
return self.cell.neighbour[(self.dir - 2) % self.world.directions]
def get_cell_on_right90(self):
return self.cell.neighbour[(self.dir + 2) % self.world.directions]
def go_towards(self, target, y=None):
if not isinstance(target, Cell):
target = self.world.grid[int(y)][int(target)]
......
......@@ -7,125 +7,139 @@ Cell = cellular.Cell
class World(cellular.World, ccm.Model):
rate = 1
fade_time = 0.5
def __init__(self, cell=None, width=None, height=None, directions=8, filename=None, map=None, **keys):
cellular.World.__init__(
self, cell, width, height, directions, filename, map)
ccm.Model.__init__(self, **keys)
def start(self):
while True:
self.update()
self._update_time = self.now()
yield self.rate
def add(self, agent, *arg, **args):
if isinstance(agent, ccm.Model):
self._ensure_converted()
agent.parent = self
agent._ensure_converted()
if hasattr(agent, 'body'):
agent = agent.body
cellular.World.add(self, agent, *arg, **args)
rate = 1
fade_time = 0.5
def __init__(self, cell=None, width=None, height=None, directions=8, filename=None, map=None, **keys):
cellular.World.__init__(
self, cell, width, height, directions, filename, map)
ccm.Model.__init__(self, **keys)
def start(self):
while True:
self.update()
self._update_time = self.now()
yield self.rate
def add(self, agent, *arg, **args):
if isinstance(agent, ccm.Model):
self._ensure_converted()
agent.parent = self
agent._ensure_converted()
if hasattr(agent, 'body'):
agent = agent.body
cellular.World.add(self, agent, *arg, **args)
def list_pts(x0, y0, x1, y1):
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
deltax = x1 - x0
if deltax == 0:
for y in range(y0, y1):
if steep:
yield (y, x0)
else:
yield (x0, y)
return
deltay = abs(y1 - y0)
error = 0
deltaerr = float(deltay) / deltax
y = y0
if y0 < y1:
ystep = 1
else:
ystep = -1
for x in range(x0, x1 + 1):
if steep:
yield (y, x)
else:
yield (x, y)
error += deltaerr
if error > 0.5:
y += ystep
error -= 1.0
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
deltax = x1 - x0
if deltax == 0:
for y in range(y0, y1):
if steep:
yield (y, x0)
else:
yield (x0, y)
return
deltay = abs(y1 - y0)
error = 0
deltaerr = float(deltay) / deltax
y = y0
if y0 < y1:
ystep = 1
else:
ystep = -1
for x in range(x0, x1 + 1):
if steep:
yield (y, x)
else:
yield (x, y)
error += deltaerr
if error > 0.5:
y += ystep
error -= 1.0
class Body(ccm.Model, cellular.Agent):
def __init__(self):
ccm.Model.__init__(self)
def __setattr__(self, key, value):
if key == 'cell':
if value is None:
self.x = None
self.y = None
else:
self.x = value.x
self.y = value.y
ccm.Model.__setattr__(self, key, value)
def _list_visible_objects(self):
for row in self.world.grid:
for cell in row:
for x, y in list_pts(self.cell.x, self.cell.y, cell.x, cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield cell
for obj in self.world.agents:
for x, y in list_pts(self.cell.x, self.cell.y, obj.cell.x, obj.cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield obj
def __init__(self):
ccm.Model.__init__(self)
def __setattr__(self, key, value):
if key == 'cell':
if value is None:
self.x = None
self.y = None
else:
self.x = value.x
self.y = value.y
ccm.Model.__setattr__(self, key, value)
def _list_visible_objects(self):
for row in self.world.grid:
for cell in row:
for x, y in list_pts(self.cell.x, self.cell.y, cell.x, cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield cell
for obj in self.world.agents:
for x, y in list_pts(self.cell.x, self.cell.y, obj.cell.x, obj.cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield obj
class VisionScanner(ccm.Model):
def __init__(self, body, visual, scan_time=0.01):
ccm.Model.__init__(self)
self._visual = visual
self._body = body
self.scan_time = scan_time
def start(self):
while True:
for obj in self._body._list_visible_objects():
if hasattr(obj, 'cell'):
if getattr(obj, 'x', None) != obj.cell.x:
obj.x = obj.cell.x
if getattr(obj, 'y', None) != obj.cell.y:
obj.y = obj.cell.y
try:
ox, oy = obj.x, obj.y
x, y = self._body.x, self._body.y
salience = self.salience(ox - x, oy - y)
except AttributeError:
salience = 1
self._visual.add(obj)
yield self.scan_time
def salience(self, dx, dy):
dist = dx * dx + dy * dy
s = 4 / (math.sqrt(dist) + 1)
if s > 1:
s = 1
return s
def __init__(self, body, visual, scan_time=0.01):
ccm.Model.__init__(self)
self._visual = visual
self._body = body
self.scan_time = scan_time
def start(self):
for obj in self._body._list_visible_objects():
if hasattr(obj, 'cell'):
if getattr(obj, 'x', None) != obj.cell.x:
obj.x = obj.cell.x
if getattr(obj, 'y', None) != obj.cell.y:
obj.y = obj.cell.y
try:
ox, oy = obj.x, obj.y
x, y = self._body.x, self._body.y
salience = self.salience(ox - x, oy - y)
except AttributeError:
salience = 1
self._visual.add(obj)
# Modified by Chris Dancy
# Any agents in the environment are now included in the list of items returned
# May want to remove ground object at same location in future
try:
for agent in self._body.world.agents:
try:
ox, oy = agent.x, agent.y
x, y = self._body.x, self._body.y
salience = self.salience(ox - x, oy - y)
except AttributeError:
salience = 1
except AttributeError:
pass
yield self.scan_time
def salience(self, dx, dy):
dist = dx * dx + dy * dy
s = 4 / (math.sqrt(dist) + 1)
if s > 1:
s = 1
return s
......@@ -12,7 +12,7 @@ def get(obj,name,key):
key1,key=key.split('.',1)
try:
a=a[key1]
except AttributeError:
except (AttributeError, TypeError):
a=getattr(a,key1)
try:
x=a[key]
......@@ -103,10 +103,12 @@ def parse(patterns,bound=None):
pass
text=text[m.end():]
if len(text)==0:
print("106")
raise PatternException("No value for slot '%s' in pattern '%s'"%(key,pattern))
namedSlots=True
else:
if namedSlots!=False:
print("111")
raise PatternException("Found unnamed slot '%s' after named slot in pattern '%s'"%(text,pattern))
if text=='?': continue
while len(text)>0:
......
......@@ -37,6 +37,10 @@ class Agent:
return self.get_cell_on_left()
elif key == 'right_cell':
return self.get_cell_on_right()
elif key == 'left90_cell':
return self.get_cell_on_left90()
elif key == 'right90_cell':
return self.get_cell_on_right90()
elif key == 'ahead_cell':
return self.get_cell_ahead()
raise AttributeError(key)
......@@ -80,6 +84,12 @@ class Agent:
def get_cell_on_right(self):
return self.cell.neighbour[(self.dir + 1) % self.world.directions]
def get_cell_on_left90(self):
return self.cell.neighbour[(self.dir - 2) % self.world.directions]
def get_cell_on_right90(self):
return self.cell.neighbour[(self.dir + 2) % self.world.directions]
def go_towards(self, target, y=None):
if not isinstance(target, Cell):
target = self.world.grid[int(y)][int(target)]
......
......@@ -7,125 +7,139 @@ Cell = cellular.Cell
class World(cellular.World, ccm.Model):
rate = 1
fade_time = 0.5
def __init__(self, cell=None, width=None, height=None, directions=8, filename=None, map=None, **keys):
cellular.World.__init__(
self, cell, width, height, directions, filename, map)
ccm.Model.__init__(self, **keys)
def start(self):
while True:
self.update()
self._update_time = self.now()
yield self.rate
def add(self, agent, *arg, **args):
if isinstance(agent, ccm.Model):
self._ensure_converted()
agent.parent = self
agent._ensure_converted()
if hasattr(agent, 'body'):
agent = agent.body
cellular.World.add(self, agent, *arg, **args)
rate = 1
fade_time = 0.5
def __init__(self, cell=None, width=None, height=None, directions=8, filename=None, map=None, **keys):
cellular.World.__init__(
self, cell, width, height, directions, filename, map)
ccm.Model.__init__(self, **keys)
def start(self):
while True:
self.update()
self._update_time = self.now()
yield self.rate
def add(self, agent, *arg, **args):
if isinstance(agent, ccm.Model):
self._ensure_converted()
agent.parent = self
agent._ensure_converted()
if hasattr(agent, 'body'):
agent = agent.body
cellular.World.add(self, agent, *arg, **args)
def list_pts(x0, y0, x1, y1):
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
deltax = x1 - x0
if deltax == 0:
for y in range(y0, y1):
if steep:
yield (y, x0)
else:
yield (x0, y)
return
deltay = abs(y1 - y0)
error = 0
deltaerr = float(deltay) / deltax
y = y0
if y0 < y1:
ystep = 1
else:
ystep = -1
for x in range(x0, x1 + 1):
if steep:
yield (y, x)
else:
yield (x, y)
error += deltaerr
if error > 0.5:
y += ystep
error -= 1.0
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
deltax = x1 - x0
if deltax == 0:
for y in range(y0, y1):
if steep:
yield (y, x0)
else:
yield (x0, y)
return
deltay = abs(y1 - y0)
error = 0
deltaerr = float(deltay) / deltax
y = y0
if y0 < y1:
ystep = 1
else:
ystep = -1
for x in range(x0, x1 + 1):
if steep:
yield (y, x)
else:
yield (x, y)
error += deltaerr
if error > 0.5:
y += ystep
error -= 1.0
class Body(ccm.Model, cellular.Agent):
def __init__(self):
ccm.Model.__init__(self)
def __setattr__(self, key, value):
if key == 'cell':
if value is None:
self.x = None
self.y = None
else:
self.x = value.x
self.y = value.y
ccm.Model.__setattr__(self, key, value)
def _list_visible_objects(self):
for row in self.world.grid:
for cell in row:
for x, y in list_pts(self.cell.x, self.cell.y, cell.x, cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield cell
for obj in self.world.agents:
for x, y in list_pts(self.cell.x, self.cell.y, obj.cell.x, obj.cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield obj
def __init__(self):
ccm.Model.__init__(self)
def __setattr__(self, key, value):
if key == 'cell':
if value is None:
self.x = None
self.y = None
else:
self.x = value.x
self.y = value.y
ccm.Model.__setattr__(self, key, value)
def _list_visible_objects(self):
for row in self.world.grid:
for cell in row:
for x, y in list_pts(self.cell.x, self.cell.y, cell.x, cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield cell
for obj in self.world.agents:
for x, y in list_pts(self.cell.x, self.cell.y, obj.cell.x, obj.cell.y):
obs = self.world.grid[y][x]
if obs.wall:
break
else:
yield obj
class VisionScanner(ccm.Model):
def __init__(self, body, visual, scan_time=0.01):
ccm.Model.__init__(self)
self._visual = visual
self._body = body
self.scan_time = scan_time
def start(self):
while True:
for obj in self._body._list_visible_objects():
if hasattr(obj, 'cell'):
if getattr(obj, 'x', None) != obj.cell.x:
obj.x = obj.cell.x
if getattr(obj, 'y', None) != obj.cell.y:
obj.y = obj.cell.y
try:
ox, oy = obj.x, obj.y
x, y = self._body.x, self._body.y
salience = self.salience(ox - x, oy - y)
except AttributeError:
salience = 1
self._visual.add(obj)
yield self.scan_time
def salience(self, dx, dy):
dist = dx * dx + dy * dy
s = 4 / (math.sqrt(dist) + 1)
if s > 1:
s = 1
return s
def __init__(self, body, visual, scan_time=0.01):
ccm.Model.__init__(self)
self._visual = visual
self._body = body
self.scan_time = scan_time
def start(self):
for obj in self._body._list_visible_objects():
if hasattr(obj, 'cell'):
if getattr(obj, 'x', None) != obj.cell.x:
obj.x = obj.cell.x
if getattr(obj, 'y', None) != obj.cell.y:
obj.y = obj.cell.y
try:
ox, oy = obj.x, obj.y
x, y = self._body.x, self._body.y
salience = self.salience(ox - x, oy - y)
except AttributeError:
salience = 1
self._visual.add(obj)
# Modified by Chris Dancy
# Any agents in the environment are now included in the list of items returned
# May want to remove ground object at same location in future
try:
for agent in self._body.world.agents:
try:
ox, oy = agent.x, agent.y
x, y = self._body.x, self._body.y
salience = self.salience(ox - x, oy - y)
except AttributeError:
salience = 1
except AttributeError:
pass
yield self.scan_time
def salience(self, dx, dy):
dist = dx * dx + dy * dy
s = 4 / (math.sqrt(dist) + 1)
if s > 1:
s = 1
return s
......@@ -12,7 +12,7 @@ def get(obj,name,key):
key1,key=key.split('.',1)
try:
a=a[key1]
except AttributeError:
except (AttributeError, TypeError):
a=getattr(a,key1)
try:
x=a[key]
......@@ -103,10 +103,12 @@ def parse(patterns,bound=None):
pass
text=text[m.end():]
if len(text)==0:
print("106")
raise PatternException("No value for slot '%s' in pattern '%s'"%(key,pattern))
namedSlots=True
else:
if namedSlots!=False:
print("111")
raise PatternException("Found unnamed slot '%s' after named slot in pattern '%s'"%(text,pattern))
if text=='?': continue
while len(text)>0:
......
......@@ -17,136 +17,138 @@ mymap="""
"""
class MyCell(grid.Cell):
dirty=False
def color(self):
if self.dirty: return 'brown'
elif self.wall: return 'black'
else: return 'white'
def load(self,char):
if char=='#': self.wall=True
elif char=='D': self.dirty=True
dirty=False
def color(self):
if self.dirty: return 'brown'
elif self.wall: return 'black'
else: return 'white'
def load(self,char):
if char=='#': self.wall=True
elif char=='D': self.dirty=True
class MotorModule(ccm.Model):
busy=False
def go_forward(self):
if self.busy: return
self.busy=True
self.action='going forward'
yield 0.4
self.parent.body.go_forward()
self.action=None
self.busy=False
def go_left(self):
if self.busy: return
self.busy=True
self.action='turning left'
yield 0.1
self.parent.body.turn_left()
self.action='going forward'
yield 0.3
self.parent.body.go_forward()
self.action=None
self.busy=False
def go_right(self):
if self.busy: return
self.busy=True
self.action='turning right'
yield 0.1
self.parent.body.turn_right()
self.action='going forward'
yield 0.3
self.parent.body.go_forward()
self.action=None
self.busy=False
def go_towards(self,x,y):
if self.busy: return
self.busy=True
self.action='going towards %s %s'%(x,y)
yield 0.4
self.parent.body.go_towards(x,y)
self.action=None
self.busy=False
def clean(self):
self.parent.body.cell.dirty=False
busy=False
def go_forward(self):
if self.busy: return
self.busy=True
self.action='going forward'
yield 0.4
self.parent.body.go_forward()
self.action=None
self.busy=False
def go_left(self):
if self.busy: return
self.busy=True
self.action='turning left'
yield 0.1
self.parent.body.turn_left()
self.action='going forward'
yield 0.3
self.parent.body.go_forward()
self.action=None
self.busy=False
def go_right(self):
if self.busy: return
self.busy=True
self.action='turning right'
yield 0.1
self.parent.body.turn_right()
self.action='going forward'
yield 0.3
self.parent.body.go_forward()
self.action=None
self.busy=False
def go_towards(self,x,y):
if self.busy: return
self.busy=True
self.action='going towards %s %s'%(x,