[GRASS-SVN] r54836 - in grass-addons/grass7/raster/r.agent: libagent tests
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Feb 2 05:08:33 PST 2013
Author: mic
Date: 2013-02-02 05:08:33 -0800 (Sat, 02 Feb 2013)
New Revision: 54836
Modified:
grass-addons/grass7/raster/r.agent/libagent/playground.py
grass-addons/grass7/raster/r.agent/libagent/world.py
grass-addons/grass7/raster/r.agent/tests/test_grassland.py
grass-addons/grass7/raster/r.agent/tests/test_playground.py
grass-addons/grass7/raster/r.agent/tests/test_world.py
Log:
position handling added
Modified: grass-addons/grass7/raster/r.agent/libagent/playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/playground.py 2013-02-02 11:12:33 UTC (rev 54835)
+++ grass-addons/grass7/raster/r.agent/libagent/playground.py 2013-02-02 13:08:33 UTC (rev 54836)
@@ -20,9 +20,17 @@
def __init__(self):
"""Create a Playground"""
self.layers = dict()
-#TODO
- self.region = dict(n=1,s=0,w=0,e=1,rows=1,cols=1)
+ self.setregion(1,1)
+ def setregion(self, rows, cols):
+ """
+ Set the geometry of the playground, based only on the number
+ of rows and columns
+ @param numeric number of rows
+ @param numeric number of columns
+ """
+ self.region = dict(s=0,n=rows,w=0,e=cols,rows=rows,cols=cols)
+
def getregion(self):
"""
Return the region information
@@ -110,11 +118,47 @@
def isvalidposition(self, position):
"""
Test if a position realy is on the playground
- @return boolean True if on, False if off the playground
+ @return list position if on, boolean False if off the playground
"""
if self.region["s"] <= position[0] < self.region["n"] and \
self.region["w"] <= position[1] < self.region["e"]:
- return True
+ return position
else:
return False
+ def getneighbourpositions(self, position, freedom):
+ """
+ Get all the positions reachable from a certain position
+ @param list coordinates of a certain cell
+ @param int number of potentially reachable neighbours
+ @return list of coordinates, or boolean False
+ """
+ positions = []
+ # test for all valid freedoms
+ if not freedom == 4 and not freedom == 8:
+ return False
+ # collect the coordinates
+ if freedom >= 4:
+ #walking south
+ positions.append(self.isvalidposition([position[0]-1, position[1]]))
+ #walking north
+ positions.append(self.isvalidposition([position[0]+1, position[1]]))
+ #walking west
+ positions.append(self.isvalidposition([position[0], position[1]-1]))
+ #walking east
+ positions.append(self.isvalidposition([position[0], position[1]+1]))
+ if freedom >= 8:
+ #walking south-west
+ positions.append(self.isvalidposition([position[0]-1,
+ position[1]-1]))
+ #walking north-west
+ positions.append(self.isvalidposition([position[0]+1,
+ position[1]-1]))
+ #walking south-east
+ positions.append(self.isvalidposition([position[0]-1,
+ position[1]+1]))
+ #walking north-east
+ positions.append(self.isvalidposition([position[0]+1,
+ position[1]+1]))
+ return positions
+
Modified: grass-addons/grass7/raster/r.agent/libagent/world.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/world.py 2013-02-02 11:12:33 UTC (rev 54835)
+++ grass-addons/grass7/raster/r.agent/libagent/world.py 2013-02-02 13:08:33 UTC (rev 54836)
@@ -32,7 +32,7 @@
self.agenttype = agenttype
self.agents = []
#self.artefacts = []
- self.freedom = freedom
+ self.agentfreedom = freedom
def addlayertopg(self, layername):
"""
@@ -105,9 +105,8 @@
@return list list of coordinates
"""
if not freedom:
- freedom = self.freedom
- pass
- #return
+ freedom = self.agentfreedom
+ return self.playground.getneighbourpositions(position, freedom)
def kill(self, agent):
"""
Modified: grass-addons/grass7/raster/r.agent/tests/test_grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_grassland.py 2013-02-02 11:12:33 UTC (rev 54835)
+++ grass-addons/grass7/raster/r.agent/tests/test_grassland.py 2013-02-02 13:08:33 UTC (rev 54836)
@@ -14,6 +14,10 @@
self.assertIsNotNone(self.pg.getregion())
self.assertEqual(self.pg.getregion(),grass.region())
+ def test_setregion(self):
+ #TODO should not be required here.. maybe "resetregion()"?
+ pass
+
def test_getbound(self):
n = self.pg.region["n"]
s = self.pg.region["s"]
@@ -34,8 +38,9 @@
self.assertEqual((n-s)/ns, r)
self.assertEqual((e-w)/ew, c)
- #def test_setlayer(self):
+ def test_setlayer(self):
# gets tested in createlayer and super()/Playground
+ pass
def test_setgrasslayer(self):
#TODO better test manually?
@@ -48,8 +53,9 @@
self.assertEqual(len(self.pg.layers["foo"]), self.pg.region["rows"])
self.assertEqual(len(self.pg.layers["foo"][0]), self.pg.region["cols"])
- #def test_getlayer(self):
+ def test_getlayer(self):
# gets tested in createlayer and super()/Playground
+ pass
def test_removelayer(self):
self.pg.layers["foo"] = [0]
Modified: grass-addons/grass7/raster/r.agent/tests/test_playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_playground.py 2013-02-02 11:12:33 UTC (rev 54835)
+++ grass-addons/grass7/raster/r.agent/tests/test_playground.py 2013-02-02 13:08:33 UTC (rev 54836)
@@ -8,6 +8,10 @@
def setUp(self):
self.pg = playground.Playground()
+ def test_setregion(self):
+ # is actually already tested by playground.Playground.__init__()
+ pass
+
def test_getregion(self):
self.assertIsNotNone(self.pg.getregion())
self.assertIs(self.pg.getregion(),self.pg.region)
@@ -79,5 +83,30 @@
self.assertTrue(position[1] >= w)
self.assertTrue(position[1] < e)
+ def test_isvalidposition(self):
+ self.pg.setregion(3,3)
+ self.assertTrue(self.pg.isvalidposition([1,1]))
+ self.assertFalse(self.pg.isvalidposition([3,3]))
+
+ def test_getneighbourpositions(self):
+ self.pg.setregion(3,3)
+ self.assertFalse(self.pg.getneighbourpositions([1,1],3))
+
+ ps = self.pg.getneighbourpositions([1,1],8)
+ self.assertTrue(ps[0])
+ self.assertTrue(ps[1])
+ self.assertTrue(ps[2])
+ self.assertTrue(ps[3])
+ self.assertTrue(ps[4])
+ self.assertTrue(ps[5])
+ self.assertTrue(ps[6])
+ self.assertTrue(ps[7])
+
+ ps = self.pg.getneighbourpositions([2,2],4)
+ self.assertTrue(ps[0])
+ self.assertFalse(ps[1])
+ self.assertTrue(ps[2])
+ self.assertFalse(ps[3])
+
# def tearDown(self):
Modified: grass-addons/grass7/raster/r.agent/tests/test_world.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_world.py 2013-02-02 11:12:33 UTC (rev 54835)
+++ grass-addons/grass7/raster/r.agent/tests/test_world.py 2013-02-02 13:08:33 UTC (rev 54836)
@@ -44,6 +44,7 @@
self.assertEqual(agent.position, position)
def test_getneighbourpositions(self):
+ # gets tested in Playground (except for freedom..). TODO?
pass
def test_kill(self):
More information about the grass-commit
mailing list