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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Nov 11 08:38:17 PST 2015


Author: mic
Date: 2015-11-11 08:38:17 -0800 (Wed, 11 Nov 2015)
New Revision: 66786

Modified:
   grass-addons/grass7/raster/r.agent.aco/libagent/agent.py
   grass-addons/grass7/raster/r.agent.aco/tests/test_agent.py
Log:
adding method for basic agents to take just one step

Modified: grass-addons/grass7/raster/r.agent.aco/libagent/agent.py
===================================================================
--- grass-addons/grass7/raster/r.agent.aco/libagent/agent.py	2015-11-11 16:15:44 UTC (rev 66785)
+++ grass-addons/grass7/raster/r.agent.aco/libagent/agent.py	2015-11-11 16:38:17 UTC (rev 66786)
@@ -43,7 +43,7 @@
         Ask the agent for its current position
         @return list coordinate of the current position
         """
-#TODO not very meaningfull yet..
+#TODO not very meaningful yet..
         return self.position
 
     def randomposition(self, positions):
@@ -54,15 +54,16 @@
         """
         return positions[randint(0, len(positions)-1)]
 
-#    def move(self, nrofsteps, direction):
-# not implemented yet
-#        """
-#        Let the agent move
-#        @param int the number of steps to walk
-#        @param char the direction to go
-#        @return list coordinate of the current position
-#        """
-#        return self.position
+    def step(self):
+        """
+        Move to a neighbour position, as in making a single step
+        @param list coordinates of a certain cell
+        @param list coordinates of a certain cell
+        """
+        position = self.getposition()
+        positions = self.world.getneighbourpositions(position)
+        # positions are already shuffled
+        self.setposition(positions[0])
 
     def age(self):
         """

Modified: grass-addons/grass7/raster/r.agent.aco/tests/test_agent.py
===================================================================
--- grass-addons/grass7/raster/r.agent.aco/tests/test_agent.py	2015-11-11 16:15:44 UTC (rev 66785)
+++ grass-addons/grass7/raster/r.agent.aco/tests/test_agent.py	2015-11-11 16:38:17 UTC (rev 66786)
@@ -25,10 +25,19 @@
         self.assertEqual(position,
                     self.agent.randomposition([position, position]))
 
-#    def test_move(self):
-# not implemented yet
-#        pass
+    def test_step(self):
+        position = self.agent.getposition();
+        oldposition = [position[0],position[1]]
+        agentneighbours = self.world.getneighbourpositions(position);
+        for i in range(len(agentneighbours)):
+            p = [agentneighbours[i][0],agentneighbours[i][1]]
+            agentneighbours[i] = p
 
+        self.agent.step()
+        newposition = self.agent.getposition()
+        self.assertIn(newposition, agentneighbours)
+        self.assertFalse(newposition == oldposition)
+
     def test_age(self):
         self.agent.ttl = 1
         age = self.agent.ttl



More information about the grass-commit mailing list