[GRASS-SVN] r55325 - grass-addons/grass7/raster/r.agent
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Mar 11 13:25:45 PDT 2013
Author: mic
Date: 2013-03-11 13:25:45 -0700 (Mon, 11 Mar 2013)
New Revision: 55325
Removed:
grass-addons/grass7/raster/r.agent/aco.py
Modified:
grass-addons/grass7/raster/r.agent/r.agent.aco
Log:
merge controllers
Deleted: grass-addons/grass7/raster/r.agent/aco.py
===================================================================
--- grass-addons/grass7/raster/r.agent/aco.py 2013-03-11 20:24:39 UTC (rev 55324)
+++ grass-addons/grass7/raster/r.agent/aco.py 2013-03-11 20:25:45 UTC (rev 55325)
@@ -1,84 +0,0 @@
-"""
-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, grassland, 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 for a playground to a world
-world = anthill.Anthill(grassland.Grassland())
-
-def setmaps(site, cost, wastecosts, inphero, outphero, wastephero):
- """
- Set the user maps in place
- """
- if site:
- # set sitemap and site list
- world.sites = world.playground.parsevectorlayer(anthill.Anthill.SITE,
- site, True)
- else:
- raise error.DataError("aco:", "The site map is mandatory.")
- if cost:
- # set cost/penalty layer
- world.playground.setgrasslayer(anthill.Anthill.COST, cost, True)
- world.overwritepenalty = wastecosts
- if inphero == outphero:
- if not wastephero:
- raise error.DataError("aco:", "May not overwrite the output map.")
- if inphero:
- world.playground.setgrasslayer(anthill.Anthill.RESULT, inphero, True)
- world.playground.grassmapnames[anthill.Anthill.RESULT] = outphero
- world.overwritepheormone = wastephero
-
-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
-
Modified: grass-addons/grass7/raster/r.agent/r.agent.aco
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco 2013-03-11 20:24:39 UTC (rev 55324)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco 2013-03-11 20:25:45 UTC (rev 55325)
@@ -31,11 +31,11 @@
#% required : no
#%end
#%flag
-#% key: overwrite
-#% description: Allow overwriting existing pheromone maps (e.g. if in == out)
+#% key: p
+#% description: Allow overwriting existing pheromone maps
#%end
#%flag
-#% key: overwritecosts
+#% key: c
#% description: Overwrite existing cost map (only used with penalty conversion)
#%end
#%option
@@ -199,65 +199,134 @@
#% required : yes
#%end
-import sys
-from libagent import error, anthill
+from sys import exit, maxsize
+from math import sqrt
+from math import exp
+from random import randint
+from libagent import error, grassland, anthill
+
try:
from grass.script import core as grass
except ImportError:
raise error.EnvError("r.agent.aco:", "Please run inside GRASS.")
-def main():
- world = anthill.Anthill()
+world = anthill.Anthill(grassland.Grassland())
- layer = world.importlayer("costs", "raster", options['penaltymap'])
- world.costsurface = layer.raster
- world.playground.setboundsfromlayer("costs")
+def setmaps(site, cost, wastecosts, inphero, outphero, wastephero):
+ """
+ Set the user maps in place
+ """
+ if site:
+ # set sitemap and site list
+ world.sites = world.playground.parsevectorlayer(anthill.Anthill.SITE,
+ site, True)
+ else:
+ raise error.DataError("aco:", "The site map is mandatory.")
+ if cost:
+ # set cost/penalty layer
+ world.playground.setgrasslayer(anthill.Anthill.COST, cost, True)
+ world.overwritepenalty = wastecosts
+ if inphero == outphero:
+ if not wastephero:
+ raise error.DataError("aco:", "May not overwrite the output map.")
+ if inphero:
+ world.playground.setgrasslayer(anthill.Anthill.RESULT, inphero, True)
+ world.playground.grassmapnames[anthill.Anthill.RESULT] = outphero
+ world.overwritepheormone = wastephero
- if options['inputmap']:
- layer = world.importlayer("phero", "raster", options['inputmap'])
- world.outfilename = options['outputmap']
+def letantsdance(rounds, outrounds):
+ """
+ Organize the agents and the pheromone on the playground.
+ """
+ if 0 < outrounds < rounds:
+ # calculate when to write output
+ mainloops = rounds / outrounds
+ nextwrite = outrounds
else:
- layer = world.createlayer("phero", "raster", None, options['outputmap'])
- world.pherovapour = layer.raster
+ # produce output only at the end
+ mainloops = 1
+ nextwrite = rounds
+ while mainloops > 0:
+ # loop and write out the contents at the end
+ loops = nextwrite
+ while loops > 0:
+ # loop without producing output
+ if len(world.agents) < world.maxants:
+ # as there is still space on the pg, produce another ant
+ # at a random site..
+ position = world.sites[randint(0, len(world.sites)-1)]
+ world.bear(world.antslife, position)
+ for ant in world.agents:
+ # let all the ants take action
+ ant.walk()
+ # let the pheromone evaporate
+ world.volatilize()
+ # count down inner
+ loops -= 1
+ # export the value maps
+ world.writeout()
+# print "nrofpaths:", world.nrop
+ # count down outer
+ mainloops -= 1
+# print "nrofrounds", nrofrounds
- layer = world.importlayer("holes", "vector", options['sitesmap'])
- world.holes = layer.objects
+def main():
- world.rounds = int(options['rounds'])
- if options['outrounds']:
- world.outrounds = int(options['outrounds'])
- if options['maxpheromone']:
- world.maxpheromone = int(options['maxpheromone'])
- if options['minpheromone']:
- world.minpheromone = int(options['minpheromone'])
- if options['volatilizationtime']:
- world.volatilizationtime = int(options['volatilizationtime'])
- if options['stepintensity']:
- world.stepintensity = int(options['stepintensity'])
- if options['pathintensity']:
- world.pathintensity = int(options['pathintensity'])
- if options['maxants']:
- 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['agentfreedom']:
- world.globalfreedom = int(options['agentfreedom'])
- if options['pheromoneweight']:
- world.pheroweight = int(options['pheromoneweight'])
- if options['randomnessweight']:
- world.randomweight = int(options['randomnessweight'])
- if options['costweight']:
- world.costweight = int(options['costweight'])
+ setmaps(options['sitesmap'],
+ options['costmap'], flags['c'],
+ options['inputmap'], options['outputmap'], flags['p'])
+
+# layer = world.importlayer("costs", "raster", options['penaltymap'])
+# world.costsurface = layer.raster
+# world.playground.setboundsfromlayer("costs")
+
+# if options['inputmap']:
+# layer = world.importlayer("phero", "raster", options['inputmap'])
+# world.outfilename = options['outputmap']
+# else:
+# layer = world.createlayer("phero", "raster", None, options['outputmap'])
+# world.pherovapour = layer.raster
+
+# layer = worldimportlayer("holes", "vector", options['sitesmap'])
+# world.holes = layer.objects
+
+# world.rounds = int(options['rounds'])
+# if options['outrounds']:
+# world.outrounds = int(options['outrounds'])
+# if options['maxpheromone']:
+# world.maxpheromone = int(options['maxpheromone'])
+# if options['minpheromone']:
+# world.minpheromone = int(options['minpheromone'])
+# if options['volatilizationtime']:
+# world.volatilizationtime = int(options['volatilizationtime'])
+# if options['stepintensity']:
+# world.stepintensity = int(options['stepintensity'])
+# if options['pathintensity']:
+# world.pathintensity = int(options['pathintensity'])
+# if options['maxants']:
+# 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['agentfreedom']:
+# world.globalfreedom = int(options['agentfreedom'])
+# if options['pheromoneweight']:
+# world.pheroweight = int(options['pheromoneweight'])
+# if options['randomnessweight']:
+# world.randomweight = int(options['randomnessweight'])
+# if options['costweight']:
+# world.costweight = int(options['costweight'])
#if arglist[0] == "stability":
#TODO ask silvia..
try:
- world.checkvalues()
- world.letantsdance()
+# world.checkvalues()
+ if not options['outrounds']:
+ options['outrounds'] = 0
+# letantsdance(int(options['rounds']), int(options['outrounds']))
grass.message("FINISH")
except error.DataError:
grass.fatal_error("Failed to parse args..")
More information about the grass-commit
mailing list