[GRASS-SVN] r65840 - in grass-addons/grass7/raster: . r.mcda.topsis

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Aug 5 14:18:44 PDT 2015


Author: gianluca
Date: 2015-08-05 14:18:44 -0700 (Wed, 05 Aug 2015)
New Revision: 65840

Added:
   grass-addons/grass7/raster/r.mcda.topsis/
   grass-addons/grass7/raster/r.mcda.topsis/Makefile
   grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.html
   grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.py
Log:
topsis addons - first implements

Added: grass-addons/grass7/raster/r.mcda.topsis/Makefile
===================================================================
--- grass-addons/grass7/raster/r.mcda.topsis/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.mcda.topsis/Makefile	2015-08-05 21:18:44 UTC (rev 65840)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.mcda.topsis
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.html
===================================================================
--- grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.html	2015-08-05 21:18:44 UTC (rev 65840)
@@ -0,0 +1,21 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.mcda.topsis</em> implements the ideal point algorithms based on TOPSIS model and returns a raster map shown the ranking geospatial alternatives. The user has to provide the weight values and preference (gain or cost) directly
+
+<h2>NOTES</h2>
+<p> For bug please contact Gianluca Massei (g_mass at libero.it)</P>
+
+
+<h2>REFERENCE</h2>
+<ol>
+	<li><p>Hwang C. L. and Yoon K. Multiple Objective Decision Making Methods and Applications, A State-of-the-Art Survey.Springer - Verlag , 1981.</</ol>
+
+<h2>SEE ALSO</h2>
+<p><em>r.mcda.fuzzy, r.mcda.electre, r.mcda.roughset, r.to.drsa, r.in.drsa</em></P>
+
+<h2>AUTHORS</h2>
+Antonio Boggia - Gianluca Massei<br>
+Department of Economics and Appraisal - University of Perugia - Italy 
+
+<p>
+<i>Last changed: $Date: 2014-01-21 23:14:02 +0100 (mar, 21 gen 2014) $</i>

Added: grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.py
===================================================================
--- grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.mcda.topsis/r.mcda.topsis.py	2015-08-05 21:18:44 UTC (rev 65840)
@@ -0,0 +1,160 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:	   	r.mcda.topsis
+# AUTHOR:	   	Gianluca Massei - Antonio Boggia
+# PURPOSE:	  	Generate a MCDA map based on TOPSIS algorthm. 
+#				Based on Hwang C. L. and Yoon K. Multiple Objective Decision 
+#				Making Methods and Applications, A State-of-the-Art Survey . 
+#				Springer - Verlag , 1981.	 
+# COPYRIGHT:  c) 2015 Gianluca Massei, Antonio Boggia  and the GRASS 
+#				Development Team. This program is free software under the 
+#				GNU General PublicLicense (>=v2). Read the file COPYING 
+#				that comes with GRASS for details.
+#
+#############################################################################
+
+#%Module
+#% description: Generates a MCDA map based on TOPSIS algorthm.
+#% keyword: raster
+#% keyword: Multi Criteria Decision Analysis (MCDA)
+#%End
+#%option
+#% key: criteria
+#% type: string
+#% multiple: yes
+#% gisprompt: old,cell,raster
+#% key_desc: name
+#% description: Name of criteria raster maps 
+#% required: yes
+#%end
+#%option
+#% key: preferences
+#% type: string
+#% key_desc: character
+#% description: preference (gain,cost)
+#% required: yes
+#%end
+#%option
+#% key: weights
+#% type: double
+#% description: weights (w1,w2,...,wn)
+#% multiple: yes
+#% required: yes
+#%end
+#%option
+#% key: topsismap   
+#% type: string
+#% gisprompt: new_file,cell,output
+#% description: Ranked raster map
+#% required: yes
+#%end
+
+
+import sys
+import grass.script as gscript
+#import grass.script.array as garray
+from time import time  
+
+def standardizedNormalizedMatrix(attributes,weights): #step1 and step2
+	criteria=[]
+	for criterion,weight in zip(attributes,weights):
+		critPow="critPow=pow(%s,2)" % (criterion)
+		gscript.mapcalc(critPow,overwrite='True')
+		stats=gscript.parse_command("r.univar",map='critPow',flags='g')
+		nameMap="_%s" % criterion
+		mapalgebra="%s=(%s/sqrt(%s))*%s" % (nameMap,criterion,stats['sum'],weight)
+		gscript.mapcalc(mapalgebra,overwrite='True')
+		criteria.append(nameMap)
+	return criteria
+
+
+def idealPoints(criteria,preference): #step3
+	idelaPointsList=[]
+	for c,p in zip(criteria,preference):
+		stats=gscript.parse_command("r.univar",map=c,flags='g')
+		if p=="gain":
+			ip=float(stats['max'])
+		elif p=="cost":
+			ip=float(stats['min'])
+		else:
+			ip=-9999
+			print "warning! %s doesn't compliant" % p
+		idelaPointsList.append(ip)
+	return idelaPointsList
+
+def worstPoints(criteria,preference):
+	worstPointsList=[]
+	for c,p in zip(criteria,preference):
+		stats=gscript.parse_command("r.univar",map=c,flags='g')
+		if p=="gain":
+			wp=float(stats['min'])
+		elif p=="cost":
+			wp=float(stats['max'])
+		else:
+			wp=-9999
+			print "warning! %s doesn't compliant" % p
+		worstPointsList.append(wp)
+	return worstPointsList
+	  
+def idealPointDistance(idelaPointsList,criteria): #step4a
+	distance=[]
+	i=0
+	for c,ip in zip(criteria,idelaPointsList):
+		mapname="tmap_%s" % i
+		mapalgebra1="%s=pow((%s-%s),2)" % (mapname,c,ip)
+		print mapalgebra1
+		gscript.mapcalc(mapalgebra1,overwrite='True')
+		distance.append(mapname)
+		i=i+1
+	mapalgebra2="IdealPointDistance=sqrt(%s)" % ("+".join(distance))
+	print mapalgebra2
+	gscript.mapcalc(mapalgebra2,overwrite='True')
+	gscript.run_command("g.remove", flags='f', type='raster', name=",".join(distance))
+	return 0
+
+def worstPointDistance(worstPointsList,criteria): #step4b
+	distance=[]
+	i=0
+	for c,wp in zip(criteria,worstPointsList):
+		mapname="tmap_%s" % i
+		mapalgebra1="%s=pow((%s-%s),2)" % (mapname,c,wp)
+		gscript.mapcalc(mapalgebra1,overwrite='True')
+		distance.append(mapname)
+		i=i+1
+	mapalgebra2="WorstPointDistance=sqrt(%s)" % ("+".join(distance))
+	gscript.mapcalc(mapalgebra2,overwrite='True')
+	gscript.run_command("g.remove", flags='f', type='raster', name=",".join(distance))
+
+
+def relativeCloseness(): #step5
+	pass
+
+
+	
+def main():
+	"main function for TOPSIS algorithm"
+	#try:
+	start=time()
+	attributes=options['criteria'].split(',')
+	preferences=options['preferences'].split(',')
+	weights=options['weights'].split(',')
+	topsismap = options['topsismap']
+
+	criteria=standardizedNormalizedMatrix(attributes,weights)
+	idelaPointsList=idealPoints(criteria,preferences)
+	worstPointsList=worstPoints(criteria,preferences)
+
+	idealPointDistance(idelaPointsList,criteria)
+	worstPointDistance(worstPointsList,criteria)
+	relativeCloseness()
+	gscript.run_command("g.remove", flags='f', type='raster', name=",".join(criteria))
+	end=time()
+	print "Time computing-> %.4f s" % (end-start)
+	
+	
+if __name__ == "__main__":
+    options, flags = gscript.parser()
+    main()
+
+


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



More information about the grass-commit mailing list