[GRASS-SVN] r65700 - in grass-addons/grass7/raster: . r.agent.aco r.agent.aco/libagent

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jul 20 04:47:11 PDT 2015


Author: annalisapg
Date: 2015-07-20 04:47:11 -0700 (Mon, 20 Jul 2015)
New Revision: 65700

Added:
   grass-addons/grass7/raster/r.agent.aco/
   grass-addons/grass7/raster/r.agent.aco/r.agent.aco.html
   grass-addons/grass7/raster/r.agent.aco/r.agent.aco.py
Removed:
   grass-addons/grass7/raster/r.agent.aco/
   grass-addons/grass7/raster/r.agent.aco/libold/
   grass-addons/grass7/raster/r.agent.aco/r.agent.aco
   grass-addons/grass7/raster/r.agent.aco/r.agent.html
   grass-addons/grass7/raster/r.agent/
Modified:
   grass-addons/grass7/raster/r.agent.aco/Makefile
   grass-addons/grass7/raster/r.agent.aco/libagent/Makefile
Log:
rename of the r.agent module and fix of makefiles

Modified: grass-addons/grass7/raster/r.agent.aco/Makefile
===================================================================
--- grass-addons/grass7/raster/r.agent/Makefile	2015-07-20 11:19:53 UTC (rev 65699)
+++ grass-addons/grass7/raster/r.agent.aco/Makefile	2015-07-20 11:47:11 UTC (rev 65700)
@@ -1,13 +1,13 @@
 MODULE_TOPDIR =../..
 
-PGM = r.agent
+PGM = r.agent.aco
 
-SUBDIRS = \
-        libagent \
-        libold
 
+SUBDIRS = libagent
+
 include $(MODULE_TOPDIR)/include/Make/Dir.make
+include $(MODULE_TOPDIR)/include/Make/Script.make
 
-default: parsubdirs htmldir
+default: script parsubdirs
 
 install: installsubdirs

Modified: grass-addons/grass7/raster/r.agent.aco/libagent/Makefile
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/Makefile	2015-07-20 11:19:53 UTC (rev 65699)
+++ grass-addons/grass7/raster/r.agent.aco/libagent/Makefile	2015-07-20 11:47:11 UTC (rev 65700)
@@ -5,7 +5,7 @@
 
 MODULES = agent anthill ant error grassland playground world
 
-ETCDIR = $(ETC)/r.agent
+ETCDIR = $(ETC)/r.agent.aco
 
 PYFILES := $(patsubst %,$(ETCDIR)/%.py,$(MODULES))
 PYCFILES := $(patsubst %,$(ETCDIR)/%.pyc,$(MODULES))

Deleted: grass-addons/grass7/raster/r.agent.aco/r.agent.aco
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco	2015-07-20 11:19:53 UTC (rev 65699)
+++ grass-addons/grass7/raster/r.agent.aco/r.agent.aco	2015-07-20 11:47:11 UTC (rev 65700)
@@ -1,373 +0,0 @@
-#!/usr/bin/env python
-"""
-MODULE:       r.agent.aco
-AUTHOR(S):    michael lustenberger inofix.ch
-PURPOSE:      r.agent.aco is used to organize ant-like agents in a raster
-              based playground. As decribed by the Ant Colony Optimization
-              algorithm, the ants wander around looking for attractors,
-              marking their paths if they find any.
-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.
-"""
-
-##TODO it is time to make this all multithreaded..
-
-#%Module
-#% description: Agents wander around on the terrain, marking paths to new locations.
-#%End
-#%option
-#% key: outputmap
-#% type: string
-#% gisprompt: old,cell,raster
-#% description: Name of pheromone output map
-#% required : yes
-#%end
-#%option
-#% key: inputmap
-#% type: string
-#% gisprompt: old,cell,raster
-#% description: Name of input pheromone raster map (e.g. from prior run)
-#% required : no
-#%end
-#%flag
-#% key: p
-#% description: Allow overwriting existing pheromone maps
-#%end
-#%flag
-#% key: s
-#% description: Produce a sequence of pheromone maps (by appending a number)
-#%end
-#%flag
-#% key: c
-#% description: Overwrite existing cost map (only used with penalty conversion)
-#%end
-#%option
-#% key: costmap
-#% type: string
-#% gisprompt: old,cell,raster
-#% description: Name of penalty resp. cost raster map (note conversion checkbox)
-#% required : yes
-#%end
-#%flag
-#% key: a
-#% description: Auto-convert cost (slope..) to penalty map (using "tobler", see docu)
-#%end
-#%option
-#% key: sitesmap
-#% type: string
-#% gisprompt: old,vect,vector
-#% description: Name of sites map, vector data with possible points of origin
-#% required : yes
-#%end
-#%option
-#% key: rounds
-#% type: integer
-#% gisprompt: number
-#% description: Number of iterations/rounds to run
-#% answer: 999
-#% options: 0-999999
-#% required : yes
-#%end
-#%option
-#% key: outrounds
-#% type: integer
-#% gisprompt: number
-#% description: Produce output after running this number of iterations/rounds
-#% options: 0-999999
-#% required : no
-#%end
-#%option
-#TODO evaluate..
-#% key: targetvisibility
-#% type: integer
-#% gisprompt: number
-#% description: Distance to target from where it might be 'sensed'
-#% options: 0-999999
-#% required : no
-#%end
-#%option
-#% key: highcostlimit
-#% type: integer
-#% gisprompt: number
-#% description: Penalty values above this point an ant considers as illegal/bogus when in 'costlymarked' modus
-#% options: 0-<max integer on system would make sense>
-#% required : no
-#%end
-#%option
-#% key: lowcostlimit
-#% type: integer
-#% gisprompt: number
-#% description: Penalty values below this point an ant considers as illegal/bogus when in 'costlymarked' modus
-#% options: -99999-99999
-#% required : no
-#%end
-#%option
-#% key: maxpheromone
-#% type: integer
-#% gisprompt: number
-#% description: Absolute maximum of pheromone intensity a position may have
-#% options: <minpheromone>-<max integer on system would make sense>
-#% required : no
-#%end
-#%option
-#% key: minpheromone
-#% type: integer
-#% gisprompt: number
-#% description: Absolute minimum of pheromone intensity to leave on playground
-#% options: 0-<maxpheromone>
-#% required : no
-#%end
-#%option
-#% key: volatilizationtime
-#% type: integer
-#% gisprompt: number
-#% description: Half-life for pheromone to volatize (e.g. =rounds)
-#% options: 0-<max integer on system would make sense>
-#% required : no
-#%end
-#%option
-#% key: stepintensity
-#% type: integer
-#% gisprompt: number
-#% description: Pheromone intensity to leave on each step
-#% options: 0-<max integer on system would make sense>
-#% required : no
-#%end
-#%option
-#% key: pathintensity
-#% type: integer
-#% gisprompt: number
-#% description: Pheromone intensity to leave on found paths
-#% options: 0-<max integer on system would make sense>
-#% required : no
-#%end
-#%option
-#% key: maxants
-#% type: integer
-#% gisprompt: number
-#% description: Maximum amount of ants that may live concurrently (x*y)
-#% options: 0-<the bigger the playground, the more space they have>
-#% required : no
-#%end
-#%option
-#% key: antslife
-#% type: integer
-#% gisprompt: number
-#% description: Time to live for an ant (e.g. four times points distance)
-#% options: 0-<max integer on system would make sense>
-#% required : no
-#%end
-#%option
-#% key: decisionalgorithm
-#% type: string
-#% gisprompt: algorithm
-#% description: Algorithm used for walking step
-#% answer: standard
-#% options: standard,marked,costlymarked,random,test
-#% required : yes
-#%end
-#%option
-#% key: evaluateposition
-#% type: string
-#% gisprompt: algorithm
-#% description: Algorithm used for finding and remembering paths
-#% answer: avoidorforgetloop
-#% options: standard,avoidloop,forgetloop,avoidorforgetloop
-#% required : yes
-#%end
-#%option
-#% key: agentfreedom
-#% type: integer
-#% gisprompt: number
-#% description: Number of possible directions the ant can take (4 or 8)
-#% options: 4,8
-#% required : no
-#%end
-#%option
-#% key: pheromoneweight
-#% type: integer
-#% gisprompt: number
-#% description: How is the pheromone value (P) weighted when walking (p*P:r*R:c*C)
-#% answer: 1
-#% options: 0-99999
-#% required : yes
-#%end
-#%option
-#% key: randomnessweight
-#% type: integer
-#% gisprompt: number
-#% description: How is the random value (R) weighted when walking (p*P:r*R:c*C)
-#% answer: 1
-#% options: 0-99999
-#% required : yes
-#%end
-#%option
-#% key: costweight
-#% type: integer
-#% gisprompt: number
-#% description: How is the penalty value (C) weighted when walking (p*P:r*R:c*C)
-#% answer: 0
-#% options: 0-99999
-#% required : yes
-#%end
-
-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.")
-
-world = anthill.Anthill(grassland.Grassland())
-
-def setmaps(site, cost, wastecosts, inphero, outphero, wastephero):
-    """
-    Set the user maps in place
-    """
-    if site:
-        if -1 == site.find('@'):
-            site = site + '@' + grass.gisenv()['MAPSET']
-        # set sitemap and site list
-        world.sites = world.playground.parsevectorlayer(anthill.Anthill.SITE,
-                                                        site, -1, True)
-        if not world.sites:
-            raise error.DataError("r.agent.aco:",
-                                    "There were no sites in" + site)
-    else:
-        raise error.DataError("r.agent.aco:", "The site map is mandatory.")
-    if cost:
-        if -1 == cost.find('@'):
-            cost = cost + '@' + grass.gisenv()['MAPSET']
-        # set cost/penalty layer
-        world.playground.setgrasslayer(anthill.Anthill.COST, cost, True)
-        world.overwritepenalty = wastecosts
-    else:
-        raise error.DataError("r.agent.aco:", "The cost map is mandatory.")
-    if outphero:
-        if -1 == outphero.find('@'):
-            outphero = outphero + '@' + grass.gisenv()['MAPSET']
-    else:
-        raise error.DataError("r.agent.aco:", "The output map is mandatory.")
-    if inphero == outphero:
-        if not wastephero:
-            raise error.DataError("aco:", "May not overwrite the output map.")
-    if inphero:
-        if -1 == inphero.find('@'):
-            inphero = inphero + '@' + grass.gisenv()['MAPSET']
-        world.playground.setgrasslayer(anthill.Anthill.RESULT, inphero, True)
-    world.playground.grassmapnames[anthill.Anthill.RESULT] = outphero
-    #TODO hopefully not really needed - workaround for broken(?) garray
-    world.playground.createlayer("copy", outphero, True)
-    world.playground.grassmapnames["copy"] = outphero
-    world.overwritepheormone = wastephero
-
-def letantsdance(rounds, outrounds):
-    """
-    Organize the agents and the pheromone on the playground.
-    """
-    if world.addsequencenumber:
-        outputmapbasename = \
-            world.playground.grassmapnames[anthill.Anthill.RESULT].split("@")
-    else:
-        outputmapbasename = False
-        outputmapname = False
-    if 0 < outrounds < rounds:
-        # calculate when to write output
-        mainloops = rounds / outrounds
-        nextwrite = outrounds
-    else:
-        # produce output only at the end
-        mainloops = 1
-        nextwrite = rounds
-    run = 0
-    while run < mainloops:
-        if outputmapbasename:
-            outputmapname = outputmapbasename[0] + str(run) + "@" + \
-                            outputmapbasename[1]
-        # loop and write out the contents at the end
-        world.letantsdance(nextwrite)
-        # Print the number of found paths
-        grass.info("Number of found paths: " + str(world.numberofpaths))
-        # export the value maps
-        #TODO hopefully not really needed - workaround for broken(?) garray
-        for i in range(len(world.playground.layers[anthill.Anthill.RESULT])):
-            for j in range(len(
-                    world.playground.layers[anthill.Anthill.RESULT][0])):
-                world.playground.layers["copy"][i][j] = \
-                        world.playground.layers[anthill.Anthill.RESULT][i][j]
-        world.playground.writelayer("copy", outputmapname,
-                                            world.overwritepheormone)
-        #TODO world.playground.writelayer(anthill.Anthill.RESULT, False,
-        #TODO                                    world.overwritepheormone)
-#        print "nrofpaths:", world.nrop
-        # count down outer
-        run += 1
-#    print "nrofrounds", nrofrounds
-
-def main():
-
-    try:
-        setmaps(options['sitesmap'],
-            options['costmap'], flags['c'],
-            options['inputmap'], options['outputmap'], flags['p'])
-
-#        world.playground.setboundsfromlayer("costs")
-        if not options['outrounds']:
-            options['outrounds'] = 0
-        elif flags['s'] and options['outrounds'] > 0:
-            world.addsequencenumber = True
-
-        if options['lowcostlimit']:
-            world.minpenalty = int(options['lowcostlimit'])
-        if options['highcostlimit']:
-            world.maxpenalty = int(options['highcostlimit'])
-        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']:
-            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['evaluateposition']:
-            world.evaluationbase = str(options['evaluateposition'])
-#        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..
-
-#        world.checkvalues()
-    except error.DataError:
-        grass.fatal("Failed to parse args..")
-        sys.exit(1)
-    letantsdance(int(options['rounds']), int(options['outrounds']))
-    grass.message("FINISH")
-
-if __name__ == "__main__":
-    options, flags = grass.parser()
-    main()
-

Copied: grass-addons/grass7/raster/r.agent.aco/r.agent.aco.html (from rev 65699, grass-addons/grass7/raster/r.agent/r.agent.html)
===================================================================
--- grass-addons/grass7/raster/r.agent.aco/r.agent.aco.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.agent.aco/r.agent.aco.html	2015-07-20 11:47:11 UTC (rev 65700)
@@ -0,0 +1,98 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.agent</em> shall provide an inital base for organizing worlds
+with raster playgrounds and agents in.
+<p>
+As this is only a first implementation the encapsulation that grants
+a modular usage is still suffering..
+<p>
+<em>r.agent</em> is written in python for more transparency and
+better extendability.
+<p>
+As a first world example there is an ACO-based environment
+(see <a href="http://en.wikipedia.org/wiki/Ant_colony_optimization_algorithms">Ant Colony Optimization</a>)
+available (in <em>libold</em> see below..).
+<p>
+The basic concept of such an ACO world, is to take some cost surface
+and transform it to a penalty layer - where for human ants for example
+this penalty layer may be expressed by the walking velocity, e.g. calculated
+with the algorithm proposed by
+<a href="http://www.geodyssey.com/papers/tobler93.html">Tobler1993</a>.
+The actors on the playground will wander around on the playground
+using the time for their paths that correspond with the values in the penalty
+grid.
+If they find some attractor, they walk home to the position they
+originated, marking their way with pheromones. While this
+pheromone vanishes over time, the following agents are more likely
+to choose their next steps to a position that smells most.
+<p>
+This first toolset was mainly developed for
+<a href="http://www.topoi.org/group/a-iii-4/">Topoi Project A-III-4</a>,
+with some
+inspirations from previous work conducted at the
+<a href="http://www.unibe.ch/">Uni Bern</a> in 2008.
+
+
+<h2>NOTES</h2>
+
+The state of this software is: "first do it".
+This is work in progress and its structure will probably change quite a lot
+in the future due to a better integration in GRASS and other refactoring.
+<p>
+The refactoring causes <em>r.agent</em> to not work for the moment. The general
+behaviour can be seen by changeing the imports in <em>r.agent.aco</em>
+to the old library <em>libold</em> -- <em>libagent</em> is coming soon.
+<p>
+Unfortunately the colletion is not very well included in GRASS yet.
+At the moment <em>libold</em> only handles ASCII grid and vector in- and
+output data. With a better GRASS integration (arrays) not only will the code
+get lighter but also these drawbacks shall vanish.
+<p>
+ACO works best on dynamic maps -- it constantly tries to improve paths...
+
+
+<h2>EXAMPLE</h2>
+
+A fictive usecase could look something like this
+(note: at the moment the
+in- and output variables with <em>libold</em>, are still ascii-files):
+<p>
+<div class="code"><pre>
+r.agent.aco outputmap=out.map penaltymap=testpenalty.grid \
+  sitesmap=sites.vect rounds=100 outrounds=100 volatilizationtime=5000 \
+  antslife=2000 maxants=400 pathintensity=1000000
+</pre></div>
+<p>
+For running the total test suite on the libraries,
+i.e. to run all the tests that are at the end
+of each python file, use this test collection
+(for certain tests, the following files must exist though:
+ "elev.grid", and "arch.vect"):
+<p>
+<div class="code"><pre>
+user at host:~$ cd /<pathtoaddons>/r.agent/libagent
+
+user at host:libold$ ./alltests.py
+</pre></div>
+
+<h2>TODO</h2>
+
+Integrate it directly within grass.
+<p>
+Improve encapsulation of classes.
+<p>
+Find good parameters, or parameter finding strategies for the ACO part.
+Try to avoid high penalty fields.
+Think about heuristics too.
+<p>
+Implement other ABM scenarios.
+
+<h2>SEE ALSO</h2>
+
+<h2>AUTHORS</h2>
+
+Michael Lustenberger inofix.ch
+
+<p><i>Last changed: $Date$</i>
+
+

Copied: grass-addons/grass7/raster/r.agent.aco/r.agent.aco.py (from rev 65699, grass-addons/grass7/raster/r.agent/r.agent.aco)
===================================================================
--- grass-addons/grass7/raster/r.agent.aco/r.agent.aco.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.agent.aco/r.agent.aco.py	2015-07-20 11:47:11 UTC (rev 65700)
@@ -0,0 +1,381 @@
+#!/usr/bin/env python
+"""
+MODULE:       r.agent.aco
+AUTHOR(S):    michael lustenberger inofix.ch
+PURPOSE:      r.agent.aco is used to organize ant-like agents in a raster
+              based playground. As decribed by the Ant Colony Optimization
+              algorithm, the ants wander around looking for attractors,
+              marking their paths if they find any.
+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.
+"""
+
+##TODO it is time to make this all multithreaded..
+
+#%Module
+#% description: Agents wander around on the terrain, marking paths to new locations.
+#%End
+#%option
+#% key: outputmap
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Name of pheromone output map
+#% required : yes
+#%end
+#%option
+#% key: inputmap
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Name of input pheromone raster map (e.g. from prior run)
+#% required : no
+#%end
+#%flag
+#% key: p
+#% description: Allow overwriting existing pheromone maps
+#%end
+#%flag
+#% key: s
+#% description: Produce a sequence of pheromone maps (by appending a number)
+#%end
+#%flag
+#% key: c
+#% description: Overwrite existing cost map (only used with penalty conversion)
+#%end
+#%option
+#% key: costmap
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Name of penalty resp. cost raster map (note conversion checkbox)
+#% required : yes
+#%end
+#%flag
+#% key: a
+#% description: Auto-convert cost (slope..) to penalty map (using "tobler", see docu)
+#%end
+#%option
+#% key: sitesmap
+#% type: string
+#% gisprompt: old,vect,vector
+#% description: Name of sites map, vector data with possible points of origin
+#% required : yes
+#%end
+#%option
+#% key: rounds
+#% type: integer
+#% gisprompt: number
+#% description: Number of iterations/rounds to run
+#% answer: 999
+#% options: 0-999999
+#% required : yes
+#%end
+#%option
+#% key: outrounds
+#% type: integer
+#% gisprompt: number
+#% description: Produce output after running this number of iterations/rounds
+#% options: 0-999999
+#% required : no
+#%end
+#%option
+#TODO evaluate..
+#% key: targetvisibility
+#% type: integer
+#% gisprompt: number
+#% description: Distance to target from where it might be 'sensed'
+#% options: 0-999999
+#% required : no
+#%end
+#%option
+#% key: highcostlimit
+#% type: integer
+#% gisprompt: number
+#% description: Penalty values above this point an ant considers as illegal/bogus when in 'costlymarked' modus
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: lowcostlimit
+#% type: integer
+#% gisprompt: number
+#% description: Penalty values below this point an ant considers as illegal/bogus when in 'costlymarked' modus
+#% options: -99999-99999
+#% required : no
+#%end
+#%option
+#% key: maxpheromone
+#% type: integer
+#% gisprompt: number
+#% description: Absolute maximum of pheromone intensity a position may have
+#% options: <minpheromone>-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: minpheromone
+#% type: integer
+#% gisprompt: number
+#% description: Absolute minimum of pheromone intensity to leave on playground
+#% options: 0-<maxpheromone>
+#% required : no
+#%end
+#%option
+#% key: volatilizationtime
+#% type: integer
+#% gisprompt: number
+#% description: Half-life for pheromone to volatize (e.g. =rounds)
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: stepintensity
+#% type: integer
+#% gisprompt: number
+#% description: Pheromone intensity to leave on each step
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: pathintensity
+#% type: integer
+#% gisprompt: number
+#% description: Pheromone intensity to leave on found paths
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: maxants
+#% type: integer
+#% gisprompt: number
+#% description: Maximum amount of ants that may live concurrently (x*y)
+#% options: 0-<the bigger the playground, the more space they have>
+#% required : no
+#%end
+#%option
+#% key: antslife
+#% type: integer
+#% gisprompt: number
+#% description: Time to live for an ant (e.g. four times points distance)
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: decisionalgorithm
+#% type: string
+#% gisprompt: algorithm
+#% description: Algorithm used for walking step
+#% answer: standard
+#% options: standard,marked,costlymarked,random,test
+#% required : yes
+#%end
+#%option
+#% key: evaluateposition
+#% type: string
+#% gisprompt: algorithm
+#% description: Algorithm used for finding and remembering paths
+#% answer: avoidorforgetloop
+#% options: standard,avoidloop,forgetloop,avoidorforgetloop
+#% required : yes
+#%end
+#%option
+#% key: agentfreedom
+#% type: integer
+#% gisprompt: number
+#% description: Number of possible directions the ant can take (4 or 8)
+#% options: 4,8
+#% required : no
+#%end
+#%option
+#% key: pheromoneweight
+#% type: integer
+#% gisprompt: number
+#% description: How is the pheromone value (P) weighted when walking (p*P:r*R:c*C)
+#% answer: 1
+#% options: 0-99999
+#% required : yes
+#%end
+#%option
+#% key: randomnessweight
+#% type: integer
+#% gisprompt: number
+#% description: How is the random value (R) weighted when walking (p*P:r*R:c*C)
+#% answer: 1
+#% options: 0-99999
+#% required : yes
+#%end
+#%option
+#% key: costweight
+#% type: integer
+#% gisprompt: number
+#% description: How is the penalty value (C) weighted when walking (p*P:r*R:c*C)
+#% answer: 0
+#% options: 0-99999
+#% required : yes
+#%end
+
+import sys
+from sys  import exit, maxsize
+from math import sqrt
+from math import exp
+from random import randint
+import grass.script as grass
+from grass.pygrass.utils import get_lib_path
+
+path = get_lib_path(modname='r.agent.aco', libname='libagent')
+if path is None:
+    grass.fatal("Not able to find the agent library directory.")
+sys.path.append(path)
+
+import error, grassland, anthill
+
+try:
+    from grass.script import core as grass
+except ImportError:
+    raise error.EnvError("r.agent.aco:", "Please run inside GRASS.")
+
+world = anthill.Anthill(grassland.Grassland())
+
+def setmaps(site, cost, wastecosts, inphero, outphero, wastephero):
+    """
+    Set the user maps in place
+    """
+    if site:
+        if -1 == site.find('@'):
+            site = site + '@' + grass.gisenv()['MAPSET']
+        # set sitemap and site list
+        world.sites = world.playground.parsevectorlayer(anthill.Anthill.SITE,
+                                                        site, -1, True)
+        if not world.sites:
+            raise error.DataError("r.agent.aco:",
+                                    "There were no sites in" + site)
+    else:
+        raise error.DataError("r.agent.aco:", "The site map is mandatory.")
+    if cost:
+        if -1 == cost.find('@'):
+            cost = cost + '@' + grass.gisenv()['MAPSET']
+        # set cost/penalty layer
+        world.playground.setgrasslayer(anthill.Anthill.COST, cost, True)
+        world.overwritepenalty = wastecosts
+    else:
+        raise error.DataError("r.agent.aco:", "The cost map is mandatory.")
+    if outphero:
+        if -1 == outphero.find('@'):
+            outphero = outphero + '@' + grass.gisenv()['MAPSET']
+    else:
+        raise error.DataError("r.agent.aco:", "The output map is mandatory.")
+    if inphero == outphero:
+        if not wastephero:
+            raise error.DataError("aco:", "May not overwrite the output map.")
+    if inphero:
+        if -1 == inphero.find('@'):
+            inphero = inphero + '@' + grass.gisenv()['MAPSET']
+        world.playground.setgrasslayer(anthill.Anthill.RESULT, inphero, True)
+    world.playground.grassmapnames[anthill.Anthill.RESULT] = outphero
+    #TODO hopefully not really needed - workaround for broken(?) garray
+    world.playground.createlayer("copy", outphero, True)
+    world.playground.grassmapnames["copy"] = outphero
+    world.overwritepheormone = wastephero
+
+def letantsdance(rounds, outrounds):
+    """
+    Organize the agents and the pheromone on the playground.
+    """
+    if world.addsequencenumber:
+        outputmapbasename = \
+            world.playground.grassmapnames[anthill.Anthill.RESULT].split("@")
+    else:
+        outputmapbasename = False
+        outputmapname = False
+    if 0 < outrounds < rounds:
+        # calculate when to write output
+        mainloops = rounds / outrounds
+        nextwrite = outrounds
+    else:
+        # produce output only at the end
+        mainloops = 1
+        nextwrite = rounds
+    run = 0
+    while run < mainloops:
+        if outputmapbasename:
+            outputmapname = outputmapbasename[0] + str(run) + "@" + \
+                            outputmapbasename[1]
+        # loop and write out the contents at the end
+        world.letantsdance(nextwrite)
+        # Print the number of found paths
+        grass.info("Number of found paths: " + str(world.numberofpaths))
+        # export the value maps
+        #TODO hopefully not really needed - workaround for broken(?) garray
+        for i in range(len(world.playground.layers[anthill.Anthill.RESULT])):
+            for j in range(len(
+                    world.playground.layers[anthill.Anthill.RESULT][0])):
+                world.playground.layers["copy"][i][j] = \
+                        world.playground.layers[anthill.Anthill.RESULT][i][j]
+        world.playground.writelayer("copy", outputmapname,
+                                            world.overwritepheormone)
+        #TODO world.playground.writelayer(anthill.Anthill.RESULT, False,
+        #TODO                                    world.overwritepheormone)
+#        print "nrofpaths:", world.nrop
+        # count down outer
+        run += 1
+#    print "nrofrounds", nrofrounds
+
+def main():
+
+    try:
+        setmaps(options['sitesmap'],
+            options['costmap'], flags['c'],
+            options['inputmap'], options['outputmap'], flags['p'])
+
+#        world.playground.setboundsfromlayer("costs")
+        if not options['outrounds']:
+            options['outrounds'] = 0
+        elif flags['s'] and options['outrounds'] > 0:
+            world.addsequencenumber = True
+
+        if options['lowcostlimit']:
+            world.minpenalty = int(options['lowcostlimit'])
+        if options['highcostlimit']:
+            world.maxpenalty = int(options['highcostlimit'])
+        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']:
+            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['evaluateposition']:
+            world.evaluationbase = str(options['evaluateposition'])
+#        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..
+
+#        world.checkvalues()
+    except error.DataError:
+        grass.fatal("Failed to parse args..")
+        sys.exit(1)
+    letantsdance(int(options['rounds']), int(options['outrounds']))
+    grass.message("FINISH")
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()
+

Deleted: grass-addons/grass7/raster/r.agent.aco/r.agent.html
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.html	2015-07-20 11:19:53 UTC (rev 65699)
+++ grass-addons/grass7/raster/r.agent.aco/r.agent.html	2015-07-20 11:47:11 UTC (rev 65700)
@@ -1,98 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<em>r.agent</em> shall provide an inital base for organizing worlds
-with raster playgrounds and agents in.
-<p>
-As this is only a first implementation the encapsulation that grants
-a modular usage is still suffering..
-<p>
-<em>r.agent</em> is written in python for more transparency and
-better extendability.
-<p>
-As a first world example there is an ACO-based environment
-(see <a href="http://en.wikipedia.org/wiki/Ant_colony_optimization_algorithms">Ant Colony Optimization</a>)
-available (in <em>libold</em> see below..).
-<p>
-The basic concept of such an ACO world, is to take some cost surface
-and transform it to a penalty layer - where for human ants for example
-this penalty layer may be expressed by the walking velocity, e.g. calculated
-with the algorithm proposed by
-<a href="http://www.geodyssey.com/papers/tobler93.html">Tobler1993</a>.
-The actors on the playground will wander around on the playground
-using the time for their paths that correspond with the values in the penalty
-grid.
-If they find some attractor, they walk home to the position they
-originated, marking their way with pheromones. While this
-pheromone vanishes over time, the following agents are more likely
-to choose their next steps to a position that smells most.
-<p>
-This first toolset was mainly developed for
-<a href="http://www.topoi.org/group/a-iii-4/">Topoi Project A-III-4</a>,
-with some
-inspirations from previous work conducted at the
-<a href="http://www.unibe.ch/">Uni Bern</a> in 2008.
-
-
-<h2>NOTES</h2>
-
-The state of this software is: "first do it".
-This is work in progress and its structure will probably change quite a lot
-in the future due to a better integration in GRASS and other refactoring.
-<p>
-The refactoring causes <em>r.agent</em> to not work for the moment. The general
-behaviour can be seen by changeing the imports in <em>r.agent.aco</em>
-to the old library <em>libold</em> -- <em>libagent</em> is coming soon.
-<p>
-Unfortunately the colletion is not very well included in GRASS yet.
-At the moment <em>libold</em> only handles ASCII grid and vector in- and
-output data. With a better GRASS integration (arrays) not only will the code
-get lighter but also these drawbacks shall vanish.
-<p>
-ACO works best on dynamic maps -- it constantly tries to improve paths...
-
-
-<h2>EXAMPLE</h2>
-
-A fictive usecase could look something like this
-(note: at the moment the
-in- and output variables with <em>libold</em>, are still ascii-files):
-<p>
-<div class="code"><pre>
-r.agent.aco outputmap=out.map penaltymap=testpenalty.grid \
-  sitesmap=sites.vect rounds=100 outrounds=100 volatilizationtime=5000 \
-  antslife=2000 maxants=400 pathintensity=1000000
-</pre></div>
-<p>
-For running the total test suite on the libraries,
-i.e. to run all the tests that are at the end
-of each python file, use this test collection
-(for certain tests, the following files must exist though:
- "elev.grid", and "arch.vect"):
-<p>
-<div class="code"><pre>
-user at host:~$ cd /<pathtoaddons>/r.agent/libagent
-
-user at host:libold$ ./alltests.py
-</pre></div>
-
-<h2>TODO</h2>
-
-Integrate it directly within grass.
-<p>
-Improve encapsulation of classes.
-<p>
-Find good parameters, or parameter finding strategies for the ACO part.
-Try to avoid high penalty fields.
-Think about heuristics too.
-<p>
-Implement other ABM scenarios.
-
-<h2>SEE ALSO</h2>
-
-<h2>AUTHORS</h2>
-
-Michael Lustenberger inofix.ch
-
-<p><i>Last changed: $Date$</i>
-
-



More information about the grass-commit mailing list