[GRASS-SVN] r42732 - grass-addons/raster/r.fuzzy.system

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 9 04:10:43 EDT 2010


Author: jarekj71
Date: 2010-07-09 08:10:43 +0000 (Fri, 09 Jul 2010)
New Revision: 42732

Modified:
   grass-addons/raster/r.fuzzy.system/description.html
   grass-addons/raster/r.fuzzy.system/f_result.png
   grass-addons/raster/r.fuzzy.system/flood.map
   grass-addons/raster/r.fuzzy.system/flood.rul
   grass-addons/raster/r.fuzzy.system/fuzzylogic.c
   grass-addons/raster/r.fuzzy.system/helpers.c
   grass-addons/raster/r.fuzzy.system/io.c
   grass-addons/raster/r.fuzzy.system/local_proto.h
   grass-addons/raster/r.fuzzy.system/main.c
   grass-addons/raster/r.fuzzy.system/map_parser.c
   grass-addons/raster/r.fuzzy.system/rule_parser.c
   grass-addons/raster/r.fuzzy.system/system.c
Log:
adding category handling, some removing some minor bugs

Modified: grass-addons/raster/r.fuzzy.system/description.html
===================================================================
--- grass-addons/raster/r.fuzzy.system/description.html	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/description.html	2010-07-09 08:10:43 UTC (rev 42732)
@@ -2,7 +2,7 @@
 
 <p>
 <DT><b>maps</b></DT>
-<DD>A text file containing maps name and  fuzzy sets connected with map definition. Maps in text file must exist on a search path, except the output map. The name of output map must be identical with that of the output name parameter. If maps are in different mapsets the name require @. Map names in database cannot contain following symbols: <B> %,$ and #</B>. Every map name must start with map name identifier:<B> %</B>. Every set definition connected with cetrain map must follow the map name and must start with set identifier :<B> $</B>. The set definition must be in braces { } and requires parameters separated by semicolon. Any whitespaces like spaces, tabs, empty lines are allowed and may used to visual format of rule file.
+<DD>A text file containing maps name and  fuzzy sets connected with map definition. Maps in text file must exist on a search path, except the output map. The name of output map name is the output name parameter. In map file outmput map is marked by special name <B>_OUTPUT_</B>  If maps are in different mapsets the name require @. Map names in database cannot contain following symbols: <B> %,$ and #</B>. Every map name must start with map name identifier:<B> %</B>. Every set definition connected with cetrain map must follow the map name and must start with set identifier :<B> $</B>. The set definition must be in braces { } and requires parameters separated by semicolon. Any whitespaces like spaces, tabs, empty lines are allowed and may used to visual format of rule file.
 
 <PRE>$ set_name {side; points; boundary_shape; hedge; height }
 <UL>
@@ -81,7 +81,7 @@
 <DT><b>defuzz: deffuzyfication method</b></DT>
 <DD>Before deffuzification all consequents are agregated into one fuzzy set. Defuzzification is the process of conversion of aggregated fuzzy set into one crisp value. The r.fuzzy.system provides 5 methods of deffuzyfication:
 <UL>
-<li><B>centeroid</B> center of mass of the fuzzy set (in practise weighted mean);
+<li><B>centroid</B> center of mass of the fuzzy set (in practise weighted mean);
 <li><B>bisector</B> a value wchich divide fuzzy set on two parts of equal area;
 <li><B>min</B> min (right limit) of highest part of the set;
 <li><B>mean</B> mean (center) of highest part of the set;
@@ -89,7 +89,7 @@
 </UL>
 </DD>
 <DT><b>res: universe resolution</b></DT>
-<DD>The universe is an interval between the lowest and highest values of consequent and agregated fuzzy sets. The resolution provides number of elements of these fuzzy sets. The minimum and maximum for univese is taken from the minimal and maximal values of fuzzy set definition of output map Because it has strong impact on computation time and precision of deffuzification, values lower than 30 may impact on precision of final result, but values above 200 radically slow down computation time.
+<DD>The universe is an interval between the lowest and highest values of consequent and agregated fuzzy sets. The resolution provides number of elements of these fuzzy sets. The minimum and maximum for univese is taken from the minimal and maximal values of fuzzy set definition of output map Because it has strong impact on computation time and precision of deffuzification, values lower than 30 may impact on precision of final result, but values above 200 may slow down computation time.
 </DD>
 <h2>VISUAL OUTPUT</h2>
 <DT><b>coordinates</b></DT>
@@ -139,9 +139,9 @@
 <P>
 Suppose we want to determine the flood risk on some area (Spearfish dataset) using two maps: distance to streams and elevation above streams. We can write some common sense rules:
 <PRE>
-IF elevation IS low AND distance IS near THEN risk IS veryprobable
+IF elevation IS low AND distance IS near THEN risk IS very high
 IF elevation IS low OR distance IS near THEN risk IS probable
-IF elevation IS high AND distance IS far THEN risk IS unprobable
+IF elevation IS high AND distance IS far THEN risk IS low
 </PRE>
 In clasical boolean sense, we would taken some limits of ideas "near" "far" etc, but what about walues near the limit? The fuzzy set uses partial memberships which abolish these restrictions. In that sense to set "near" belongs all areas with distance no more than 100 m with full membership and from 100 to 200 m with partial membership grater than 0. Over 200 m we can assume that is not near.
 This allow to formulate fuzzy rules for distance map:
@@ -154,10 +154,12 @@
 First we need two maps created with r.stream package:
 
 <PRE>
-r.stream.extract elevation=elevation.10m at PERMANENT threshold=2000 stream_rast=streams direction=dirs 
-r.stream.order stream=streams dir=dirs horton=horton
-r.mapcalc "horton3=if(horton>2,horton,null())"
-r.stream.distance stream=streams dir=dirs dem=elevation.10m method=downstream distance=distance elevation=elevation 
+r.watershed -f elevation=elevation.10m at PERMANENT accumulation=accum
+r.mapcalc 'accum_abs=abs(accum)'
+r.stream.extract elevation="elevation.10m at PERMANENT" threshold=1000 stream_rast="stream" direction="dirs"
+r.stream.order stream=streams dir=dirs horton=horton 
+r.mapcalc horton3=if(horton>2,1,0)
+r.stream.distance stream=horton3 dir=dirs dem=elevation.10m at PERMANENT  distance=distance elevation=elevation
 </PRE>
 
 Next, to perform analysis we need two files: one with definition of map used in analysis and definition of fuzzy sets for every map, and second with fuzzy rules. For this example:
@@ -165,33 +167,50 @@
 <P>MAPS
 <PRE>
 #flood.map
+
 	%elevation
-$ low {right; 2,6; sshaped; 0; 1}
-$ high {left; 2,6; sshaped; 0; 1}
+$ low {right; 2,4; sshaped; 0; 1}
+$ moderate {both; 2,4,5,10; sshaped; 0; 1}
+$ high {left; 5,10; sshaped; 0; 1}
+
 	%distance
-$ near {right; 40,80; sshaped; 0; 1}
-$ far {left; 40,80; sshaped; 0; 1}
+$ near {right; 30,70; sshaped; 0; 1}
+$ medium {both; 30,70,100,150; sshaped; 0; 1}
+$ far {both; 100,150,200,300; sshaped; 0; 1}
+$ veryfar {left; 200,300; sshaped; 0; 1}
+	
+	%accum_abs
+$ low {right; 500,5000; sshaped; 0; 1}
+$ $ high {left; 500,5000; sshaped; 0; 1}
+
 #output map
-	%flood
-$ unprob {both; 0,20,20,40; linear; 0;1}
-$ prob {both; 20,40,40,60; linear; 0;1}
-$ veryprob {both; 40,60,60,80; linear; 0;1}
 
+	%_OUTPUT_
+$ none {both; 0,20,20,40; linear; 0;1}
+$ low {both; 20,40,40,60; linear; 0;1}
+$ moderate {both; 40,60,60,80; linear; 0;1}
+$ high {both; 60,80,80,100; linear; 0;1}
+$ veryhigh {both; 80,100,100,120; linear; 0;1}
+
 </PRE>
 <P>RULES:
 <PRE>
 #flood.rul
-$ unprob {elevation = high & distance = far}
-$ prob {distance = near | elevation = low}
-$ veryprob {distance = near & elevation = low}
+$ none {distance = veryfar | elevation = high}
+$ low {distance ~ near & accum_abs = high}
+$ moderate {(distance = medium | distance = far) & (elevation = low | elevation = moderate)}
+$ high {(distance = medium & elevation = low)|(distance = near & elevation = moderate)}
+$ veryhigh {distance = near & elevation = low}
 </PRE>
 
 finally we need run r.fuzzy.system:
 <PRE>
-r.fuzzy.system maps=flood.map rules=flod.rul output=flood 
+A) r.fuzzy.system maps=~/flood.map rules=~/flood.rul family=Zadeh defuz=centroid imp=minimum res=100 output=flood_z_cent
+B) r.fuzzy.system maps=~/flood.map rules=~/flood.rul family=drastic defuz=max_of_highest imp=minimum res=100 output=flood_d_max
+C) r.fuzzy.system maps=~/flood.map rules=~/flood.rul family=Hamacher defuz=mean_of_highest imp=minimum res=100 output=flood_h_mean
 </PRE>
 
-Resulting map should look like this below. Yellow color means no risk, red high risk, green, blue end so on moderate risk.
+Resulting map should look like this below.
 <CENTER><img src=f_result.png border=1></CENTER><br>
 
 <h2>SEE ALSO</h2>

Modified: grass-addons/raster/r.fuzzy.system/f_result.png
===================================================================
(Binary files differ)

Modified: grass-addons/raster/r.fuzzy.system/flood.map
===================================================================
--- grass-addons/raster/r.fuzzy.system/flood.map	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/flood.map	2010-07-09 08:10:43 UTC (rev 42732)
@@ -1,15 +1,25 @@
 #flood.map
 
 	%elevation
-$ low {right; 2,6; sshaped; 0; 1}
-$ high {left; 2,6; sshaped; 0; 1}
+$ low {right; 2,4; sshaped; 0; 1}
+$ moderate {both; 2,4,5,10; sshaped; 0; 1}
+$ high {left; 5,10; sshaped; 0; 1}
 
 	%distance
-$ near {right; 40,80; sshaped; 0; 1}
-$ far {left; 40,80; sshaped; 0; 1}
+$ near {right; 30,70; sshaped; 0; 1}
+$ medium {both; 30,70,100,150; sshaped; 0; 1}
+$ far {both; 100,150,200,300; sshaped; 0; 1}
+$ veryfar {left; 200,300; sshaped; 0; 1}
+	
+	%accum_abs
+$ low {right; 500,5000; sshaped; 0; 1}
+$ $ high {left; 500,5000; sshaped; 0; 1}
 
 #output map
-	%flood
-$ unprob {both; 0,20,20,40; linear; 0;1}
-$ prob {both; 20,40,40,60; linear; 0;1}
-$ veryprob {both; 40,60,60,80; linear; 0;1}
+
+	%_OUTPUT_
+$ none {both; 0,20,20,40; linear; 0;1}
+$ low {both; 20,40,40,60; linear; 0;1}
+$ moderate {both; 40,60,60,80; linear; 0;1}
+$ high {both; 60,80,80,100; linear; 0;1}
+$ veryhigh {both; 80,100,100,120; linear; 0;1}

Modified: grass-addons/raster/r.fuzzy.system/flood.rul
===================================================================
--- grass-addons/raster/r.fuzzy.system/flood.rul	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/flood.rul	2010-07-09 08:10:43 UTC (rev 42732)
@@ -1,4 +1,6 @@
 #flood.rul
-$ unprob {elevation = high & distance = far};
-$ prob {distance = near | elevation = low};
-$ veryprob {distance = near & elevation = low};
+$ none {distance = veryfar | elevation = high}
+$ low {distance ~ near & accum_abs = high}
+$ moderate {(distance = medium | distance = far) & (elevation = low | elevation = moderate)}
+$ high {(distance = medium & elevation = low)|(distance = near & elevation = moderate)}
+$ veryhigh {distance = near & elevation = low}

Modified: grass-addons/raster/r.fuzzy.system/fuzzylogic.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/fuzzylogic.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/fuzzylogic.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -2,7 +2,7 @@
 
 float fuzzy(FCELL cell, SETS * set)
 {
-   
+
     float x;
 
     if (!set->side) {		/* both left and right */
@@ -53,6 +53,7 @@
 
     if (set->height < 1)
 	x = x * set->height;
+
     return (x >= 0 && x <= 1) ? x : -1;
 }
 

Modified: grass-addons/raster/r.fuzzy.system/helpers.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/helpers.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/helpers.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -81,6 +81,8 @@
     for (i = 0; i < resolution; ++i)
 	universe[i] = min + ((max - min) / resolution) * i;
 
+/* visual output */
+
     return 0;
 }
 
@@ -94,6 +96,14 @@
     int num_points;
     float result;
 
+		visual_output = (float **)G_malloc(resolution * sizeof(float *));
+
+				for (j = 0; j < nrules; ++j) 
+		for (i = 0; i < resolution; ++i) {
+	    visual_output[i] = (float *)G_calloc(nrules + 2, sizeof(float));
+	    visual_output[i][0] = universe[i];
+		}
+			
     G_get_window(&window);
     num_points = sscanf(answer, "%lf,%lf", &x, &y);
 
@@ -133,6 +143,10 @@
 	G_close_cell(s_maps[i].cfd);
     }
 
+			for (j = 0; j < nrules; ++j)
+				G_free(visual_output[j]);
+    G_free(visual_output);
+    
     G_free(antecedents);
     G_free(s_maps);
     G_free(s_rules);
@@ -198,3 +212,56 @@
 		exit(EXIT_SUCCESS);
 	
 }
+
+int set_cats(void)
+{
+struct Categories cats;
+float fmin, fmax;
+int cmin, cmax;
+int i, j;
+
+fmin=universe[0];
+fmax=universe[resolution];
+cmin=(int)universe[0];
+cmax=(int)universe[resolution];
+double tmp1, tmp2;
+float fuzzy_val=0;
+char buf[200];
+float rang;
+
+rang=(universe[0]+universe[1])/2.;
+G_read_raster_cats(output, G_mapset(), &cats);
+G_set_raster_cats_title("Result membership", &cats);
+
+	for (i=0;i<resolution;++i) {
+		strcpy(buf,"");
+		for (j=0; j<s_maps[output_index].nsets; ++j) { 
+			fuzzy_val=fuzzy(universe[i],&s_maps[output_index].sets[j]);
+				
+				if(universe[i]<s_maps[output_index].sets[1].points[0] ||
+					universe[i]>s_maps[output_index].sets[s_maps[output_index].nsets-2].points[3])
+			fuzzy_val=1;		
+				if (fuzzy_val>0.)
+			sprintf(buf,"%s %s=%2.2f", buf, s_maps[output_index].sets[j].setname,fuzzy_val);	
+			
+		}
+
+			tmp1=(double)universe[i]-rang;
+			tmp2=(double)universe[i]+rang;
+			if(i==0) tmp1=(double)universe[i];
+			if(i==resolution) tmp2=(double)universe[i];
+			G_set_d_raster_cat(&tmp1, &tmp2, buf, &cats);
+	}	
+G_write_raster_cats(output, &cats);
+G_free_raster_cats(&cats);
+return 0;
+}
+
+
+
+void set_colors(void) { /* not in use yet */
+struct Colors colors;
+G_init_colors(&colors);
+G_add_color_rule(0, 255, 255, 255, 2, 255, 255, 0, &colors);
+G_write_colors(output, G_mapset(), &colors);
+}

Modified: grass-addons/raster/r.fuzzy.system/io.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/io.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/io.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -77,7 +77,7 @@
 	    if (G_is_null_value(&c, CELL_TYPE))
 		return 1;
 	    else
-		s_maps[i].cell = (FCELL) c;
+		s_maps[i].cell = (DCELL) c;
 	    break;
 
 	case FCELL_TYPE:
@@ -85,7 +85,7 @@
 	    if (G_is_null_value(&f, FCELL_TYPE))
 		return 1;
 	    else
-		s_maps[i].cell = (FCELL) f;
+		s_maps[i].cell = (DCELL) f;
 	    break;
 
 	case DCELL_TYPE:
@@ -93,7 +93,7 @@
 	    if (G_is_null_value(&d, DCELL_TYPE))
 		return 1;
 	    else
-		s_maps[i].cell = (FCELL) d;
+		s_maps[i].cell = (DCELL) d;
 	    break;
 	}
     }				/* end for */

Modified: grass-addons/raster/r.fuzzy.system/local_proto.h
===================================================================
--- grass-addons/raster/r.fuzzy.system/local_proto.h	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/local_proto.h	2010-07-09 08:10:43 UTC (rev 42732)
@@ -112,26 +112,26 @@
     RASTER_MAP_TYPE raster_type;
     fpos_t position;
     void *in_buf;
-    float cell;
+    DCELL cell;
     int cfd;			/* file descriptor */
     SETS *sets;
 } MAPS;
 
 typedef struct
 {
-    float *value;
+    DCELL *value;
     SETS *set;
     char oper;
 } VALUES;
 
 
-typedef struct
+typedef struct /* stores queues with rules */
 {
     char outname[20];
     int output_set_index;
-    char parse_stack[STACKMAX][VARMAX];
-    int work_stack[STACKMAX];
-    VALUES value_stack[STACKMAX];
+    char parse_queue[STACKMAX][VARMAX]; /* original rule */
+    int work_queue[STACKMAX]; /* symbols of values and operators */
+    VALUES value_queue[STACKMAX]; /* pointers to values, operators and sets */
     float weight;
 } RULES;
 
@@ -139,7 +139,7 @@
 {
     char output_name[52];
     int ofd;			/* output file descriptor */
-    float *out_buf;
+    FCELL *out_buf;
 } OUTPUTS;
 
 
@@ -152,6 +152,7 @@
 float **visual_output;
 float *universe;
 float *antecedents;
+float *agregate;
 int nmaps, nrules, output_index, multiple, membership_only, coor_proc;
 int resolution;
 implications implication;
@@ -164,6 +165,7 @@
 int char_copy(const char *buf, char *res, int start, int stop);
 int get_nsets(FILE * fd, fpos_t position);
 int get_universe(void);
+int set_cats(void);
 
 int parse_map_file(STRING file);
 int parse_rule_file(STRING file);
@@ -173,7 +175,7 @@
 int get_rows(int row);
 int get_cells(int col);
 
-
+int HERE;
 int parse_sets(SETS * set, char buf[], const char mapname[]);
 int parse_rules(int rule_num, int n, char buf[]);
 void process_coors(char *answer);
@@ -181,16 +183,13 @@
 
 float implicate(void);
 float parse_expression(int n);
-float defuzzify(float *agregate, int defuzzification, float max_antecedent);
+float defuzzify(int defuzzification, float max_agregate);
 
 float f_and(float cellx, float celly, logics family);
 float f_or(float cellx, float celly, logics family);
 float f_not(float cellx, logics family);
 float fuzzy(FCELL cell, SETS * set);
 
-
 int parse(void);
-
-
-
 void display(void);
+

Modified: grass-addons/raster/r.fuzzy.system/main.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/main.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/main.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -59,7 +59,7 @@
     par_defuzzify = G_define_option();
     par_defuzzify->key = "defuz";
     par_defuzzify->type = TYPE_STRING;
-    par_defuzzify->options = "centeroid,bisector,min,max,mean";
+    par_defuzzify->options = "centroid,bisector,min_of_highest,max_of_highest,mean_of_highest";
     par_defuzzify->answer = "bisector";
     par_defuzzify->required = YES;
     par_defuzzify->description = _("Defuzzyfication method");
@@ -68,7 +68,7 @@
     par_implication = G_define_option();
     par_implication->key = "imp";
     par_implication->type = TYPE_STRING;
-    par_implication->options = "minimum, product";
+    par_implication->options = "minimum,product";
     par_implication->answer = "minimum";
     par_implication->required = YES;
     par_implication->description = _("Implication method");
@@ -146,15 +146,15 @@
     else if (!strcmp(par_implication->answer, "product"))
 	implication = i_PROD;
 
-    if (!strcmp(par_defuzzify->answer, "centeroid"))
+    if (!strcmp(par_defuzzify->answer, "centroid"))
 	defuzzyfication = d_CENTEROID;
     else if (!strcmp(par_defuzzify->answer, "bisector"))
 	defuzzyfication = d_BISECTOR;
-    else if (!strcmp(par_defuzzify->answer, "min_of_heightest"))
+    else if (!strcmp(par_defuzzify->answer, "min_of_highest"))
 	defuzzyfication = d_MINOFHIGHEST;
-    else if (!strcmp(par_defuzzify->answer, "max_of_heightest"))
+    else if (!strcmp(par_defuzzify->answer, "max_of_highest"))
 	defuzzyfication = d_MAXOFHIGHEST;
-    else if (!strcmp(par_defuzzify->answer, "mean_of_heightest"))
+    else if (!strcmp(par_defuzzify->answer, "mean_of_highest"))
 	defuzzyfication = d_MEANOFHIGHEST;
 
     nrows = G_window_rows();
@@ -169,6 +169,7 @@
     open_maps();
 	
 		antecedents = (float *)G_malloc(nrules * sizeof(float));
+		agregate = (float *)G_calloc(resolution, sizeof(float));
 			if (coor_proc)
 		process_coors(in_coor_opt->answer);
 
@@ -179,8 +180,8 @@
 
     if (multiple)
 	create_output_maps();
-	
-    G_message("Calculate...");
+  
+		G_message("Calculate...");
 
     for (row = 0; row < nrows; ++row) {
 		G_percent(row, nrows, 2);
@@ -219,25 +220,30 @@
 
     }
     G_percent(row, nrows, 2);
+		
+    G_message("Close...");
 
-    G_message("Close...");
-    for (i = 0; i < nmaps; ++i) {
-	G_free(s_maps[i].sets);
-	if (s_maps[i].output)
+		
+
+    G_close_cell(outfd);
+    G_short_history(output, "raster", &history);
+    G_command_history(&history);
+    G_write_history(output, &history);
+		set_cats();
+		/* free */
+			for (i = 0; i < nmaps; ++i) {
+		G_free(s_maps[i].sets);
+				if (s_maps[i].output)
 	    continue;
-	G_free(s_maps[i].in_buf);
-	G_close_cell(s_maps[i].cfd);
-    }
-
+		G_free(s_maps[i].in_buf);
+		G_close_cell(s_maps[i].cfd);
+			}
     G_free(antecedents);
+    G_free(agregate);
     G_free(out_buf);
     G_free(s_maps);
     G_free(s_rules);
-    G_close_cell(outfd);
-    G_short_history(output, "raster", &history);
-    G_command_history(&history);
-    G_write_history(output, &history);
-
+   
     if (multiple)
 	for (i = 0; i < nrules; ++i) {
 	    G_free(m_outputs[i].out_buf);

Modified: grass-addons/raster/r.fuzzy.system/map_parser.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/map_parser.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/map_parser.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -51,13 +51,19 @@
 	    G_strip(map);
 	    mapset = G_find_cell2(map, "");
 
-	    if (mapset == NULL && strcmp(map, output))
+	    if (mapset == NULL && strcmp(map, "_OUTPUT_"))
 		G_fatal_error(_("Raster map <%s> not found"), map);
 
+				if (!strcmp(map, "_OUTPUT_")) {
+			G_strcpy(s_maps[nmaps2].name, output);
+			s_maps[nmaps2].output=1;
+				} else {
 	    G_strcpy(s_maps[nmaps2].name, map);
+	    s_maps[nmaps2].output=0;
+				}
 	    s_maps[nmaps2].position = position;
 	    s_maps[nmaps2].nsets = get_nsets(fd, position);
-	    s_maps[nmaps2].output = (strcmp(map, output)) ? 0 : 1;
+	    
 
 	    if (!s_maps[nmaps2].nsets)
 		G_warning(_("map <%s> has no rules"), s_maps[nmaps2].name);

Modified: grass-addons/raster/r.fuzzy.system/rule_parser.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/rule_parser.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/rule_parser.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -3,7 +3,7 @@
 int parse_rule_file(STRING file)
 {
     FILE *fd;
-    char buf[800];
+    char buf[1000];
     char tmp[30];
     STRING mapset;
     char map[30];
@@ -116,24 +116,24 @@
 		    char_copy(buf, tmp, j, i);
 		    if (i > 0) {
 			char_strip(tmp, buf[j]);
-			s_rules[rule_num].parse_stack[stack_top][0] = buf[j];
-			s_rules[rule_num].parse_stack[stack_top++][1] = '\0';
+			s_rules[rule_num].parse_queue[stack_top][0] = buf[j];
+			s_rules[rule_num].parse_queue[stack_top++][1] = '\0';
 			G_strip(tmp);
 		    }
 		    if (strlen(tmp))	/* is not blank */
-			strcpy(s_rules[rule_num].parse_stack[stack_top++],
+			strcpy(s_rules[rule_num].parse_queue[stack_top++],
 			       tmp);
 
 		    j = i;
 		    break;
 		}
 	    }
-	    if (i > 799)
+	    if (i > 999)
 		G_fatal_error(_("rule string is too long or lack of closing element"));
 
-	} while (buf[i++] != '}');	/* rule is rot */
+	} while (buf[i++] != '}');	/* rule has been read */
 
-	strcpy(s_rules[rule_num].parse_stack[stack_top++], "}");	/* add closing element only if OK */
+	strcpy(s_rules[rule_num].parse_queue[stack_top++], "}");	/* add closing element only if OK */
 
 
     }				/* end parse rule expression and create parse stack */
@@ -159,44 +159,44 @@
 
     {				/* check if rule syntax is proper and map names and vars values exist */
 	int k;
-	int work_stack_pos = 0;
-	int lbrc = 0, rbrc = 0;	/* left and right brackets */
+	int work_queue_pos = 0;
+	int lbrc = 0, rbrc = 0;	/* left and right braces */
 
 	done = 1;
 	for (i = 0; i < stack_top; ++i) {	/* most external loop */
-	    if (*s_rules[rule_num].parse_stack[i] == '{') {
+	    if (*s_rules[rule_num].parse_queue[i] == '{') {
 
-		s_rules[rule_num].work_stack[work_stack_pos] = t_START;
+		s_rules[rule_num].work_queue[work_queue_pos] = t_START;
 
 		if (i > 0)
 		    G_fatal_error(_("line %d Syntax error near <%s %s>"),
 				  rule_num + 1,
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i]);
 
-		work_stack_pos++;
+		work_queue_pos++;
 		continue;
 	    }			/* END { */
 
-	    if (*s_rules[rule_num].parse_stack[i] == '=' || *s_rules[rule_num].parse_stack[i] == '~') {	/* =, ~ */
+	    if (*s_rules[rule_num].parse_queue[i] == '=' || *s_rules[rule_num].parse_queue[i] == '~') {	/* =, ~ */
 
 		for (j = 0; j < nmaps; ++j) {
 		    if (!strcmp
-			(s_rules[rule_num].parse_stack[i - 1],
+			(s_rules[rule_num].parse_queue[i - 1],
 			 s_maps[j].name)) {
 			for (k = 0; k < s_maps[j].nsets; ++k) {
 			    if (!strcmp
-				(s_rules[rule_num].parse_stack[i + 1],
+				(s_rules[rule_num].parse_queue[i + 1],
 				 s_maps[j].sets[k].setname)) {
 
-				s_rules[rule_num].work_stack[work_stack_pos] =
+				s_rules[rule_num].work_queue[work_queue_pos] =
 				    t_VAL;
-				s_rules[rule_num].value_stack[work_stack_pos].
+				s_rules[rule_num].value_queue[work_queue_pos].
 				    value = &s_maps[j].cell;
-				s_rules[rule_num].value_stack[work_stack_pos].
+				s_rules[rule_num].value_queue[work_queue_pos].
 				    set = &s_maps[j].sets[k];
-				s_rules[rule_num].value_stack[work_stack_pos].
-				    oper = *s_rules[rule_num].parse_stack[i];
+				s_rules[rule_num].value_queue[work_queue_pos].
+				    oper = *s_rules[rule_num].parse_queue[i];
 				done = 0;
 				break;
 			    }
@@ -206,101 +206,101 @@
 
 		if (done)
 		    G_fatal_error(_("There is no map <%s> or variable <%s>"),
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i + 1]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i + 1]);
 		/* end for j */
 
-		if (s_rules[rule_num].work_stack[work_stack_pos - 1] != t_AND
-		    && s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		if (s_rules[rule_num].work_queue[work_queue_pos - 1] != t_AND
+		    && s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_OR &&
-		    s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		    s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_START &&
-		    s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		    s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_LBRC)
 		    G_fatal_error(_("line %d Syntax error near <%s %s>"),
 				  rule_num + 1,
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i]);
 
-		work_stack_pos++;
+		work_queue_pos++;
 		continue;
 	    }			/* END =, ~ */
 
-	    if (*s_rules[rule_num].parse_stack[i] == '&' || *s_rules[rule_num].parse_stack[i] == '|') {	/* &, | */
+	    if (*s_rules[rule_num].parse_queue[i] == '&' || *s_rules[rule_num].parse_queue[i] == '|') {	/* operators &, | */
 
-		s_rules[rule_num].work_stack[work_stack_pos] =
-		    (*s_rules[rule_num].parse_stack[i] == '|') ? t_OR : t_AND;
+		s_rules[rule_num].work_queue[work_queue_pos] =
+		    (*s_rules[rule_num].parse_queue[i] == '|') ? t_OR : t_AND;
 
-		if (s_rules[rule_num].work_stack[work_stack_pos - 1] != t_VAL
-		    && s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		if (s_rules[rule_num].work_queue[work_queue_pos - 1] != t_VAL
+		    && s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_RBRC)
 		    G_fatal_error(_("line %d Syntax error near <%s %s>"),
 				  rule_num + 1,
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i]);
 
-		work_stack_pos++;
+		work_queue_pos++;
 		continue;
 	    }			/* END &, | */
 
-	    if (*s_rules[rule_num].parse_stack[i] == '(') {
+	    if (*s_rules[rule_num].parse_queue[i] == '(') {
 
-		s_rules[rule_num].work_stack[work_stack_pos] = t_LBRC;
+		s_rules[rule_num].work_queue[work_queue_pos] = t_LBRC;
 		lbrc++;
 
-		if (s_rules[rule_num].work_stack[work_stack_pos - 1] != t_AND
-		    && s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		if (s_rules[rule_num].work_queue[work_queue_pos - 1] != t_AND
+		    && s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_OR &&
-		    s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		    s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_START &&
-		    s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		    s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_LBRC)
 		    G_fatal_error(_("line %d Syntax error near <%s %s>"),
 				  rule_num + 1,
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i]);
 
-		work_stack_pos++;
+		work_queue_pos++;
 		continue;
 	    }			/* END ( */
 
-	    if (*s_rules[rule_num].parse_stack[i] == ')') {
+	    if (*s_rules[rule_num].parse_queue[i] == ')') {
 
-		s_rules[rule_num].work_stack[work_stack_pos] = t_RBRC;
+		s_rules[rule_num].work_queue[work_queue_pos] = t_RBRC;
 		rbrc++;
 
-		if (s_rules[rule_num].work_stack[work_stack_pos - 1] != t_VAL
-		    && s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		if (s_rules[rule_num].work_queue[work_queue_pos - 1] != t_VAL
+		    && s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_RBRC)
 		    G_fatal_error(_("line %d Syntax error near <%s %s>"),
 				  rule_num + 1,
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i]);
 
-		work_stack_pos++;
+		work_queue_pos++;
 		continue;
 	    }			/* END ) */
 
-	    if (*s_rules[rule_num].parse_stack[i] == '}') {
+	    if (*s_rules[rule_num].parse_queue[i] == '}') {
 
-		s_rules[rule_num].work_stack[work_stack_pos] = t_STOP;
+		s_rules[rule_num].work_queue[work_queue_pos] = t_STOP;
 
-		if (s_rules[rule_num].work_stack[work_stack_pos - 1] != t_VAL
-		    && s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+		if (s_rules[rule_num].work_queue[work_queue_pos - 1] != t_VAL
+		    && s_rules[rule_num].work_queue[work_queue_pos - 1] !=
 		    t_RBRC)
 		    G_fatal_error(_("line %d Syntax error near <%s %s>"),
 				  rule_num + 1,
-				  s_rules[rule_num].parse_stack[i - 1],
-				  s_rules[rule_num].parse_stack[i]);
+				  s_rules[rule_num].parse_queue[i - 1],
+				  s_rules[rule_num].parse_queue[i]);
 
-		work_stack_pos++;
+		work_queue_pos++;
 		continue;
 	    }			/* END } */
 
 	}			/* most external loop */
 
 	if (lbrc != rbrc)
-	    G_fatal_error(_("line %d Left and right of brackets do not match"),
+	    G_fatal_error(_("line %d Left and right of braces do not match"),
 			  rule_num + 1);
 
     }				/* END check if rule syntax is proper and map names and vars values exist */

Modified: grass-addons/raster/r.fuzzy.system/system.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/system.c	2010-07-09 01:53:44 UTC (rev 42731)
+++ grass-addons/raster/r.fuzzy.system/system.c	2010-07-09 08:10:43 UTC (rev 42732)
@@ -4,36 +4,28 @@
 {
 
     int i, j;
-    float *agregate = NULL;
+    //float *agregate = NULL;
     int set_index;
     float consequent;
     float max_antecedent = 0;
+    float max_agregate = 0;
     float result;
 
-    agregate = (float *)G_calloc(resolution, sizeof(float));
+    memset(agregate,0,resolution * sizeof(float));
 
-    if (coor_proc) /* this is allocated only once */
-	visual_output = (float **)G_malloc(resolution * sizeof(float *));
-
     for (j = 0; j < nrules; ++j) {
 	antecedents[j] = parse_expression(j);
 	max_antecedent =
 	    (max_antecedent >
-	     antecedents[j]) ? max_antecedent : antecedents[j];
-    }
+	     antecedents[j]) ? max_antecedent : antecedents[j]; 
+   }
 
     if (max_antecedent == 0. && !coor_proc)
 	return -9999;		/* for all rules value is 0 */
 
-    if (coor_proc)
-	for (i = 0; i < resolution; ++i) {
-	    visual_output[i] = (float *)G_calloc(nrules + 2, sizeof(float));
-	    visual_output[i][0] = universe[i];
-	}
-
     for (j = 0; j < nrules; ++j) {
 
-	if (defuzzyfication > d_BISECTOR && antecedents[j] < max_antecedent)
+	if (defuzzyfication > d_BISECTOR && antecedents[j] < max_antecedent && !coor_proc)
 	    continue;
 	    
 	    set_index = s_rules[j].output_set_index;
@@ -46,17 +38,21 @@
 	    consequent = (!implication) ? MIN(antecedents[j], consequent) :
 		antecedents[j] * consequent;
 	    agregate[i] = MAX(agregate[i], consequent);
+	    
+	   max_agregate = (max_agregate > agregate[i]) 
+				? max_agregate : agregate[i];
+	   
 
 	    if (coor_proc)
 		visual_output[i][j + 1] = consequent;
 	}
+	
     }
     if (coor_proc)
 	for (i = 0; i < resolution; ++i)
 	    visual_output[i][j + 1] = agregate[i];
 
-     result=defuzzify(agregate, defuzzyfication, max_antecedent);
-     G_free(agregate);
+     result=defuzzify(defuzzyfication, max_agregate);
      return result;
 }
 
@@ -89,31 +85,31 @@
 
 
     do {
-	if (s_rules[n].work_stack[i] == t_START) {	/* first token */
+	if (s_rules[n].work_queue[i] == t_START) {	/* first token */
 	    if (i > 0)
 		G_fatal_error("operator stack error, contact author");
 	    operator_stack[opr_top] = t_START;
 	    continue;
 	}
 
-	if (s_rules[n].work_stack[i] == t_VAL) {
+	if (s_rules[n].work_queue[i] == t_VAL) {
 	    f_value =
-		fuzzy(*s_rules[n].value_stack[i].value, s_rules[n].value_stack[i].set);
-	    values_stack[++val_top] =	(s_rules[n].value_stack[i].oper == '~') ? 
+		fuzzy(*s_rules[n].value_queue[i].value, s_rules[n].value_queue[i].set);
+	    values_stack[++val_top] =	(s_rules[n].value_queue[i].oper == '~') ? 
 				f_not(f_value, family) :	f_value;
 	    continue;
 	}
 
-	if (s_rules[n].work_stack[i] < t_size) {
+	if (s_rules[n].work_queue[i] < t_size) {
 	    switch (parse_tab[operator_stack[opr_top]]
-		    [s_rules[n].work_stack[i]]) {
+		    [s_rules[n].work_queue[i]]) {
 
 	    case E:		/* error */
 		G_fatal_error("Stack error, contact author");
 		break;
 
 	    case S:		/* shift */
-		operator_stack[++opr_top] = s_rules[n].work_stack[i];
+		operator_stack[++opr_top] = s_rules[n].work_queue[i];
 		break;
 
 	    case R:		/* reduce */
@@ -146,21 +142,20 @@
 	    case A:		/* accept */
 	
 		if (!val_top)
-		    G_fatal_error("Last Stack error, contact autor");
+		    G_fatal_error("Stack error at end, contact autor");
 		return values_stack[val_top];
 	
 	    }
 	}
 
-    } while (s_rules[n].work_stack[i++] != t_STOP);
+    } while (s_rules[n].work_queue[i++] != t_STOP);
     
     G_fatal_error("Parse Stack empty, contact autor");
 }
 
 
 
-
-float defuzzify(float *agregate, int defuzzification, float max_antecedent)
+float defuzzify(int defuzzyfication, float max_agregate)
 {
     int i;
     float d_value = 0;
@@ -169,7 +164,7 @@
 
     for (i = 0; i < resolution; sum_agregate += agregate[i++]) ;
 
-    switch (defuzzification) {
+    switch (defuzzyfication) {
 
     case d_CENTEROID:
 	for (i = 0; i < resolution; ++i)
@@ -181,17 +176,17 @@
 	return universe[i];
 
     case d_MINOFHIGHEST:
-	for (i = 0; agregate[i++] < max_antecedent;) ;
+	for (i = 0; agregate[i] < max_agregate;++i) ;
 	return universe[i];
 
     case d_MAXOFHIGHEST:
-	for (i = resolution; agregate[i--] < max_antecedent;) ;
+	for (i = resolution; agregate[i] < max_agregate;--i) ;
 	return universe[i];
 
     case d_MEANOFHIGHEST:
 	sum_agregate = 0;
 	for (i = 0; i < resolution; ++i) {
-	    if (agregate[i] < max_antecedent)
+	    if (agregate[i] < max_agregate)
 		continue;
 	    d_value += (universe[i] * agregate[i]);
 	    sum_agregate += agregate[i];



More information about the grass-commit mailing list