[GRASS-SVN] r41681 - in grass-addons/raster: r.fuzzy r.fuzzy.logic
r.fuzzy.system
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Apr 3 04:59:38 EDT 2010
Author: hamish
Date: 2010-04-03 04:59:38 -0400 (Sat, 03 Apr 2010)
New Revision: 41681
Modified:
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
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
grass-addons/raster/r.fuzzy/fuzzy.c
grass-addons/raster/r.fuzzy/local_proto.h
grass-addons/raster/r.fuzzy/main.c
Log:
run grass_indent.sh
Modified: grass-addons/raster/r.fuzzy/fuzzy.c
===================================================================
--- grass-addons/raster/r.fuzzy/fuzzy.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy/fuzzy.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,52 +1,53 @@
#include <grass/glocale.h>
#include "local_proto.h"
-float fuzzy (FCELL cell) {
+float fuzzy(FCELL cell)
+{
- float x, m;
-
- if (!side) { /* both left and right */
-
- if (cell <= p[0] || cell >= p[3])
- return 0.;
- if (cell >= p[1] && cell <= p[2])
- return 1.;
+ float x, m;
- x = (cell<p[1]) ? (cell-p[0])/(p[1]-p[0])
- : (p[3]-cell)/(p[3]-p[2]);
- }
-
- if (side) { /* left: 1 OR right: 2 */
+ if (!side) { /* both left and right */
- if (cell <= p[0])
- return (side == 1) ? 0. : 1.;
- if (cell >= p[1])
- return (side == 1) ? 1. : 0.;
-
- x = (side == 1) ? (cell-p[0])/(p[1]-p[0])
- : (p[1]-cell)/(p[1]-p[0]);
- }
+ if (cell <= p[0] || cell >= p[3])
+ return 0.;
+ if (cell >= p[1] && cell <= p[2])
+ return 1.;
- if (type == LINEAR)
+ x = (cell < p[1]) ? (cell - p[0]) / (p[1] - p[0])
+ : (p[3] - cell) / (p[3] - p[2]);
+ }
+
+ if (side) { /* left: 1 OR right: 2 */
+
+ if (cell <= p[0])
+ return (side == 1) ? 0. : 1.;
+ if (cell >= p[1])
+ return (side == 1) ? 1. : 0.;
+
+ x = (side == 1) ? (cell - p[0]) / (p[1] - p[0])
+ : (p[1] - cell) / (p[1] - p[0]);
+ }
+
+ if (type == LINEAR)
return x;
-
- if (type == JSHAPE) {
- m = (shape < 0) ? pow(2,1+shape)
- : pow(2,exp(2*shape));
- return pow(tan(x * PI4),m);
- }
-
- if (type == GSHAPE) {
- m = (shape > 0) ? pow(2,1-shape)
- : pow(2,exp(-2*shape));
- return pow(tan(x * PI4),1/m);
- }
-
- if(type==SSHAPE) {
- m = pow(2,exp(2*abs(shape)));
- return (shape < 0) ? pow(1-cos(x*PI2),m)
- : pow(sin(x*PI2),m);
- }
-return -1; /* error */
+ if (type == JSHAPE) {
+ m = (shape < 0) ? pow(2, 1 + shape)
+ : pow(2, exp(2 * shape));
+ return pow(tan(x * PI4), m);
+ }
+
+ if (type == GSHAPE) {
+ m = (shape > 0) ? pow(2, 1 - shape)
+ : pow(2, exp(-2 * shape));
+ return pow(tan(x * PI4), 1 / m);
+ }
+
+ if (type == SSHAPE) {
+ m = pow(2, exp(2 * abs(shape)));
+ return (shape < 0) ? pow(1 - cos(x * PI2), m)
+ : pow(sin(x * PI2), m);
+ }
+
+ return -1; /* error */
}
Modified: grass-addons/raster/r.fuzzy/local_proto.h
===================================================================
--- grass-addons/raster/r.fuzzy/local_proto.h 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy/local_proto.h 2010-04-03 08:59:38 UTC (rev 41681)
@@ -5,18 +5,18 @@
#include <grass/gis.h>
#include <grass/glocale.h>
/*
-PI2= PI/2
-PI4= PI/4
-*/
+ PI2= PI/2
+ PI4= PI/4
+ */
#ifndef PI2
- #define PI2 (2*atan(1))
+#define PI2 (2*atan(1))
#endif
#ifndef PI4
- #define PI4 (atan(1))
+#define PI4 (atan(1))
#endif
-
+
#define LINEAR 0
#define SSHAPE 1
#define JSHAPE 2
@@ -29,7 +29,7 @@
char *input, *output;
float shape, height;
int type, side;
-double p[4]; /* inflection points */
+double p[4]; /* inflection points */
int num_points;
-float fuzzy (FCELL cell);
+float fuzzy(FCELL cell);
Modified: grass-addons/raster/r.fuzzy/main.c
===================================================================
--- grass-addons/raster/r.fuzzy/main.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy/main.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -19,228 +19,227 @@
int main(int argc, char *argv[])
{
- struct GModule *module;
- struct Option *par_input,
- *par_output,
- *par_points,
- *par_side,
- *par_type,
- *par_height,
- *par_shape;
-
- struct Cell_head cellhd;
- struct History history;
-
-
- char *mapset;
- int nrows, ncols;
- int row, col;
- int infd, outfd;
- void *in_buf;
- unsigned char *out_buf;
- RASTER_MAP_TYPE raster_type;
- FCELL tmp=0;
-
- G_gisinit(argv[0]);
-
- par_input = G_define_standard_option(G_OPT_R_INPUT);
- par_input->description = _("Raster map to be fuzzified");
-
- par_output = G_define_standard_option(G_OPT_R_OUTPUT);
- par_output->description = _("Membership map");
-
- par_points=G_define_option();
- par_points->key = "points";
- par_points->type = "TYPE_STRING";
- par_points->answer = "a,b[,c,d]";
- par_points->multiple = YES;
- par_points->required = YES;
- par_points->description = _("Inflection points: a,b[,c,d]");
-
- par_side=G_define_option();
- par_side->key = "side";
- par_side->type = "TYPE_STRING";
- par_side->options = "both,left,right";
- par_side->answer = "both";
- par_side->multiple = NO;
- par_side->required = YES;
- par_side->description = _("Fuzzy range");
-
- par_type=G_define_option();
- par_type->key = "boundary";
- par_type->type = "TYPE_STRING";
- par_type->options = "Linear,S-shaped,J-shaped,G-shaped";
- par_type->answer = "S-shaped";
- par_type->multiple = NO;
- par_type->required = YES;
- par_type->description = _("Type of fuzzy boundaries");
- par_type->guisection = _("Default options");
-
- par_shape=G_define_option();
- par_shape->key = "shape";
- par_shape->type = "TYPE_FLOAT";
- par_shape->options = "-1 to 1";
- par_shape->answer = "0.";
- par_shape->multiple = NO;
- par_shape->required = YES;
- par_shape->description = _("Shape modifier: -1 to 1");
- par_shape->guisection = _("Default options");
-
- par_height=G_define_option();
- par_height->key = "height";
- par_height->type = "TYPE_FLOAT";
- par_height->options = "0 to 1";
- par_height->answer = "1";
- par_height->multiple = NO;
- par_height->required = YES;
- par_height->description = _("Membership height: 0 to 1");
- par_height->guisection = _("Default options");
-
- if (G_parser(argc, argv))
+ struct GModule *module;
+ struct Option *par_input,
+ *par_output,
+ *par_points, *par_side, *par_type, *par_height, *par_shape;
+
+ struct Cell_head cellhd;
+ struct History history;
+
+
+ char *mapset;
+ int nrows, ncols;
+ int row, col;
+ int infd, outfd;
+ void *in_buf;
+ unsigned char *out_buf;
+ RASTER_MAP_TYPE raster_type;
+ FCELL tmp = 0;
+
+ G_gisinit(argv[0]);
+
+ par_input = G_define_standard_option(G_OPT_R_INPUT);
+ par_input->description = _("Raster map to be fuzzified");
+
+ par_output = G_define_standard_option(G_OPT_R_OUTPUT);
+ par_output->description = _("Membership map");
+
+ par_points = G_define_option();
+ par_points->key = "points";
+ par_points->type = "TYPE_STRING";
+ par_points->answer = "a,b[,c,d]";
+ par_points->multiple = YES;
+ par_points->required = YES;
+ par_points->description = _("Inflection points: a,b[,c,d]");
+
+ par_side = G_define_option();
+ par_side->key = "side";
+ par_side->type = "TYPE_STRING";
+ par_side->options = "both,left,right";
+ par_side->answer = "both";
+ par_side->multiple = NO;
+ par_side->required = YES;
+ par_side->description = _("Fuzzy range");
+
+ par_type = G_define_option();
+ par_type->key = "boundary";
+ par_type->type = "TYPE_STRING";
+ par_type->options = "Linear,S-shaped,J-shaped,G-shaped";
+ par_type->answer = "S-shaped";
+ par_type->multiple = NO;
+ par_type->required = YES;
+ par_type->description = _("Type of fuzzy boundaries");
+ par_type->guisection = _("Default options");
+
+ par_shape = G_define_option();
+ par_shape->key = "shape";
+ par_shape->type = "TYPE_FLOAT";
+ par_shape->options = "-1 to 1";
+ par_shape->answer = "0.";
+ par_shape->multiple = NO;
+ par_shape->required = YES;
+ par_shape->description = _("Shape modifier: -1 to 1");
+ par_shape->guisection = _("Default options");
+
+ par_height = G_define_option();
+ par_height->key = "height";
+ par_height->type = "TYPE_FLOAT";
+ par_height->options = "0 to 1";
+ par_height->answer = "1";
+ par_height->multiple = NO;
+ par_height->required = YES;
+ par_height->description = _("Membership height: 0 to 1");
+ par_height->guisection = _("Default options");
+
+ if (G_parser(argc, argv))
exit(EXIT_FAILURE);
-
- input=par_input->answer;
- output=par_output->answer;
-
- if (!strcmp(par_type->answer, "Linear"))
- type=LINEAR;
- else if (!strcmp(par_type->answer, "S-shaped"))
- type=SSHAPE;
- else if (!strcmp(par_type->answer, "J-shaped"))
- type=JSHAPE;
- else if (!strcmp(par_type->answer, "G-shaped"))
- type=GSHAPE;
-
- if (!strcmp(par_side->answer, "both"))
- side=BOTH;
- else if (!strcmp(par_side->answer, "left"))
- side=LEFT;
- else if (!strcmp(par_side->answer, "right"))
- side=RIGHT;
-
- shape = atof(par_shape->answer);
- if(shape<-1. || shape>1.)
+
+ input = par_input->answer;
+ output = par_output->answer;
+
+ if (!strcmp(par_type->answer, "Linear"))
+ type = LINEAR;
+ else if (!strcmp(par_type->answer, "S-shaped"))
+ type = SSHAPE;
+ else if (!strcmp(par_type->answer, "J-shaped"))
+ type = JSHAPE;
+ else if (!strcmp(par_type->answer, "G-shaped"))
+ type = GSHAPE;
+
+ if (!strcmp(par_side->answer, "both"))
+ side = BOTH;
+ else if (!strcmp(par_side->answer, "left"))
+ side = LEFT;
+ else if (!strcmp(par_side->answer, "right"))
+ side = RIGHT;
+
+ shape = atof(par_shape->answer);
+ if (shape < -1. || shape > 1.)
G_fatal_error(_("Shape modifier must be between -1 and 1"));
-
- height = atof(par_height->answer);
- if(height>1 || height<0)
+
+ height = atof(par_height->answer);
+ if (height > 1 || height < 0)
G_fatal_error(_("Heght modifier must be between 0 and 1"));
-
- num_points=sscanf(par_points->answer,
- "%lf,%lf,%lf,%lf",
- &p[0], &p[1], &p[2], &p[3]);
-
- if(!side && num_points != 4)
+
+ num_points = sscanf(par_points->answer,
+ "%lf,%lf,%lf,%lf", &p[0], &p[1], &p[2], &p[3]);
+
+ if (!side && num_points != 4)
G_fatal_error(_("Wrong number of values: got %d but need 4"),
num_points);
-
- if(side && num_points != 2)
+
+ if (side && num_points != 2)
G_fatal_error(_("Wrong number of values: got %d but need 2"),
num_points);
-
- if (num_points==2) {
- if( p[0]>p[1])
- G_fatal_error(_("Point sequence must be: a <= b"));
- } else {
- if( p[0]>p[1]
- ||p[1]>p[2]
- ||p[2]>p[3])
- G_fatal_error(_("Point sequence must be: a <= b; b <= c; c <= d;"));
- }
- /* end of interface */
+ if (num_points == 2) {
+ if (p[0] > p[1])
+ G_fatal_error(_("Point sequence must be: a <= b"));
+ }
+ else {
+ if (p[0] > p[1]
+ || p[1] > p[2]
+ || p[2] > p[3])
+ G_fatal_error(_("Point sequence must be: a <= b; b <= c; c <= d;"));
+ }
- mapset = G_find_cell2(input, "");
-
- if (mapset == NULL)
+ /* end of interface */
+
+ mapset = G_find_cell2(input, "");
+
+ if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), input);
-
- if ((infd = G_open_cell_old(input, mapset)) < 0)
+
+ if ((infd = G_open_cell_old(input, mapset)) < 0)
G_fatal_error(_("Unable to open raster map <%s>"), input);
-
- if (G_get_cellhd(input, mapset, &cellhd) < 0)
+
+ if (G_get_cellhd(input, mapset, &cellhd) < 0)
G_fatal_error(_("Unable to read file header of <%s>"), input);
-
-
- nrows = G_window_rows();
- ncols = G_window_cols();
- raster_type = G_get_raster_map_type(infd);
-
- if ((outfd = G_open_raster_new(output, FCELL_TYPE)) < 0)
+
+
+ nrows = G_window_rows();
+ ncols = G_window_cols();
+ raster_type = G_get_raster_map_type(infd);
+
+ if ((outfd = G_open_raster_new(output, FCELL_TYPE)) < 0)
G_fatal_error(_("Unable to create raster map <%s>"), output);
-
- in_buf = G_allocate_raster_buf(raster_type);
- out_buf = G_allocate_raster_buf(FCELL_TYPE);
-
- /* processing */
- for (row = 0; row < nrows; row++) {
- G_percent(row, nrows, 2);
- CELL c;
- FCELL f;
- DCELL d;
-
- if (G_get_raster_row(infd, in_buf, row, raster_type)<0) {
- G_close_cell(infd);
- G_fatal_error(_("Cannot to read <%s> at row <%d>"), input,row);
- }
- for (col = 0; col < ncols; col++) {
-
- switch (raster_type) {
+ in_buf = G_allocate_raster_buf(raster_type);
+ out_buf = G_allocate_raster_buf(FCELL_TYPE);
- case CELL_TYPE:
- c = ((CELL *) in_buf)[col];
- if(G_is_null_value(&c,CELL_TYPE))
- G_set_f_null_value(&tmp, 1);
- else {
- if(0>(tmp = fuzzy ((FCELL) c)))
- G_warning("Cannot determine memebership at row %d, col %d", row,col);
- ((FCELL *) out_buf)[col] = tmp;
- }
- break;
+ /* processing */
+ for (row = 0; row < nrows; row++) {
+ G_percent(row, nrows, 2);
+ CELL c;
+ FCELL f;
+ DCELL d;
- case FCELL_TYPE:
- f = ((FCELL *) in_buf)[col];
- if(G_is_null_value(&f,FCELL_TYPE))
- G_set_f_null_value(&tmp, 1);
- else {
- tmp = fuzzy((FCELL) f);
- if(0>(tmp = fuzzy((FCELL) f)))
- G_warning("Cannot determine memebership at row %d, col %d", row,col);
- ((FCELL *) out_buf)[col] = tmp;
- }
- break;
+ if (G_get_raster_row(infd, in_buf, row, raster_type) < 0) {
+ G_close_cell(infd);
+ G_fatal_error(_("Cannot to read <%s> at row <%d>"), input, row);
+ }
- case DCELL_TYPE:
- d = ((DCELL *) in_buf)[col];
- if(G_is_null_value(&d,DCELL_TYPE))
- G_set_f_null_value(&tmp, 1);
- else {
- if(0>(tmp = fuzzy ((FCELL) d)))
- G_warning("Cannot determine memebership at row %d, col %d", row,col);
- ((FCELL *) out_buf)[col] = tmp;
- }
- break;
- }
+ for (col = 0; col < ncols; col++) {
+ switch (raster_type) {
+
+ case CELL_TYPE:
+ c = ((CELL *) in_buf)[col];
+ if (G_is_null_value(&c, CELL_TYPE))
+ G_set_f_null_value(&tmp, 1);
+ else {
+ if (0 > (tmp = fuzzy((FCELL) c)))
+ G_warning
+ ("Cannot determine memebership at row %d, col %d",
+ row, col);
+ ((FCELL *) out_buf)[col] = tmp;
}
- if (G_put_raster_row(outfd, out_buf, FCELL_TYPE) < 0)
+ break;
+
+ case FCELL_TYPE:
+ f = ((FCELL *) in_buf)[col];
+ if (G_is_null_value(&f, FCELL_TYPE))
+ G_set_f_null_value(&tmp, 1);
+ else {
+ tmp = fuzzy((FCELL) f);
+ if (0 > (tmp = fuzzy((FCELL) f)))
+ G_warning
+ ("Cannot determine memebership at row %d, col %d",
+ row, col);
+ ((FCELL *) out_buf)[col] = tmp;
+ }
+ break;
+
+ case DCELL_TYPE:
+ d = ((DCELL *) in_buf)[col];
+ if (G_is_null_value(&d, DCELL_TYPE))
+ G_set_f_null_value(&tmp, 1);
+ else {
+ if (0 > (tmp = fuzzy((FCELL) d)))
+ G_warning
+ ("Cannot determine memebership at row %d, col %d",
+ row, col);
+ ((FCELL *) out_buf)[col] = tmp;
+ }
+ break;
+ }
+
+ }
+ if (G_put_raster_row(outfd, out_buf, FCELL_TYPE) < 0)
G_fatal_error(_("Failed writing raster map <%s>"), output);
- } /* end for */
-
- G_free(in_buf);
- G_free(out_buf);
+ } /* end for */
- G_close_cell(infd);
- G_close_cell(outfd);
+ G_free(in_buf);
+ G_free(out_buf);
- G_short_history(output, "raster", &history);
- G_command_history(&history);
- G_write_history(output, &history);
-
- exit(EXIT_SUCCESS);
-}
+ G_close_cell(infd);
+ G_close_cell(outfd);
-
-
+ G_short_history(output, "raster", &history);
+ G_command_history(&history);
+ G_write_history(output, &history);
+
+ exit(EXIT_SUCCESS);
+}
Modified: grass-addons/raster/r.fuzzy.logic/local_proto.h
===================================================================
--- grass-addons/raster/r.fuzzy.logic/local_proto.h 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.logic/local_proto.h 2010-04-03 08:59:38 UTC (rev 41681)
@@ -11,7 +11,7 @@
#define _IMP 4
#define ZADEH 1
-#define PRODUCT 2
+#define PRODUCT 2
#define DRASTIC 3
#define LUKASIEWICZ 4
#define FODOR 5
@@ -25,4 +25,3 @@
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);
-
Modified: grass-addons/raster/r.fuzzy.logic/logic.c
===================================================================
--- grass-addons/raster/r.fuzzy.logic/logic.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.logic/logic.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -2,104 +2,107 @@
#include <grass/glocale.h>
#include "local_proto.h"
-float f_and(float x, float y, int family) {
+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;
-
+ switch (family) {
-
- }
- return -1; /* error */
-}
+ case ZADEH:
+ return MIN(x, y);
+ break;
-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 */
+ 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_imp(float x, float y, int family) {
-
- switch (family) {
-
- case ZADEH:
- return (x <= y) ? 1 : y;
- break;
+float f_or(float x, float y, int family)
+{
+ float b;
- 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 */
+ 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_not(float x, int family) {
-
- if (family==HAMACHER)
- return (1-x)/(1+x);
- else
- return ((1-x)<0 || (1-x)>1) ? -1 : 1-x;
+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;
+}
Modified: grass-addons/raster/r.fuzzy.logic/main.c
===================================================================
--- grass-addons/raster/r.fuzzy.logic/main.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.logic/main.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -3,7 +3,7 @@
* 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
+ * 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
@@ -21,232 +21,237 @@
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;
+ struct GModule *module;
+ struct Option *par_inputx,
+ *par_inputy, *par_output, *par_operation, *par_family;
- 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");
+ struct Cell_head cellhdx;
+ struct Cell_head cellhdy;
+ struct Range membership_range;
+ struct History history;
- 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))
+ 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)
+
+ 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)
+
+ 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)
+
+ 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)
+
+ 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)
+
+ 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)
+
+ 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) {
-
+
+ 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);
-
+
+ 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_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");
-
-
+ 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)
+ } /* 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);
+ 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);
}
-
- 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);
+ 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);
}
Modified: grass-addons/raster/r.fuzzy.system/fuzzylogic.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/fuzzylogic.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/fuzzylogic.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,118 +1,136 @@
#include "local_proto.h"
-float fuzzy(FCELL cell, SETS* set) {
+float fuzzy(FCELL cell, SETS * set)
+{
- float x;
-
- if (!set->side) { /* both left and right */
-
- if (cell <= set->points[0] || cell >= set->points[3])
- return 0.;
- if (cell >= set->points[1] && cell <= set->points[2])
- return 1.;
+ float x;
- x = (cell < set->points[1]) ? (cell - set->points[0])/(set->points[1] - set->points[0])
- : (set->points[3] - cell)/(set->points[3] - set->points[2]);
- }
-
- if (set->side) { /* left: 1 OR right: 2 */
+ if (!set->side) { /* both left and right */
- if (cell <= set->points[0])
- return (set->side == 1) ? 0. : 1.;
- if (cell >= set->points[1])
- return (set->side == 1) ? 1. : 0.;
-
- x = (set->side == s_LEFT) ? (cell - set->points[0])/(set->points[1] - set->points[0])
- : (set->points[1] - cell)/(set->points[1]-set->points[0]);
- }
+ if (cell <= set->points[0] || cell >= set->points[3])
+ return 0.;
+ if (cell >= set->points[1] && cell <= set->points[2])
+ return 1.;
- switch (set->shape) {
- case s_LINEAR: break;
- case s_SSHAPE: x= pow(sin(x*PI2),2); break;
- case s_JSHAPE: x= pow(tan(x * PI4),2); break;
- case s_GSHAPE: x= pow(tan(x * PI4),1/2.); break;
- }
-
- if(set->hedge)
- x= (set->hedge>0) ? pow(x,pow(2,set->hedge)) : pow(x,pow(0.5,(-set->hedge)));
+ x = (cell <
+ set->points[1]) ? (cell - set->points[0]) / (set->points[1] -
+ set->points[0])
+ : (set->points[3] - cell) / (set->points[3] - set->points[2]);
+ }
- if(set->height<1)
- x= x * set->height;
+ if (set->side) { /* left: 1 OR right: 2 */
-return (x>=0 && x<=1) ? x : -1;
+ if (cell <= set->points[0])
+ return (set->side == 1) ? 0. : 1.;
+ if (cell >= set->points[1])
+ return (set->side == 1) ? 1. : 0.;
+
+ x = (set->side ==
+ s_LEFT) ? (cell - set->points[0]) / (set->points[1] -
+ set->points[0])
+ : (set->points[1] - cell) / (set->points[1] - set->points[0]);
+ }
+
+ switch (set->shape) {
+ case s_LINEAR:
+ break;
+ case s_SSHAPE:
+ x = pow(sin(x * PI2), 2);
+ break;
+ case s_JSHAPE:
+ x = pow(tan(x * PI4), 2);
+ break;
+ case s_GSHAPE:
+ x = pow(tan(x * PI4), 1 / 2.);
+ break;
+ }
+
+ if (set->hedge)
+ x = (set->hedge > 0) ? pow(x, pow(2, set->hedge)) : pow(x,
+ pow(0.5,
+ (-set->
+ hedge)));
+
+ if (set->height < 1)
+ x = x * set->height;
+
+ return (x >= 0 && x <= 1) ? x : -1;
}
-float f_and(float x, float y, int family) {
+float f_and(float x, float y, int family)
+{
- switch (family) {
-
- case l_ZADEH:
- return MIN(x,y);
- break;
-
- case l_PRODUCT:
- return x*y;
- break;
-
- case l_DRASTIC:
- return MAX(x, y) == 1 ? MIN(x, y) : 0;
- break;
-
- case l_LUKASIEWICZ:
- return MAX((x+y-1),0);
- break;
-
- case l_FODOR:
- return (x+y)>1 ? MIN(x,y) : 0;
- break;
-
- case l_HAMACHER:
- return (x==y==0) ? 0 : (x*y)/((x+y)-x*y);
- break;
-
+ switch (family) {
-
- }
- return -1; /* error */
+ case l_ZADEH:
+ return MIN(x, y);
+ break;
+
+ case l_PRODUCT:
+ return x * y;
+ break;
+
+ case l_DRASTIC:
+ return MAX(x, y) == 1 ? MIN(x, y) : 0;
+ break;
+
+ case l_LUKASIEWICZ:
+ return MAX((x + y - 1), 0);
+ break;
+
+ case l_FODOR:
+ return (x + y) > 1 ? MIN(x, y) : 0;
+ break;
+
+ case l_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 l_ZADEH:
- return MAX(x,y);
- break;
-
- case l_PRODUCT:
- return x + y -x * y;
- break;
-
- case l_DRASTIC:
- return (MIN(x, y) == 0) ? MAX(x, y) : 1;
- break;
-
- case l_LUKASIEWICZ:
- return MIN((x+y),1);
- break;
-
- case l_FODOR:
- return (x+y<1) ? MAX(x,y) : 1;
- break;
-
- case l_HAMACHER:
- return (x+y)/(1+x*y);
- break;
- }
- return -1; /* error */
+float f_or(float x, float y, int family)
+{
+ float b;
+
+ switch (family) {
+
+ case l_ZADEH:
+ return MAX(x, y);
+ break;
+
+ case l_PRODUCT:
+ return x + y - x * y;
+ break;
+
+ case l_DRASTIC:
+ return (MIN(x, y) == 0) ? MAX(x, y) : 1;
+ break;
+
+ case l_LUKASIEWICZ:
+ return MIN((x + y), 1);
+ break;
+
+ case l_FODOR:
+ return (x + y < 1) ? MAX(x, y) : 1;
+ break;
+
+ case l_HAMACHER:
+ return (x + y) / (1 + x * y);
+ break;
+ }
+ return -1; /* error */
}
-float f_not(float x, int family) {
-
- if (family==l_HAMACHER)
- return (1-x)/(1+x);
- else
- return ((1-x)<0 || (1-x)>1) ? -1 : 1-x;
+float f_not(float x, int family)
+{
+
+ if (family == l_HAMACHER)
+ return (1 - x) / (1 + x);
+ else
+ return ((1 - x) < 0 || (1 - x) > 1) ? -1 : 1 - x;
}
-
Modified: grass-addons/raster/r.fuzzy.system/helpers.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/helpers.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/helpers.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,81 +1,85 @@
#include "local_proto.h"
-int get_nsets(FILE* fd, fpos_t position) {
-
- int nsets=0;
- fsetpos (fd, &position);
- char buf[500];
+int get_nsets(FILE * fd, fpos_t position)
+{
- fgetpos (fd, &position);
+ int nsets = 0;
-
- while (fgets(buf, sizeof buf, fd)) {
- G_strip(buf);
+ fsetpos(fd, &position);
+ char buf[500];
- if (*buf == '#' || *buf == 0 || *buf=='\n')
- continue;
-
- if (*buf == '$')
- nsets++;
-
- if (*buf == '%')
- break;
- }
- fsetpos (fd, &position);
- return nsets;
+ fgetpos(fd, &position);
+
+
+ while (fgets(buf, sizeof buf, fd)) {
+ G_strip(buf);
+
+ if (*buf == '#' || *buf == 0 || *buf == '\n')
+ continue;
+
+ if (*buf == '$')
+ nsets++;
+
+ if (*buf == '%')
+ break;
+ }
+ fsetpos(fd, &position);
+ return nsets;
}
int char_strip(char *buf, char rem)
{
- register char *a, *b;
- for (a = b = buf; *a == rem || *a == ' ' || *a == ' \t'; a++) ;
- if (a != b)
+ register char *a, *b;
+
+ for (a = b = buf; *a == rem || *a == ' ' || *a == ' \t'; a++) ;
+ if (a != b)
while ((*b++ = *a++)) ;
-
- return 0;
+
+ return 0;
}
int char_copy(const char *buf, char *res, int start, int stop)
{
- register int i,j;
- for(i=start,j=0;i<stop;res[j++] = buf[i++]);
- res[j]='\0';
+ register int i, j;
- return 0;
+ for (i = start, j = 0; i < stop; res[j++] = buf[i++]) ;
+ res[j] = '\0';
+
+ return 0;
}
-int get_universe (void) {
- int i,j;
- float min=100000000., max=0;
+int get_universe(void)
+{
+ int i, j;
+ float min = 100000000., max = 0;
- resolution +=1;
-
- for (i=0;i<nmaps;++i) {
- if (s_maps[i].output)
- break;
- }
- output_index=i;
-
- for (j=0;j<s_maps[i].nsets;++j) {
-
- min = (s_maps[i].sets[j].points[0]<min) ?
- s_maps[i].sets[j].points[0] : min;
-
- if (s_maps[i].sets[j].side)
- max = (s_maps[i].sets[j].points[1]>max) ?
- s_maps[i].sets[j].points[1] : max;
- else
- max = (s_maps[i].sets[j].points[3]>max) ?
- s_maps[i].sets[j].points[3] : max;
- }
-
-
- universe =(float*) G_calloc(resolution, sizeof(float));
- for (i=0;i<resolution;++i)
- universe[i]= min + ((max-min)/resolution) * i;
+ resolution += 1;
- return 0;
+ for (i = 0; i < nmaps; ++i) {
+ if (s_maps[i].output)
+ break;
+ }
+ output_index = i;
+
+ for (j = 0; j < s_maps[i].nsets; ++j) {
+
+ min = (s_maps[i].sets[j].points[0] < min) ?
+ s_maps[i].sets[j].points[0] : min;
+
+ if (s_maps[i].sets[j].side)
+ max = (s_maps[i].sets[j].points[1] > max) ?
+ s_maps[i].sets[j].points[1] : max;
+ else
+ max = (s_maps[i].sets[j].points[3] > max) ?
+ s_maps[i].sets[j].points[3] : max;
+ }
+
+
+ universe = (float *)G_calloc(resolution, sizeof(float));
+ for (i = 0; i < resolution; ++i)
+ universe[i] = min + ((max - min) / resolution) * i;
+
+ return 0;
}
-
Modified: grass-addons/raster/r.fuzzy.system/io.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/io.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/io.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,156 +1,170 @@
#include "local_proto.h"
-
-int open_maps(void) {
-
- int i;
- char* mapset;
- struct Cell_head cellhd;
-
- for (i=0;i<nmaps;++i) {
-
- if (s_maps[i].output) {
- s_maps[i].in_buf=NULL;
- continue;
- }
-
- mapset = G_find_cell2(s_maps[i].name, "");
-
- if (mapset == NULL)
- G_fatal_error(_("Raster map <%s> not found"), s_maps[i].name);
-
- if ((s_maps[i].cfd = G_open_cell_old(s_maps[i].name, mapset)) < 0)
- G_fatal_error(_("Unable to open raster map <%s>"), s_maps[i].name);
-
- if (G_get_cellhd(s_maps[i].name, mapset, &cellhd) < 0)
- G_fatal_error(_("Unable to read file header of <%s>"), s_maps[i].name);
-
- s_maps[i].raster_type = G_get_raster_map_type(s_maps[i].cfd);
- s_maps[i].in_buf = G_allocate_raster_buf(s_maps[i].raster_type);
-
+
+int open_maps(void)
+{
+
+ int i;
+ char *mapset;
+ struct Cell_head cellhd;
+
+ for (i = 0; i < nmaps; ++i) {
+
+ if (s_maps[i].output) {
+ s_maps[i].in_buf = NULL;
+ continue;
}
-return 0;
+ mapset = G_find_cell2(s_maps[i].name, "");
+
+ if (mapset == NULL)
+ G_fatal_error(_("Raster map <%s> not found"), s_maps[i].name);
+
+ if ((s_maps[i].cfd = G_open_cell_old(s_maps[i].name, mapset)) < 0)
+ G_fatal_error(_("Unable to open raster map <%s>"),
+ s_maps[i].name);
+
+ if (G_get_cellhd(s_maps[i].name, mapset, &cellhd) < 0)
+ G_fatal_error(_("Unable to read file header of <%s>"),
+ s_maps[i].name);
+
+ s_maps[i].raster_type = G_get_raster_map_type(s_maps[i].cfd);
+ s_maps[i].in_buf = G_allocate_raster_buf(s_maps[i].raster_type);
+
+ }
+
+ return 0;
}
-int get_rows (int row) {
-
- int i;
- for (i=0;i<nmaps;++i) {
+int get_rows(int row)
+{
- if (s_maps[i].output)
- continue;
+ int i;
- if (G_get_raster_row(s_maps[i].cfd, s_maps[i].in_buf, row, s_maps[i].raster_type)<0) {
- G_close_cell(s_maps[i].cfd);
- G_fatal_error(_("Cannot to read <%s> at row <%d>"), s_maps[i].name,row);
- }
+ for (i = 0; i < nmaps; ++i) {
+
+ if (s_maps[i].output)
+ continue;
+
+ if (G_get_raster_row
+ (s_maps[i].cfd, s_maps[i].in_buf, row,
+ s_maps[i].raster_type) < 0) {
+ G_close_cell(s_maps[i].cfd);
+ G_fatal_error(_("Cannot to read <%s> at row <%d>"),
+ s_maps[i].name, row);
}
+ }
-return 0;
+ return 0;
}
-int get_cells (int col) {
- int i;
- CELL c;
- FCELL f;
- DCELL d;
-
- for (i=0;i<nmaps;++i) {
+int get_cells(int col)
+{
+ int i;
+ CELL c;
+ FCELL f;
+ DCELL d;
- if (s_maps[i].output)
- continue;
-
- switch (s_maps[i].raster_type) {
+ for (i = 0; i < nmaps; ++i) {
- case CELL_TYPE:
- c = ((CELL *) s_maps[i].in_buf)[col];
- if (G_is_null_value(&c,CELL_TYPE))
- return 1;
- else
- s_maps[i].cell = (FCELL) c;
- break;
+ if (s_maps[i].output)
+ continue;
- case FCELL_TYPE:
- f = ((FCELL *) s_maps[i].in_buf)[col];
- if (G_is_null_value(&f,FCELL_TYPE))
- return 1;
- else
- s_maps[i].cell = (FCELL) f;
- break;
+ switch (s_maps[i].raster_type) {
- case DCELL_TYPE:
- d = ((DCELL *) s_maps[i].in_buf)[col];
- if (G_is_null_value(&d,DCELL_TYPE))
- return 1;
- else
- s_maps[i].cell = (FCELL) d;
- break;
- }
- } /* end for */
+ case CELL_TYPE:
+ c = ((CELL *) s_maps[i].in_buf)[col];
+ if (G_is_null_value(&c, CELL_TYPE))
+ return 1;
+ else
+ s_maps[i].cell = (FCELL) c;
+ break;
-return 0;
+ case FCELL_TYPE:
+ f = ((FCELL *) s_maps[i].in_buf)[col];
+ if (G_is_null_value(&f, FCELL_TYPE))
+ return 1;
+ else
+ s_maps[i].cell = (FCELL) f;
+ break;
+
+ case DCELL_TYPE:
+ d = ((DCELL *) s_maps[i].in_buf)[col];
+ if (G_is_null_value(&d, DCELL_TYPE))
+ return 1;
+ else
+ s_maps[i].cell = (FCELL) d;
+ break;
+ }
+ } /* end for */
+
+ return 0;
}
-int create_output_maps(void) {
-
- STRING connector="_";
- int i;
- m_outputs = (OUTPUTS* )G_malloc(nrules * sizeof (OUTPUTS));
+int create_output_maps(void)
+{
- for (i=0;i<nrules;++i) {
- strcpy(m_outputs[i].output_name,output);
- strcat(m_outputs[i].output_name,connector);
- strcat(m_outputs[i].output_name,s_rules[i].outname);
-
- if ((m_outputs[i].ofd = G_open_raster_new(m_outputs[i].output_name, FCELL_TYPE)) < 0)
- G_fatal_error(_("Unable to create raster map <%s>"), m_outputs[i].output_name);
-
- m_outputs[i].out_buf = G_allocate_f_raster_buf();
+ STRING connector = "_";
+ int i;
- }
+ m_outputs = (OUTPUTS *) G_malloc(nrules * sizeof(OUTPUTS));
+ for (i = 0; i < nrules; ++i) {
+ strcpy(m_outputs[i].output_name, output);
+ strcat(m_outputs[i].output_name, connector);
+ strcat(m_outputs[i].output_name, s_rules[i].outname);
+
+ if ((m_outputs[i].ofd =
+ G_open_raster_new(m_outputs[i].output_name, FCELL_TYPE)) < 0)
+ G_fatal_error(_("Unable to create raster map <%s>"),
+ m_outputs[i].output_name);
+
+ m_outputs[i].out_buf = G_allocate_f_raster_buf();
+
+ }
+
}
-int process_coors (char *answer) {
+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);
+ struct Cell_head window;
+ double x, y;
+ int i, j;
+ int r, c;
+ int num_points;
+ float result;
- 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);
+ 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-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/local_proto.h 2010-04-03 08:59:38 UTC (rev 41681)
@@ -12,15 +12,15 @@
#endif
/*
-PI2= PI/2
-PI4= PI/4
-*/
+ PI2= PI/2
+ PI4= PI/4
+ */
#ifndef PI2
- #define PI2 (2*atan(1))
+#define PI2 (2*atan(1))
#endif
#ifndef PI4
- #define PI4 (atan(1))
+#define PI4 (atan(1))
#endif
#define STACKMAX 50
@@ -30,154 +30,166 @@
#define MIN(a,b) ((a) < (b) ? (a) : (b))
-typedef char* STRING;
+typedef char *STRING;
-typedef enum {
-l_ZADEH,
-l_PRODUCT,
-l_DRASTIC,
-l_LUKASIEWICZ,
-l_FODOR,
-l_HAMACHER
+typedef enum
+{
+ l_ZADEH,
+ l_PRODUCT,
+ l_DRASTIC,
+ l_LUKASIEWICZ,
+ l_FODOR,
+ l_HAMACHER
} logics;
-typedef enum {
-s_LINEAR,
-s_SSHAPE,
-s_JSHAPE,
-s_GSHAPE
+typedef enum
+{
+ s_LINEAR,
+ s_SSHAPE,
+ s_JSHAPE,
+ s_GSHAPE
} shapes;
-typedef enum {
- s_BOTH,
- s_LEFT,
- s_RIGHT
-} sides;
+typedef enum
+{
+ s_BOTH,
+ s_LEFT,
+ s_RIGHT
+} sides;
-typedef enum {
- i_MIN,
- i_PROD
-} implications;
+typedef enum
+{
+ i_MIN,
+ i_PROD
+} implications;
-typedef enum {
- d_CENTEROID,
- d_BISECTOR,
- d_MINOFHIGHEST,
- d_MAXOFHIGHEST,
- d_MEANOFHIGHEST
+typedef enum
+{
+ d_CENTEROID,
+ d_BISECTOR,
+ d_MINOFHIGHEST,
+ d_MAXOFHIGHEST,
+ d_MEANOFHIGHEST
} defuzz;
-typedef enum {
- E, /* ERROR */
- S, /* SHIFT */
- R, /* REDUCE */
- A /* ACCEPT */
+typedef enum
+{
+ E, /* ERROR */
+ S, /* SHIFT */
+ R, /* REDUCE */
+ A /* ACCEPT */
} actions;
-typedef enum {
- t_START, /* { */
- t_AND, /* & */
- t_OR, /* | */
- t_IS_NOT, /* ~ */
- t_IS, /* = */
- t_LBRC, /* ( */
- t_RBRC, /* ) */
- t_STOP, /* } */
- t_size, /* number of tokens */
- t_VAL /* value a product of MAP and VARIABLE */
+typedef enum
+{
+ t_START, /* { */
+ t_AND, /* & */
+ t_OR, /* | */
+ t_IS_NOT, /* ~ */
+ t_IS, /* = */
+ t_LBRC, /* ( */
+ t_RBRC, /* ) */
+ t_STOP, /* } */
+ t_size, /* number of tokens */
+ t_VAL /* value a product of MAP and VARIABLE */
} tokens;
-typedef struct _sets { /* membership definition */
- char setname[21];
- sides side;
- float points[4];
- shapes shape;
- int hedge;
- float height;
-}SETS;
+typedef struct _sets
+{ /* membership definition */
+ char setname[21];
+ sides side;
+ float points[4];
+ shapes shape;
+ int hedge;
+ float height;
+} SETS;
-typedef struct map {
- char name[30];
- int nsets;
- int output; /* is output map? */
- RASTER_MAP_TYPE raster_type;
- fpos_t position;
- void *in_buf;
- float cell;
- int cfd; /* file descriptor */
- SETS *sets;
+typedef struct map
+{
+ char name[30];
+ int nsets;
+ int output; /* is output map? */
+ RASTER_MAP_TYPE raster_type;
+ fpos_t position;
+ void *in_buf;
+ float cell;
+ int cfd; /* file descriptor */
+ SETS *sets;
} MAPS;
-typedef struct valuestacks {
- float* value;
- SETS* set;
- char oper;
- } VALUES;
+typedef struct valuestacks
+{
+ float *value;
+ SETS *set;
+ char oper;
+} VALUES;
-typedef struct rule {
- char outname[20];
- int output_set_index;
- char parse_stack[STACKMAX][VARMAX];
- int work_stack[STACKMAX];
- VALUES value_stack[STACKMAX];
- float weight;
+typedef struct rule
+{
+ char outname[20];
+ int output_set_index;
+ char parse_stack[STACKMAX][VARMAX];
+ int work_stack[STACKMAX];
+ VALUES value_stack[STACKMAX];
+ float weight;
} RULES;
-typedef struct _outs {
- char output_name[52];
- int ofd; /* output file descriptor */
- float* out_buf;
-} OUTPUTS;
+typedef struct _outs
+{
+ char output_name[52];
+ int ofd; /* output file descriptor */
+ float *out_buf;
+} OUTPUTS;
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;
+MAPS *s_maps;
+RULES *s_rules;
+OUTPUTS *m_outputs;
+float **visual_output;
+float *universe;
+float *antecedents;
int nmaps, nrules, output_index, multiple, coor_proc;
int resolution;
implications implication;
defuzz defuzzyfication;
logics family;
-char** rules;
+char **rules;
int char_strip(char *buf, char rem);
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 get_nsets(FILE * fd, fpos_t position);
+int get_universe(void);
-int parse_map_file (STRING file);
-int parse_rule_file (STRING file);
-int parser (void);
+int parse_map_file(STRING file);
+int parse_rule_file(STRING file);
+int parser(void);
int open_maps(void);
int create_output_maps(void);
int get_rows(int row);
int get_cols(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);
+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);
-float implicate (void);
+float implicate(void);
float parse_expression(int n);
-float defuzzify(float *agregate,int defuzzification, float max_antecedent);
+float defuzzify(float *agregate, int defuzzification, float max_antecedent);
float f_and(float cellx, float celly, int family);
float f_or(float cellx, float celly, int family);
float f_not(float cellx, int family);
-float fuzzy(FCELL cell, SETS* set);
+float fuzzy(FCELL cell, SETS * set);
int parse(void);
-void display (void);
+void display(void);
Modified: grass-addons/raster/r.fuzzy.system/main.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/main.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/main.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -3,7 +3,7 @@
* MODULE: r.fuzzy.system
* AUTHOR(S): Jarek Jasiewicz <jarekj amu.edu.pl>
* PURPOSE: Full fuzzy logic standalone classification system with few fuzzy logic families
- * implication and defuzzification and methods.
+ * implication and defuzzification and methods.
* COPYRIGHT: (C) 1999-2010 by the GRASS Development Team
*
* This program is free software under the GNU General Public
@@ -17,221 +17,221 @@
int main(int argc, char **argv)
{
- struct Option *file_vars,
- *file_rules,
- *par_family,
- *par_resolution,
- *par_defuzzify,
- *par_implication,
- *in_coor_opt,
- *opt_output;
-
- struct History history;
-
- struct Flag *out_multiple;
-
- int nrows, ncols;
- int row, col;
- int outfd;
- float tmp;
- FCELL *out_buf;
-
- int i,j, n;
- G_gisinit(argv[0]);
+ struct Option *file_vars,
+ *file_rules,
+ *par_family,
+ *par_resolution,
+ *par_defuzzify, *par_implication, *in_coor_opt, *opt_output;
- file_vars = G_define_standard_option(G_OPT_F_INPUT);
- file_vars->key = "maps";
- file_vars->required = NO;
- file_vars->description = _("Name of fuzzy variable file");
-
- file_rules = G_define_standard_option(G_OPT_F_INPUT);
- file_rules->key = "rules";
- file_rules->required = NO;
- file_rules->description = _("Name of rules file");
-
- 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->required = YES;
- par_family->description = _("Fuzzy logic family");
- par_family->guisection = _("Advanced options");
+ struct History history;
- par_defuzzify=G_define_option();
- par_defuzzify->key = "defuz";
- par_defuzzify->type = TYPE_STRING;
- par_defuzzify->options = "centeroid,bisector,min,max,mean";
- par_defuzzify->answer = "bisector";
- par_defuzzify->required = YES;
- par_defuzzify->description = _("Defuzzyfication method");
- par_defuzzify->guisection = _("Advanced options");
-
- par_implication=G_define_option();
- par_implication->key = "imp";
- par_implication->type = TYPE_STRING;
- par_implication->options = "minimum, product";
- par_implication->answer = "minimum";
- par_implication->required = YES;
- par_implication->description = _("Implication method");
- par_implication->guisection = _("Advanced options");
-
- par_resolution=G_define_option();
- par_resolution->key = "res";
- par_resolution->type = TYPE_INTEGER;
- par_resolution->answer = "100";
- par_resolution->required = YES;
- par_resolution->description = _("Universe resolution");
- par_resolution->guisection = _("Advanced options");
-
- 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->answer = NULL;
- in_coor_opt->required = NO;
- in_coor_opt->multiple = NO;
- in_coor_opt->description = "Coordinate of cell for witch output detail data";
- in_coor_opt->guisection = _("Visual Output");
+ struct Flag *out_multiple;
- out_multiple = G_define_flag();
- out_multiple->key = 'm';
- out_multiple->description = _("Create addational fuzzy output maps for every rule");
-
-
- opt_output = G_define_standard_option(G_OPT_R_OUTPUT);
- opt_output->description = _("Name of output file");
-
+ int nrows, ncols;
+ int row, col;
+ int outfd;
+ float tmp;
+ FCELL *out_buf;
+
+ int i, j, n;
+
+ G_gisinit(argv[0]);
+
+ file_vars = G_define_standard_option(G_OPT_F_INPUT);
+ file_vars->key = "maps";
+ file_vars->required = NO;
+ file_vars->description = _("Name of fuzzy variable file");
+
+ file_rules = G_define_standard_option(G_OPT_F_INPUT);
+ file_rules->key = "rules";
+ file_rules->required = NO;
+ file_rules->description = _("Name of rules file");
+
+ 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->required = YES;
+ par_family->description = _("Fuzzy logic family");
+ par_family->guisection = _("Advanced options");
+
+ par_defuzzify = G_define_option();
+ par_defuzzify->key = "defuz";
+ par_defuzzify->type = TYPE_STRING;
+ par_defuzzify->options = "centeroid,bisector,min,max,mean";
+ par_defuzzify->answer = "bisector";
+ par_defuzzify->required = YES;
+ par_defuzzify->description = _("Defuzzyfication method");
+ par_defuzzify->guisection = _("Advanced options");
+
+ par_implication = G_define_option();
+ par_implication->key = "imp";
+ par_implication->type = TYPE_STRING;
+ par_implication->options = "minimum, product";
+ par_implication->answer = "minimum";
+ par_implication->required = YES;
+ par_implication->description = _("Implication method");
+ par_implication->guisection = _("Advanced options");
+
+ par_resolution = G_define_option();
+ par_resolution->key = "res";
+ par_resolution->type = TYPE_INTEGER;
+ par_resolution->answer = "100";
+ par_resolution->required = YES;
+ par_resolution->description = _("Universe resolution");
+ par_resolution->guisection = _("Advanced options");
+
+ 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->answer = NULL;
+ in_coor_opt->required = NO;
+ in_coor_opt->multiple = NO;
+ in_coor_opt->description =
+ "Coordinate of cell for witch output detail data";
+ in_coor_opt->guisection = _("Visual Output");
+
+ out_multiple = G_define_flag();
+ out_multiple->key = 'm';
+ out_multiple->description =
+ _("Create addational fuzzy output maps for every rule");
+
+
+ opt_output = G_define_standard_option(G_OPT_R_OUTPUT);
+ opt_output->description = _("Name of output file");
+
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);
- coor_proc = (in_coor_opt->answer) ? 1 : 0;
-
- resolution = atoi(par_resolution->answer);
- if(resolution<10)
+
+
+ var_name_file = file_vars->answer;
+ rule_name_file = file_rules->answer;
+ output = opt_output->answer;
+ multiple = (out_multiple->answer != 0);
+ coor_proc = (in_coor_opt->answer) ? 1 : 0;
+
+ resolution = atoi(par_resolution->answer);
+ if (resolution < 10)
G_fatal_error(_("Universe resolution too small, choose greater value"));
- if(resolution>500)
+ if (resolution > 500)
G_warning(_("Universe resolution is very high, computation may take a long time"));
- if (!strcmp(par_family->answer, "Zadeh"))
- family=l_ZADEH;
- else if (!strcmp(par_family->answer, "product"))
- family=l_PRODUCT;
- else if (!strcmp(par_family->answer, "drastic"))
- family=l_DRASTIC;
- else if (!strcmp(par_family->answer, "Lukasiewicz"))
- family=l_LUKASIEWICZ;
- else if (!strcmp(par_family->answer, "Fodor"))
- family=l_FODOR;
- else if (!strcmp(par_family->answer, "Hamacher"))
- family=l_HAMACHER;
-
- if (!strcmp(par_implication->answer, "minimum"))
- implication=i_MIN;
- else if (!strcmp(par_implication->answer, "product"))
- implication=i_PROD;
+ if (!strcmp(par_family->answer, "Zadeh"))
+ family = l_ZADEH;
+ else if (!strcmp(par_family->answer, "product"))
+ family = l_PRODUCT;
+ else if (!strcmp(par_family->answer, "drastic"))
+ family = l_DRASTIC;
+ else if (!strcmp(par_family->answer, "Lukasiewicz"))
+ family = l_LUKASIEWICZ;
+ else if (!strcmp(par_family->answer, "Fodor"))
+ family = l_FODOR;
+ else if (!strcmp(par_family->answer, "Hamacher"))
+ family = l_HAMACHER;
- if (!strcmp(par_defuzzify->answer, "centeroid"))
- defuzzyfication=d_CENTEROID;
- else if (!strcmp(par_defuzzify->answer, "bisector"))
- defuzzyfication=d_BISECTOR;
- else if (!strcmp(par_defuzzify->answer, "min_of_heightest"))
- defuzzyfication=d_MINOFHIGHEST;
- else if (!strcmp(par_defuzzify->answer, "max_of_heightest"))
- defuzzyfication=d_MAXOFHIGHEST;
- else if (!strcmp(par_defuzzify->answer, "mean_of_heightest"))
- defuzzyfication=d_MEANOFHIGHEST;
-
- nrows = G_window_rows();
- ncols = G_window_cols();
-
- parse_map_file (var_name_file);
- parse_rule_file (rule_name_file);
- get_universe();
- open_maps();
-
- if(coor_proc)
- process_coors (in_coor_opt->answer);
-
- if ((outfd = G_open_raster_new(output, FCELL_TYPE)) < 0)
+ if (!strcmp(par_implication->answer, "minimum"))
+ implication = i_MIN;
+ else if (!strcmp(par_implication->answer, "product"))
+ implication = i_PROD;
+
+ if (!strcmp(par_defuzzify->answer, "centeroid"))
+ defuzzyfication = d_CENTEROID;
+ else if (!strcmp(par_defuzzify->answer, "bisector"))
+ defuzzyfication = d_BISECTOR;
+ else if (!strcmp(par_defuzzify->answer, "min_of_heightest"))
+ defuzzyfication = d_MINOFHIGHEST;
+ else if (!strcmp(par_defuzzify->answer, "max_of_heightest"))
+ defuzzyfication = d_MAXOFHIGHEST;
+ else if (!strcmp(par_defuzzify->answer, "mean_of_heightest"))
+ defuzzyfication = d_MEANOFHIGHEST;
+
+ nrows = G_window_rows();
+ ncols = G_window_cols();
+
+ parse_map_file(var_name_file);
+ parse_rule_file(rule_name_file);
+ get_universe();
+ open_maps();
+
+ if (coor_proc)
+ process_coors(in_coor_opt->answer);
+
+ if ((outfd = G_open_raster_new(output, FCELL_TYPE)) < 0)
G_fatal_error(_("Unable to create raster map <%s>"), output);
- out_buf = G_allocate_f_raster_buf();
-
-
-
- if(multiple)
+ out_buf = G_allocate_f_raster_buf();
+
+
+
+ if (multiple)
create_output_maps();
-
- G_message("Calculate...");
-
- for (row=0;row<nrows;++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);
-
- if(multiple) {
- for (i=0;i<nrules;++i)
- G_set_f_null_value(&m_outputs[i].out_buf[col],1);
- }
- } else {
- out_buf[col]=implicate();
- if (out_buf[col]==-9999)
- G_set_f_null_value(&out_buf[col],1);
-
- if(multiple) {
- for (i=0;i<nrules;++i)
- m_outputs[i].out_buf[col]=antecedents[i];
- }
- }
- }
+ G_message("Calculate...");
- if (G_put_raster_row(outfd, out_buf, FCELL_TYPE) < 0)
- G_fatal_error(_("Failed writing raster map <%s> at row <%d>"), output,row);
-
- if(multiple)
- for (i=0;i<nrules;++i)
- if (G_put_raster_row(m_outputs[i].ofd, m_outputs[i].out_buf, FCELL_TYPE) < 0)
- G_fatal_error(_("Failed writing raster map <%s> at row <%d>"), m_outputs[i].output_name,row);
+ for (row = 0; row < nrows; ++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);
+
+ if (multiple) {
+ for (i = 0; i < nrules; ++i)
+ G_set_f_null_value(&m_outputs[i].out_buf[col], 1);
}
- G_percent(row, nrows, 2);
+ }
+ else {
+ out_buf[col] = implicate();
+ if (out_buf[col] == -9999)
+ G_set_f_null_value(&out_buf[col], 1);
-
- G_message("Close...");
- for (i=0;i<nmaps;++i) {
- if (s_maps[i].output)
- continue;
-
- G_free(s_maps[i].in_buf);
- G_close_cell(s_maps[i].cfd);
+ if (multiple) {
+ for (i = 0; i < nrules; ++i)
+ m_outputs[i].out_buf[col] = antecedents[i];
+ }
+ }
}
- G_free(out_buf);
- 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);
- G_close_cell(m_outputs[i].ofd);
- G_short_history(m_outputs[i].output_name, "raster", &history);
- G_command_history(&history);
- G_write_history(m_outputs[i].output_name, &history);
- }
-G_message("Done!");
-exit(EXIT_SUCCESS);
+ if (G_put_raster_row(outfd, out_buf, FCELL_TYPE) < 0)
+ G_fatal_error(_("Failed writing raster map <%s> at row <%d>"),
+ output, row);
-}
+ if (multiple)
+ for (i = 0; i < nrules; ++i)
+ if (G_put_raster_row
+ (m_outputs[i].ofd, m_outputs[i].out_buf, FCELL_TYPE) < 0)
+ G_fatal_error(_("Failed writing raster map <%s> at row <%d>"),
+ m_outputs[i].output_name, row);
+ }
+ G_percent(row, nrows, 2);
+ G_message("Close...");
+ for (i = 0; i < nmaps; ++i) {
+ if (s_maps[i].output)
+ continue;
+ G_free(s_maps[i].in_buf);
+ G_close_cell(s_maps[i].cfd);
+ }
+ G_free(out_buf);
+ 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);
+ G_close_cell(m_outputs[i].ofd);
+ G_short_history(m_outputs[i].output_name, "raster", &history);
+ 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/map_parser.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/map_parser.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/map_parser.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,213 +1,216 @@
#include "local_proto.h"
-int parse_map_file (STRING file)
-{
- FILE* fd;
- int line=0;
- char buf[500];
- STRING mapset;
- char map[30];
- int nmaps2=0, nsets=0;
- fpos_t init;
- fpos_t position;
- int set_num;
-
- int i;
-
- fd = fopen(file, "r");
- if (!fd)
+int parse_map_file(STRING file)
+{
+ FILE *fd;
+ int line = 0;
+ char buf[500];
+ STRING mapset;
+ char map[30];
+ int nmaps2 = 0, nsets = 0;
+ fpos_t init;
+ fpos_t position;
+ int set_num;
+
+ int i;
+
+ fd = fopen(file, "r");
+ if (!fd)
G_fatal_error(_("Cannot open varaible file '%s'"), file);
- fgetpos (fd, &init);
-
- nmaps=0;
- /* number of maps */
- while (fgets(buf, sizeof buf, fd)) {
- G_strip(buf);
- if (*buf != '%')
- continue;
- nmaps++;
- }
- s_maps=(MAPS*)G_malloc(nmaps * sizeof(MAPS));
- fsetpos (fd, &init); /* reset position */
-
- while (fgets(buf, sizeof buf, fd)) {
- line++;
-
+ fgetpos(fd, &init);
+
+ nmaps = 0;
+ /* number of maps */
+ while (fgets(buf, sizeof buf, fd)) {
G_strip(buf);
- if (*buf == '#' || *buf == 0 || *buf=='\n')
- continue;
-
- if (*buf != '%' && *buf !='$')
- G_fatal_error(_("Wrong syntax at line %d: line must start with #, % or $ or be empty line"), line);
-
- if (*buf == '%') {
-
- fgetpos (fd, &position);
+ if (*buf != '%')
+ continue;
+ nmaps++;
+ }
+ s_maps = (MAPS *) G_malloc(nmaps * sizeof(MAPS));
+ fsetpos(fd, &init); /* reset position */
- sscanf(buf, "%[^\n]", map);
- char_strip(map,'%');
- G_strip(map);
- mapset = G_find_cell2(map, "");
+ while (fgets(buf, sizeof buf, fd)) {
+ line++;
- if (mapset == NULL && strcmp(map,output))
- G_fatal_error(_("Raster map <%s> not found"), map);
-
- G_strcpy(s_maps[nmaps2].name,map);
- 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);
+ G_strip(buf);
+ if (*buf == '#' || *buf == 0 || *buf == '\n')
+ continue;
- if (s_maps[nmaps2].nsets)
- s_maps[nmaps2].sets = (SETS *)G_malloc(s_maps[nmaps2].nsets * sizeof(SETS));
-
- set_num=0;
- while (fgets(buf, sizeof buf, fd)) {
- G_strip(buf);
+ if (*buf != '%' && *buf != '$')
+ G_fatal_error(_("Wrong syntax at line %d: line must start with #, % or $ or be empty line"),
+ line);
- if (*buf == '#' || *buf == 0 || *buf=='\n')
- continue;
- if (*buf == '%')
- break;
+ if (*buf == '%') {
- parse_sets(&s_maps[nmaps2].sets[set_num++],buf,s_maps[nmaps2].name);
+ fgetpos(fd, &position);
- }
- fsetpos (fd, &position);
+ sscanf(buf, "%[^\n]", map);
+ char_strip(map, '%');
+ G_strip(map);
+ mapset = G_find_cell2(map, "");
- if (nmaps2++ > nmaps)
- G_fatal_error(_("number of maps do not match"));
-
- }
-
- } /* end while */
-
- fclose(fd);
- return 0;
+ if (mapset == NULL && strcmp(map, output))
+ G_fatal_error(_("Raster map <%s> not found"), map);
+
+ G_strcpy(s_maps[nmaps2].name, map);
+ 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);
+
+ if (s_maps[nmaps2].nsets)
+ s_maps[nmaps2].sets =
+ (SETS *) G_malloc(s_maps[nmaps2].nsets * sizeof(SETS));
+
+ set_num = 0;
+ while (fgets(buf, sizeof buf, fd)) {
+ G_strip(buf);
+
+ if (*buf == '#' || *buf == 0 || *buf == '\n')
+ continue;
+ if (*buf == '%')
+ break;
+
+ parse_sets(&s_maps[nmaps2].sets[set_num++], buf,
+ s_maps[nmaps2].name);
+
+ }
+ fsetpos(fd, &position);
+
+ if (nmaps2++ > nmaps)
+ G_fatal_error(_("number of maps do not match"));
+
+ }
+
+ } /* end while */
+
+ fclose(fd);
+ return 0;
}
-int parse_sets (SETS* set, char buf[], const char mapname[]) {
-
- char tmp[20];
- int i;
-
- char_strip(buf,'$');
-
- { /* check length of fuzzy value (cannot be longer than 20) */
- sscanf(buf, "%[^{] %[^\n]",tmp,buf);
+int parse_sets(SETS * set, char buf[], const char mapname[])
+{
+
+ char tmp[20];
+ int i;
+
+ char_strip(buf, '$');
+
+ { /* check length of fuzzy value (cannot be longer than 20) */
+ sscanf(buf, "%[^{] %[^\n]", tmp, buf);
G_strip(tmp);
- if(strlen(tmp)<21)
- G_strcpy(set->setname,tmp);
- else
- G_fatal_error(_("Map: <%s>, Membership: <%s>: Value name cannot be longer than 20 chars, but has %d chars"),
- mapname,tmp,strlen(tmp));
- } /* check length of fuzzy value */
+ if (strlen(tmp) < 21)
+ G_strcpy(set->setname, tmp);
+ else
+ G_fatal_error(_("Map: <%s>, Membership: <%s>: Value name cannot be longer than 20 chars, but has %d chars"),
+ mapname, tmp, strlen(tmp));
+ } /* check length of fuzzy value */
- { /* check if side is valid (both,left,right) */
- int done=1;
- STRING sides[]={"both","left","right"};
-
- char_strip(buf,'{');
- sscanf(buf, "%[^;] %[^\n]", tmp,buf);
-
- done=0;
- for(i=0;i<3;++i)
- if(strcmp(tmp,sides[i])==0) {
- done=1;
+ { /* check if side is valid (both,left,right) */
+ int done = 1;
+ STRING sides[] = { "both", "left", "right" };
+
+ char_strip(buf, '{');
+ sscanf(buf, "%[^;] %[^\n]", tmp, buf);
+
+ done = 0;
+ for (i = 0; i < 3; ++i)
+ if (strcmp(tmp, sides[i]) == 0) {
+ done = 1;
break;
- }
-
- if (done)
- set->side=i;
- else
- G_fatal_error(_(" Map: <%s>, Membership: <%s>: <side> parameter must be: <left,right,both> but is: <%s>"),
- mapname, set->setname, tmp);
- } /* end check if side is valid */
-
-
- { /* check number of points and order. Point number limited to 10 chars including coma*/
+ }
+
+ if (done)
+ set->side = i;
+ else
+ G_fatal_error(_(" Map: <%s>, Membership: <%s>: <side> parameter must be: <left,right,both> but is: <%s>"),
+ mapname, set->setname, tmp);
+ } /* end check if side is valid */
+
+
+ { /* check number of points and order. Point number limited to 10 chars including coma */
char p[11];
int npoints;
-
- char_strip(buf,';');
- sscanf(buf, "%[^;] %[^\n]", tmp,buf);
- char_strip(tmp,';');
-
- npoints= set->side ? 2 : 4;
-
- for (i=0;i<npoints;++i) {
- sscanf(tmp, "%[^,] %[^\n]", p,tmp);
- if(tmp[0]!=',' && i<(npoints-1))
- G_fatal_error(_("Map: <%s>, Variable: <%s>: Require %d points but has %d"),
- mapname, set->setname, npoints, i+1);
-
- if(sscanf(p,"%f",&set->points[i])!=1)
+ char_strip(buf, ';');
+ sscanf(buf, "%[^;] %[^\n]", tmp, buf);
+ char_strip(tmp, ';');
+
+ npoints = set->side ? 2 : 4;
+
+ for (i = 0; i < npoints; ++i) {
+ sscanf(tmp, "%[^,] %[^\n]", p, tmp);
+
+ if (tmp[0] != ',' && i < (npoints - 1))
+ G_fatal_error(_("Map: <%s>, Variable: <%s>: Require %d points but has %d"),
+ mapname, set->setname, npoints, i + 1);
+
+ if (sscanf(p, "%f", &set->points[i]) != 1)
G_fatal_error(_("Map: <%s>, Variable: <%s>: Points must be numeric but is: %s or space between '-' and digit"),
- mapname, set->setname, p);
-
- char_strip(tmp,',');
-
- if (i>0 && (set->points[i] < set->points[i-1]))
- G_fatal_error(_("Map: <%s>, Membership: <%s>: Points sequence must be non-declining"),
- mapname, set->setname);
- }
-
- } /* check number of points and order */
-
-
- { /* check if shape is valid (linear,sshaped,jshaped,gshaped) */
- int done=1;
- STRING shapes[]={"linear","sshaped","jshaped", "gshaped"};
-
- char_strip(buf,';');
- sscanf(buf, "%[^;] %[^\n]", tmp,buf);
-
- done=0;
- for(i=0;i<4;++i)
- if(strcmp(tmp,shapes[i])==0) {
- done=1;
+ mapname, set->setname, p);
+
+ char_strip(tmp, ',');
+
+ if (i > 0 && (set->points[i] < set->points[i - 1]))
+ G_fatal_error(_("Map: <%s>, Membership: <%s>: Points sequence must be non-declining"),
+ mapname, set->setname);
+ }
+
+ } /* check number of points and order */
+
+
+ { /* check if shape is valid (linear,sshaped,jshaped,gshaped) */
+ int done = 1;
+ STRING shapes[] = { "linear", "sshaped", "jshaped", "gshaped" };
+
+ char_strip(buf, ';');
+ sscanf(buf, "%[^;] %[^\n]", tmp, buf);
+
+ done = 0;
+ for (i = 0; i < 4; ++i)
+ if (strcmp(tmp, shapes[i]) == 0) {
+ done = 1;
break;
- }
-
- if (done)
- set->shape=i;
- else
- G_fatal_error(_(" Map: <%s>, Variable: <%s>: <shape> parameter must be: <linear,sshaped,jshaped,gshaped> but is: <%s>"),
- mapname, set->setname, tmp);
- } /* end check if shape is valid */
-
-
- { /* check if hedge is valid (integer) */
- char_strip(buf,';');
- sscanf(buf, "%[^;] %[^\n]", tmp,buf);
-
- if(sscanf(tmp,"%d",&set->hedge)!=1)
- G_fatal_error(_("Map: <%s>, variable: <%s>: Hedge must be numeric but is: %s or space between '-' and digit"),
- mapname, set->setname, tmp);
-
- } /* end check if hedge is valid */
-
-
- { /* check if height is valid (float 0-1) */
- char_strip(buf,';');
- sscanf(buf, "%[^}] %[^\n]", tmp,buf);
+ }
- if(sscanf(tmp,"%f",&set->height)!=1)
- G_fatal_error(_("Map: <%s>, Variable: <%s>: Height must be non-negative numeric lesser than 1 but is: %s"),
- mapname, set->setname, tmp);
-
- if(set->height<=0 || set->height>1)
- G_fatal_error(_("Map: <%s>, Variable: <%s>: Height must be non-negative numeric lesser than 1 but is: %f"),
- mapname, set->setname, set->height);
-
- } /* end check if height is valid */
+ if (done)
+ set->shape = i;
+ else
+ G_fatal_error(_(" Map: <%s>, Variable: <%s>: <shape> parameter must be: <linear,sshaped,jshaped,gshaped> but is: <%s>"),
+ mapname, set->setname, tmp);
+ } /* end check if shape is valid */
-return 0;
-}
+ { /* check if hedge is valid (integer) */
+ char_strip(buf, ';');
+ sscanf(buf, "%[^;] %[^\n]", tmp, buf);
+
+ if (sscanf(tmp, "%d", &set->hedge) != 1)
+ G_fatal_error(_("Map: <%s>, variable: <%s>: Hedge must be numeric but is: %s or space between '-' and digit"),
+ mapname, set->setname, tmp);
+
+ } /* end check if hedge is valid */
+
+
+ { /* check if height is valid (float 0-1) */
+ char_strip(buf, ';');
+ sscanf(buf, "%[^}] %[^\n]", tmp, buf);
+
+ if (sscanf(tmp, "%f", &set->height) != 1)
+ G_fatal_error(_("Map: <%s>, Variable: <%s>: Height must be non-negative numeric lesser than 1 but is: %s"),
+ mapname, set->setname, tmp);
+
+ if (set->height <= 0 || set->height > 1)
+ G_fatal_error(_("Map: <%s>, Variable: <%s>: Height must be non-negative numeric lesser than 1 but is: %f"),
+ mapname, set->setname, set->height);
+
+ } /* end check if height is valid */
+
+ return 0;
+}
Modified: grass-addons/raster/r.fuzzy.system/rule_parser.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/rule_parser.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/rule_parser.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,277 +1,306 @@
#include "local_proto.h"
-int parse_rule_file(STRING file) {
- FILE* fd;
- char buf[800];
- char tmp[30];
- STRING mapset;
- char map[30];
- fpos_t init;
- int rule_num=0;
- int n=0, i;
-
- fd = fopen(file, "r");
- if (!fd)
+int parse_rule_file(STRING file)
+{
+ FILE *fd;
+ char buf[800];
+ char tmp[30];
+ STRING mapset;
+ char map[30];
+ fpos_t init;
+ int rule_num = 0;
+ int n = 0, i;
+
+ fd = fopen(file, "r");
+ if (!fd)
G_fatal_error(_("Cannot open rules file '%s'"), file);
- fgetpos (fd, &init);
-
- while (fgets(buf, sizeof buf, fd)) {
- G_strip(buf);
- if (*buf != '$')
- continue;
- nrules++;
- }
-
- s_rules =(RULES *) G_malloc((nrules)* sizeof(RULES));
- rules = (char** )G_malloc ((nrules)* sizeof(char*));
- for (i=0;i<nrules;++i)
- rules[i]=(char* )G_malloc (21* sizeof(char*));
-
- fsetpos (fd, &init); /* reset position */
-
- for (n=nmaps;n>0;--n)
- if (!strcmp(s_maps[n-1].name,output))
- break; /*max index of output map */
-
- if(n==0)
- G_fatal_error (_("No defininition for output map: <%s> in map file"), output);
-
- while (fgets(buf, sizeof buf, fd)) {
- G_strip(buf);
- if (*buf != '$')
- continue;
+ fgetpos(fd, &init);
- parse_rules(rule_num++,n,buf);
+ while (fgets(buf, sizeof buf, fd)) {
+ G_strip(buf);
+ if (*buf != '$')
+ continue;
+ nrules++;
+ }
- /* next rule */
- }
-
+ s_rules = (RULES *) G_malloc((nrules) * sizeof(RULES));
+ rules = (char **)G_malloc((nrules) * sizeof(char *));
+ for (i = 0; i < nrules; ++i)
+ rules[i] = (char *)G_malloc(21 * sizeof(char *));
- fclose(fd);
- return 0;
+ fsetpos(fd, &init); /* reset position */
-} /* END parse_rule_file */
+ for (n = nmaps; n > 0; --n)
+ if (!strcmp(s_maps[n - 1].name, output))
+ break; /*max index of output map */
+ if (n == 0)
+ G_fatal_error(_("No defininition for output map: <%s> in map file"),
+ output);
-
-int parse_rules(int rule_num, int n, char buf[]) {
+ while (fgets(buf, sizeof buf, fd)) {
+ G_strip(buf);
+ if (*buf != '$')
+ continue;
- int i,j,l,o;
- int done=1;
- int stack_top;
- char tmp[30];
- char opr[] = {'=','&','|',';','~','(',')','{','}'};
-
- i=j=stack_top=0; /* variables of the while loop */
- char_strip(buf,'$');
-
+ parse_rules(rule_num++, n, buf);
-{ /* check the name of fuzzy value (must be defined in rules) */
- int s,p;
-
- sscanf(buf, "%[^{] %[^\n]",tmp,buf);
+ /* next rule */
+ }
+
+
+ fclose(fd);
+ return 0;
+
+} /* END parse_rule_file */
+
+
+
+int parse_rules(int rule_num, int n, char buf[])
+{
+
+ int i, j, l, o;
+ int done = 1;
+ int stack_top;
+ char tmp[30];
+ char opr[] = { '=', '&', '|', ';', '~', '(', ')', '{', '}' };
+
+ i = j = stack_top = 0; /* variables of the while loop */
+ char_strip(buf, '$');
+
+
+ { /* check the name of fuzzy value (must be defined in rules) */
+ int s, p;
+
+ sscanf(buf, "%[^{] %[^\n]", tmp, buf);
G_strip(buf);
G_strip(tmp);
-
- done=1;
- for (s=0;s<=s_maps[n-1].nsets;++s) {/* output map */
-
- if(!strcmp(s_maps[n-1].sets[s].setname,tmp)) {
- for (p=0;p<=rule_num;++p) /* check if values are unique */
- if(!strcmp(tmp,rules[p]))
- G_fatal_error(_("Variable value <%s> exist, Variable values must be unique"),tmp);
-
- strcpy(rules[rule_num],tmp);
- done=0;
- break;
- }
- }
- if(done)
- G_fatal_error(_("Output map do not have variable value <%s>"),tmp);
- strcpy(s_rules[rule_num].outname,tmp);
- s_rules[rule_num].output_set_index=s;
+ done = 1;
+ for (s = 0; s <= s_maps[n - 1].nsets; ++s) { /* output map */
-} /* check the name of fuzzy value */
+ if (!strcmp(s_maps[n - 1].sets[s].setname, tmp)) {
-/* ******************************************************************* */
-
-{ /* parse rule expression and create parse stack */
-
-
+ for (p = 0; p <= rule_num; ++p) /* check if values are unique */
+ if (!strcmp(tmp, rules[p]))
+ G_fatal_error(_("Variable value <%s> exist, Variable values must be unique"),
+ tmp);
+
+ strcpy(rules[rule_num], tmp);
+ done = 0;
+ break;
+ }
+ }
+ if (done)
+ G_fatal_error(_("Output map do not have variable value <%s>"),
+ tmp);
+ strcpy(s_rules[rule_num].outname, tmp);
+ s_rules[rule_num].output_set_index = s;
+
+ } /* check the name of fuzzy value */
+
+ /* ******************************************************************* */
+
+ { /* parse rule expression and create parse stack */
+
+
do {
- for(o=0;o<(sizeof opr);++o) {
- if(buf[i]==opr[o]) {
- 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';
- G_strip(tmp);
- }
- if(strlen(tmp)) /* is not blank */
- strcpy(s_rules[rule_num].parse_stack[stack_top++],tmp);
-
- j=i;
- break;
- }
- }
- if(i>799)
+ for (o = 0; o < (sizeof opr); ++o) {
+ if (buf[i] == opr[o]) {
+ 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';
+ G_strip(tmp);
+ }
+ if (strlen(tmp)) /* is not blank */
+ strcpy(s_rules[rule_num].parse_stack[stack_top++],
+ tmp);
+
+ j = i;
+ break;
+ }
+ }
+ if (i > 799)
G_fatal_error(_("rule string is too long or lack of closing element"));
-
- } while (buf[i++]!='}'); /* rule is rot */
-
- strcpy(s_rules[rule_num].parse_stack[stack_top++],"}"); /* add closing element only if OK*/
+ } while (buf[i++] != '}'); /* rule is rot */
-} /* end parse rule expression and create parse stack */
+ strcpy(s_rules[rule_num].parse_stack[stack_top++], "}"); /* add closing element only if OK */
-/* ******************************************************************* */
+ } /* end parse rule expression and create parse stack */
-{ /* adding weight */
+ /* ******************************************************************* */
-char local[900];
-char weight[10];
-sscanf(buf, "%[^}] %[^;]",local,weight);
-char_strip(weight,'}');
-G_strip(weight);
- if(strlen(weight)==0)
-strcpy(weight,"1");
-s_rules[rule_num].weight=atof(weight);
- if(s_rules[rule_num].weight<=0.)
-G_fatal_error(_("Weight must be grater than 0 or non-number character"));
+ { /* adding weight */
-}
+ char local[900];
+ char weight[10];
-{ /* 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 */
+ sscanf(buf, "%[^}] %[^;]", local, weight);
+ char_strip(weight, '}');
+ G_strip(weight);
+ if (strlen(weight) == 0)
+ strcpy(weight, "1");
+ s_rules[rule_num].weight = atof(weight);
+ if (s_rules[rule_num].weight <= 0.)
+ G_fatal_error(_("Weight must be grater than 0 or non-number character"));
- 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;
-
- 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]);
+ }
- work_stack_pos++;
- continue;
- } /* END { */
-
- if(*s_rules[rule_num].parse_stack[i]=='=' ||
- *s_rules[rule_num].parse_stack[i]=='~') { /* =, ~ */
-
- for (j=0;j<nmaps;++j) {
- if(!strcmp(s_rules[rule_num].parse_stack[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_maps[j].sets[k].setname)) {
+ { /* 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 */
- s_rules[rule_num].work_stack[work_stack_pos]= t_VAL;
- s_rules[rule_num].value_stack[work_stack_pos].value=&s_maps[j].cell;
- s_rules[rule_num].value_stack[work_stack_pos].set =&s_maps[j].sets[k];
- s_rules[rule_num].value_stack[work_stack_pos].oper =*s_rules[rule_num].parse_stack[i];
- done=0;
- break;
- }
- }
- }
- } /* END for j */
-
- 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]);
- /* 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] != t_OR &&
- s_rules[rule_num].work_stack[work_stack_pos-1] != t_START &&
- s_rules[rule_num].work_stack[work_stack_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]);
-
- work_stack_pos++;
- continue;
- } /* END =, ~ */
-
- if(*s_rules[rule_num].parse_stack[i]=='&' ||
- *s_rules[rule_num].parse_stack[i]=='|') { /* &, | */
+ 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]=
- (*s_rules[rule_num].parse_stack[i]=='|') ? t_OR : t_AND;
+ s_rules[rule_num].work_stack[work_stack_pos] = t_START;
- if (s_rules[rule_num].work_stack[work_stack_pos-1] != t_VAL &&
- s_rules[rule_num].work_stack[work_stack_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]);
-
- work_stack_pos++;
- continue;
- } /* END &, | */
-
- if(*s_rules[rule_num].parse_stack[i]=='(') {
-
- s_rules[rule_num].work_stack[work_stack_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] != t_OR &&
- s_rules[rule_num].work_stack[work_stack_pos-1] != t_START &&
- s_rules[rule_num].work_stack[work_stack_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]);
-
- work_stack_pos++;
- continue;
- } /* END ( */
+ 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]);
- if(*s_rules[rule_num].parse_stack[i]==')') {
+ work_stack_pos++;
+ continue;
+ } /* END { */
- s_rules[rule_num].work_stack[work_stack_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] != 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]);
-
- work_stack_pos++;
- continue;
- } /* END ) */
-
- if(*s_rules[rule_num].parse_stack[i]=='}') {
+ if (*s_rules[rule_num].parse_stack[i] == '=' || *s_rules[rule_num].parse_stack[i] == '~') { /* =, ~ */
- s_rules[rule_num].work_stack[work_stack_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] != 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]);
-
- work_stack_pos++;
- continue;
- } /* END } */
+ for (j = 0; j < nmaps; ++j) {
+ if (!strcmp
+ (s_rules[rule_num].parse_stack[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_maps[j].sets[k].setname)) {
- } /* most external loop */
+ s_rules[rule_num].work_stack[work_stack_pos] =
+ t_VAL;
+ s_rules[rule_num].value_stack[work_stack_pos].
+ value = &s_maps[j].cell;
+ s_rules[rule_num].value_stack[work_stack_pos].
+ set = &s_maps[j].sets[k];
+ s_rules[rule_num].value_stack[work_stack_pos].
+ oper = *s_rules[rule_num].parse_stack[i];
+ done = 0;
+ break;
+ }
+ }
+ }
+ } /* END for j */
- if (lbrc!=rbrc)
-G_fatal_error(_("line %d Left and right of brackets do not match"),rule_num+1);
-
-} /* END check if rule syntax is proper and map names and vars values exist*/
+ 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]);
+ /* end for j */
-return 0;
-} /* END parse_rules */
+ if (s_rules[rule_num].work_stack[work_stack_pos - 1] != t_AND
+ && s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+ t_OR &&
+ s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+ t_START &&
+ s_rules[rule_num].work_stack[work_stack_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]);
+ work_stack_pos++;
+ continue;
+ } /* END =, ~ */
+ if (*s_rules[rule_num].parse_stack[i] == '&' || *s_rules[rule_num].parse_stack[i] == '|') { /* &, | */
+ s_rules[rule_num].work_stack[work_stack_pos] =
+ (*s_rules[rule_num].parse_stack[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] !=
+ 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]);
+ work_stack_pos++;
+ continue;
+ } /* END &, | */
+ if (*s_rules[rule_num].parse_stack[i] == '(') {
+
+ s_rules[rule_num].work_stack[work_stack_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] !=
+ t_OR &&
+ s_rules[rule_num].work_stack[work_stack_pos - 1] !=
+ t_START &&
+ s_rules[rule_num].work_stack[work_stack_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]);
+
+ work_stack_pos++;
+ continue;
+ } /* END ( */
+
+ if (*s_rules[rule_num].parse_stack[i] == ')') {
+
+ s_rules[rule_num].work_stack[work_stack_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] !=
+ 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]);
+
+ work_stack_pos++;
+ continue;
+ } /* END ) */
+
+ if (*s_rules[rule_num].parse_stack[i] == '}') {
+
+ s_rules[rule_num].work_stack[work_stack_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] !=
+ 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]);
+
+ work_stack_pos++;
+ continue;
+ } /* END } */
+
+ } /* most external loop */
+
+ if (lbrc != rbrc)
+ G_fatal_error(_("line %d Left and right of brackets do not match"),
+ rule_num + 1);
+
+ } /* END check if rule syntax is proper and map names and vars values exist */
+
+ return 0;
+} /* END parse_rules */
Modified: grass-addons/raster/r.fuzzy.system/system.c
===================================================================
--- grass-addons/raster/r.fuzzy.system/system.c 2010-04-03 08:49:00 UTC (rev 41680)
+++ grass-addons/raster/r.fuzzy.system/system.c 2010-04-03 08:59:38 UTC (rev 41681)
@@ -1,186 +1,200 @@
#include "local_proto.h"
-float implicate (void) {
-
-int i, j;
-float* agregate=NULL;
-int set_index;
-float consequent;
-float max_antecedent=0;
+float implicate(void)
+{
-agregate =(float*) G_calloc(resolution, sizeof(float));
-antecedents=(float*) G_malloc(nrules * sizeof(float));
+ int i, j;
+ float *agregate = NULL;
+ int set_index;
+ float consequent;
+ float max_antecedent = 0;
- if(coor_proc)
-visual_output= (float**) G_malloc (resolution * sizeof(float*));
+ agregate = (float *)G_calloc(resolution, sizeof(float));
+ antecedents = (float *)G_malloc(nrules * sizeof(float));
- for (j=0;j<nrules;++j) {
- antecedents[j]=parse_expression(j);
- max_antecedent=(max_antecedent > antecedents[j]) ? max_antecedent : antecedents[j];
+ if (coor_proc)
+ 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];
+ }
+
+ if (max_antecedent == 0.)
+ 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];
}
-
- if(max_antecedent==0.)
- 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)
+ continue;
+
+ for (i = 0; i < resolution; ++i) {
+
+ consequent = fuzzy(universe[i],
+ &s_maps[output_index].sets[set_index]);
+ set_index = s_rules[j].output_set_index;
+ consequent = (!implication) ? MIN(antecedents[j], consequent) :
+ antecedents[j] * consequent;
+ agregate[i] = MAX(agregate[i], consequent);
+
+ if (coor_proc)
+ visual_output[i][j + 1] = consequent;
}
-
- for (j=0;j<nrules;++j) {
-
- if(defuzzyfication > d_BISECTOR && antecedents[j]< max_antecedent)
- continue;
+ }
+ if (coor_proc)
+ for (i = 0; i < resolution; ++i)
+ visual_output[i][j + 1] = agregate[i];
- for (i=0;i<resolution;++i) {
+ return defuzzify(agregate, defuzzyfication, max_antecedent);
+}
- consequent=fuzzy(universe[i],
- &s_maps[output_index].sets[set_index]);
- set_index=s_rules[j].output_set_index;
- consequent=(!implication) ? MIN(antecedents[j],consequent) :
- antecedents[j]*consequent;
- agregate[i]=MAX(agregate[i],consequent);
+float parse_expression(int n)
+{
- if(coor_proc)
- visual_output[i][j+1]=consequent;
+ /* 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, R, R, E, E, S, R, R},
+ /* | */ {E, R, R, E, E, S, R, R},
+ /* ~ */ {E, E, E, E, E, E, E, E},
+ /* = */ {E, E, E, E, E, E, E, E},
+ /* ( */ {E, S, S, E, E, S, S, E},
+ /* ) */ {E, R, R, E, E, E, R, R},
+ /* } */ {E, E, E, E, E, E, E, E}
+ };
+
+ tokens operator_stack[STACKMAX];
+ float values_stack[STACKMAX];
+
+ int i = 0, k = 0;
+ int opr_top = 0;
+ int val_top = 0;
+ 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");
+ operator_stack[opr_top] = t_START;
+ continue;
+ }
+
+ if (s_rules[n].work_stack[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 == '~') ? 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]]) {
+
+ case E: /* error */
+ G_fatal_error("Stack error, contact author");
+ break;
+
+ case S: /* shift */
+ operator_stack[++opr_top] = s_rules[n].work_stack[i];
+ break;
+
+ case R: /* reduce */
+
+ switch (operator_stack[opr_top]) {
+
+ case t_AND:
+ values_stack[val_top - 1] =
+ f_and(values_stack[val_top],
+ values_stack[val_top - 1], family);
+ val_top--;
+ break;
+
+ case t_OR:
+ values_stack[val_top - 1] =
+ f_or(values_stack[val_top], values_stack[val_top - 1],
+ family);
+ val_top--;
+ break;
+
+ case t_RBRC:
+ opr_top--;
+ break;
}
- }
- if(coor_proc)
- for (i=0;i<resolution;++i)
- visual_output[i][j+1]=agregate[i];
-
- return defuzzify(agregate,defuzzyfication,max_antecedent);
+
+ opr_top--;
+ i--;
+ 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);
}
-float parse_expression(int n) {
-
-/* 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, R, R, E, E, S, R, R },
-/* | */ { E, R, R, E, E, S, R, R },
-/* ~ */ { E, E, E, E, E, E, E, E },
-/* = */ { E, E, E, E, E, E, E, E },
-/* ( */ { E, S, S, E, E, S, S, E },
-/* ) */ { E, R, R, E, E, E, R, R },
-/* } */ { E, E, E, E, E, E, E, E }
-};
-tokens operator_stack[STACKMAX];
-float values_stack[STACKMAX];
-
-int i=0, k=0;
-int opr_top=0;
-int val_top=0;
-int set_index;
-float f_value;
- do {
+float defuzzify(float *agregate, int defuzzification, float max_antecedent)
+{
+ int i;
+ float d_value = 0;
+ float sum_agregate = 0;
+ float tmp;
- if(s_rules[n].work_stack[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) {
- 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=='~') ?
- f_not(f_value,family) : f_value;
- continue;
- }
+ for (i = 0; i < resolution; sum_agregate += agregate[i++]) ;
- if (s_rules[n].work_stack[i]<t_size) {
-
- switch (parse_tab[operator_stack[opr_top]][s_rules[n].work_stack[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];
- break;
-
- case R: /* reduce */
-
- switch (operator_stack[opr_top]) {
-
- case t_AND :
- values_stack[val_top-1]=f_and(values_stack[val_top],values_stack[val_top-1],family);
- val_top--;
- break;
-
- case t_OR :
- values_stack[val_top-1]=f_or(values_stack[val_top],values_stack[val_top-1],family);
- val_top--;
- break;
-
- case t_RBRC :
- opr_top--;
- break;
- }
-
- opr_top--;
- i--;
- 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);
-}
+ switch (defuzzification) {
+ case d_CENTEROID:
+ for (i = 0; i < resolution; ++i)
+ d_value += (universe[i] * agregate[i]);
+ return d_value / sum_agregate;
+ case d_BISECTOR:
+ for (i = 0, tmp = 0; (tmp += agregate[i]) < sum_agregate / 2; ++i) ;
+ return universe[i];
+ case d_MINOFHIGHEST:
+ for (i = 0; agregate[i++] < max_antecedent;) ;
+ return universe[i];
-float defuzzify(float *agregate,int defuzzification, float max_antecedent) {
- int i;
- float d_value=0;
- float sum_agregate=0;
- float tmp;
+ case d_MAXOFHIGHEST:
+ for (i = resolution; agregate[i--] < max_antecedent;) ;
+ return universe[i];
- for (i=0;i<resolution;sum_agregate += agregate[i++]);
-
- switch (defuzzification) {
-
- case d_CENTEROID :
- for (i=0;i<resolution;++i)
- d_value += (universe[i] * agregate[i]);
- return d_value/sum_agregate;
-
- case d_BISECTOR :
- for (i=0, tmp=0;(tmp+= agregate[i])<sum_agregate/2;++i);
- return universe[i];
-
- case d_MINOFHIGHEST :
- for (i=0; agregate[i++]<max_antecedent;);
- return universe[i];
-
- case d_MAXOFHIGHEST :
- for (i=resolution; agregate[i--]<max_antecedent;);
- return universe[i];
-
- case d_MEANOFHIGHEST :
- sum_agregate=0;
- for (i=0;i<resolution;++i) {
- if(agregate[i]<max_antecedent)
- continue;
- d_value += (universe[i] * agregate[i]);
- sum_agregate += agregate[i];
- }
- return d_value/sum_agregate;
+ case d_MEANOFHIGHEST:
+ sum_agregate = 0;
+ for (i = 0; i < resolution; ++i) {
+ if (agregate[i] < max_antecedent)
+ continue;
+ d_value += (universe[i] * agregate[i]);
+ sum_agregate += agregate[i];
}
+ return d_value / sum_agregate;
+ }
- return -9999;
+ return -9999;
}
More information about the grass-commit
mailing list