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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 21 08:24:17 PDT 2013


Author: mic
Date: 2013-05-21 08:24:16 -0700 (Tue, 21 May 2013)
New Revision: 56350

Modified:
   grass-addons/grass7/raster/r.agent/libagent/ant.py
   grass-addons/grass7/raster/r.agent/tests/test_ant.py
Log:
add pydoc and tests

Modified: grass-addons/grass7/raster/r.agent/libagent/ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/ant.py	2013-05-21 13:28:01 UTC (rev 56349)
+++ grass-addons/grass7/raster/r.agent/libagent/ant.py	2013-05-21 15:24:16 UTC (rev 56350)
@@ -44,6 +44,13 @@
 
     def check(self, positions):
         """
+        Check a list of positions for a position with a value of interest (<0)
+        in the penalty layer, if such a position is really found, the ant
+        happily turns back home by setting the next step to it's last.
+        If it was only the homeposition, the removes it from the list and
+        goes on.
+        @param positions list of positions
+        @return boolean whether such a position was found
         """
         for p in positions[:]:
             if self.world.getpenalty(p) < 0:
@@ -65,6 +72,10 @@
 
     def choose(self):
         """
+        Make the decisions about where to go to next by first collecting
+        all the possibilities (positions around), then looking whether
+        a goal position is reached or else sorting out unwanted positions
+        and finally choosing a next step by smell and random.
         """
         positions = self.world.getneighbourpositions(self.position)
         if not self.evaluate(positions):
@@ -72,6 +83,8 @@
 
     def walk(self):
         """
+        Do all the things necessary for performing a regualar step when
+        walking around.
         """
         self.laststeps.append(self.position)
         self.position = self.nextstep

Modified: grass-addons/grass7/raster/r.agent/tests/test_ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_ant.py	2013-05-21 13:28:01 UTC (rev 56349)
+++ grass-addons/grass7/raster/r.agent/tests/test_ant.py	2013-05-21 15:24:16 UTC (rev 56350)
@@ -12,26 +12,61 @@
         self.world.sites = [[1,1]]
         self.agent = self.world.bear()
 
+    def test_check(self):
+        positions = [[0,0],[1,1]]
+        self.laststeps = [1,1]
+        # An empty test
+        self.pg.layers[anthill.Anthill.COST][0][0] = 0
+        self.pg.layers[anthill.Anthill.COST][1][1] = 0
+        self.assertFalse(self.agent.check(positions))
+        self.assertIsNone(self.agent.nextstep[0])
+        # set the value of interest but at the homeplace
+        self.pg.layers[anthill.Anthill.COST][1][1] = -1
+        self.assertFalse(self.agent.check(positions))
+        self.assertIsNone(self.agent.nextstep[0])
+        # and this time at a good position
+        self.pg.layers[anthill.Anthill.COST][0][0] = -1
+        self.assertTrue(self.agent.check(positions))
+        self.assertIsNotNone(self.agent.nextstep[0])
+        self.assertEqual(1, self.agent.nextstep[0])
+
+    def test_choose(self):
+        # This one is quite difficult to test as it is mainly a fork of
+        # possible other methods which are directly testable
+        pass
+
+    def test_walk(self):
+        self.agent.position = [0,0]
+        self.agent.nextstep = [1,1]
+        self.agent.walk()
+        self.assertEqual([1,1], self.agent.position)
+        self.assertIsNone(self.agent.nextstep[0])
+        self.assertLess(0, self.pg.layers[anthill.Anthill.RESULT][1][1])
+
+    def test_work(self):
+        # This is the ants main-loop and not that simple to test, every
+        # method that is called from here is tested directly
+        pass
+
     def test_setposition(self):
+        # This is an Agent method, tested there..
         pass
 
     def test_getposition(self):
+        # This is an Agent method, tested there..
         pass
 
-    def test_move(self):
-        pass    
+#    def test_move(self):
+        # This is an Agent method, tested there..
+#        pass    
     
     def test_age(self):
+        # This is an Agent method, tested there..
         pass    
     
     def test_snuffit(self):
+        # This is an Agent method, tested there..
         pass
 
-    def test_walk(self):
-        pass
-
-    def test_work(self):
-        pass
-
 #    def tearDown(self):
 



More information about the grass-commit mailing list