[GRASS-SVN] r41275 - grass-addons/raster/r.fuzzy.logic

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Mar 3 01:57:38 EST 2010


Author: jarekj71
Date: 2010-03-03 01:57:38 -0500 (Wed, 03 Mar 2010)
New Revision: 41275

Added:
   grass-addons/raster/r.fuzzy.logic/Makefile
   grass-addons/raster/r.fuzzy.logic/description.html
   grass-addons/raster/r.fuzzy.logic/local_proto.h
   grass-addons/raster/r.fuzzy.logic/logic.c
   grass-addons/raster/r.fuzzy.logic/main.c
Log:
"files for fuzzy logic"


Added: grass-addons/raster/r.fuzzy.logic/Makefile
===================================================================
--- grass-addons/raster/r.fuzzy.logic/Makefile	                        (rev 0)
+++ grass-addons/raster/r.fuzzy.logic/Makefile	2010-03-03 06:57:38 UTC (rev 41275)
@@ -0,0 +1,15 @@
+# fix this relative to include/
+# or use absolute path to the GRASS source code
+MODULE_TOPDIR = ../..
+
+PGM = r.fuzzy.logic
+
+LIBES     = $(VECTLIB) $(DBMILIB) $(GISLIB)
+DEPENDENCIES = $(VECTDEP) $(DBMIDEP) $(GISDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS) 
+DEPENDENCIES = $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd


Property changes on: grass-addons/raster/r.fuzzy.logic/Makefile
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy.logic/description.html
===================================================================
--- grass-addons/raster/r.fuzzy.logic/description.html	                        (rev 0)
+++ grass-addons/raster/r.fuzzy.logic/description.html	2010-03-03 06:57:38 UTC (rev 41275)
@@ -0,0 +1,69 @@
+<h2>OPTIONS</h2>
+<DT><b>xmap</b></DT>
+<DD>Name of input x membership operand. This map must be of type FCELL with range 0 :1 and may require null values. Otherwise program will print error message and stops.
+</DD>
+<p>
+<DT><b>xmap</b></DT>
+<DD>Name of input y membership operand. This map must be of type FCELL with range 0 :1 and may require null values. Otherwise program will print error message and stops. This map is optional bit is required for all operation except NOT
+</DD>
+<p>
+<DT><b>operator</b></DT>
+<DD>A fuzzy set operators are generalization of crisp operators. There is more than one possible generalization of every opeartor. There are three operations: fuzzy complements, fuzzy intersections, and fuzzy unions. Addational implication operator is also provided.
+<ul>
+<li>fuzzy intersection (<B>AND</B>) use T-norm of given family for calculation;
+<li>fuzzy union (<B>OR</B>) use T-conorm of given family for calculation;
+<li>fuzzy complement (<B>NOT</B>) fuzzy negation ussualy 1-x;
+<li>fuzzy implication (<B>IMP</B>) use residuum of given family if available;
+</ul>
+</DD>
+<p>
+
+<DT><b>family</b></DT>
+<DD>T-norms, T-conorms and residuals are a generalization of the two-valued logical conjunction,  disjunction and implication used by boolean logic, for fuzzy logics. Because there is more than one possible generalisation of logial operations, r.fuzzy.logic provides 6 most popular families for fuzzy operations:
+<ul>
+<li><b>Zadeh</b> with minimum (Godel) t-norm and maximum T-conorm;
+<li><b>product</b> with product T-norm and probabilistic sum as T-conorm;
+<li><b>drastic</b> with drastic T-norm and drastic T-conorm;
+<li><b>Łukasiewicz</b> with Łukasiewicz T-norm and bounded sum as a T-conorm;
+<li><b>Fodor</b> with nilpotent minimum as T-norm and nilpotent maximum as T-conorm;
+<li><b>Hamacher</b> (simplified) with Hamacher product as T-norm and Einstein sum as T-conorm;
+</ul>
+There is no residuum for drastic and Hamacher families.
+For more details see <a href="http://www.jstatsoft.org/v31/i02">Meyer D, Hornik K (2009)</a>; <a href="http://en.wikipedia.org/wiki/T-norm">T-norms</a>; 
+</DD>
+<p>
+
+
+<h2>OUTPUTS</h2>
+<DT><b>output</b></DT>
+<DD>Map containing result of two-values operations. Multivalued operations will be avaialbel in the future. Map is always of type FCELLS and contains values from 0 (no membership) to 1 (full membership). Values between 0 and 1 indicate partial membership</DD>
+<p>
+
+
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="r.fuzzy.html">r.fuzzy</a>,
+<a href="r.mapcalc.html">r.mapcalc</a>,
+</em>
+
+
+
+<h2>REFERENCES</h2>
+
+Zadeh, L.A. (1965). "Fuzzy sets". Information and Control 8 (3): 338–353. doi:10.1016/S0019-9958(65)90241-X. ISSN 0019-9958.<P>
+
+Novák, Vilém (1989). Fuzzy Sets and Their Applications. Bristol: Adam Hilger. ISBN 0-85274-583-4.<P>
+
+Klir, George J.; Yuan, Bo (1995). Fuzzy sets and fuzzy logic: theory and applications. Upper Saddle River, NJ: Prentice Hall PTR. ISBN 0-13-101171-5.<P>
+
+Klir, George J.; St Clair, Ute H.; Yuan, Bo (1997). Fuzzy set theory: foundations and applications. Englewood Cliffs, NJ: Prentice Hall. ISBN 0133410587.<P>
+
+Meyer D, Hornik K (2009a). \Generalized and Customizable Sets in R." Journal of Statistical Software, 31(2), 1{27. URL http://www.jstatsoft.org/v31/i02/.<P>
+
+Meyer D, Hornik K (2009b). sets: Sets, Generalized Sets, and Customizable Sets. R~package version~1.0, URL http://CRAN.R-project.org/package=sets.<P>
+
+<h2>AUTHOR</h2>
+Jarek  Jasiewicz
+
+


Property changes on: grass-addons/raster/r.fuzzy.logic/description.html
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy.logic/local_proto.h
===================================================================
--- grass-addons/raster/r.fuzzy.logic/local_proto.h	                        (rev 0)
+++ grass-addons/raster/r.fuzzy.logic/local_proto.h	2010-03-03 06:57:38 UTC (rev 41275)
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#define _AND	1
+#define _OR		2
+#define _NOT	3
+#define _IMP	4
+
+#define ZADEH 			1
+#define PRODUCT			2	
+#define DRASTIC			3
+#define LUKASIEWICZ	4
+#define FODOR				5
+#define HAMACHER		6
+
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+
+
+float f_and(float cellx, float celly, int family);
+float f_or(float cellx, float celly, int family);
+float f_imp(float cellx, float celly, int family);
+float f_not(float cellx, int family);
+


Property changes on: grass-addons/raster/r.fuzzy.logic/local_proto.h
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy.logic/logic.c
===================================================================
--- grass-addons/raster/r.fuzzy.logic/logic.c	                        (rev 0)
+++ grass-addons/raster/r.fuzzy.logic/logic.c	2010-03-03 06:57:38 UTC (rev 41275)
@@ -0,0 +1,105 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+float f_and(float x, float y, int family) {
+
+	switch (family) {
+		
+			case ZADEH:
+		return MIN(x,y);
+			break;
+		
+			case PRODUCT:
+		return x*y;
+			break;	
+		
+			case DRASTIC:
+		return MAX(x, y) == 1 ? MIN(x, y) : 0;
+			break;	
+		
+			case LUKASIEWICZ:
+		return MAX((x+y-1),0);
+			break;
+		
+			case FODOR:
+		return (x+y)>1 ? MIN(x,y) : 0;	
+			break;
+			
+			case HAMACHER:
+		return (x==y==0) ? 0 : (x*y)/((x+y)-x*y);	
+			break;
+			
+
+				
+	}
+	return -1; /* error */
+}
+
+float f_or(float x, float y, int family) {
+			float b;
+	switch (family) {
+		
+			case ZADEH:
+		return MAX(x,y);
+			break;
+		
+			case PRODUCT:
+		return x + y -x * y;
+			break;	
+		
+			case DRASTIC:
+		return (MIN(x, y) == 0) ? MAX(x, y) : 1;
+			break;	
+		
+			case LUKASIEWICZ:
+		return MIN((x+y),1);
+			break;
+	
+			case FODOR:
+		return (x+y<1) ? MAX(x,y) : 1;		
+			break;
+			
+			case HAMACHER:
+		return (x+y)/(1+x*y);
+			break;
+	}
+	return -1; /* error */
+}
+
+float f_imp(float x, float y, int family) {
+		
+	switch (family) {
+			
+			case ZADEH:
+		return (x <= y) ? 1 : y;
+			break;
+
+			case PRODUCT:
+		return MIN(y/x,1);
+			break;	
+		
+			case DRASTIC:
+		return -1; /* not avaialble */	
+			break;	
+			
+			case LUKASIEWICZ:
+		return MIN((1-x+y),1);	
+			break;
+			
+			case FODOR:
+		return (x<=y) ? 1 : MAX(1-x,y);		
+			break;
+	}
+	return -1; /* error */	
+}
+
+float f_not(float x, int family) {
+	
+		if (family==HAMACHER)
+	return (1-x)/(1+x);
+		else
+	return ((1-x)<0 || (1-x)>1) ? -1 : 1-x;
+}
+
+


Property changes on: grass-addons/raster/r.fuzzy.logic/logic.c
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy.logic/main.c
===================================================================
--- grass-addons/raster/r.fuzzy.logic/main.c	                        (rev 0)
+++ grass-addons/raster/r.fuzzy.logic/main.c	2010-03-03 06:57:38 UTC (rev 41275)
@@ -0,0 +1,252 @@
+/* ***************************************************************************
+ *
+ * MODULE:       r.fuzzy.logic
+ * AUTHOR(S):    Jarek Jasiewicz <jarekj amu.edu.pl>
+ * PURPOSE:      Peroforms logical operatations on membership images created with 
+ * 							 r.fuzzy or dofferent method. Use families for fuzzy logic
+ * COPYRIGHT:    (C) 1999-2010 by 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.
+ *
+ ****************************************************************************
+ */
+
+#define MAIN
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+int main(int argc, char *argv[])
+{
+
+	struct GModule *module;
+	struct Option *par_inputx,
+								*par_inputy,
+								*par_output,
+								*par_operation,
+								*par_family;
+	
+	struct Cell_head cellhdx;
+	struct Cell_head cellhdy;  
+  struct Range membership_range;
+  struct History history;
+
+	char *mapsetx, *mapsety;
+	char *inputx, *inputy;
+	char *output;
+	int nrows, ncols;
+	int row, col;
+	int infdx, infdy, outfd;
+	void *in_bufx, *in_bufy;
+	unsigned char *out_buf;
+	float tmp;
+	FCELL c_min, c_max;
+	int family, operator;
+	
+	G_gisinit(argv[0]);
+	
+	par_inputx = G_define_standard_option(G_OPT_R_INPUT);
+  par_inputx->description = _("x operand (membership map)");
+  par_inputx->key = "xmap";
+  
+  par_inputy = G_define_standard_option(G_OPT_R_INPUT);
+  par_inputy->description = _("y operand (membership map)");
+  par_inputy->key = "ymap";
+  par_inputy->required = NO;
+  
+  par_output = G_define_standard_option(G_OPT_R_OUTPUT);
+	par_output->description = _("Resulting map");
+
+	par_operation=G_define_option();
+	par_operation->key = "operator";
+	par_operation->type = "TYPE_STRING";
+	par_operation->options = "AND,OR,NOT,IMP";
+	par_operation->answer = "AND";
+	par_operation->multiple = NO;
+	par_operation->required = YES;
+	par_operation->description = _("Fuzzy logic operation");
+	
+	par_family=G_define_option();
+	par_family->key = "family";
+	par_family->type = "TYPE_STRING";
+	par_family->options = "Zadeh,product,drastic,Lukasiewicz,Fodor,Hamacher";
+	par_family->answer = "Zadeh";
+	par_family->multiple = NO;
+	par_family->required = YES;
+	par_family->description = _("Fuzzy logic family");
+	
+  	if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+	
+	inputx=par_inputx->answer;
+	inputy=par_inputy->answer;
+	output=par_output->answer;
+	
+			if (!strcmp(par_operation->answer, "AND"))
+	operator=_AND;
+		else if (!strcmp(par_operation->answer, "OR"))
+	operator=_OR;
+		else if (!strcmp(par_operation->answer, "NOT"))
+	operator=_NOT;
+		else if (!strcmp(par_operation->answer, "IMP"))
+	operator=_IMP;
+	  
+  		if (!strcmp(par_family->answer, "Zadeh"))
+	family=ZADEH;
+		else if (!strcmp(par_family->answer, "product"))
+	family=PRODUCT;
+		else if (!strcmp(par_family->answer, "drastic"))
+	family=DRASTIC;
+		else if (!strcmp(par_family->answer, "Lukasiewicz"))
+	family=LUKASIEWICZ;
+		else if (!strcmp(par_family->answer, "Fodor"))
+	family=FODOR;
+			else if (!strcmp(par_family->answer, "Hamacher"))
+	family=HAMACHER;
+	
+		if(operator == _NOT && inputy)
+	G_warning("Negation is unary operaton ymap is ignored");
+	
+		if(operator != _NOT && !inputy)
+	G_fatal_error("For binary operation (AND, OR, IMP) ymap is required");
+	
+		if(family == DRASTIC && operator == _IMP)
+  G_fatal_error("implication is not available for <drastic> family");
+	
+	/* end of interface */
+	
+	mapsetx = G_find_cell2(inputx, "");
+	
+	  if (mapsetx == NULL)
+	G_fatal_error(_("Raster map <%s> not found"), inputx);
+	
+	  if ((infdx = G_open_cell_old(inputx, mapsetx)) < 0)
+	G_fatal_error(_("Unable to open raster map <%s>"), inputx);
+	
+	  if (G_get_cellhd(inputx, mapsetx, &cellhdx) < 0)
+	G_fatal_error(_("Unable to read file header of <%s>"), inputx);
+	
+		if (G_raster_map_type(inputx, mapsetx) != FCELL_TYPE)
+	 G_fatal_error(_("<%s> is not of type CELL"), inputx);
+	 
+	G_init_fp_range(&membership_range);
+	G_read_fp_range(inputx,mapsetx,&membership_range);
+	G_get_fp_range_min_max(&membership_range, &c_min, &c_max);
+		if(c_min<0 || c_max>1)
+	G_fatal_error("Membership out of range");
+	
+	in_bufx = G_allocate_raster_buf(FCELL_TYPE);
+	
+		if(inputy) {
+	
+	mapsety = G_find_cell2(inputy, "");
+	
+	  if (mapsety == NULL)
+	G_fatal_error(_("Raster map <%s> not found"), inputy);
+	
+	  if ((infdy = G_open_cell_old(inputy, mapsety)) < 0)
+	G_fatal_error(_("Unable to open raster map <%s>"), inputy);
+	
+	  if (G_get_cellhd(inputy, mapsety, &cellhdy) < 0)
+	G_fatal_error(_("Unable to read file header of <%s>"), inputy);
+	
+		if (G_raster_map_type(inputy, mapsety) != FCELL_TYPE)
+	G_fatal_error(_("<%s> is not of type CELL"), inputy);
+	
+	G_init_fp_range(&membership_range);
+	G_read_fp_range(inputy,mapsety,&membership_range);
+	G_get_fp_range_min_max(&membership_range, &c_min, &c_max);
+		if(c_min<0 || c_max>1)
+	G_fatal_error("Membership out of range");
+	
+	
+	in_bufy = G_allocate_raster_buf(FCELL_TYPE);
+		
+		} /* end if inputy */
+	
+	nrows = G_window_rows();
+	ncols = G_window_cols();
+
+		if ((outfd = G_open_raster_new(output, FCELL_TYPE)) < 0)
+	G_fatal_error(_("Unable to create raster map <%s>"), output);
+	  	
+	out_buf = G_allocate_raster_buf(FCELL_TYPE);
+	
+	
+	/* binary processing */
+	for (row = 0; row < nrows; row++) {
+		G_percent(row, nrows, 2);
+		
+		FCELL fx, fy=0;
+		
+		if (G_get_raster_row(infdx, in_bufx, row, FCELL_TYPE)<0) {
+			G_close_cell(infdx);
+			G_fatal_error(_("Cannot to read <%s> at row <%d>"), inputx,row);
+		}
+		
+			if(inputy)
+		if (G_get_raster_row(infdy, in_bufy, row, FCELL_TYPE)<0) {
+			G_close_cell(infdy);
+			G_fatal_error(_("Cannot to read <%s> at row <%d>"), inputy,row);
+		}
+		
+		for (col = 0; col < ncols; col++) {
+				
+				fx = ((FCELL *) in_bufx)[col];
+					if(inputy)
+				fy = ((FCELL *) in_bufy)[col];
+
+				 if (G_is_f_null_value(&fx) || G_is_f_null_value(&fy))
+			G_set_f_null_value(&tmp, 1); 
+				 
+				 else
+			
+			switch (operator) {
+				case _AND:
+					//if((row+col)%100==0)
+						if(0>(tmp = f_and(fx,fy,family)))
+					G_warning("Cannot determine result at row %d, col %d", row,col);
+					break;
+				
+				case _OR:
+						if(0>(tmp=f_or(fx,fy,family)))
+					G_warning("Cannot determine result at row %d, col %d", row,col);
+					break;
+					
+				case _IMP:
+						if((tmp=f_imp(fx,fy,family))<0)
+					G_warning("Cannot determine result at row %d, col %d", row,col);
+					break;	
+				
+				case _NOT:
+						if((tmp=f_not(fx, family))<0)
+					G_warning("Cannot determine result at row %d, col %d", row,col);
+					break;
+			} /* end switch */
+			
+			((FCELL *) out_buf)[col] = tmp;
+		}
+	
+			if (G_put_raster_row(outfd, out_buf, FCELL_TYPE) < 0)
+	   G_fatal_error(_("Failed writing raster map <%s>"), output);
+	} /* end for row */
+	
+  G_free(in_bufx);
+	G_close_cell(infdx);
+	
+		if(inputy) {
+  G_free(in_bufy);
+  G_close_cell(infdy);
+	}
+	
+	G_free(out_buf);
+	G_close_cell(outfd);
+
+	G_short_history(output, "raster", &history);
+	G_command_history(&history);
+	G_write_history(output, &history);
+	
+	exit(EXIT_SUCCESS);
+}


Property changes on: grass-addons/raster/r.fuzzy.logic/main.c
___________________________________________________________________
Added: svn:executable
   + 



More information about the grass-commit mailing list