[GRASS-SVN] r34904 - grass-addons/raster/mcda/r.roughset
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Dec 15 18:17:11 EST 2008
Author: gianluca
Date: 2008-12-15 18:17:11 -0500 (Mon, 15 Dec 2008)
New Revision: 34904
Modified:
grass-addons/raster/mcda/r.roughset/main.c
grass-addons/raster/mcda/r.roughset/rules_extr.c
Log:
r.roughset: more options (rules from txt) and better interface
Modified: grass-addons/raster/mcda/r.roughset/main.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/main.c 2008-12-15 23:09:20 UTC (rev 34903)
+++ grass-addons/raster/mcda/r.roughset/main.c 2008-12-15 23:17:11 UTC (rev 34904)
@@ -41,7 +41,7 @@
struct GModule *module; /* GRASS module for parsing arguments */
- struct Option *attr_map, *dec_map, *genrules, *clssfy, *output_txt, *output_map; /* options */
+ struct Option *attr_map, *dec_map, *dec_txt, *genrules, *clssfy, *output_txt, *output_map; /* options */
/*struct Flag *flagQuiet flags */
/* initialize GIS environment */
@@ -65,7 +65,7 @@
dec_map = G_define_option() ;
dec_map->key = "decision";
dec_map->type = TYPE_STRING;
- dec_map->required = YES;
+ dec_map->required = NO;
dec_map->gisprompt = "old,cell,raster" ;
dec_map->description = "Input geographics DECISION in information system";
@@ -74,13 +74,22 @@
genrules->type = TYPE_STRING;
genrules->required = YES;
genrules->options = "Very fast,Fast,Medium,Best,All,Low,Upp,Normal";
+ genrules->answer = "Very fast";
genrules->description = "Strategies for generating rules";
+ dec_txt = G_define_option();
+ dec_txt->key = "sample";
+ dec_txt->type = TYPE_STRING;
+ dec_txt->required = NO;
+ dec_txt->gisprompt = "old_file,file,input";
+ dec_txt->description = "Input text file with data and decision sample";
+
clssfy = G_define_option() ;
- clssfy->key = "clssfy";
- clssfy->type = TYPE_STRING;
- clssfy->required = YES;
- clssfy->options = "Classify1,Classify2,Classify3";
+ clssfy->key = "clssfy";
+ clssfy->type = TYPE_STRING;
+ clssfy->required = YES;
+ clssfy->options = "Classify1,Classify2,Classify3";
+ clssfy->answer ="Classify1";
clssfy->description = "Strategies for classified map (conflict resolution)";
output_txt = G_define_option();
@@ -104,8 +113,10 @@
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
+ /* Either decision map or sample file are necesary*/
+ if (dec_map->answer==NULL && dec_txt->answer==NULL)
+ G_fatal_error(_("Either decision map or sample file are necessary!"));
-
/***********************************************************************/
/********Prepare and controlling Information System files **************/
/***********************************************************************/
@@ -168,9 +179,16 @@
}
/* determine the inputmap DECISION type (CELL/FCELL/DCELL) */
- data_type = G_raster_map_type(dec_map->answer, mapset);
-
+ data_type = CELL_TYPE; //G_raster_map_type(dec_map->answer, mapset);
+ /* Allocate output buffer, use input map data_type */
+ nrows = G_window_rows();
+ ncols = G_window_cols();
+ outrast = G_allocate_raster_buf(data_type);
+
+
/* DECISION grid (at last column in Information System matrix) */
+ if(dec_map->answer!=NULL)
+ {
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. */
@@ -179,18 +197,15 @@
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 * */
+ 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 * */
+ rough_set_library_out(nrows, ncols, nattributes, attributes, output_txt->answer);/*build RSL standard file*/
+ }
- /* 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*/
+ rough_analysis(nrows, ncols, output_txt->answer, classify_vect, attributes, dec_txt->answer, 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)
Modified: grass-addons/raster/mcda/r.roughset/rules_extr.c
===================================================================
--- grass-addons/raster/mcda/r.roughset/rules_extr.c 2008-12-15 23:09:20 UTC (rev 34903)
+++ grass-addons/raster/mcda/r.roughset/rules_extr.c 2008-12-15 23:17:11 UTC (rev 34904)
@@ -33,7 +33,7 @@
#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);
+int rough_analysis(int nrows, int ncols, char *name, int *classify_vect, struct input *attributes, char *file_sample_txt, int strgy, int cls);
void rough_set_library_out(int nrows, int ncols, int nattributes, struct input *attributes, char *file_out_sys);
@@ -50,7 +50,7 @@
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*/
+int rough_analysis(int nrows, int ncols, char *name, int *classify_vect, struct input *attributes, char *file_sample_txt, int strgy, int cls)
{
SYSTEM *sys1, *sys2; /* Information system descriptor structures.*/
/* It contains information about the system parameters (number of objects, number of attributes, system name. */
@@ -59,22 +59,27 @@
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;
+ FILE *file_out_txt; /* pointer to text output file */
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.*/
-
+
+ if(file_sample_txt != NULL) /*use sample txt file if input in dec_txt->answer isn't NUL*/
+ {name=file_sample_txt;
+ G_message("Using %s sys file for rules generation",name);} /* Imports a system from a file of the special format.*/
+
+
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");
-
+
+ strcat(name,".out");
+
if (!(file_out_txt=fopen(name,"a"))) /*output text file*/
{G_fatal_error(" Can't open output file \n");
return(1);}
@@ -82,7 +87,7 @@
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);
+ {G_fatal_error("Can't open information system <%s>\n",sys1->name);
return(1); }
@@ -123,13 +128,13 @@
break;
case 1: genrules = FastRules;
break;
- case 2: genrules = Rules;
+ case 2: genrules = Rules;
break;
case 3: genrules = BestRules;
break;
case 4: genrules = AllRules;
break;
- case 5: genrules = LowRules;
+ case 5: genrules = LowRules;
break;
case 6: genrules = UppRules;
break;
@@ -148,17 +153,18 @@
/**************************close all********************************************/
- CloseSys(sys1); /* close sys1 */
+ //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. */
+ 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.*/
+ SetName(sys2,"classys");
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); }
@@ -181,20 +187,21 @@
}
}
-
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);
+ classify_vect[j]=Classify1(buf+j*_mainsys->attributes_num,rules,r,P,Q);
+ G_percent(j, _mainsys->objects_num, 1);
}
}
break;
@@ -202,7 +209,8 @@
{
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);
+ classify_vect[j]=Classify2(buf+j*_mainsys->attributes_num,rules,r,P,Q);
+ G_percent(j, _mainsys->objects_num, 1);
}
}
break;
@@ -210,12 +218,13 @@
{
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);
+ classify_vect[j]=Classify3(buf+j*_mainsys->attributes_num,rules,r,opr,P,Q);
+ G_percent(j, _mainsys->objects_num, 1);
}
}
break;
- default:;
+ default: 0;
break;
}
@@ -258,7 +267,7 @@
for (row = 0; row < nrows; row++)
{
- G_percent(row, nrows, 1);
+ //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*/
More information about the grass-commit
mailing list