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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Apr 26 10:05:11 EDT 2010


Author: jarekj71
Date: 2010-04-26 10:05:09 -0400 (Mon, 26 Apr 2010)
New Revision: 42031

Modified:
   grass-addons/raster/r.fuzzy.system/description.html
   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/rule_parser.c
   grass-addons/raster/r.fuzzy.system/system.c
Log:
minor bugs fixing

Modified: grass-addons/raster/r.fuzzy.system/description.html
===================================================================
--- grass-addons/raster/r.fuzzy.system/description.html	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/description.html	2010-04-26 14:05:09 UTC (rev 42031)
@@ -93,8 +93,11 @@
 </DD>
 <h2>VISUAL OUTPUT</h2>
 <DT><b>coordinates</b></DT>
-<DD>Coordinates of points to which output universe, all consequents sets and agregate set. It is useful for visual presentation or detail analysis of fuzzy rules behaviour. In that cases calculations are peroforemd n=only for selected point.
+<DD>Coordinates of points for which output: universe, all consequents sets and agregate set. It is useful for visual presentation or detail analysis of fuzzy rules behaviour. In that cases calculations are peroforemd n=only for selected point.
+<DT><b>membership only flag</b></DT>
+<DD>Prints for all maps sat of values in map range (map universe) and values of fuzzy sets (linguistic values). Number of values is taken from resolution (default 100). This option is useful for visual control fuzzy set definitions for evrey map.
 
+
 <h2>OUTPUTS</h2>
 <DT><b>output</b></DT>
 <DD>Map containing deffuzified values. Map is always of type FCELLS and contains values defined in output universe. The output name must be the same as one of maps in maps definition file.

Modified: grass-addons/raster/r.fuzzy.system/fuzzylogic.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/fuzzylogic.c	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/fuzzylogic.c	2010-04-26 14:05:09 UTC (rev 42031)
@@ -2,7 +2,6 @@
 
 float fuzzy(FCELL cell, SETS * set)
 {
-
     float x;
 
     if (!set->side) {		/* both left and right */
@@ -83,7 +82,7 @@
 	break;
 
     case l_HAMACHER:
-	return (x == y == 0) ? 0 : (x * y) / ((x + y) - x * y);
+	return (x == 0||y == 0) ? 0 : (x * y) / ((x + y) - x * y);
 	break;
 
 

Modified: grass-addons/raster/r.fuzzy.system/helpers.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/helpers.c	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/helpers.c	2010-04-26 14:05:09 UTC (rev 42031)
@@ -32,7 +32,7 @@
 {
     register char *a, *b;
 
-    for (a = b = buf; *a == rem || *a == ' ' || *a == ' \t'; a++) ;
+    for (a = b = buf; *a == rem || *a == ' ' || *a == '\t'; a++) ;
     if (a != b)
 	while ((*b++ = *a++)) ;
 
@@ -83,3 +83,117 @@
 
     return 0;
 }
+
+void process_coors(char *answer)
+{
+
+    struct Cell_head window;
+    double x, y;
+    int i, j;
+    int r, c;
+    int num_points;
+    float result;
+
+    G_get_window(&window);
+    num_points = sscanf(answer, "%lf,%lf", &x, &y);
+
+    r = (int)G_easting_to_col(x, &window);
+    c = (int)G_northing_to_row(y, &window);
+    
+		get_rows(r);
+    get_cells(c);
+    result = implicate(); /* jump to different function */
+
+    for (i = 0; i < nrules; ++i)
+	fprintf(stdout, "ANTECEDENT %s: %5.3f\n", s_rules[i].outname,
+		antecedents[i]);
+
+  fprintf(stdout, "RESULT (deffuzified):  %5.3f\n", result);
+  
+  fprintf(stdout, "UNIVERSE,");
+    for (i = 0; i < nrules; ++i)
+	fprintf(stdout, "%s,", s_rules[i].outname);
+    fprintf(stdout, "AGREGATE \n");
+
+    for (i = 0; i < resolution; ++i)
+	for (j = 0; j < nrules + 2; ++j) {
+	    fprintf(stdout, "%5.3f", visual_output[i][j]);
+	    if (j < nrules + 1)
+		fprintf(stdout, ",");
+	    else
+		fprintf(stdout, "\n");
+	}
+
+			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(antecedents);
+    G_free(s_maps);
+    G_free(s_rules);
+   
+    exit(EXIT_SUCCESS);
+}
+
+
+void show_membership(void) {
+	int i,j,k;
+	STRING cur_mapset;
+	struct FPRange map_range;
+	DCELL min, max;
+	float* map_universe;
+	
+	resolution=resolution+1;
+	map_universe = (float *)G_calloc(resolution, sizeof(float));
+
+	for(i=0;i<nmaps;++i) {
+		
+		if(s_maps[i].output)
+			continue;
+		G_init_fp_range(&map_range);
+		cur_mapset = G_find_cell2(s_maps[i].name, "");
+		G_read_fp_range(s_maps[i].name,cur_mapset,&map_range);
+		G_get_fp_range_min_max(&map_range,&min, &max);
+		
+			for (k = 0; k < resolution; ++k)
+		map_universe[k] = min + ((max - min) / resolution) * k;
+		
+		fprintf(stdout,"#MAP: %s \n",s_maps[i].name);
+		
+		fprintf(stdout,"value,");
+			for(j=0;j<s_maps[i].nsets;++j) {
+		fprintf(stdout,"%s",s_maps[i].sets[j].setname);
+				if (j < s_maps[i].nsets-1)
+			fprintf(stdout, ",");
+				else
+			fprintf(stdout, "\n");
+			}
+			
+			for(k=0;k<resolution;++k) { 
+				fprintf(stdout,"%5.3f,",map_universe[k]);
+				for(j=0;j<s_maps[i].nsets;++j) {
+					fprintf(stdout,"%5.3f",
+					fuzzy(map_universe[k],&s_maps[i].sets[j]));
+						if (j < s_maps[i].nsets-1)
+							fprintf(stdout, ",");
+						else
+							fprintf(stdout, "\n");
+				}
+			}
+	}
+
+	G_free(map_universe);
+
+    for (i = 0; i < nmaps; ++i)
+	G_free(s_maps[i].sets);
+
+    G_free(s_maps);
+    G_free(s_rules);
+
+		exit(EXIT_SUCCESS);
+	
+}

Modified: grass-addons/raster/r.fuzzy.system/io.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/io.c	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/io.c	2010-04-26 14:05:09 UTC (rev 42031)
@@ -122,49 +122,6 @@
 	m_outputs[i].out_buf = G_allocate_f_raster_buf();
 
     }
-
+return 0;
 }
 
-int process_coors(char *answer)
-{
-
-    struct Cell_head window;
-    double x, y;
-    int i, j;
-    int r, c;
-    int num_points;
-    float result;
-
-    G_get_window(&window);
-    num_points = sscanf(answer, "%lf,%lf", &x, &y);
-
-    r = (int)G_easting_to_col(x, &window);
-    c = (int)G_northing_to_row(y, &window);
-
-    get_rows(r);
-    get_cells(c);
-    result = implicate();
-
-    for (i = 0; i < nrules; ++i)
-
-	fprintf(stdout, "ANTECEDENT %s: %5.3f\n", s_rules[i].outname,
-		antecedents[i]);
-    fprintf(stdout, "RESULT (deffuzified):  %5.3f\n", result);
-
-
-    fprintf(stdout, "UNIVERSE,");
-    for (i = 0; i < nrules; ++i)
-	fprintf(stdout, "%s,", s_rules[i].outname);
-    fprintf(stdout, "AGREGATE \n");
-
-    for (i = 0; i < resolution; ++i)
-	for (j = 0; j < nrules + 2; ++j) {
-	    fprintf(stdout, "%5.3f", visual_output[i][j]);
-	    if (j < nrules + 1)
-		fprintf(stdout, ",");
-	    else
-		fprintf(stdout, "\n");
-	}
-
-    exit(EXIT_SUCCESS);
-}

Modified: grass-addons/raster/r.fuzzy.system/local_proto.h
===================================================================
--- grass-addons/raster/r.fuzzy.system/local_proto.h	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/local_proto.h	2010-04-26 14:05:09 UTC (rev 42031)
@@ -94,7 +94,7 @@
     t_VAL			/* value a product of MAP and VARIABLE */
 } tokens;
 
-typedef struct _sets
+typedef struct
 {				/* membership definition */
     char setname[21];
     sides side;
@@ -104,7 +104,7 @@
     float height;
 } SETS;
 
-typedef struct map
+typedef struct
 {
     char name[30];
     int nsets;
@@ -117,7 +117,7 @@
     SETS *sets;
 } MAPS;
 
-typedef struct valuestacks
+typedef struct
 {
     float *value;
     SETS *set;
@@ -125,7 +125,7 @@
 } VALUES;
 
 
-typedef struct rule
+typedef struct
 {
     char outname[20];
     int output_set_index;
@@ -143,22 +143,22 @@
 } OUTPUTS;
 
 
-GLOBAL STRING var_name_file;
-GLOBAL STRING rule_name_file;
-GLOBAL STRING output;
-GLOBAL MAPS *s_maps;
-GLOBAL RULES *s_rules;
-GLOBAL OUTPUTS *m_outputs;
-GLOBAL float **visual_output;
-GLOBAL float *universe;
-GLOBAL float *antecedents;
-GLOBAL int nmaps, nrules, output_index, multiple, coor_proc;
-GLOBAL int resolution;
-GLOBAL implications implication;
-GLOBAL defuzz defuzzyfication;
-GLOBAL logics family;
+STRING var_name_file;
+STRING rule_name_file;
+STRING output;
+MAPS *s_maps;
+RULES *s_rules;
+OUTPUTS *m_outputs;
+float **visual_output;
+float *universe;
+float *antecedents;
+int nmaps, nrules, output_index, multiple, membership_only, coor_proc;
+int resolution;
+implications implication;
+defuzz defuzzyfication;
+logics family;
 
-GLOBAL char **rules;
+char **rules;
 
 int char_strip(char *buf, char rem);
 int char_copy(const char *buf, char *res, int start, int stop);
@@ -171,12 +171,13 @@
 int open_maps(void);
 int create_output_maps(void);
 int get_rows(int row);
-int get_cols(int col);
 int get_cells(int col);
 
+
 int parse_sets(SETS * set, char buf[], const char mapname[]);
 int parse_rules(int rule_num, int n, char buf[]);
-int process_coors(char *answer);
+void process_coors(char *answer);
+void show_membership(void);
 
 float implicate(void);
 float parse_expression(int n);

Modified: grass-addons/raster/r.fuzzy.system/main.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/main.c	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/main.c	2010-04-26 14:05:09 UTC (rev 42031)
@@ -25,7 +25,7 @@
 
     struct History history;
 
-    struct Flag *out_multiple;
+    struct Flag *out_multiple, *out_membership;
 
     int nrows, ncols;
     int row, col;
@@ -85,14 +85,20 @@
     in_coor_opt = G_define_option();	/* input coordinates de outlet */
     in_coor_opt->key = "coors";
     in_coor_opt->type = TYPE_STRING;
-    in_coor_opt->key_desc = "x,y";
+    in_coor_opt->key_desc = "x|y";
     in_coor_opt->answer = NULL;
     in_coor_opt->required = NO;
     in_coor_opt->multiple = NO;
     in_coor_opt->description =
-	"Coordinate of cell for witch output  detail data";
+	"Coordinate of cell for detail data (print end exit)";
     in_coor_opt->guisection = _("Visual Output");
 
+    out_membership = G_define_flag();
+    out_membership->key = 'o';
+    out_membership->description =
+	_("Print only membership values and exit");
+		out_membership->guisection = _("Visual Output");
+
     out_multiple = G_define_flag();
     out_multiple->key = 'm';
     out_multiple->description =
@@ -105,11 +111,11 @@
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-
     var_name_file = file_vars->answer;
     rule_name_file = file_rules->answer;
     output = opt_output->answer;
     multiple = (out_multiple->answer != 0);
+    membership_only = (out_membership->answer != 0);
     coor_proc = (in_coor_opt->answer) ? 1 : 0;
 
     resolution = atoi(par_resolution->answer);
@@ -152,9 +158,14 @@
 
     parse_map_file(var_name_file);
     parse_rule_file(rule_name_file);
+			
+			if (membership_only)
+    show_membership();
+
     get_universe();
     open_maps();
-
+	
+	antecedents = (float *)G_malloc(nrules * sizeof(float));
     if (coor_proc)
 	process_coors(in_coor_opt->answer);
 
@@ -165,13 +176,13 @@
 
     if (multiple)
 	create_output_maps();
-	antecedents = (float *)G_malloc(nrules * sizeof(float));
+
 	
     G_message("Calculate...");
 
     for (row = 0; row < nrows; ++row) {
-	G_percent(row, nrows, 2);
-	get_rows(row);
+		G_percent(row, nrows, 2);
+		get_rows(row);
 	for (col = 0; col < ncols; ++col) {
 	    if (get_cells(col)) {
 		G_set_f_null_value(&out_buf[col], 1);
@@ -207,18 +218,19 @@
     }
     G_percent(row, nrows, 2);
 
-
     G_message("Close...");
     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(antecedents);
     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);
@@ -232,6 +244,7 @@
 	    G_command_history(&history);
 	    G_write_history(m_outputs[i].output_name, &history);
 	}
+
    G_message("Done!");
     exit(EXIT_SUCCESS);
 

Modified: grass-addons/raster/r.fuzzy.system/rule_parser.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/rule_parser.c	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/rule_parser.c	2010-04-26 14:05:09 UTC (rev 42031)
@@ -49,7 +49,9 @@
 	/* next rule */
     }
 
-
+			for (i = 0; i < nrules; ++i)
+		G_free(rules[i]);
+		G_free(rules);
     fclose(fd);
     return 0;
 
@@ -162,8 +164,6 @@
 
 	done = 1;
 	for (i = 0; i < stack_top; ++i) {	/* most external loop */
-	    //if(i>0)
-	    //G_message("PARSE STACK %d %d",s_rules[rule_num].work_stack[i-1],i-1)  ;
 	    if (*s_rules[rule_num].parse_stack[i] == '{') {
 
 		s_rules[rule_num].work_stack[work_stack_pos] = t_START;

Modified: grass-addons/raster/r.fuzzy.system/system.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/system.c	2010-04-25 11:18:42 UTC (rev 42030)
+++ grass-addons/raster/r.fuzzy.system/system.c	2010-04-26 14:05:09 UTC (rev 42031)
@@ -36,7 +36,7 @@
 	if (defuzzyfication > d_BISECTOR && antecedents[j] < max_antecedent)
 	    continue;
 	    
-	    set_index = s_rules[j].output_set_index; //było niżej
+	    set_index = s_rules[j].output_set_index;
 
 	for (i = 0; i < resolution; ++i) {
 	    
@@ -66,9 +66,9 @@
     /*  tokens and actions must heve the same order */
     actions parse_tab[t_size][t_size] = {
 	/* stk -----------INPUT------------------ */
-	/*        {    &    |    ~    =    (    )    }     */
-	/*        --   --   --   --   --   --   --   --
-	   /* { */ {E, S, S, E, E, S, E, A},
+	/*  		{   &  | ~   =  (  )  }   */
+	/*      --  -- -- -- -- -- -- -- */
+	/* { */ {E, S, S, E, E, S, E, A},
 	/* & */ {E, R, R, E, E, S, R, R},
 	/* | */ {E, R, R, E, E, S, R, R},
 	/* ~ */ {E, E, E, E, E, E, E, E},
@@ -87,9 +87,7 @@
     int set_index;
     float f_value;
 
-
     do {
-
 	if (s_rules[n].work_stack[i] == t_START) {	/* first token */
 	    if (i > 0)
 		G_fatal_error("operator stack error, contact author");
@@ -102,14 +100,12 @@
 		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 == '~') ? f_not(f_value,
-								family) :
-		f_value;
+		(s_rules[n].value_stack[i].oper == '~') ? 
+			f_not(f_value, family) :	f_value;
 	    continue;
 	}
 
 	if (s_rules[n].work_stack[i] < t_size) {
-
 	    switch (parse_tab[operator_stack[opr_top]]
 		    [s_rules[n].work_stack[i]]) {
 
@@ -149,13 +145,17 @@
 		break;
 
 	    case A:		/* accept */
+	
 		if (!val_top)
 		    G_fatal_error("Last Stack error, contact autor");
 		return values_stack[val_top];
-
+	
 	    }
 	}
+
     } while (s_rules[n].work_stack[i++] != t_STOP);
+    
+    G_fatal_error("Parse Stack empty, contact autor");
 }
 
 



More information about the grass-commit mailing list