[GRASS-SVN] r56312 - in grass-addons/grass7/raster/r.agent: libagent tests

svn_grass at osgeo.org svn_grass at osgeo.org
Sun May 19 04:38:15 PDT 2013


Author: mic
Date: 2013-05-19 04:38:15 -0700 (Sun, 19 May 2013)
New Revision: 56312

Modified:
   grass-addons/grass7/raster/r.agent/libagent/playground.py
   grass-addons/grass7/raster/r.agent/tests/test_playground.py
Log:
changed position handling to pass penalties on

Modified: grass-addons/grass7/raster/r.agent/libagent/playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/playground.py	2013-05-19 11:37:27 UTC (rev 56311)
+++ grass-addons/grass7/raster/r.agent/libagent/playground.py	2013-05-19 11:38:15 UTC (rev 56312)
@@ -10,12 +10,15 @@
 """
 
 import numpy, error, random
+from math import sqrt
 
 class Playground(object):
     """
     A Playground is a major component of a World, defining
     and organizing space.
     """
+    STRAIGHT = 0
+    DIAGONAL = sqrt(2)-1
 
     def __init__(self):
         """Create a Playground"""
@@ -158,6 +161,17 @@
         else:
             return False
 
+    def addneighbourposition(self, positions, position):
+        """
+        Try adding a position to a list of positions.
+        @param list to be filled up
+        @param position to be verified and added
+        @return the new list
+        """
+        if self.isvalidposition(position):
+            positions.append(position)
+        return positions
+
     def getneighbourpositions(self, position, freedom):
         """
         Get all the positions reachable from a certain position
@@ -171,27 +185,31 @@
             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]))
+            #walking south (=0)
+            self.addneighbourposition(positions,
+                [position[0]-1, position[1], 0, Playground.STRAIGHT])
+            #walking north (=1)
+            self.addneighbourposition(positions,
+                [position[0]+1, position[1], 1, Playground.STRAIGHT])
+            #walking west (=2)
+            self.addneighbourposition(positions,
+                [position[0], position[1]-1, 2, Playground.STRAIGHT])
+            #walking east (=3)
+            self.addneighbourposition(positions,
+                [position[0], position[1]+1, 3, Playground.STRAIGHT])
         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]))
+            #walking south-west (=4)
+            self.addneighbourposition(positions,
+                [position[0]-1, position[1]-1, 4, Playground.DIAGONAL])
+            #walking north-west (=5)
+            self.addneighbourposition(positions,
+                [position[0]+1, position[1]-1, 5, Playground.DIAGONAL])
+            #walking south-east (=6)
+            self.addneighbourposition(positions,
+                [position[0]-1, position[1]+1, 6, Playground.DIAGONAL])
+            #walking north-east (=7)
+            self.addneighbourposition(positions,
+                [position[0]+1, position[1]+1, 7, Playground.DIAGONAL])
         return positions
 
     def getcellvalue(self, layername, position):
@@ -199,7 +217,7 @@
         Get the content of a certain cell in a layer specified.
         @param layername the name of the layer to query
         @param position the exact position of the cell in question
-        @return long the value stored in the cell
+        @return the value stored in the cell
         """
         return self.layers[layername][position[0]][position[1]]
 

Modified: grass-addons/grass7/raster/r.agent/tests/test_playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_playground.py	2013-05-19 11:37:27 UTC (rev 56311)
+++ grass-addons/grass7/raster/r.agent/tests/test_playground.py	2013-05-19 11:38:15 UTC (rev 56312)
@@ -1,7 +1,7 @@
 
 import unittest2 as unittest
 #import unittest
-
+from math import sqrt
 from libagent import playground, error
 
 class TestPlayground(unittest.TestCase):
@@ -96,26 +96,30 @@
         self.assertTrue(self.pg.isvalidposition([1,1]))
         self.assertFalse(self.pg.isvalidposition([3,3]))
 
+    def test_addneighbourposition(self):
+        self.pg.setregion(3,3)
+        positions = []
+        ps = positions[:]
+        self.assertItemsEqual(ps,
+                self.pg.addneighbourposition(positions, [9,9]))
+        ps.append([1,1])
+        self.assertItemsEqual(ps,
+                self.pg.addneighbourposition(positions, [1,1]))
+
     def test_getneighbourpositions(self):
         self.pg.setregion(3,3)
         self.assertFalse(self.pg.getneighbourpositions([1,1],3))
 
+        ps = self.pg.getneighbourpositions([2,2],4)
+        self.assertEqual(2, len(ps))
+        self.assertEqual(0, ps[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])
+        self.assertEqual(8, len(ps))
+        self.assertEqual(7, ps[7][2])
+        self.assertEqual(0, ps[3][3])
+        self.assertEqual(sqrt(2)-1, ps[6][3])
 
-        ps = self.pg.getneighbourpositions([2,2],4)
-        self.assertTrue(ps[0])
-        self.assertFalse(ps[1])
-        self.assertTrue(ps[2])
-        self.assertFalse(ps[3])
-
     def test_getcellvalue(self):
         l = "bar"
         self.pg.createlayer(l)



More information about the grass-commit mailing list