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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Nov 14 16:21:47 PST 2015


Author: mic
Date: 2015-11-14 16:21:47 -0800 (Sat, 14 Nov 2015)
New Revision: 66828

Modified:
   grass-addons/grass7/raster/r.agent/libagent/agent.py
   grass-addons/grass7/raster/r.agent/libagent/ant.py
   grass-addons/grass7/raster/r.agent/libagent/anthill.py
   grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.py
   grass-addons/grass7/raster/r.agent/tests/test_ant.py
Log:
Adding a way to shorten, resp. forget, the path back home

Modified: grass-addons/grass7/raster/r.agent/libagent/agent.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/agent.py	2015-11-14 18:20:20 UTC (rev 66827)
+++ grass-addons/grass7/raster/r.agent/libagent/agent.py	2015-11-15 00:21:47 UTC (rev 66828)
@@ -2,7 +2,7 @@
 MODULE:       r.agent.*
 AUTHOR(S):    michael lustenberger inofix.ch
 PURPOSE:      library file for the r.agent.* suite
-COPYRIGHT:    (C) 2011 by Michael Lustenberger and the GRASS Development Team
+COPYRIGHT:    (C) 2015 by Michael Lustenberger and the GRASS Development Team
 
               This program is free software under the GNU General Public
               License (>=v2). Read the file COPYING that comes with GRASS

Modified: grass-addons/grass7/raster/r.agent/libagent/ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/ant.py	2015-11-14 18:20:20 UTC (rev 66827)
+++ grass-addons/grass7/raster/r.agent/libagent/ant.py	2015-11-15 00:21:47 UTC (rev 66828)
@@ -2,7 +2,7 @@
 MODULE:       r.agent.*
 AUTHOR(S):    michael lustenberger inofix.ch
 PURPOSE:      library file for the r.agent.* suite
-COPYRIGHT:    (C) 2011 by Michael Lustenberger and the GRASS Development Team
+COPYRIGHT:    (C) 2015 by Michael Lustenberger and the GRASS Development Team
 
               This program is free software under the GNU General Public
               License (>=v2). Read the file COPYING that comes with GRASS
@@ -178,6 +178,12 @@
         if len(self.laststeps) > 1:
             # walk only up to the gates of the hometown
             self.nextstep = self.laststeps.pop()
+            # try to avoid loops
+            if (self.world.antavoidsloops):
+                # Find the first occurence of this step in the path array
+                i = self.laststeps.index(self.nextstep)
+                # Forget the path (the loop) inbetween
+                self.laststeps = self.laststeps[0:i]
             self.penalty += self.nextstep[3] + \
                               self.world.getpenalty(self.nextstep)
         else:
@@ -205,9 +211,9 @@
         if not self.age():
             # exit if we died in the meantime..
             return False
-        # past this point we must have decided yet where to go to next..
+        # at this point either we already know where to go to next..
         if self.nextstep[0] == None:
-            # so we'll have to decide it now if it was not clear yet
+            # ..or we'll have to decide it now if it was not clear yet
             self.choose()
             self.penalty += self.nextstep[3] + \
                                 self.world.getpenalty(self.nextstep)

Modified: grass-addons/grass7/raster/r.agent/libagent/anthill.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/anthill.py	2015-11-14 18:20:20 UTC (rev 66827)
+++ grass-addons/grass7/raster/r.agent/libagent/anthill.py	2015-11-15 00:21:47 UTC (rev 66828)
@@ -84,6 +84,7 @@
         self.maxants = self.playground.gettotalcount()
         ## the ants ttl will be set by user or based on playground size
         self.antslife = 2 * self.playground.getdiagonalcount()
+        self.antavoidsloops = False
         self.decisionbase = "standard"
         self.evaluationbase = "standard"
         self.numberofpaths = 0

Modified: grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.py
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.py	2015-11-14 18:20:20 UTC (rev 66827)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.py	2015-11-15 00:21:47 UTC (rev 66828)
@@ -55,6 +55,9 @@
 #% key: a
 #% description: Auto-convert cost (slope..) to penalty map (using "tobler", see docu)
 #%end
+#%flag
+#% key: l
+#% description: Avoid loops on the way back
 #%option
 #% key: sitesmap
 #% type: string
@@ -333,6 +336,8 @@
         elif flags['s'] and options['outrounds'] > 0:
             world.addsequencenumber = True
 
+        if flags['s']:
+            world.antavoidsloops = True
         if options['lowcostlimit']:
             world.minpenalty = int(options['lowcostlimit'])
         if options['highcostlimit']:

Modified: grass-addons/grass7/raster/r.agent/tests/test_ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_ant.py	2015-11-14 18:20:20 UTC (rev 66827)
+++ grass-addons/grass7/raster/r.agent/tests/test_ant.py	2015-11-15 00:21:47 UTC (rev 66828)
@@ -90,7 +90,6 @@
         self.assertEqual(1, self.world.numberofpaths)
 
     def test_walkhome(self):
-        #TODO walking home
         self.agent.nextstep = [0,0,0,0]
         self.agent.laststeps = [[1,1,0,0], [0,1,0,0]]
         self.agent.walkhome()
@@ -99,6 +98,16 @@
         self.assertEqual([[1,1,0,0]], self.agent.laststeps)
         self.agent.walkhome()
         self.assertEqual(0, len(self.world.agents))
+        self.world.antavoidsloops = False
+        self.agent.laststeps = [[1,1,0,0], [0,1,0,0], [1,1,0,0]]
+        self.agent.walkhome()
+        expected = [[1,1,0,0], [0,1,0,0]]
+        self.assertEqual(expected, self.agent.laststeps)
+        self.world.antavoidsloops = True
+        self.agent.laststeps = [[1,1,0,0], [0,1,0,0], [1,1,0,0]]
+        self.agent.walkhome()
+        expected = []
+        self.assertEqual(expected, self.agent.laststeps)
 
     def test_walkaround(self):
         self.agent.position = [0,0]



More information about the grass-commit mailing list