[GRASS-SVN] r56357 - in grass-addons/grass7/raster/r.agent: libagent tests
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue May 21 14:52:13 PDT 2013
Author: mic
Date: 2013-05-21 14:52:12 -0700 (Tue, 21 May 2013)
New Revision: 56357
Modified:
grass-addons/grass7/raster/r.agent/libagent/ant.py
grass-addons/grass7/raster/r.agent/tests/test_ant.py
Log:
implement first version of walk back home
Modified: grass-addons/grass7/raster/r.agent/libagent/ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-05-21 16:27:24 UTC (rev 56356)
+++ grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-05-21 21:52:12 UTC (rev 56357)
@@ -24,9 +24,9 @@
@param list coordinate of the current position
"""
super(Ant, self).__init__(timetolive, world, position)
- self.position.extend([None,None])
+ self.position = [position[0], position[1], None, 0]
self.home = self.position[:]
- self.laststeps = [self.position[:]]
+ self.laststeps = []
self.visitedsteps = []
self.done = False
self.nextstep = [None,None,None,0]
@@ -72,6 +72,7 @@
# now, head back home..
self.nextstep = self.laststeps.pop()
return True
+ # TODO instead of work and walk reset work, which initially points to walkaround, to to headhome here!
return False
def choose(self):
@@ -88,12 +89,24 @@
def walk(self):
"""
Do all the things necessary for performing a regualar step when
- walking around.
+ walking around or going back home.
"""
- self.laststeps.append(self.position)
- self.position = self.nextstep
- self.nextstep = [None,None,None,0]
- self.world.setsteppheromone(self.position)
+ if self.done:
+ self.position = self.nextstep
+ if len(self.laststeps) > 1:
+ # walk only up to the gates of the hometown
+ self.nextstep = self.laststeps.pop()
+ self.penalty += self.nextstep[3] + \
+ self.world.getpenalty(self.nextstep)
+ else:
+ # retire after work.
+ self.snuffit()
+ self.world.setpathpheromone(self.position)
+ else:
+ self.laststeps.append(self.position)
+ self.position = self.nextstep
+ self.nextstep = [None,None,None,0]
+ self.world.setsteppheromone(self.position)
def work(self):
"""
@@ -106,6 +119,7 @@
return False
# past this point we must have decided yet where to go to next..
if self.nextstep[0] == None:
+ # so we decide it now if it is not clear yet
self.choose()
self.penalty += self.nextstep[3] + \
self.world.getpenalty(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 16:27:24 UTC (rev 56356)
+++ grass-addons/grass7/raster/r.agent/tests/test_ant.py 2013-05-21 21:52:12 UTC (rev 56357)
@@ -14,7 +14,7 @@
def test_check(self):
positions = [[0,0],[1,1]]
- self.laststeps = [1,1]
+ self.agent.laststeps = [[1,1]]
# An empty test
self.pg.layers[anthill.Anthill.SITE][0][0] = 0
self.pg.layers[anthill.Anthill.SITE][1][1] = 0
@@ -36,6 +36,7 @@
pass
def test_walk(self):
+ #TODO walking home
self.agent.position = [0,0]
self.agent.nextstep = [1,1]
self.agent.walk()
More information about the grass-commit
mailing list