[GRASS-SVN] r56792 - in grass-addons/grass7/raster/r.agent: . libagent tests
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Jun 19 03:12:53 PDT 2013
Author: mic
Date: 2013-06-19 03:12:53 -0700 (Wed, 19 Jun 2013)
New Revision: 56792
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/playground.py
grass-addons/grass7/raster/r.agent/r.agent.aco
grass-addons/grass7/raster/r.agent/tests/test_ant.py
grass-addons/grass7/raster/r.agent/tests/test_playground.py
Log:
utilized the time in the airoplane, added new decision algorithm, more random and cleanups included
Modified: grass-addons/grass7/raster/r.agent/libagent/ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-06-19 06:34:35 UTC (rev 56791)
+++ grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-06-19 10:12:53 UTC (rev 56792)
@@ -9,7 +9,7 @@
for details.
"""
-from random import choice #, randint
+from random import uniform #, choice, randint
import agent
import error
@@ -24,6 +24,11 @@
@param list coordinate of the current position
"""
super(Ant, self).__init__(timetolive, world, position)
+ # position layout: [x][y][orientation][penalty]
+ # orientation (to the last position):
+ # south (=0) north (=1) east (=3) south-west (=4)
+ # north-west (=5) south-east (=6) north-east (=7)
+ # penalty: 1 straight, sqr(2) diagonal
self.position = [position[0], position[1], None, 0]
self.home = self.position[:]
self.laststeps = []
@@ -32,11 +37,11 @@
self.goal = []
self.penalty = 0.0
self.walk = self.walkaround
- if self.world.decisionbase == "standard":
+ if self.world.decisionbase == "random":
# TODO: for now like 'else'..
self.decide = self.randomposition
else:
- self.decide = self.randomposition
+ self.decide = self.markedposition
if self.world.evaluationbase == "standard":
self.evaluate = self.check
else:
@@ -74,12 +79,33 @@
return True
return False
+ def markedposition(self, positions):
+ """
+ Based on the value on a certain layer combined with a random
+ value, pick a posiiton out of a list of positions.
+ @param positions list of possible positions
+ @return position the decision for a position
+ """
+ position = positions[0]
+ tmpval = self.world.getpheromone(position) * self.world.pheroweight +\
+ uniform(self.world.minrandom, self.world.maxrandom) *\
+ self.world.randomweight
+ for i in xrange(1, len(positions)):
+ p = positions[i]
+ newval = self.world.getpheromone(p) * self.world.pheroweight +\
+ uniform(self.world.minrandom, self.world.maxrandom) *\
+ self.world.randomweight
+ if ( newval > tmpval ):
+ position = p
+ tmpval = newval
+ return position
+
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.
+ and finally choosing a next step by smell and/or random.
"""
positions = self.world.getneighbourpositions(self.position)
# check if we found a goal node, else pick a next step from the list
Modified: grass-addons/grass7/raster/r.agent/libagent/anthill.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/anthill.py 2013-06-19 06:34:35 UTC (rev 56791)
+++ grass-addons/grass7/raster/r.agent/libagent/anthill.py 2013-06-19 10:12:53 UTC (rev 56792)
@@ -55,8 +55,11 @@
## max/min possible value of pheromone intensity
self.maxpheromone = maxsize
self.minpheromone = 0
+ ## max/min value for random values
+ self.maxrandom = self.maxpheromone
+ self.minrandom = self.minpheromone
## half value period for pheromone
- self.volatilizationtime = 1
+ self.volatilizationtime = 8
## ants mark every step with this pheromone value
self.stepintensity = 10
## ants mark every found path with this pheromone intensity
@@ -99,8 +102,8 @@
Let the agents do their job. The actual main loop in such a world.
"""
while rounds > 0:
- if len(self.agents) < self.maxants:
-# grass.info(len(self.agents))
+ #grass.info(len(self.agents))
+ if len(self.agents) <= self.maxants:
# as there is still space on the pg, produce another ant
self.bear()
for ant in self.agents:
Modified: grass-addons/grass7/raster/r.agent/libagent/playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/playground.py 2013-06-19 06:34:35 UTC (rev 56791)
+++ grass-addons/grass7/raster/r.agent/libagent/playground.py 2013-06-19 10:12:53 UTC (rev 56792)
@@ -172,9 +172,10 @@
positions.append(position)
return positions
- def getneighbourpositions(self, position, freedom):
+ def getorderedneighbourpositions(self, position, freedom):
"""
- Get all the positions reachable from a certain position
+ Get all the positions reachable from a certain position and
+ ordered by their orientation from the given position
@param list coordinates of a certain cell
@param int number of potentially reachable neighbours
@return list of coordinates, or boolean False
@@ -212,6 +213,18 @@
[position[0]+1, position[1]+1, 7, Playground.DIAGONAL])
return positions
+ def getneighbourpositions(self, position, freedom):
+ """
+ Get all the positions reachable from a certain position and shuffel
+ @param list coordinates of a certain cell
+ @param int number of potentially reachable neighbours
+ @return list of coordinates, or boolean False
+ """
+ positions = self.getorderedneighbourpositions(position, freedom)
+ if positions:
+ random.shuffle(positions)
+ return positions
+
def getcellvalue(self, layername, position):
"""
Get the content of a certain cell in a layer specified.
Modified: grass-addons/grass7/raster/r.agent/r.agent.aco
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco 2013-06-19 06:34:35 UTC (rev 56791)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco 2013-06-19 10:12:53 UTC (rev 56792)
@@ -90,7 +90,7 @@
#% key: highcostlimit
#% type: integer
#% gisprompt: number
-#% description: Penalty values above this point an ant considers as illegal
+#% description: Penalty values above this point an ant considers as illegal/bogus
#% options: 0-<max integer on system would make sense>
#% required : no
#%end
@@ -98,7 +98,7 @@
#% key: lowcostlimit
#% type: integer
#% gisprompt: number
-#% description: Penalty values below this point an ant considers as illegal
+#% description: Penalty values below this point an ant considers as illegal/bogus
#% options: <min integer on system would make sense>-0
#% required : no
#%end
@@ -325,8 +325,10 @@
if options['maxpheromone']:
world.maxpheromone = int(options['maxpheromone'])
+ world.maxrandom = world.maxpheromone
if options['minpheromone']:
world.minpheromone = int(options['minpheromone'])
+ world.minrandom = world.minpheromone
if options['volatilizationtime']:
world.volatilizationtime = int(options['volatilizationtime'])
if options['stepintensity']:
Modified: grass-addons/grass7/raster/r.agent/tests/test_ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_ant.py 2013-06-19 06:34:35 UTC (rev 56791)
+++ grass-addons/grass7/raster/r.agent/tests/test_ant.py 2013-06-19 10:12:53 UTC (rev 56792)
@@ -33,6 +33,18 @@
self.assertEqual(1, self.agent.nextstep[0])
self.assertEqual(1, self.world.numberofpaths)
+ def test_markedpositions(self):
+ self.world.pheroweight = 1
+ self.world.randomweight = 1
+ positions = [[0,0,0,0],[1,1,3,0]]
+ self.pg.layers[anthill.Anthill.RESULT][0][0] = 0
+ self.pg.layers[anthill.Anthill.RESULT][1][1] = 999
+ self.world.minrandom = 0
+ self.world.maxrandom = 1
+ p = self.agent.markedposition(positions)
+ self.assertEqual([1,1], p[:2])
+ # we do not test random here..
+
def test_choose(self):
# every second step should be a goal..
self.assertEqual(0, self.world.numberofpaths)
Modified: grass-addons/grass7/raster/r.agent/tests/test_playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_playground.py 2013-06-19 06:34:35 UTC (rev 56791)
+++ grass-addons/grass7/raster/r.agent/tests/test_playground.py 2013-06-19 10:12:53 UTC (rev 56792)
@@ -106,20 +106,26 @@
self.assertItemsEqual(ps,
self.pg.addneighbourposition(positions, [1,1]))
- def test_getneighbourpositions(self):
+ def test_getorderedneighbourpositions(self):
self.pg.setregion(3,3)
- self.assertFalse(self.pg.getneighbourpositions([1,1],3))
+ self.assertFalse(self.pg.getorderedneighbourpositions([1,1],3))
- ps = self.pg.getneighbourpositions([2,2],4)
+ ps = self.pg.getorderedneighbourpositions([2,2],4)
self.assertEqual(2, len(ps))
self.assertEqual(0, ps[1][3])
- ps = self.pg.getneighbourpositions([1,1],8)
+ ps = self.pg.getorderedneighbourpositions([1,1],8)
self.assertEqual(8, len(ps))
self.assertEqual(7, ps[7][2])
self.assertEqual(0, ps[3][3])
self.assertEqual(sqrt(2)-1, ps[6][3])
+ def test_getneighbourpositions(self):
+ self.pg.setregion(3,3)
+ ps = self.pg.getneighbourpositions([2,2],4)
+ self.assertEqual(2, len(ps))
+ self.assertEqual(0, ps[1][3])
+
def test_getcellvalue(self):
l = "bar"
self.pg.createlayer(l)
More information about the grass-commit
mailing list