[GRASS-SVN] r34768 - grass-addons/raster/mcda/r.roughset
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Dec 7 01:35:30 EST 2008
Author: gianluca
Date: 2008-12-07 01:35:29 -0500 (Sun, 07 Dec 2008)
New Revision: 34768
Added:
grass-addons/raster/mcda/r.roughset/main.c
grass-addons/raster/mcda/r.roughset/rules_extr.c
Removed:
grass-addons/raster/mcda/r.roughset/main.c
grass-addons/raster/mcda/r.roughset/rules_extr.c
Log:
new output and new files organizzation
Deleted: grass-addons/raster/mcda/r.roughset/main.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/main.c 2008-12-07 03:55:48 UTC (rev 34767)
+++ grass-addons/raster/mcda/r.roughset/main.c 2008-12-07 06:35:29 UTC (rev 34768)
@@ -1,235 +0,0 @@
-/****************************************************************************
- *
- * MODULE: r.roughset
- * AUTHOR(S): GRASS module authors ad Rough Set Library (RSL) maintain:
- * G.Massei (g_massa at libero.it)-A.Boggia (boggia at unipg.it)
- * Rough Set Library (RSL) ver. 2 original develop:
- * M.Gawrys - J.Sienkiewicz
- *
- * PURPOSE: Geographics rough set analisys and knowledge discovery
- *
- * COPYRIGHT: (C) GRASS Development Team (2008)
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
-
-#include "localproto.h"
-
-
-void rough_sel_library_out(int nrows, int ncols, int nattribute, struct input *attributes, char *fileOutput);
-
-
-int main(int argc, char *argv[])
-{
- struct Cell_head cellhd; /* it stores region information, and header of rasters */
- char *mapset; /* mapset name */
- int i,j, nattribute; /* index and files number*/
- int nrows, ncols;
- int strgy; /* strategy rules extraction index*/
-
- RASTER_MAP_TYPE data_type; /* type of the map (CELL/DCELL/...) */
- /*int value, decvalue;*/ /*single attribute and decision value*/
-
- struct input *attributes;
- struct History history; /* holds meta-data (title, comments,..) */
-
- struct GModule *module; /* GRASS module for parsing arguments */
-
- struct Option *attribute, *decision, *strategy, *output; /* options */
- /*struct Flag *flagQuiet flags */
-
- /* initialize GIS environment */
- G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
-
- /* initialize module */
- module = G_define_module();
- module->keywords = _("raster,geographics knowledge discovery");
- module->description = _("Rough set based geographics knowledg ");
-
-
- /* Define the different options as defined in gis.h */
- attribute = G_define_option() ;
- attribute->key = "attributes";
- attribute->type = TYPE_STRING;
- attribute->required = YES;
- attribute->multiple = YES;
- attribute->gisprompt = "old,cell,raster" ;
- attribute->description = "Input geographics attributes in information system";
-
- decision = G_define_option() ;
- decision->key = "decision";
- decision->type = TYPE_STRING;
- decision->required = YES;
- decision->gisprompt = "old,cell,raster" ;
- decision->description = "Input geographics decision in information system";
-
- strategy = G_define_option() ;
- strategy->key = "strategy";
- strategy->type = TYPE_STRING;
- strategy->required = YES;
- strategy->options = "Very fast,Fast,Medium,Best,All,Low,Upp,Normal";
- strategy->description = "Choose strategy for generating rules";
-
- output = G_define_option();
- output->key = "InfoSys";
- output->type = TYPE_STRING;
- output->required = YES;
- output->gisprompt = "new_file,file,output";
- output->answer ="InfoSys";
- output->description = "Output information system file";
-
-
- /* options and flags parser */
- if (G_parser(argc, argv))
- exit(EXIT_FAILURE);
-
-
-
-/***********************************************************************/
-/********Prepare and controlling Information System files **************/
-/***********************************************************************/
-
- /* number of file (=attributes) */
- nattribute=0;
- while (attribute->answers[nattribute]!=NULL)
- {
- nattribute++;
- }
-
-
-/*Convert strategy answer in index. strcmp return 0 if answer is the passed string*/
- if(strcmp(strategy->answer,"Very fast")==0)
- strgy=0;
- else if(strcmp(strategy->answer,"Fast")==0)
- strgy=1;
- else if(strcmp(strategy->answer,"Medium")==0)
- strgy=2;
- else if(strcmp(strategy->answer,"Best")==0)
- strgy=3;
- else if(strcmp(strategy->answer,"All")==0)
- strgy=4;
- else if(strcmp(strategy->answer,"Low")==0)
- strgy=5;
- else if(strcmp(strategy->answer,"Upp")==0)
- strgy=6;
- else
- strgy=7;
-
- G_message("choose:%d,%s",strgy,strategy->answer);
-
-
- /* process the input maps:*/
- /* ATTRIBUTES grid */
- attributes = G_malloc((nattribute+1) * sizeof(struct input)); /*attributes is input struct defined in localproto.h*/
-
- for (i = 0; i < nattribute; i++)
- {
- struct input *p = &attributes[i];
- p->name = attribute->answers[i];
- p->mapset = G_find_cell(p->name,""); /* G_find_cell: Looks for the raster map "name" in the database. */
- if (!p->mapset)
- G_fatal_error(_("Raster file <%s> not found"), p->name);
- p->fd = G_open_cell_old(p->name, p->mapset);
- if (p->fd < 0)
- G_fatal_error(_("Unable to open input map <%s> in mapset <%s>"),p->name, p->mapset);
- p->buf = G_allocate_d_raster_buf(); /* Allocate an array of DCELL based on the number of columns in the current region. Return DCELL * */
- }
-
- /* DECISION grid (at last column in Information System matrix) */
- struct input *p = &attributes[nattribute];
- p->name = decision->answer;
- p->mapset = G_find_cell(p->name,""); /* G_find_cell: Looks for the raster map "name" in the database. */
- if (!p->mapset)
- G_fatal_error(_("Raster file <%s> not found"), p->name);
- p->fd = G_open_cell_old(p->name, p->mapset);/*opens the raster file name in mapset for reading. A nonnegative file descriptor is returned if the open is successful.*/
- if (p->fd < 0)
- G_fatal_error(_("Unable to open input map <%s> in mapset <%s>"),p->name, p->mapset);
- p->buf = G_allocate_d_raster_buf(); /* Allocate an array of DCELL based on the number of columns in the current region. Return DCELL * */
-
-
- nrows = G_window_rows();
- ncols = G_window_cols();
-
- rough_sel_library_out(nrows, ncols, nattribute, attributes, output->answer);/*build RSL standard file*/
- RulsExtraction(output->answer,attributes,strgy); /* extract rules from RSL*/
-
- exit(EXIT_SUCCESS);
-}
-
-
-void rough_sel_library_out(int nrows, int ncols, int nattribute, struct input *attributes, char *fileOutput)
-
-{
- int row, col, i, j;
- int value, decvalue;
- int nobject;
- char cell_buf[300];
- FILE *fp; /*file pointer for ASCII output*/
-
- /* open *.sys file for writing or use stdout */
- if(NULL == (fp = fopen(fileOutput, "w")))
- G_fatal_error("Not able to open file [%s]",fileOutput);
-
- fprintf(fp,"NAME: %s\nATTRIBUTES: %d\nOBJECTS: %s\n",fileOutput,nattribute+1," ");
-
- /************** process the data *************/
-
- nobject=0;
-
- fprintf (stderr, _("Percent complete ... "));
-
- for (row = 0; row < nrows; row++)
- {
- G_percent(row, nrows, 2);
- for(i=0;i<=nattribute;i++)
- {
- G_get_d_raster_row (attributes[i].fd, attributes[i].buf, row);/* Reads appropriate information into the buffer buf associated with the requested row*/
- }
- for (col=0;col<ncols;col++)
- { /*make a cast on the DCELL output value*/
- decvalue=(int)attributes[nattribute].buf[col];
- if(0<decvalue)
- {
- for(j=0;j<nattribute;j++)
- { /*make a cast on the DCELL output value*/
- value = (int)(attributes[j].buf[col]);
- sprintf(cell_buf, "%d",value);
- G_trim_decimal (cell_buf);
- fprintf (fp,"%s ",cell_buf);
- }
- fprintf(fp,"%d \n",decvalue);
- nobject++;
- }
- }
- }
- /************** write code file*************/
-
- for(i=0;i<=nattribute;i++)
- {
- fprintf(fp,"\n%s",attributes[i].name);
- }
-
- /************** write header file*************/
-
- if(0<fseek(fp,0L,0)) /*move file pointer to header file*/
- G_fatal_error("Not able to write file [%s]",fileOutput);
- else
- fprintf(fp,"NAME: %s\nATTRIBUTES: %d\nOBJECTS: %d\n",fileOutput,nattribute+1,nobject);
-
- /************** close all and exit ***********/
-
- for (i = 0; i<=nattribute; i++)
- G_close_cell(attributes[i].fd);
-
- fclose(fp);
- G_percent(row, nrows, 2);
-
-}
-
-
-
-
-
Added: grass-addons/raster/mcda/r.roughset/main.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/main.c (rev 0)
+++ grass-addons/raster/mcda/r.roughset/main.c 2008-12-07 06:35:29 UTC (rev 34768)
@@ -0,0 +1,239 @@
+/****************************************************************************
+ *
+ * MODULE: r.roughset
+ * AUTHOR(S): GRASS module authors ad Rough Set Library (RSL) maintain:
+ * G.Massei (g_massa at libero.it)-A.Boggia (boggia at unipg.it)
+ * Rough Set Library (RSL) ver. 2 original develop:
+ * M.Gawrys - J.Sienkiewicz
+ *
+ * PURPOSE: Geographics rough set analisys and knowledge discovery
+ *
+ * COPYRIGHT: (C) GRASS Development Team (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include "localproto.h"
+
+
+int main(int argc, char *argv[])
+{
+ struct Cell_head cellhd; /* it stores region information, and header of rasters */
+ char *mapset; /* mapset name */
+ int i,j; /* index and files number*/
+ int row,col,nrows, ncols;
+ int nattributes; /*attributes numer*/
+ int strgy, cls; /* strategy rules extraction and classifiy index*/
+
+ char *result; /* output raster name */
+ int *classify_vect; /* matrix for classified value storage*/
+ int outfd; /* output file descriptor */
+ unsigned char *outrast; /* output buffer */
+
+ RASTER_MAP_TYPE data_type; /* type of the map (CELL/DCELL/...) */
+ /*int value, decvalue;*/ /*single attribute and decision value*/
+
+ struct input *attributes;
+ struct History history; /* holds meta-data (title, comments,..) */
+
+ struct GModule *module; /* GRASS module for parsing arguments */
+
+ struct Option *attr_map, *dec_map, *genrules, *clssfy, *output_txt, *output_map; /* options */
+ /*struct Flag *flagQuiet flags */
+
+ /* initialize GIS environment */
+ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
+
+ /* initialize module */
+ module = G_define_module();
+ module->keywords = _("raster,geographics knowledge discovery");
+ module->description = _("Rough set based geographics knowledge ");
+
+
+ /* Define the different options as defined in gis.h */
+ attr_map = G_define_option() ; /* Allocates memory for the Option structure and returns a pointer to this memory */
+ attr_map->key = "attributes";
+ attr_map->type = TYPE_STRING;
+ attr_map->required = YES;
+ attr_map->multiple = YES;
+ attr_map->gisprompt = "old,cell,raster" ;
+ attr_map->description = "Input geographics ATTRIBUTES in information system";
+
+ dec_map = G_define_option() ;
+ dec_map->key = "decision";
+ dec_map->type = TYPE_STRING;
+ dec_map->required = YES;
+ dec_map->gisprompt = "old,cell,raster" ;
+ dec_map->description = "Input geographics DECISION in information system";
+
+ genrules = G_define_option() ;
+ genrules->key = "strgy";
+ genrules->type = TYPE_STRING;
+ genrules->required = YES;
+ genrules->options = "Very fast,Fast,Medium,Best,All,Low,Upp,Normal";
+ genrules->description = "Strategies for generating rules";
+
+ clssfy = G_define_option() ;
+ clssfy->key = "clssfy";
+ clssfy->type = TYPE_STRING;
+ clssfy->required = YES;
+ clssfy->options = "Classify1,Classify2,Classify3";
+ clssfy->description = "Strategies for classified map (conflict resolution)";
+
+ output_txt = G_define_option();
+ output_txt->key = "outTXT";
+ output_txt->type = TYPE_STRING;
+ output_txt->required = YES;
+ output_txt->gisprompt = "new_file,file,output";
+ output_txt->answer ="InfoSys";
+ output_txt->description = "Output information system file";
+
+ output_map = G_define_option();
+ output_map->key = "outMAP";
+ output_map->type = TYPE_STRING;
+ output_map->required = YES;
+ output_map->gisprompt = "new,cell,raster";
+ output_map->answer ="classify";
+ output_map->description = "classified output map";
+
+
+ /* options and flags parser */
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+
+
+/***********************************************************************/
+/********Prepare and controlling Information System files **************/
+/***********************************************************************/
+
+ /* number of file (=attributes) */
+ nattributes=0;
+ while (attr_map->answers[nattributes]!=NULL)
+ {
+ nattributes++;
+ }
+
+/* store output classified MAP name in variable*/
+ result=output_map->answer;
+
+/*Convert strategy rules extraction answer in index. strcmp return 0 if answer is the passed string*/
+ if(strcmp(genrules->answer,"Very fast")==0)
+ strgy=0;
+ else if(strcmp(genrules->answer,"Fast")==0)
+ strgy=1;
+ else if(strcmp(genrules->answer,"Medium")==0)
+ strgy=2;
+ else if(strcmp(genrules->answer,"Best")==0)
+ strgy=3;
+ else if(strcmp(genrules->answer,"All")==0)
+ strgy=4;
+ else if(strcmp(genrules->answer,"Low")==0)
+ strgy=5;
+ else if(strcmp(genrules->answer,"Upp")==0)
+ strgy=6;
+ else
+ strgy=7;
+
+ /*Convert strategy map lassify answer in index. strcmp return 0 if answer is the passed string*/
+
+ if(strcmp(clssfy->answer,"Classify1")==0)
+ cls=0;
+ else if(strcmp(clssfy->answer,"Classify2")==0)
+ cls=1;
+ else if(strcmp(clssfy->answer,"Classify3")==0)
+ cls=2;
+ else
+ cls=0;
+
+
+ /* process the input maps:*/
+ /* ATTRIBUTES grid */
+ attributes = G_malloc((nattributes+1) * sizeof(struct input)); /*attributes is input struct defined in localproto.h*/
+
+ for (i = 0; i < nattributes; i++)
+ {
+ struct input *p = &attributes[i];
+ p->name = attr_map->answers[i];
+ p->mapset = G_find_cell(p->name,""); /* G_find_cell: Looks for the raster map "name" in the database. */
+ if (!p->mapset)
+ G_fatal_error(_("Raster file <%s> not found"), p->name);
+ p->fd = G_open_cell_old(p->name, p->mapset);
+ if (p->fd < 0)
+ G_fatal_error(_("Unable to open input map <%s> in mapset <%s>"),p->name, p->mapset);
+ p->buf = G_allocate_d_raster_buf(); /* Allocate an array of DCELL based on the number of columns in the current region. Return DCELL * */
+ }
+
+ /* determine the inputmap DECISION type (CELL/FCELL/DCELL) */
+ data_type = G_raster_map_type(dec_map->answer, mapset);
+
+ /* DECISION grid (at last column in Information System matrix) */
+ struct input *p = &attributes[nattributes];
+ p->name = dec_map->answer;
+ p->mapset = G_find_cell(p->name,""); /* G_find_cell: Looks for the raster map "name" in the database. */
+ if (!p->mapset)
+ G_fatal_error(_("Raster file <%s> not found"), p->name);
+ p->fd = G_open_cell_old(p->name, p->mapset);/*opens the raster file name in mapset for reading. A nonnegative file descriptor is returned if the open is successful.*/
+ if (p->fd < 0)
+ G_fatal_error(_("Unable to open input map <%s> in mapset <%s>"),p->name, p->mapset);
+ p->buf = G_allocate_raster_buf(data_type); /* Allocate an array of DCELL based on the number of columns in the current region. Return DCELL * */
+
+ /* Allocate output buffer, use input map data_type */
+ nrows = G_window_rows();
+ ncols = G_window_cols();
+ outrast = G_allocate_raster_buf(data_type);
+
+ rough_set_library_out(nrows, ncols, nattributes, attributes, output_txt->answer);/*build RSL standard file*/
+
+ classify_vect = G_malloc(sizeof(int) * (nrows*ncols)); /* memory allocation*/
+
+ rough_analysis(nrows, ncols, output_txt->answer, classify_vect, attributes,strgy,cls); /* extract rules from RSL and generate classified vectpr*/
+
+ /* controlling, if we can write the raster */
+ if ((outfd = G_open_raster_new(result, CELL_TYPE)) < 0)
+ G_fatal_error(_("Unable to create raster map <%s>"), result);
+
+ /* generate classified map*/
+ G_message("Building calssified map...");
+ j=0; /* builder map index*/
+ for(row = 0; row < nrows; row++)
+ {
+ CELL c;
+ G_percent(row, nrows, 2);
+ for (col = 0; col < ncols; col++)
+ {
+ c=((CELL *) classify_vect[j]);
+ ((CELL *) outrast)[col] = c;
+ j++;
+ }
+
+ if (G_put_raster_row(outfd, outrast, data_type) < 0)
+ G_fatal_error(_("Failed writing raster map <%s>"), result);
+ }
+
+
+ /* memory cleanup */
+ for (i = 0; i<=nattributes; i++)
+ G_close_cell(attributes[i].fd);
+ G_close_cell(outfd);
+
+ for (i = 0; i<nattributes; i++)
+ G_free(attributes[i].buf);
+
+ //G_free(outrast);
+
+ G_message("End: %s, with %d cells.",G_date(),j);
+ /* add command line incantation to history file */
+ G_short_history(result, "raster", &history);
+ G_command_history(&history);
+ G_write_history(result, &history);
+
+ exit(EXIT_SUCCESS);
+}
+
+/*dom 07 dic 2008 07:05:15 CET */
+
+
Property changes on: grass-addons/raster/mcda/r.roughset/main.c
___________________________________________________________________
Name: svn:executable
+ *
Deleted: grass-addons/raster/mcda/r.roughset/rules_extr.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/rules_extr.c 2008-12-07 03:55:48 UTC (rev 34767)
+++ grass-addons/raster/mcda/r.roughset/rules_extr.c 2008-12-07 06:35:29 UTC (rev 34768)
@@ -1,344 +0,0 @@
-/************************************************************************
-EXTRACT RULE FROM GEOGRAPHICS THEMES (Based on Rough Set Library
- written by M.Gawrys J.Sienkiewiczbrary )
-
-The RSL defines three types to be used in applications:
- setA - set of attributes,
- setO - set of objects and
- SYSTEM - information system descriptor.
-
-The descriptor contains pointers to the information system data matrices:
-- MATRIX A, is the attribute-value table.
-- MATRIX D, is the discernibility matrix
-- MATRIX X, is called a reduced discernibility matrix.
-
-/***********************************************************************/
-
-#include "rough.h"
-#include "localproto.h"
-
-
-
-int RulsExtraction(char *name, struct input *attributes, int strgy);
-void fPrintSetA( FILE *f, setA set );
-void fPrintSetO( FILE *f, setO set );
-void fPrintRules( FILE *file, value_type* rules, int N, int *opr, setA P, setA Q, struct input *attributes);
-void fPrintToScript( FILE *file, value_type* rules, int N, int *opr, setA P, setA Q, struct input *attributes);
-float MeanAttrInRule( value_type *rules, int N, setA P );
-int LowRules( value_type **rules, setA P, setA Q, int mat );
-int UppRules( value_type **rules, setA P, setA Q, int mat );
-int NormRules( value_type **rules, setA P, setA Q, int mat );
-
-
-
-int RulsExtraction(char *name,struct input *attributes, int strgy) /*old MAIN*/
-{
- SYSTEM *sys1, *sys2, *trainsys, *testsys; /* Information system descriptor structures. It contains information about the system parameters (number of objects, number of attributes, system name. */
- value_type *buf,*rules; /* stores a single value of attribute (used for attribute-value table and rule implementation)*/
- char c;
- int ob,at,at2,n,j,i,work=1,good,r;
- int *opr;
- setA beg,P,Q,full,core; /*set of attributes*/
- setO train; /*set of object*/
- FILE *file, *file2, *file3;
- int (*genrules)( value_type**, setA, setA, int );
- int p30, sample;
- clock_t time1, time2;
- int inf;
- char *nameFILE; /*storage root file names*/
-
-
- sys1=InitEmptySys(); /* Allocates memory for a system descriptor and returns a pointer.*/
-
-
- FileToSys(sys1,name); /* Imports a system from a file of the special format.*/
-
-
- if (_rerror>0)
- { G_fatal_error(" Can't open data file \n");
- return(1); }
-
-
- strcat(name,".out");
-
- if (!(file2=fopen(name,"a"))) /*output text file*/
- {G_fatal_error(" Can't open output file \n");
- return(1);}
-
- strcat(name,".sh");
-
- if (!(file3=fopen(name,"w"))) /*output mapcalc file*/
- {G_fatal_error(" Can't open output file \n");
- return(1);}
-
-
- UseSys(sys1); /* Activates a system. All routines will work on this indicated system data and parameters.*/
- full=InitFullSetA(); /*Initialize a full set. Allocates memory for a set and initialize it with all elements of
- domain based on the active information system */
-
- InitD(sys1); /*Generates MATRIX D from MATRIX A.Connected to sys and filled*/
-
- P=InitEmptySetA(); /* Initialize an empty set. Memory size determined by active information system parameters */
- Q=InitEmptySetA();
-
-
-
- /* define attribute */
- for(i=0;i<(AttributesNum(sys1)-1);i++)
- {
- AddSetA(P,i); /* Adds a single element to a set */
- }
-
-
- /* define decision */
- AddSetA(Q,(AttributesNum(sys1)-1));
-
-
- /*printf("number of objects: %i \n",ObjectsNum(sys1)); Returns number of objects in a system*/
- /*printf("number of attributes: %i \n",AttributesNum(sys1)); Returns a number of attributes in a system.*/
-
-
- fprintf(file2,"Condition attributes are\n");
- fPrintSetA(file2,P);
- PrintSetA(P); /* Writes a formatted condition set to the standard output.Prints elements in brackets. */
- fprintf(file2,"\nDecision attributes are\n");
- fPrintSetA(file2,Q);
- PrintSetA(Q); /* Writes a formatted decision set to the standard output.Prints elements in brackets. */
- fprintf(file2,"\nDependCoef = %f\n",DependCoef(P,Q,MATD)); /* Returns degree of dependency Q from P in the active information system.*/
-
- InitX(sys1,P,Q,MATD); /*Generates MATRIX X from another matrix designed for use in core and reducts queries. */
- core=InitEmptySetA();
- Core(core,MATX); /* Puts a core of the active information system to core. */
- fprintf(file2,"CORE = ");
- fPrintSetA(file2,core);
- RedOptim( core, MATX ); /* Provides the optimized heuristic search for a single reduct */
- fprintf(file2,"\nRedOptim = ");
- fPrintSetA(file2,core);
- CloseSetA(core); /* Frees memory allocated for a set */
- n=RedFew(&beg,MATX); /*Finds all reducts shorter than the first one found.*/
- CloseMat(sys1,MATX); /* Closes a matrix. */
-
- fprintf( file2,"\nFew reducts ( %i ):\n", n );
- for (i=0;i<n;i++)
- {
- fPrintSetA( file2, beg+i*sys1->setAsize );
- fprintf(file2,"\n");
- }
- if ( n>0 ) free( beg );
-
-
-
-
-
- fprintf(file2, "%d strategy of generating rules\n", strgy);
-
- switch ( strgy )
- {
- case 0: genrules = VeryFastRules;
- break;
- case 1: genrules = FastRules;
- break;
- case 2: genrules = Rules;
- break;
- case 3: genrules = BestRules;
- break;
- case 4: genrules = AllRules;
- break;
- case 5: genrules = LowRules;
- break;
- case 6: genrules = UppRules;
- break;
- default: genrules = NormRules;
- break;
- }
-
-/*****************************************************************************/
- time1 = clock();
- r = genrules ( &rules, P, Q, MATD );
- time2 = clock();
- if (r>0)
- { fprintf(file2,"Time of generating rules = %ds\n", time2, (time2-time1)/CLOCKS_PER_SEC );
- opr = StrengthOfRules( rules, r ); /* Creates a table of rules strengths*/
- fprintf(file2,"Rules ( %i )\n", r );
- fPrintRules( file2, rules, r, opr, P, Q, attributes ); /*print to file generated rules*/
- fPrintToScript( file3, rules, r, opr, P, Q, attributes );/*build mapcalc file*/
- fprintf(file2,"Mean number of attributes in rule = %.1f\n",MeanAttrInRule( rules, r, P ));
- free( rules );
- free( opr );
- }
-/******************************************************************************/
-
-
- CloseSetA(P);
- CloseSetA(Q);
- CloseSetA(full);
- CloseSys(sys1);
- fclose(file2);
- fclose(file3);
- G_message("O.K. output placed to files");
- return (0);
-}
-
-
-void fPrintSetA( FILE *f, setA set )
-{ int attr;
- fprintf(f,"{");
- for (attr=0; attr<_mainsys->attributes_num ;attr++)
- if (ContSetA(set,attr))
- { fprintf(f," %d",attr);
- attr++;
- break;
- }
- for (; attr<_mainsys->attributes_num ;attr++)
- if (ContSetA(set,attr)) fprintf(f,",%d",attr);
- fprintf(f," }");
-}
-
-void fPrintSetO( FILE *f, setO set )
-{ int obj;
- fprintf(f,"{");
- for (obj=0; obj<_mainsys->objects_num ;obj++)
- if (ContSetO(set,obj))
- { fprintf(f," %d",obj);
- obj++;
- break;
- }
- for (; obj<_mainsys->objects_num ;obj++)
- if (ContSetO(set,obj)) fprintf(f,",%d",obj);
- fprintf(f," }");
-}
-
-void fPrintRules( FILE *file, value_type* rules,
- int N, int *opr, setA P, setA Q, struct input *attributes )
-{ int n,j;
-
- for (n=0;n<N;n++)
- { for (j=0;j<_mainsys->attributes_num;j++)
- if ( ContSetA( P,j ) )
- if (rules[n*_mainsys->attributes_num+j]!=MINUS)
- fprintf(file,"%s=%d ", attributes[j].name,rules[n*_mainsys->attributes_num+j]);
- fprintf(file," => ");
- for (j=0;j<_mainsys->attributes_num;j++)
- if ( ContSetA( Q, j ) )
- if ((rules+n*_mainsys->attributes_num)[j]!=MINUS)
- fprintf(file,"%s=%d ", attributes[j].name,(rules+n*_mainsys->attributes_num)[j]);
- fprintf(file," ( %i objects )\n", opr[n] );
- }
- return;
-}
-
-
-void fPrintToScript( FILE *file, value_type* rules, int N, int *opr, setA P, setA Q, struct input *attributes )
-{
- int n,j,i;
- fprintf(file,"#!/bin/bash\nRASTERrule=${1}\nif [ $# -ne 1 ]; then\n\techo 'Output file named roughmap, "
- "Consider renaming'\n\tRASTERrule=roughmap\nfi\n");
- for (n=0;n<N;n++)
- {
- i=0;
- for (j=0;j<_mainsys->attributes_num;j++)
- {
- if ( ContSetA( P,j ) ) /*Tests if a set contains an element*/
- if (rules[n*_mainsys->attributes_num+j]!=MINUS) /*Missing values are coded by MINUS*/
- {
- if(i==0) /*if the role is only one or the firsth ...*/
- fprintf(file,"r.mapcalc 'maprule%d=if(%s==%d",n,attributes[j].name,rules[n*_mainsys->attributes_num+j]);
- else /*... otherwise ...*/
- fprintf(file," && %s==%d",attributes[j].name,rules[n*_mainsys->attributes_num+j]);
- i++;
- }
- }
- for (j=0;j<_mainsys->attributes_num;j++)
- if ( ContSetA( Q,j ) ) /*Tests if a set contains an element*/
- if ((rules+n*_mainsys->attributes_num)[j]!=MINUS) /*Missing values are coded by MINUS*/
- fprintf(file,",%d,null())'\n",(rules+n*_mainsys->attributes_num)[j]);
- }
-
-
- fprintf(file,"\nr.patch --overwrite input=");
- for (n=0;n<N;n++)
- {
- fprintf(file,"maprule%d",n);
- if(n<N-1) {fprintf(file,",");}
- }
- fprintf(file," output=$RASTERrule");
-
- fprintf(file,"\ng.remove rast=");
- for (n=0;n<N;n++)
- {
- fprintf(file,"maprule%d",n);
- if(n<N-1) {fprintf(file,",");}
- }
-
- fprintf(file,"\nr.colors map=$RASTERrule color=ryg ");
-
- return;
-}
-
-/*
-void fPrintToScript( FILE *file, value_type* rules,
- int N, int *opr, setA P, setA Q, struct input *attributes )
-{ int n,j,i;
-
- fprintf(file,"roughmap=");
- for (n=0;n<N;n++)
- {
- i=0;
- for (j=0;j<_mainsys->attributes_num;j++)
- {
- if ( ContSetA( P,j ) )
- if (rules[n*_mainsys->attributes_num+j]!=MINUS)
- {
- if(i==0)
- fprintf(file,"if(%s==%d",attributes[j].name,rules[n*_mainsys->attributes_num+j]);
- else
- fprintf(file," && %s==%d",attributes[j].name,rules[n*_mainsys->attributes_num+j]);
- i++;
- }
-
- }
-
- for (j=0;j<_mainsys->attributes_num;j++)
- if ( ContSetA( Q,j ) )
- if ((rules+n*_mainsys->attributes_num)[j]!=MINUS)
- fprintf(file,",%d)",(rules+n*_mainsys->attributes_num)[j]);
- if(n<N-1)
- fprintf(file,"\\ \n + ");
- }
-
- fprintf(file,"\nend\n");
- return;
-}
-*/
-
-
-float MeanAttrInRule( value_type *rules, int N, setA P )
-{ int counter=0;
- int i,j;
- int size=_mainsys->attributes_num;
- for (i=0; i<N; i++)
- for (j=0; j<size; j++)
- if ( ContSetA( P, j ) )
- if ( (rules+i*size)[j]!=MINUS ) counter++;
- return (float)counter/N;
-}
-
-int LowRules( value_type **rules, setA P, setA Q, int mat )
-{
- return ApprRules( rules, P, Q, LOWER, mat );
-}
-
-int UppRules( value_type **rules, setA P, setA Q, int mat )
-{
- return ApprRules( rules, P, Q, UPPER, mat );
-}
-
-int NormRules( value_type **rules, setA P, setA Q, int mat )
-{
- return ApprRules( rules, P, Q, NORMAL, mat );
-}
-
-
-
-
-
Added: grass-addons/raster/mcda/r.roughset/rules_extr.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/rules_extr.c (rev 0)
+++ grass-addons/raster/mcda/r.roughset/rules_extr.c 2008-12-07 06:35:29 UTC (rev 34768)
@@ -0,0 +1,415 @@
+/****************************************************************************
+ *
+ * MODULE: r.roughset
+ * AUTHOR(S): GRASS module authors ad Rough Set Library (RSL) maintain:
+ * G.Massei (g_massa at libero.it)-A.Boggia (boggia at unipg.it)
+ * Rough Set Library (RSL) ver. 2 original develop:
+ * M.Gawrys - J.Sienkiewicz
+ *
+ * PURPOSE: Geographics rough set analisys and knowledge discovery
+ *
+ * COPYRIGHT: (C) GRASS Development Team (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+/************************************************************************
+** EXTRACT RULE FROM GEOGRAPHICS THEMES (Based on Rough Set Library
+** written by M.Gawrys J.Sienkiewiczbrary )
+**
+** The RSL defines three types to be used in applications:
+** setA - set of attributes,
+** setO - set of objects and
+** SYSTEM - information system descriptor.
+
+** The descriptor contains pointers to the information system data matrices:
+** - MATRIX A, is the attribute-value table.
+** - MATRIX D, is the discernibility matrix
+** - MATRIX X, is called a reduced discernibility matrix.
+
+/***********************************************************************/
+
+#include "rough.h"
+#include "localproto.h"
+
+int rough_analysis(int nrows, int ncols, char *name, int *classify_vect, struct input *attributes, int strgy, int cls);
+
+void rough_set_library_out(int nrows, int ncols, int nattributes, struct input *attributes, char *file_out_sys);
+
+void output_to_txt( FILE *file_out_txt, value_type* rules, setA P,setA Q, setA core, setA beg, int n, SYSTEM * sys1, int strgy, int r, int *opr, struct input *attributes);
+
+void fPrintSetA( FILE *file, setA set );
+void fPrintSetO( FILE *file, setO set );
+void fPrintRules( FILE *file, value_type* rules, int N, int *opr, setA P, setA Q, struct input *attributes);
+
+float MeanAttrInRule( value_type *rules, int N, setA P );
+
+int LowRules( value_type **rules, setA P, setA Q, int mat );
+int UppRules( value_type **rules, setA P, setA Q, int mat );
+int NormRules( value_type **rules, setA P, setA Q, int mat );
+
+
+int rough_analysis(int nrows, int ncols, char *name, int *classify_vect, struct input *attributes, int strgy, int cls) /*old MAIN*/
+{
+ SYSTEM *sys1, *sys2; /* Information system descriptor structures.*/
+ /* It contains information about the system parameters (number of objects, number of attributes, system name. */
+ value_type value, *buf,*rules; /* stores a single value of attribute (used for attribute-value table and rule implementation)*/
+ char c;
+ int n,j,i,r, *opr;
+ setA beg,P,Q,full,core; /*set of attributes*/
+ setO train; /*set of object*/
+ FILE *file_out_sys, *file_out_txt;
+ int (*genrules)( value_type**, setA, setA, int );
+
+ int nattributes, nobjects=nrows*ncols; /*objects in information system are all raster cells in working location with a defined resolution */
+ int row, col,object,attribute; /*index and counter*/
+
+ sys1=InitEmptySys(); /* Allocates memory for a system descriptor and returns a pointer.*/
+
+ FileToSys(sys1,name); /* Imports a system from a file of the special format.*/
+
+ if (_rerror>0)
+ { G_fatal_error(" Can't open data file \n");
+ return(1);}
+
+ strcat(name,".out");
+
+ if (!(file_out_txt=fopen(name,"a"))) /*output text file*/
+ {G_fatal_error(" Can't open output file \n");
+ return(1);}
+
+ UseSys(sys1); /* Activates a system. All routines will work on this indicated system data and parameters.*/
+
+ if (_rerror>0)
+ {G_fatal_error("Can't open information system <%s>\n",sys2->name);
+ return(1); }
+
+
+ full=InitFullSetA(); /*Initialize a full set. Allocates memory for a set and initialize it with all elements of
+ domain based on the active information system */
+
+ InitD(sys1); /*Generates MATRIX D from MATRIX A.Connected to sys and filled*/
+
+ P=InitEmptySetA(); /* Initialize an empty set. Memory size determined by active information system parameters */
+ Q=InitEmptySetA();
+
+ nattributes=(AttributesNum(sys1)-1);
+
+ /* define attribute */
+ for(i=0;i<nattributes;i++)
+ {
+ AddSetA(P,i); /* Adds a single element to a set */
+ }
+
+ /* define decision */
+ AddSetA(Q,(AttributesNum(sys1)-1));
+
+ InitX(sys1,P,Q,MATD); /*Generates MATRIX X from another matrix designed for use in core and reducts queries. */
+ core=InitEmptySetA();
+ Core(core,MATX); /* Puts a core of the active information system to core. */
+
+ RedOptim( core, MATX ); /* Provides the optimized heuristic search for a single reduct */
+
+ CloseSetA(core); /* Frees memory allocated for a set */
+ n=RedFew(&beg,MATX); /*Finds all reducts shorter than the first one found.*/
+ CloseMat(sys1,MATX); /* Closes a matrix. */
+
+ if ( n>0 ) free( beg );
+
+ switch ( strgy )
+ {
+ case 0: genrules = VeryFastRules;
+ break;
+ case 1: genrules = FastRules;
+ break;
+ case 2: genrules = Rules;
+ break;
+ case 3: genrules = BestRules;
+ break;
+ case 4: genrules = AllRules;
+ break;
+ case 5: genrules = LowRules;
+ break;
+ case 6: genrules = UppRules;
+ break;
+ default: genrules = NormRules;
+ break;
+ }
+
+ r = genrules ( &rules, P, Q, MATD ); /* rules generator*/
+
+ if (r>0) {opr = StrengthOfRules( rules, r ); } /* Creates a table of rules strengths*/
+
+
+/**************************Output text files************************************/
+/***********print output about sys1 in a txt file (file_out_txt)****************/
+ output_to_txt(file_out_txt,rules,P,Q,core,beg,n,sys1,strgy,r,opr,attributes);
+
+/**************************close all********************************************/
+
+ CloseSys(sys1); /* close sys1 */
+
+/*******************************************************************************/
+/**************************Classify*********************************************/
+
+ sys2=InitEmptySys();
+ SetParameters(sys2,nobjects,nattributes); /* assigning system parameters */
+ ConnectA(sys2,malloc(MatMemSize(sys2,MATA))); /* Connects MATRIX A to a system descriptor. */
+ /*MatMemSize: Returns size of memory used by matrix.*/
+ UseSys(sys2); /* active system sys2 was created in application and has only MATRIX A */
+
+ if (_rerror>0)
+ { G_fatal_error("Can't open information system <%s>\n",_mainsys->name);
+ return(1); }
+
+ G_message("Build information system for classification ");
+ for(i=0;i<nattributes;i++)
+ {
+ object=0; /* set object numbers =0 and increase it until rows*cells for each attribute */
+ for (row = 0; row < nrows; row++)
+ {
+ G_percent(row, nrows, 1);
+ /* Reads appropriate information into the buffer buf associated with the requested row*/
+ G_get_d_raster_row (attributes[i].fd, attributes[i].buf, row);
+ for (col=0;col<ncols;col++)
+ {
+ value = (attributes[i].buf[col]); /*make a cast on the DCELL output value*/
+ PutA(_mainsys,object,i,value); /* filling MATRIX A */
+ object++;
+ }
+ }
+ }
+
+
+ buf = MatExist(_mainsys,MATA); /*Returns pointer to specified matrix if exists*/
+
+ if (!buf)
+ { G_fatal_error("Error in the information system <%s>\n",_mainsys->name);
+ return(1); }
+
+ switch ( cls )
+ {
+ case 0:
+ {
+ for (j=0;j<_mainsys->objects_num;j++)
+ { /*Chooses the best rule to cover sample. Strategy no. 1*/
+ classify_vect[j]=Classify1(buf+j*_mainsys->attributes_num,rules,r,P,Q);
+ }
+ }
+ break;
+ case 1:
+ {
+ for (j=0;j<_mainsys->objects_num;j++)
+ { /*Chooses the best rule to cover sample. Strategy no. 2*/
+ classify_vect[j]=Classify2(buf+j*_mainsys->attributes_num,rules,r,P,Q);
+ }
+ }
+ break;
+ case 2:
+ {
+ for (j=0;j<_mainsys->objects_num;j++)
+ { /*Chooses the best rule to cover sample. Strategy no. 3*/
+ classify_vect[j]=Classify3(buf+j*_mainsys->attributes_num,rules,r,opr,P,Q);
+ }
+ }
+ break;
+
+ default:;
+ break;
+ }
+
+
+ G_message("All cells classified (%d)",j);
+
+/*****************************************************************************/
+
+ free( rules );
+ free( opr );
+ CloseSetA(P);
+ CloseSetA(Q);
+ CloseSetA(full);
+ CloseSys(sys2);
+ //fclose(file);
+ fclose(file_out_txt);
+ return (0);
+}
+
+void rough_set_library_out(int nrows, int ncols, int nattribute, struct input *attributes, char *file_out_sys)
+
+{
+ int row, col, i, j;
+ int value, decvalue;
+ int nobject;
+ char cell_buf[300];
+ FILE *fp; /*file pointer for ASCII output*/
+
+ /* open *.sys file for writing or use stdout */
+ if(NULL == (fp = fopen(file_out_sys, "w")))
+ G_fatal_error("Not able to open file [%s]",file_out_sys);
+
+ fprintf(fp,"NAME: %s\nATTRIBUTES: %d\-nOBJECTS: %s\n",file_out_sys,nattribute+1," ");
+
+ /************** process the data *************/
+
+ G_message("Build information system for rules extraction");
+
+ nobject=0;
+
+ for (row = 0; row < nrows; row++)
+ {
+ G_percent(row, nrows, 1);
+ for(i=0;i<=nattribute;i++)
+ {
+ G_get_d_raster_row (attributes[i].fd, attributes[i].buf, row);/* Reads appropriate information into the buffer buf associated with the requested row*/
+ }
+ for (col=0;col<ncols;col++)
+ { /*make a cast on the DCELL output value*/
+ decvalue=(int)attributes[nattribute].buf[col];
+ if(0<decvalue) /* TODO: correct form will: decval!=null */
+ {
+ for(j=0;j<nattribute;j++)
+ { /*make a cast on the DCELL output value*/
+ value = (int)(attributes[j].buf[col]);
+ sprintf(cell_buf, "%d",value);
+ G_trim_decimal (cell_buf);
+ fprintf (fp,"%s ",cell_buf);
+ }
+ fprintf(fp,"%d \n",decvalue);
+ nobject++;
+ }
+ }
+ }
+
+ /************** write code file*************/
+
+ for(i=0;i<=nattribute;i++)
+ {
+ fprintf(fp,"\n%s",attributes[i].name);
+ }
+
+ /************** write header file*************/
+
+ if(0<fseek(fp,0L,0)) /*move file pointer to header file*/
+ G_fatal_error("Not able to write file [%s]",file_out_sys);
+ else
+ fprintf(fp,"NAME: %s\nATTRIBUTES: %d\nOBJECTS: %d\n",file_out_sys,nattribute+1,nobject);
+
+ /************** close all and exit ***********/
+
+ fclose(fp);
+
+}
+
+
+void output_to_txt( FILE *file_out_txt, value_type* rules, setA P,setA Q, setA core, setA beg, int n, SYSTEM * sys1, int strgy, int r, int *opr, struct input *attributes)
+{
+ int i;
+ fprintf(file_out_txt,"Condition attributes are\n");
+ fPrintSetA(file_out_txt,P);
+ fprintf(file_out_txt,"\nDecision attributes are\n");
+ fPrintSetA(file_out_txt,Q);
+ fprintf(file_out_txt,"\nDependCoef = %f\n",DependCoef(P,Q,MATD)); /* Returns degree of dependency Q from P in the active information system.*/
+ fprintf(file_out_txt,"CORE = ");
+ fPrintSetA(file_out_txt,core);
+ fprintf(file_out_txt,"\nRedOptim = ");
+ fPrintSetA(file_out_txt,core);
+ fprintf( file_out_txt,"\nFew reducts ( %i ):\n", n );
+
+ for (i=0;i<n;i++)
+ {
+ fPrintSetA( file_out_txt, beg+i*sys1->setAsize );
+ fprintf(file_out_txt,"\n");
+ }
+
+ fprintf(file_out_txt, "%d strategy of generating rules\n", strgy);
+
+ if (r>0)
+ { fprintf(file_out_txt,"Rules ( %i )\n", r );
+ fPrintRules( file_out_txt, rules, r, opr, P, Q, attributes ); /*print to file generated rules*/
+ fprintf(file_out_txt,"Mean number of attributes in rule = %.1f\n",MeanAttrInRule( rules, r, P ));
+ }
+}
+
+
+
+
+void fPrintSetA( FILE *f, setA set )
+{ int attr;
+ fprintf(f,"{");
+ for (attr=0; attr<_mainsys->attributes_num ;attr++)
+ if (ContSetA(set,attr))
+ { fprintf(f," %d",attr);
+ attr++;
+ break;
+ }
+ for (; attr<_mainsys->attributes_num ;attr++)
+ if (ContSetA(set,attr)) fprintf(f,",%d",attr);
+ fprintf(f," }");
+}
+
+void fPrintSetO( FILE *f, setO set )
+{ int obj;
+ fprintf(f,"{");
+ for (obj=0; obj<_mainsys->objects_num ;obj++)
+ if (ContSetO(set,obj))
+ { fprintf(f," %d",obj);
+ obj++;
+ break;
+ }
+ for (; obj<_mainsys->objects_num ;obj++)
+ if (ContSetO(set,obj)) fprintf(f,",%d",obj);
+ fprintf(f," }");
+}
+
+void fPrintRules( FILE *file, value_type* rules,
+ int N, int *opr, setA P, setA Q, struct input *attributes )
+{ int n,j;
+
+ for (n=0;n<N;n++)
+ { for (j=0;j<_mainsys->attributes_num;j++)
+ if ( ContSetA( P,j ) )
+ if (rules[n*_mainsys->attributes_num+j]!=MINUS)
+ fprintf(file,"%s=%d ", attributes[j].name,rules[n*_mainsys->attributes_num+j]);
+ fprintf(file," => ");
+ for (j=0;j<_mainsys->attributes_num;j++)
+ if ( ContSetA( Q, j ) )
+ if ((rules+n*_mainsys->attributes_num)[j]!=MINUS)
+ fprintf(file,"%s=%d ", attributes[j].name,(rules+n*_mainsys->attributes_num)[j]);
+ fprintf(file," ( %i objects )\n", opr[n] );
+ }
+ return;
+}
+
+
+
+float MeanAttrInRule( value_type *rules, int N, setA P )
+{ int counter=0;
+ int i,j;
+ int size=_mainsys->attributes_num;
+ for (i=0; i<N; i++)
+ for (j=0; j<size; j++)
+ if ( ContSetA( P, j ) )
+ if ( (rules+i*size)[j]!=MINUS ) counter++;
+ return (float)counter/N;
+}
+
+int LowRules( value_type **rules, setA P, setA Q, int mat )
+{
+ return ApprRules( rules, P, Q, LOWER, mat );
+}
+
+int UppRules( value_type **rules, setA P, setA Q, int mat )
+{
+ return ApprRules( rules, P, Q, UPPER, mat );
+}
+
+int NormRules( value_type **rules, setA P, setA Q, int mat )
+{
+ return ApprRules( rules, P, Q, NORMAL, mat );
+}
+
+
+/*dom 07 dic 2008 07:04:58 CET */
+
+
Property changes on: grass-addons/raster/mcda/r.roughset/rules_extr.c
___________________________________________________________________
Name: svn:executable
+ *
More information about the grass-commit
mailing list