[GRASS-SVN] r56319 - in grass-addons/grass7/raster/r.agent: . libagent tests
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun May 19 14:15:50 PDT 2013
Author: mic
Date: 2013-05-19 14:15:49 -0700 (Sun, 19 May 2013)
New Revision: 56319
Modified:
grass-addons/grass7/raster/r.agent/libagent/ant.py
grass-addons/grass7/raster/r.agent/libagent/anthill.py
grass-addons/grass7/raster/r.agent/libagent/grassland.py
grass-addons/grass7/raster/r.agent/r.agent.aco
grass-addons/grass7/raster/r.agent/tests/test_grassland.py
Log:
implement basic ant behaviour
Modified: grass-addons/grass7/raster/r.agent/libagent/ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-05-19 18:38:49 UTC (rev 56318)
+++ grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-05-19 21:15:49 UTC (rev 56319)
@@ -24,20 +24,60 @@
@param list coordinate of the current position
"""
super(Ant, self).__init__(timetolive, world, position)
- self.position.extend([None,None,0,0])
+ self.position.extend([None,None])
self.home = self.position[:]
self.laststeps = [self.position[:]]
self.visitedsteps = []
self.done = False
- self.nextstep = [None,None,0,0,0,0]
+ self.nextstep = [None,None,None,0]
self.goal = []
self.penalty = 0.0
- if self.world.decisionbase == "default":
+ if self.world.decisionbase == "standard":
# TODO: for now like 'else'..
- self.chooseposition = self.randomposition
+ self.decide = self.randomposition
else:
- self.chooseposition = self.randomposition
+ self.decide = self.randomposition
+ if self.world.evaluationbase == "standard":
+ self.evaluate = self.check
+ else:
+ self.evaluate = self.check
+ def check(self, positions):
+ """
+ """
+ for p in positions[:]:
+ if self.world.getpenalty(p) < 0:
+ # this is what we are looking for!
+ if p[0] == self.home[0] and p[1] == self.home[1]:
+ # ok, unfortunately we have only found the home position..
+ positions.remove(p)
+ # no other special should be so close to home, return..
+ return False
+ else:
+ # goal node found!
+ # add one to the counter
+ #self.world.nrop += 1
+ self.done = True
+ # now, head back home..
+ self.nextstep = self.laststeps.pop()
+ return True
+ return False
+
+ def choose(self):
+ """
+ """
+ positions = self.world.getneighbourpositions(self.position)
+ if not self.evaluate(positions):
+ self.nextstep = self.decide(positions)
+
+ def walk(self):
+ """
+ """
+ self.laststeps.append(self.position)
+ self.position = self.nextstep
+ self.nextstep = [None,None,None,0]
+ self.world.setsteppheromone(self.position)
+
def work(self):
"""
Wander around searching for fields of interest, mark the
@@ -47,7 +87,15 @@
# we are all only getting older..
if self.age() == False:
return False
- self.position = self.chooseposition(
- self.world.getneighbourpositions(self.position))
- self.world.setsteppheromone(self.position)
+ # past this point we must have decided yet where to go to next..
+ if self.nextstep[0] == None:
+ self.choose()
+ self.penalty += self.nextstep[3] + \
+ self.world.getpenalty(self.nextstep)
+ # if penalty is positive, wait one round
+ if self.penalty > 0:
+ self.penalty -= 1
+ return True
+ else:
+ self.walk()
Modified: grass-addons/grass7/raster/r.agent/libagent/anthill.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/anthill.py 2013-05-19 18:38:49 UTC (rev 56318)
+++ grass-addons/grass7/raster/r.agent/libagent/anthill.py 2013-05-19 21:15:49 UTC (rev 56319)
@@ -71,8 +71,8 @@
self.maxants = 100
## the ants ttl will be set by user or based on playground size
self.antslife = 0
- self.decisionbase = "default"
-#TODO self.rememberbase = "default"
+ self.decisionbase = "standard"
+ self.evaluationbase = "standard"
def bear(self):
"""
Modified: grass-addons/grass7/raster/r.agent/libagent/grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/grassland.py 2013-05-19 18:38:49 UTC (rev 56318)
+++ grass-addons/grass7/raster/r.agent/libagent/grassland.py 2013-05-19 21:15:49 UTC (rev 56319)
@@ -130,6 +130,8 @@
layer = self.layers[layername]
if halflife > 0:
layer = layer*0.5**(1.0/halflife)
+ #TODO find out why 'filename' is lost - numpy vs. garray..
+ #self.layers[layername] = layer
#TODO think about moving 'minimum' to a predifined matrix in anthill
if minimum > 0:
mask = garray.numpy.ones_like(layer) + minimum
Modified: grass-addons/grass7/raster/r.agent/r.agent.aco
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco 2013-05-19 18:38:49 UTC (rev 56318)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco 2013-05-19 21:15:49 UTC (rev 56319)
@@ -160,16 +160,16 @@
#% gisprompt: algorithm
#% description: Algorithm used for walking step
#% answer: standard
-#% options: default,random,test
+#% options: standard,random,test
#% required : yes
#%end
#%option
-#% key: validposition
+#% key: evaluateposition
#% type: string
#% gisprompt: algorithm
#% description: Algorithm used for finding and remembering paths
#% answer: avoidorforgetloop
-#% options: specials,avoidloop,forgetloop,avoidorforgetloop
+#% options: standard,avoidloop,forgetloop,avoidorforgetloop
#% required : yes
#%end
#%option
@@ -307,10 +307,10 @@
world.maxants = int(options['maxants'])
if options['antslife']:
world.antslife = int(options['antslife'])
-# if options['decisionalgorithm']:
-# world.decisionbase = str(options['decisionalgorithm'])
-# if options['validposition']:
-# world.validposition = str(options['validposition'])
+ if options['decisionalgorithm']:
+ world.decisionbase = str(options['decisionalgorithm'])
+ if options['evaluateposition']:
+ world.evaluationbase = str(options['evaluateposition'])
# if options['agentfreedom']:
# world.globalfreedom = int(options['agentfreedom'])
if options['pheromoneweight']:
Modified: grass-addons/grass7/raster/r.agent/tests/test_grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_grassland.py 2013-05-19 18:38:49 UTC (rev 56318)
+++ grass-addons/grass7/raster/r.agent/tests/test_grassland.py 2013-05-19 21:15:49 UTC (rev 56319)
@@ -122,11 +122,23 @@
# force write it again..
self.pg.writelayer(self.rastlayername, self.rastlayername, True)
- def test_parsegrasslayer(self):
+ def test_parsevectorlayer(self):
if self.vectlayername:
#TODO find a way to write vector files..
pass
+ def test_decaycellvalues(self):
+ l = "bar"
+ #TODO find out why 'filename' is lost - numpy vs. garray..
+# self.pg.createlayer(l)
+# self.pg.layers[l][0][0] = 100
+# self.pg.decaycellvalues(l, 3)
+# self.assertEqual(int(round(self.pg.layers[l][0][0])), 79)
+# self.pg.decaycellvalues(l, 3)
+# self.assertEqual(int(round(self.pg.layers[l][0][0])), 63)
+# self.pg.decaycellvalues(l, 3)
+# self.assertEqual(int(round(self.pg.layers[l][0][0])), 50)
+
def tearDown(self):
if self.rastlayername:
grass.try_remove(grass.find_file(name = self.rastlayername,
More information about the grass-commit
mailing list