[GRASS-SVN] r55317 - in grass-addons/grass7/raster/r.agent: . libagent tests
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Mar 11 09:09:33 PDT 2013
Author: mic
Date: 2013-03-11 09:09:33 -0700 (Mon, 11 Mar 2013)
New Revision: 55317
Added:
grass-addons/grass7/raster/r.agent/aco.py
grass-addons/grass7/raster/r.agent/libagent/anthill.py
grass-addons/grass7/raster/r.agent/tests/test_anthill.py
Removed:
grass-addons/grass7/raster/r.agent/libagent/aco.py
grass-addons/grass7/raster/r.agent/tests/test_aco.py
Modified:
grass-addons/grass7/raster/r.agent/libagent/ant.py
grass-addons/grass7/raster/r.agent/r.agent.aco
Log:
express the MVC structure and rename aco
Added: grass-addons/grass7/raster/r.agent/aco.py
===================================================================
--- grass-addons/grass7/raster/r.agent/aco.py (rev 0)
+++ grass-addons/grass7/raster/r.agent/aco.py 2013-03-11 16:09:33 UTC (rev 55317)
@@ -0,0 +1,66 @@
+"""
+MODULE: r.agent.* aco
+AUTHOR(S): michael lustenberger inofix.ch
+PURPOSE: the controller part for r.agent.aco of the r.agent.* suite or
+ similar.
+COPYRIGHT: (C) 2011 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
+ for details.
+"""
+
+from libagent import error
+from libagent import anthill
+
+from sys import maxsize
+from math import sqrt
+from math import exp
+from random import randint
+
+# initialize the number of iterations
+rounds = 0
+outrounds = 0
+
+# take out a subscription to a world
+world = anthill.Anthill()
+
+def set_maps(site, cost, result):
+ pass
+
+def letantsdance(self):
+ """
+ Organize the agents and the pheromone on the playground.
+ """
+ if 0 < self.outrounds < self.rounds:
+ # calculate when to write output
+ mainloops = self.rounds / self.outrounds
+ nextwrite = self.outrounds
+ else:
+ # produce output only at the end
+ mainloops = 1
+ nextwrite = self.rounds
+ while mainloops > 0:
+ # loop and write out the contents at the end
+ loops = nextwrite
+ while loops > 0:
+ # loop without producing output
+ if len(self.agents) < self.maxants:
+ # as there is still space on the pg, produce another ant
+ # at a random site..
+ position = self.sites[randint(0, len(self.sites)-1)]
+ self.bear(self.antslife, position)
+ for ant in self.agents:
+ # let all the ants take action
+ ant.walk()
+ # let the pheromone evaporate
+ self.volatilize()
+ # count down inner
+ loops -= 1
+ # export the value maps
+ self.writeout()
+# print "nrofpaths:", self.nrop
+ # count down outer
+ mainloops -= 1
+# print "nrofrounds", nrofrounds
+
Deleted: grass-addons/grass7/raster/r.agent/libagent/aco.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/aco.py 2013-03-11 15:21:57 UTC (rev 55316)
+++ grass-addons/grass7/raster/r.agent/libagent/aco.py 2013-03-11 16:09:33 UTC (rev 55317)
@@ -1,130 +0,0 @@
-"""
-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
-
- This program is free software under the GNU General Public
- License (>=v2). Read the file COPYING that comes with GRASS
- for details.
-"""
-
-import error
-import world
-import ant
-
-from sys import maxsize
-from math import sqrt
-from math import exp
-from random import randint
-
-class ACO(world.World):
- """
- World class for using the Ant Colony Optimization Algorithm for
- modelling Agent Based worlds.
- """
- # constant names for the layers
- SITE = "sitemap"
- COST = "penaltymap"
- RESULT = "pheromap"
-
- def __init__(self):
- """
- Create a world based on the natural laws of the Ant Colony Optimization
- algorithm (plus adaptions honouring the complexity of the raster case).
- """
- # get all attributes from the basic world
- super(ACO, self).__init__(ant.Ant)
- # add the main layers
- ## one containing the points of interest
- self.addlayertopg(ACO.SITE)
- ## one containing the time cost for traversing cells
- self.addlayertopg(ACO.COST)
- ## allow overwriting the cost map
- self.overwritepenalty = False
- ## and finally the markings from the agents
- self.addlayertopg(ACO.RESULT)
- ## allow overwriting the main map (if it exists)
- self.overwritepheormone = False
- # list of the points of origin / sites
- self.sites = []
- # default values
- ## how many rounds to go
- self.rounds = 0
- ## when to produce output
- self.outrounds = 0
- ## let ant die if penalty grows too big
- self.maxpenalty = 0
- ## max/min possible value of pheromone intensity
- self.maxpheromone = maxsize
- self.minpheromone = 10
- ## half value period for pheromone
- self.volatilizationtime = 1
- ## ants mark every step with this pheromone value
- self.stepintensity = 10
- ## ants mark every found path with this pheromone intensity
- self.pathintensity = 10000
- ## penalty values above this point an ant considers as illegal
- self.highcostlimit = 0
- ## penalty values below this point an ant considers as illegal
- self.lowcostlimit = 0
- ## how to weigh the values found on the playground
- self.pheroweight = 1
- self.randomweight = 1
- self.costweight = 1
- ## maximum number of ants on the playground
- self.maxants = 100
- ## the ants ttl will be set by user or based on playground size
- self.antslife = 0
-#TODO self.decisionbase = "default"
-#TODO self.rememberbase = "default"
-
- def letantsdance(self):
- """
- Organize the agents and the pheromone on the playground.
- """
- if 0 < self.outrounds < self.rounds:
- # calculate when to write output
- mainloops = self.rounds / self.outrounds
- nextwrite = self.outrounds
- else:
- # produce output only at the end
- mainloops = 1
- nextwrite = self.rounds
- while mainloops > 0:
- # loop and write out the contents at the end
- loops = nextwrite
- while loops > 0:
- # loop without producing output
- if len(self.agents) < self.maxants:
- # as there is still space on the pg, produce another ant
- # at a random site..
- position = self.sites[randint(0, len(self.sites)-1)]
- self.bear(self.antslife, position)
- for ant in self.agents:
- # let all the ants take action
- ant.walk()
- # let the pheromone evaporate
- self.volatilize()
- # count down inner
- loops -= 1
- # export the value maps
- self.writeout()
-# print "nrofpaths:", self.nrop
- # count down outer
- mainloops -= 1
-# print "nrofrounds", nrofrounds
-
- def volatilize(self):
- """
- Let the pheromone evaporate over time.
- """
- self.playground.decaycellvalues(ACO.RESULT, self.volatilizationtime,
- self.minpheromone)
-
- def writeout(self):
- """
- Write the results back onto the maps.
- """
- pass
-
Modified: grass-addons/grass7/raster/r.agent/libagent/ant.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-03-11 15:21:57 UTC (rev 55316)
+++ grass-addons/grass7/raster/r.agent/libagent/ant.py 2013-03-11 16:09:33 UTC (rev 55317)
@@ -14,11 +14,11 @@
import error
class Ant(agent.Agent):
- """Implementation of an Ant Agent for an ACO kind of World."""
+ """Implementation of an Ant Agent for an Anthill, an ACO kind of World."""
def __init__(self, timetolive, world, position):
"""
- Create an Agent for an ACO World
+ Create an Agent for an Anthill World
@param int time to live
@param World the agent knows the worlds he lives in
@param list coordinate of the current position
Added: grass-addons/grass7/raster/r.agent/libagent/anthill.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/anthill.py (rev 0)
+++ grass-addons/grass7/raster/r.agent/libagent/anthill.py 2013-03-11 16:09:33 UTC (rev 55317)
@@ -0,0 +1,126 @@
+"""
+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
+
+ This program is free software under the GNU General Public
+ License (>=v2). Read the file COPYING that comes with GRASS
+ for details.
+"""
+
+import error
+import world
+import ant
+
+from sys import maxsize
+from math import sqrt
+from math import exp
+from random import randint
+
+class Anthill(world.World):
+ """
+ World class for using the Ant Colony Optimization Algorithm for
+ modelling Agent Based worlds.
+ """
+ # constant names for the layers
+ SITE = "sitemap"
+ COST = "penaltymap"
+ RESULT = "pheromap"
+
+ def __init__(self):
+ """
+ Create a world based on the natural laws of the Ant Colony Optimization
+ algorithm (plus adaptions honouring the complexity of the raster case).
+ """
+ # get all attributes from the basic world
+ super(Anthill, self).__init__(ant.Ant)
+ # add the main layers
+ ## one containing the points of interest
+ self.addlayertopg(Anthill.SITE)
+ ## one containing the time cost for traversing cells
+ self.addlayertopg(Anthill.COST)
+ ## allow overwriting the cost map
+ self.overwritepenalty = False
+ ## and finally the markings from the agents
+ self.addlayertopg(Anthill.RESULT)
+ ## allow overwriting the main map (if it exists)
+ self.overwritepheormone = False
+ # list of the points of origin / sites
+ self.sites = []
+ # default values
+ ## let ant die if penalty grows too big
+ self.maxpenalty = 0
+ ## max/min possible value of pheromone intensity
+ self.maxpheromone = maxsize
+ self.minpheromone = 10
+ ## half value period for pheromone
+ self.volatilizationtime = 1
+ ## ants mark every step with this pheromone value
+ self.stepintensity = 10
+ ## ants mark every found path with this pheromone intensity
+ self.pathintensity = 10000
+ ## penalty values above this point an ant considers as illegal
+ self.highcostlimit = 0
+ ## penalty values below this point an ant considers as illegal
+ self.lowcostlimit = 0
+ ## how to weigh the values found on the playground
+ self.pheroweight = 1
+ self.randomweight = 1
+ self.costweight = 1
+ ## maximum number of ants on the playground
+ self.maxants = 100
+ ## the ants ttl will be set by user or based on playground size
+ self.antslife = 0
+#TODO self.decisionbase = "default"
+#TODO self.rememberbase = "default"
+
+ def letantsdance(self):
+ """
+ Organize the agents and the pheromone on the playground.
+ """
+ if 0 < self.outrounds < self.rounds:
+ # calculate when to write output
+ mainloops = self.rounds / self.outrounds
+ nextwrite = self.outrounds
+ else:
+ # produce output only at the end
+ mainloops = 1
+ nextwrite = self.rounds
+ while mainloops > 0:
+ # loop and write out the contents at the end
+ loops = nextwrite
+ while loops > 0:
+ # loop without producing output
+ if len(self.agents) < self.maxants:
+ # as there is still space on the pg, produce another ant
+ # at a random site..
+ position = self.sites[randint(0, len(self.sites)-1)]
+ self.bear(self.antslife, position)
+ for ant in self.agents:
+ # let all the ants take action
+ ant.walk()
+ # let the pheromone evaporate
+ self.volatilize()
+ # count down inner
+ loops -= 1
+ # export the value maps
+ self.writeout()
+# print "nrofpaths:", self.nrop
+ # count down outer
+ mainloops -= 1
+# print "nrofrounds", nrofrounds
+
+ def volatilize(self):
+ """
+ Let the pheromone evaporate over time.
+ """
+ self.playground.decaycellvalues(Anthill.RESULT, self.volatilizationtime,
+ self.minpheromone)
+
+ def writeout(self):
+ """
+ Write the results back onto the maps.
+ """
+ pass
+
Modified: grass-addons/grass7/raster/r.agent/r.agent.aco
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco 2013-03-11 15:21:57 UTC (rev 55316)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco 2013-03-11 16:09:33 UTC (rev 55317)
@@ -200,7 +200,7 @@
#%end
import sys
-from libagent import error, aco
+from libagent import error, anthill
try:
from grass.script import core as grass
@@ -208,7 +208,7 @@
raise error.EnvError("r.agent.aco:", "Please run inside GRASS.")
def main():
- world = aco.ACO()
+ world = anthill.Anthill()
layer = world.importlayer("costs", "raster", options['penaltymap'])
world.costsurface = layer.raster
Deleted: grass-addons/grass7/raster/r.agent/tests/test_aco.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_aco.py 2013-03-11 15:21:57 UTC (rev 55316)
+++ grass-addons/grass7/raster/r.agent/tests/test_aco.py 2013-03-11 16:09:33 UTC (rev 55317)
@@ -1,53 +0,0 @@
-
-import unittest2 as unittest
-#import unittest
-
-from libagent import aco
-
-class TestACO(unittest.TestCase):
- def setUp(self):
- self.world = aco.ACO()
-
- def test_addlayertopg(self):
- # gets tested in World
- pass
-
- def test_removelayerfrompg(self):
- # gets tested in World
- pass
-
- def test_getlayer(self):
- # gets tested in World
- pass
-
- def findposition(self):
- # gets tested in World
- pass
-
- def test_bear(self):
- # gets tested in World
- pass
-
- def test_move(self):
- # gets tested in World
- pass
-
- def test_getneighbourpositions(self):
- # gets tested in World
- pass
-
- def test_kill(self):
- # gets tested in World
- pass
-
- def letantsdance(self):
- pass
-
- def volatilize(self):
- pass
-
- def writeout(self):
- pass
-
-# def tearDown(self):
-
Added: grass-addons/grass7/raster/r.agent/tests/test_anthill.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_anthill.py (rev 0)
+++ grass-addons/grass7/raster/r.agent/tests/test_anthill.py 2013-03-11 16:09:33 UTC (rev 55317)
@@ -0,0 +1,50 @@
+
+import unittest2 as unittest
+#import unittest
+
+from libagent import anthill
+
+class TestAnthill(unittest.TestCase):
+ def setUp(self):
+ self.world = anthill.Anthill()
+
+ def test_addlayertopg(self):
+ # gets tested in World
+ pass
+
+ def test_removelayerfrompg(self):
+ # gets tested in World
+ pass
+
+ def test_getlayer(self):
+ # gets tested in World
+ pass
+
+ def findposition(self):
+ # gets tested in World
+ pass
+
+ def test_bear(self):
+ # gets tested in World
+ pass
+
+ def test_move(self):
+ # gets tested in World
+ pass
+
+ def test_getneighbourpositions(self):
+ # gets tested in World
+ pass
+
+ def test_kill(self):
+ # gets tested in World
+ pass
+
+ def volatilize(self):
+ pass
+
+ def writeout(self):
+ pass
+
+# def tearDown(self):
+
More information about the grass-commit
mailing list