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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Nov 11 08:44:29 PST 2015


Author: mic
Date: 2015-11-11 08:44:29 -0800 (Wed, 11 Nov 2015)
New Revision: 66787

Added:
   grass-addons/grass7/raster/r.agent/
   grass-addons/grass7/raster/r.agent/r.agent.aco/
   grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.html
   grass-addons/grass7/raster/r.agent/r.agent.rand/
   grass-addons/grass7/raster/r.agent/r.agent.rand/r.agent.rand.py
Removed:
   grass-addons/grass7/raster/r.agent.aco/
   grass-addons/grass7/raster/r.agent/r.agent.aco.html
Log:
reorganizing r.agent module by copying the structure of r.modis

Copied: grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.html (from rev 66786, grass-addons/grass7/raster/r.agent.aco/r.agent.aco.html)
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco/r.agent.aco.html	2015-11-11 16:44:29 UTC (rev 66787)
@@ -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>
+
+

Deleted: grass-addons/grass7/raster/r.agent/r.agent.aco.html
===================================================================
--- grass-addons/grass7/raster/r.agent.aco/r.agent.aco.html	2015-11-11 16:38:17 UTC (rev 66786)
+++ grass-addons/grass7/raster/r.agent/r.agent.aco.html	2015-11-11 16:44:29 UTC (rev 66787)
@@ -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>
-
-

Added: grass-addons/grass7/raster/r.agent/r.agent.rand/r.agent.rand.py
===================================================================
--- grass-addons/grass7/raster/r.agent/r.agent.rand/r.agent.rand.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.agent/r.agent.rand/r.agent.rand.py	2015-11-11 16:44:29 UTC (rev 66787)
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+"""
+MODULE:       r.agent.rand
+AUTHOR(S):    michael lustenberger inofix.ch
+PURPOSE:      r.agent.rand is used to get simple agents wander around
+              in a raster based playground just by chance. This is the
+              most basic application for the libant library (i.e. a test).
+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 step output map
+#% required : yes
+#%end
+#%flag
+#% key: p
+#% description: Allow overwriting existing output maps
+#%end
+#%option
+#% key: costmap
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Name of penalty resp. cost raster map (note conversion checkbox)
+#% 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: mark
+#% type: integer
+#% gisprompt: number
+#% description: Mark each step an agent takes
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%end
+#%option
+#% key: maxagents
+#% type: integer
+#% gisprompt: number
+#% description: Maximum amount of agents that may live concurrently (x*y)
+#% options: 0-<the bigger the playground, the more space they have>
+#% required : no
+#%end
+#%option
+#% key: agentslife
+#% type: integer
+#% gisprompt: number
+#% description: Time to live for an agent
+#% options: 0-<max integer on system would make sense>
+#% required : no
+#%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
+
+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.rand', libname='libagent')
+if path is None:
+    grass.fatal("Not able to find the agent library directory.")
+sys.path.append(path)
+
+import error, grassland, world
+
+try:
+    from grass.script import core as grass
+except ImportError:
+    raise error.EnvError("r.agent.rand:", "Please run inside GRASS.")
+
+world = world.World(grassland.Grassland())
+
+def setmaps(cost, output):
+    """
+    Set the user maps in place
+    """
+    if cost:
+        if -1 == cost.find('@'):
+            cost = cost + '@' + grass.gisenv()['MAPSET']
+        # set cost/penalty layer
+        world.playground.setgrasslayer('COST', cost, True)
+    else:
+        raise error.DataError("r.agent.rand:", "The cost map is mandatory.")
+    if output:
+        if -1 == output.find('@'):
+            output = output + '@' + grass.gisenv()['MAPSET']
+    else:
+        raise error.DataError("r.agent.rand:", "The output map is mandatory.")
+    world.playground.grassmapnames['RESULT'] = output
+    #TODO hopefully not really needed - workaround for broken(?) garray
+    world.playground.createlayer("copy", output, True)
+    world.playground.grassmapnames["copy"] = output
+
+def run(rounds, maxagents, agentlife, mark, overwrite):
+    """
+    Organize the agents on the playground.
+    """
+
+    for i in range(rounds):
+        if i in range(maxagents - len(world.agents)):
+            world.bear(agentlife)
+        for i in range(len(world.agents)):
+            agent = world.agents[i]
+            agent.step()
+            position = agent.getposition()
+            newvalue = world.playground.getcellvalue('RESULT', position) + mark
+            world.playground.setcellvalue('RESULT', positon, newvalue)
+        run += 1
+
+    world.playground.writelayer("copy")
+
+def main():
+    try:
+        setmaps(options['costmap'], options['outputmap'])
+    except error.DataError:
+        grass.fatal("Failed to parse args..")
+        sys.exit(1)
+    run(int(options['rounds']), options['maxants'], options['antslife'],
+            options['mark'], flags['p'])
+    grass.message("FINISH")
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    main()
+


Property changes on: grass-addons/grass7/raster/r.agent/r.agent.rand/r.agent.rand.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list