[GRASS-SVN] r53911 - in grass-addons/grass6/raster: . r.roughset
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Nov 19 05:37:34 PST 2012
Author: gianluca
Date: 2012-11-19 05:37:34 -0800 (Mon, 19 Nov 2012)
New Revision: 53911
Added:
grass-addons/grass6/raster/r.roughset/
grass-addons/grass6/raster/r.roughset/Makefile
grass-addons/grass6/raster/r.roughset/description.html
grass-addons/grass6/raster/r.roughset/localproto.h
grass-addons/grass6/raster/r.roughset/main.c
grass-addons/grass6/raster/r.roughset/raccess.c
grass-addons/grass6/raster/r.roughset/raccess.h
grass-addons/grass6/raster/r.roughset/rbasic.c
grass-addons/grass6/raster/r.roughset/rbasic.h
grass-addons/grass6/raster/r.roughset/rclass.c
grass-addons/grass6/raster/r.roughset/rclass.h
grass-addons/grass6/raster/r.roughset/rcore.c
grass-addons/grass6/raster/r.roughset/rcore.h
grass-addons/grass6/raster/r.roughset/reduct1.c
grass-addons/grass6/raster/r.roughset/reduct1.h
grass-addons/grass6/raster/r.roughset/reduct2.c
grass-addons/grass6/raster/r.roughset/reduct2.h
grass-addons/grass6/raster/r.roughset/rerror.h
grass-addons/grass6/raster/r.roughset/rough.h
grass-addons/grass6/raster/r.roughset/rset.c
grass-addons/grass6/raster/r.roughset/rset.h
grass-addons/grass6/raster/r.roughset/rsystem.c
grass-addons/grass6/raster/r.roughset/rsystem.h
grass-addons/grass6/raster/r.roughset/rule1.c
grass-addons/grass6/raster/r.roughset/rule1.h
grass-addons/grass6/raster/r.roughset/rule2.c
grass-addons/grass6/raster/r.roughset/rule2.h
grass-addons/grass6/raster/r.roughset/rules_extr.c
Log:
Added: grass-addons/grass6/raster/r.roughset/Makefile
===================================================================
--- grass-addons/grass6/raster/r.roughset/Makefile (rev 0)
+++ grass-addons/grass6/raster/r.roughset/Makefile 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../../..
+
+PGM = r.roughset
+
+LIBES= $(GISLIB) $(GMATHLIB)
+DEPENDENCIES = $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
Added: grass-addons/grass6/raster/r.roughset/description.html
===================================================================
--- grass-addons/grass6/raster/r.roughset/description.html (rev 0)
+++ grass-addons/grass6/raster/r.roughset/description.html 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,68 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.roughset</em> is the implementation of the rough set theory [1,2] in GRASS GIS environment. It requires the following input:
+<br>1. the geographical attributes constituting the information system for the rough set analysis; they have to describe environmental, economic or social issues(<b>attributes</b>=<em>string[,string,...]</em>);<br> 2.the theme in which areas with the issues to be studied are identified (<b>decision</b>=<em>string</em>).
+
+<p>An information system is generated and the functions of version 2 of rough set library (RSL, ver. 2.0) [3] are applied, following one of the decision strategies available with the option<b>strgy</b>=<em>string.</em></P>
+<p><em>The choice of the classification strategy, </em><em><b>clssfy</b></em><em>=string
+</em><em> is necessary to generate a map based on the attributes and the decision rules. Three options are available:</em><em><I>Classify1,Classify2,Classify3</I></em><em> </em><em>each of them is referred to a different algorithm. [3]. </em><br><br>It is possible to get three different output files; two of them are text files, the third is a raster file: </P>
+<p>a. <I>text file</I> structured following the standard of the rough set library, having the name registered in the <b>outTXT</b><I><SPAN STYLE="font-weight: normal">=string</I> without any additional extension. The file can be used as well with RSL implementation out of GIS environment.</P>
+<p><I>NAME: name of the information system </I></P>
+<p><I>ATTRIBUTES: 4 (number of attributes) </I></P>
+<p><I>OBJECTS: 8 (number of objects)</I></P>
+<p><I>0 0 0 0 </I>
+</P>
+<p><I>1 0 1 1</I></P>
+<p><I>1 1 0 0 </I>
+</P>
+<p><I>0 2 0 1 </I>
+</P>
+<p><I>1 2 0 1 </I>
+</P>
+<p><I>1 0 0 0 </I>
+</P>
+<p><I>1 2 0 1 </I>
+</P>
+<p><I>0 0 1 1 </I>
+</P>
+
+<p>The number of attributes is the number of raster given as input at <b>attributes</b>=<em>string[,string,...]
+</em><em> plus the decision raster </em><em>(</em><em><b>decision</b></em><em>=string) </em><em>located in the last column of the information system, by default. </em></P>
+<p>b. <I>text file</I>, having the name defined in <b>outTXT</b><I><SPAN STYLE="font-weight: normal">=string</I>
+and the .<I>out</I>, extension. It contains the decision rules extracted from the geographic information system; decision rules are expressed as:
+ <I>if ... then </I> and they are easy to read and to be interpreted by the user; <br>c. <I>raster, </I>having the name defined in the field <b>outMAP</b><I>=string,
+</I> resulting from the application of the decision rules generated by the geographic information system following one of the classification strategies in <em><b>clssfy</b></em><em>=string.
+</em><em> Rules to apply can derive from an information system generated from the data entered in </em><em><b>attributes</b></em><em>=</em><em>string
+</em><em>and</em><em>
+</em><em><b>decision</b></em><em>=string
+</em><em>or from a text file generated from a previous processing stage. In this case the file structure must be as the one reported in box 1. </em></P>
+
+
+<h2>NOTES</h2>
+<p>The module can process CELL raster, and calculations can be made only with integer (int) variables. This is a constraint imposed by the rough set library. The decision rules, therefore, provide only integer information. </P>
+
+
+<h2>REFERENCE</h2>
+<ol>
+ <li><p>Pawlak Z. Rough Sets, International Journal of
+ Information and Computer Science Vol. 11, No. 5, 1982, pp.
+ 344-356.</P>
+ <li><p>Pawlak Z. Rough Sets, Theoretical Aspects of
+ Reasoning about Data, Kluwer Academic Publishers, 1991.</P>
+ <li><p>Gawrys M., Sienkiewicz J. Rough Set
+ Library - User's manual (ver. 2.0), September 1993</P>
+ <li><p><a href="http://en.wikipedia.org/wiki/Rough_set">http://en.wikipedia.org/wiki/Rough_set</a>
+ - “rough set”</P>
+ <p>
+ </P>
+</ol>
+
+<h2>SEE ALSO</h2>
+<p><em>r.mcda.fuzzy, r.mcda.electre, r.mcda.regime</em></P>
+
+<h2>AUTHORS</h2>
+Antonio Boggia - Gianluca Massei<br>
+Department of Economics and Appraisal - University of Perugia - Italy
+
+<p>
+<i>Last changed: $Date: 2011-11-08 22:56:45 +0100 (mar, 08 nov 2011) $</i>
Added: grass-addons/grass6/raster/r.roughset/localproto.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/localproto.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/localproto.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stddef.h>
+#include <time.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+
+struct input
+{
+ char *name, *mapset;
+ int fd;
+ CELL *buf;
+};
Added: grass-addons/grass6/raster/r.roughset/main.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/main.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/main.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,276 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (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, *dec_txt, *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 = NO;
+ 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->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->answer = "Classify1";
+ 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->answer = "classify";
+ output_map->description = _("Output classified map");
+
+
+ /* options and flags parser */
+ 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 **************/
+
+ /***********************************************************************/
+
+ /* 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. */
+
+ p->fd = G_open_cell_old(p->name, p->mapset);
+
+ if (!p->mapset)
+ G_fatal_error(_("Raster file <%s> not found"), p->name);
+
+ if (p->fd < 0)
+ G_fatal_error(_("Unable to open input map <%s> in mapset <%s>"),
+ p->name, p->mapset);
+
+ if (CELL_TYPE != G_raster_map_type(p->name, p->mapset))
+ G_fatal_error(_("Input map <%s> in mapset <%s> isn't CELL type"),
+ p->name, p->mapset);
+
+ p->buf = G_allocate_c_raster_buf(); /* Allocate an array of CELL based on the number of columns in the current region. Return DCELL * */
+
+ }
+
+
+ /* define the inputmap DECISION type (CELL) */
+ data_type = CELL_TYPE; //
+ /* 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. */
+ 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 * */
+ 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, dec_txt->answer, strgy, cls); /* extract rules from RSL and generate classified vector */
+
+ /* 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;
+ G_message("%d", 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 */
Added: grass-addons/grass6/raster/r.roughset/raccess.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/raccess.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/raccess.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,232 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************
+
+ FUNCTION OF ACCESS TO SYSTEM TABLES
+
+ part of the ROUGH system written by M.Gawrys J. Sienkiewicz
+
+****************************************************************************/
+
+
+#include "rough.h"
+
+setA _table_element = NULL;
+setA _table_end = NULL;
+int _table_row;
+int _table_column;
+int _table_no;
+
+static void (*_current_next) (void);
+
+void next_of_d(void)
+{
+ if (++_table_column >= _table_row)
+ _table_column = 0, _table_row++;
+ _table_element = (_mainsys->matD) + (_mainsys->setAsize * ++_table_no);
+}
+
+void next_of_x(void)
+{
+ _table_element += _mainsys->setAsize;
+ ++_table_no;
+}
+
+void next_of_a(void)
+{
+ ++_table_no;
+ if (++_table_column >= _table_row)
+ {
+ _table_column = 0;
+ if (++_table_row >= _mainsys->objects_num)
+ {
+ CloseSetA(_table_element);
+ _table_end = _table_element;
+ _table_row = 0;
+ return;
+ }
+ }
+ GetDfromA(_table_element, _table_row, _table_column);
+ return;
+}
+
+
+int start_of_tab(int matrix_type)
+{
+ if (matrix_type == MATD)
+ {
+ _table_no = 0;
+ _table_row = 1;
+ _table_column = 0;
+ _table_element = _mainsys->matD;
+ _table_end = _table_element + Dsize(_mainsys);
+ _current_next = next_of_d;
+ }
+ else if (matrix_type == MATX)
+ {
+ START_OF_X;
+ _table_no = 0;
+ _current_next = next_of_x;
+ }
+ else if (matrix_type == MATA)
+ {
+ _table_no = 0;
+ _table_row = 0;
+ _table_column = 0;
+ _table_element = InitEmptySetA();
+ _table_end = _table_element + 1;
+ _current_next = next_of_a;
+ next_of_a();
+ }
+ if (_table_element == NULL)
+ ERROR(3)
+ return (1);
+}
+
+int end_of_tab(void)
+{
+ return (_table_element < _table_end);
+}
+
+void next_of_tab(void)
+{
+ (*_current_next) ();
+}
+
+
+setA GetD(int ob1, int ob2)
+{
+ int pom;
+
+ if (ob2 > ob1)
+ {
+ pom = ob1;
+ ob1 = ob2;
+ ob2 = pom;
+ }
+ return (_mainsys->matD) + ((ob1 - 1) * ob1 / 2 +
+ ob2) * _mainsys->setAsize;
+}
+
+value_type GetA(int ob, int atr)
+{
+ return (_mainsys->matA)[ob * (_mainsys->attributes_num) + atr];
+}
+
+int GetDfromA(setA elem, int obj1, int obj2)
+{
+ int nby = (_mainsys->attributes_num) / _cluster_bits; /* number of full clusters in element */
+ int nbi = _mainsys->attributes_num % _cluster_bits; /* number of bits in the last cluster */
+ int atrnum = _mainsys->attributes_num; /* number of attributes */
+ value_type *A = _mainsys->matA;
+ cluster_type val;
+ int cluster, bit;
+
+ for (cluster = 0; cluster < nby; cluster++)
+ {
+ val = 0;
+ for (bit = 0; bit < _cluster_bits; bit++)
+ if (A[obj1 * atrnum + _cluster_bits * cluster + bit] !=
+ A[obj2 * atrnum + _cluster_bits * cluster + bit] &&
+ A[obj1 * atrnum + _cluster_bits * cluster + bit] !=
+ (value_type) - 1 &&
+ A[obj2 * atrnum + _cluster_bits * cluster + bit] !=
+ (value_type) - 1)
+ val = val | _mask[bit];
+ elem[cluster] = val;
+ }
+ if (nbi)
+ {
+ val = 0;
+ for (bit = 0; bit < nbi; bit++)
+ if (A[obj1 * atrnum + _cluster_bits * cluster + bit] !=
+ A[obj2 * atrnum + _cluster_bits * cluster + bit] &&
+ A[obj1 * atrnum + _cluster_bits * cluster + bit] !=
+ (value_type) - 1 &&
+ A[obj2 * atrnum + _cluster_bits * cluster + bit] !=
+ (value_type) - 1)
+ val = val | _mask[bit];
+ elem[cluster] = val;
+ }
+ return 1;
+}
+
+int CompareA(int ob1, int ob2, setA P)
+{
+ int attr;
+
+ for (attr = 0; attr < _mainsys->attributes_num; attr++)
+ if (ContSetA(P, attr))
+ if ((_mainsys->matA)[ob1 * (_mainsys->attributes_num) + attr] ==
+ (value_type) - 1 ||
+ (_mainsys->matA)[ob2 * (_mainsys->attributes_num) + attr] ==
+ (value_type) - 1)
+ continue;
+ else if ((_mainsys->matA)[ob1 * (_mainsys->attributes_num) +
+ attr] !=
+ (_mainsys->matA)[ob2 * (_mainsys->attributes_num) +
+ attr])
+ return 0;
+ return 1;
+}
+
+int CompareD(int ob1, int ob2, setA P)
+{
+ int pom;
+ int cluster;
+ setA elem;
+
+ if (ob2 > ob1)
+ {
+ pom = ob1;
+ ob1 = ob2;
+ ob2 = pom;
+ }
+ elem =
+ (_mainsys->matD) + ((ob1 - 1) * ob1 / 2 + ob2) * (_mainsys->setAsize);
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ if (elem[cluster] & P[cluster])
+ return 0;
+ return 1;
+}
+
+int SingCompA(int ob1, int ob2, int atr)
+{
+ return ((_mainsys->matA)[ob1 * (_mainsys->attributes_num) + atr] ==
+ (_mainsys->matA)[ob2 * (_mainsys->attributes_num) + atr] ||
+ (_mainsys->matA)[ob1 * (_mainsys->attributes_num) + atr] ==
+ (value_type) - 1 ||
+ (_mainsys->matA)[ob2 * (_mainsys->attributes_num) + atr] ==
+ (value_type) - 1);
+}
+
+int SingCompD(int ob1, int ob2, int atr)
+{
+ int pom;
+
+ if (ob2 > ob1)
+ {
+ pom = ob1;
+ ob1 = ob2;
+ ob2 = pom;
+ }
+ return !((_mainsys->matD)[((ob1 - 1) * ob1 / 2 + ob2)
+ * (_mainsys->setAsize) +
+ atr / _cluster_bits] & _mask[atr %
+ _cluster_bits]);
+}
Added: grass-addons/grass6/raster/r.roughset/raccess.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/raccess.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/raccess.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,77 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** FUNCTIONS OF ACCESS TO SYSTEM TABLES ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J. Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+#define START_OF_D _table_element=_mainsys->matD,_table_end=_table_element+Dsize(_mainsys)
+#define START_OF_X _table_element=_mainsys->matX,_table_end=_table_element+_mainsys->matXsize
+#define START_OF_MAT(set,num) _table_element=(set),_table_end=_table_element+(num)*_mainsys->setAsize
+#define END_OF_MAT (_table_element<_table_end)
+#define NEXT_OF_MAT _table_element+=_mainsys->setAsize
+#define ELEM_OF_MAT _table_element
+#define ElemOfRule(rules,num,attr) (rules)[(num)*(_mainsys->attributes_num)+(attr)]
+
+extern setA _table_element;
+extern setA _table_end;
+extern int _table_row;
+extern int _table_column;
+extern int _table_no;
+
+
+int start_of_tab(int matrix_type);
+void next_of_tab(void);
+int end_of_tab(void);
+
+
+int CompareA(int ob1, int ob2, setA P);
+int CompareD(int ob1, int ob2, setA P);
+
+ /* compares two objects, returns 1 if all attributes from */
+ /* the set P are identical, otherwise returns 0 */
+
+int SingCompA(int ob1, int ob2, int atr);
+int SingCompD(int ob1, int ob2, int atr);
+
+ /* compares two objects on the sigle attribute atr */
+ /* returns 1 if identical, otherwise returns 0 */
+
+setA GetD(int ob1, int ob2);
+
+ /* returns a single element of matrix D, if this set */
+ /* is going to be changed, it has to be first copied */
+
+value_type GetA(int obj, int atr);
+
+ /* returns value of attribute atr of object obj */
+ /* from matrix A */
+
+int GetDfromA(setA elem, int ob1, int ob2);
+
+ /* generate a single element of matrix D into elem */
Added: grass-addons/grass6/raster/r.roughset/rbasic.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rbasic.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rbasic.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,631 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** BASIC QUERIES FOR ACTIVE SYSTEM ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include "rough.h"
+
+int LowAppr(setO lowappr, setO X, setA P, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return LowApprA(lowappr, X, P);
+ case MATD:
+ return LowApprD(lowappr, X, P);
+ default:
+ ERROR(8)
+ }
+}
+
+int LowApprA(setO lowappr, setO X, setA P)
+{
+ setO notX;
+ int ob1, ob2, disc;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ notX = InitEmptySetO();
+ ClearSetO(lowappr);
+ NotSetO(notX, X);
+ for (ob1 = 0; ob1 < _mainsys->objects_num; ob1++)
+ if (ContSetO(X, ob1))
+ {
+ disc = 1;
+ for (ob2 = 0; ob2 < _mainsys->objects_num; ob2++)
+ if (ContSetO(notX, ob2))
+ if (CompareA(ob1, ob2, P))
+ {
+ disc = 0;
+ break;
+ }
+ if (disc)
+ AddSetO(lowappr, ob1);
+ }
+ CloseSetO(notX);
+ return 0;
+}
+
+int LowApprD(setO lowappr, setO X, setA P)
+{
+ setO notX;
+ int ob1, ob2, disc;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ notX = InitEmptySetO();
+ ClearSetO(lowappr);
+ NotSetO(notX, X);
+ for (ob1 = 0; ob1 < _mainsys->objects_num; ob1++)
+ if (ContSetO(X, ob1))
+ {
+ disc = 1;
+ for (ob2 = 0; ob2 < _mainsys->objects_num; ob2++)
+ if (ContSetO(notX, ob2))
+ if (CompareD(ob1, ob2, P))
+ {
+ disc = 0;
+ break;
+ }
+ if (disc)
+ AddSetO(lowappr, ob1);
+ }
+ CloseSetO(notX);
+ return 0;
+}
+
+int UppAppr(setO uppappr, setO X, setA P, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return UppApprA(uppappr, X, P);
+ case MATD:
+ return UppApprD(uppappr, X, P);
+ default:
+ ERROR(8)
+ }
+}
+
+int UppApprA(setO uppappr, setO X, setA P)
+{
+ setO notX;
+ int ob1, ob2;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ CopySetO(uppappr, X);
+ notX = InitEmptySetO();
+ NotSetO(notX, X);
+ for (ob1 = 0; ob1 < _mainsys->objects_num; ob1++)
+ if (ContSetO(X, ob1))
+ for (ob2 = 0; ob2 < _mainsys->objects_num; ob2++)
+ if (ContSetO(notX, ob2))
+ if (CompareA(ob1, ob2, P))
+ AddSetO(uppappr, ob2);
+ CloseSetO(notX);
+ return 0;
+}
+
+int UppApprD(setO uppappr, setO X, setA P)
+{
+ setO notX;
+ int ob1, ob2;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ CopySetO(uppappr, X);
+ notX = InitEmptySetO();
+ NotSetO(notX, X);
+ for (ob1 = 0; ob1 < _mainsys->objects_num; ob1++)
+ if (ContSetO(X, ob1))
+ for (ob2 = 0; ob2 < _mainsys->objects_num; ob2++)
+ if (ContSetO(notX, ob2))
+ if (CompareD(ob1, ob2, P))
+ AddSetO(uppappr, ob2);
+ CloseSetO(notX);
+ return 0;
+}
+
+int Bound(setO bound, setO X, setA P, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return BoundA(bound, X, P);
+ case MATD:
+ return BoundD(bound, X, P);
+ default:
+ ERROR(8)
+ }
+}
+
+int BoundA(setO bound, setO X, setA P)
+{
+ setO lower, upper;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ lower = InitEmptySetO();
+ upper = InitEmptySetO();
+ LowApprA(lower, X, P);
+ UppApprA(upper, X, P);
+ DifSetO(bound, upper, lower);
+ CloseSetO(lower);
+ CloseSetO(upper);
+ return 0;
+}
+
+int BoundD(setO bound, setO X, setA P)
+{
+ setO lower, upper;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ lower = InitEmptySetO();
+ upper = InitEmptySetO();
+ LowApprD(lower, X, P);
+ UppApprD(upper, X, P);
+ DifSetO(bound, upper, lower);
+ CloseSetO(lower);
+ CloseSetO(upper);
+ return 0;
+}
+
+float AccurCoef(setO X, setA P, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return AccurCoefA(X, P);
+ case MATD:
+ return AccurCoefD(X, P);
+ default:
+ ERROR(8)
+ }
+}
+
+float AccurCoefA(setO X, setA P)
+{
+ setO lower, upper;
+ float coef;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ lower = InitEmptySetO();
+ upper = InitEmptySetO();
+ LowApprA(lower, X, P);
+ UppApprA(upper, X, P);
+ coef = (float)CardSetO(lower) / (float)CardSetO(upper);
+ CloseSetO(lower);
+ CloseSetO(upper);
+ return coef;
+}
+
+float AccurCoefD(setO X, setA P)
+{
+ setO lower, upper;
+ float coef;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ lower = InitEmptySetO();
+ upper = InitEmptySetO();
+ LowApprD(lower, X, P);
+ UppApprD(upper, X, P);
+ coef = (float)CardSetO(lower) / (float)CardSetO(upper);
+ CloseSetO(lower);
+ CloseSetO(upper);
+ return coef;
+}
+
+float ClassCoef(setO X, setA P, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return ClassCoefA(X, P);
+ case MATD:
+ return ClassCoefD(X, P);
+ default:
+ ERROR(8)
+ }
+}
+
+float ClassCoefA(setO X, setA P)
+{
+ float coef;
+ setO notX, pos;
+ int ob1, ob2;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ notX = InitEmptySetO();
+ pos = InitFullSetO();
+ NotSetO(notX, X);
+ for (ob1 = 0; ob1 < _mainsys->objects_num; ob1++)
+ for (ob2 = 0; ob2 < _mainsys->objects_num; ob2++)
+ if (ContSetO(X, ob1) && ContSetO(notX, ob2) &&
+ CompareA(ob1, ob2, P))
+ {
+ DelSetO(pos, ob1);
+ DelSetO(pos, ob2);
+ }
+ coef = (float)CardSetO(pos) / _mainsys->objects_num;
+ CloseSetO(notX);
+ CloseSetO(pos);
+ return coef;
+}
+
+float ClassCoefD(setO X, setA P)
+{
+ float coef;
+ setO notX, pos;
+ int ob1, ob2;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ notX = InitEmptySetO();
+ pos = InitFullSetO();
+ NotSetO(notX, X);
+ for (ob1 = 0; ob1 < _mainsys->objects_num; ob1++)
+ for (ob2 = 0; ob2 < _mainsys->objects_num; ob2++)
+ if (ContSetO(X, ob1) && ContSetO(notX, ob2) &&
+ CompareD(ob1, ob2, P))
+ {
+ DelSetO(pos, ob1);
+ DelSetO(pos, ob2);
+ }
+ CloseSetO(notX);
+ coef = (float)CardSetO(pos) / _mainsys->objects_num;
+ CloseSetO(pos);
+ return coef;
+}
+
+int Pos(setO pos, setA P, setA Q, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return PosA(pos, P, Q);
+ case MATD:
+ return PosD(pos, P, Q);
+ default:
+ ERROR(8)
+ }
+}
+
+int PosA(setO pos, setA P, setA Q)
+{
+ int i, j;
+ setA redQ;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ if (IsEmptySetA(P))
+ {
+ ClearSetO(pos);
+ return 0;
+ }
+ redQ = InitEmptySetA();
+ DifSetA(redQ, Q, P);
+ FillSetO(pos);
+ if (IsEmptySetA(redQ))
+ return 0;
+ for (i = _mainsys->objects_num - 1; i > 0; i--)
+ for (j = i - 1; j >= 0; j--)
+ if (!CompareA(i, j, redQ) && CompareA(i, j, P))
+ {
+ DelSetO(pos, i);
+ DelSetO(pos, j);
+ }
+ return 0;
+}
+
+int PosD(setO pos, setA P, setA Q)
+{
+ int i, j;
+ setA elem, redQ;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ if (IsEmptySetA(P))
+ {
+ ClearSetO(pos);
+ return 0;
+ }
+ redQ = InitEmptySetA();
+ DifSetA(redQ, Q, P);
+ FillSetO(pos);
+ if (IsEmptySetA(redQ))
+ return 0;
+ for (i = _mainsys->objects_num - 1; i > 0; i--)
+ for (j = i - 1; j >= 0; j--)
+ {
+ elem = GetD(i, j);
+ if (InterSetA(elem, redQ) && !InterSetA(elem, P))
+ {
+ DelSetO(pos, i);
+ DelSetO(pos, j);
+ }
+ }
+ CloseSetA(redQ);
+ return 0;
+}
+
+float DependCoef(setA P, setA Q, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return DependCoefA(P, Q);
+ case MATD:
+ return DependCoefD(P, Q);
+ default:
+ ERROR(8)
+ }
+}
+
+float DependCoefA(setA P, setA Q)
+{
+ setO pos;
+ float coef;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ pos = InitEmptySetO();
+ PosA(pos, P, Q);
+ coef = (float)CardSetO(pos) / (float)_mainsys->objects_num;
+ CloseSetO(pos);
+ return coef;
+}
+
+float DependCoefD(setA P, setA Q)
+{
+ setO pos;
+ float coef;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ pos = InitEmptySetO();
+ PosD(pos, P, Q);
+ coef = (float)CardSetO(pos) / (float)_mainsys->objects_num;
+ CloseSetO(pos);
+ return coef;
+}
+
+float SignifCoef(int attr, setA P, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return SignifCoefA(attr, P);
+ case MATD:
+ return SignifCoefD(attr, P);
+ default:
+ ERROR(8)
+ }
+}
+
+float SignifCoefA(int attr, setA P)
+{
+ setO pos1, pos2;
+ setA Pprim;
+ int i, j;
+ float coef;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ if (IsEmptySetA(P) || !ContSetA(P, attr))
+ return 0;
+ pos1 = InitFullSetO();
+ pos2 = InitFullSetO();
+ Pprim = InitEmptySetA();
+ CopySetA(Pprim, P);
+ DelSetA(Pprim, attr);
+ for (i = _mainsys->objects_num - 1; i > 0; i--)
+ for (j = i - 1; j >= 0; j--)
+ if (CompareA(i, j, P))
+ {
+ DelSetO(pos1, i);
+ DelSetO(pos2, i);
+ DelSetO(pos1, j);
+ DelSetO(pos2, j);
+ continue;
+ }
+ else if (CompareA(i, j, Pprim))
+ {
+ DelSetO(pos2, i);
+ DelSetO(pos2, j);
+ }
+ DifSetO(pos1, pos1, pos2);
+ coef = (float)CardSetO(pos1) / (float)_mainsys->objects_num;
+ CloseSetO(pos1);
+ CloseSetO(pos2);
+ CloseSetA(Pprim);
+ return coef;
+}
+
+float SignifCoefD(int attr, setA P)
+{
+ setO pos1, pos2;
+ setA Pprim, elem;
+ int i, j;
+ float coef;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ if (IsEmptySetA(P) || !ContSetA(P, attr))
+ return 0;
+ pos1 = InitFullSetO();
+ pos2 = InitFullSetO();
+ Pprim = InitEmptySetA();
+ CopySetA(Pprim, P);
+ DelSetA(Pprim, attr);
+ for (i = _mainsys->objects_num - 1; i > 0; i--)
+ for (j = i - 1; j >= 0; j--)
+ {
+ elem = GetD(i, j);
+ if (!InterSetA(elem, P))
+ {
+ DelSetO(pos1, i);
+ DelSetO(pos2, i);
+ DelSetO(pos1, j);
+ DelSetO(pos2, j);
+ continue;
+ }
+ if (!InterSetA(elem, Pprim))
+ {
+ DelSetO(pos2, i);
+ DelSetO(pos2, j);
+ }
+ }
+ DifSetO(pos1, pos1, pos2);
+ coef = (float)CardSetO(pos1) / (float)_mainsys->objects_num;
+ CloseSetO(pos1);
+ CloseSetO(pos2);
+ CloseSetA(Pprim);
+ return coef;
+}
+
+float SignifRelCoef(int attr, setA P, setA Q, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return SignifRelCoefA(attr, P, Q);
+ case MATD:
+ return SignifRelCoefD(attr, P, Q);
+ default:
+ ERROR(8)
+ }
+}
+
+float SignifRelCoefA(int attr, setA P, setA Q)
+{
+ setO pos1, pos2;
+ setA Pprim, redQ;
+ int i, j;
+ float coef;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ Pprim = InitEmptySetA();
+ redQ = InitEmptySetA();
+ CopySetA(Pprim, P);
+ DelSetA(Pprim, attr);
+ DifSetA(redQ, Q, Pprim);
+ if (IsEmptySetA(P) || !ContSetA(P, attr))
+ return 0;
+ if (IsEmptySetA(redQ))
+ return 1;
+ pos1 = InitFullSetO();
+ pos2 = InitFullSetO();
+ for (i = _mainsys->objects_num - 1; i > 0; i--)
+ for (j = i - 1; j >= 0; j--)
+ if (CompareA(i, j, redQ))
+ continue;
+ else if (CompareA(i, j, P))
+ {
+ DelSetO(pos1, i);
+ DelSetO(pos2, i);
+ DelSetO(pos1, j);
+ DelSetO(pos2, j);
+ continue;
+ }
+ else if (CompareA(i, j, Pprim))
+ {
+ DelSetO(pos2, i);
+ DelSetO(pos2, j);
+ }
+ DifSetO(pos1, pos1, pos2);
+ coef = (float)CardSetO(pos1) / (float)_mainsys->objects_num;
+ CloseSetO(pos1);
+ CloseSetO(pos2);
+ CloseSetA(Pprim);
+ CloseSetA(redQ);
+ return coef;
+}
+
+float SignifRelCoefD(int attr, setA P, setA Q)
+{
+ setO pos1, pos2;
+ setA Pprim, redQ, elem;
+ int i, j;
+ float coef;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ Pprim = InitEmptySetA();
+ redQ = InitEmptySetA();
+ CopySetA(Pprim, P);
+ DelSetA(Pprim, attr);
+ DifSetA(redQ, Q, Pprim);
+ if (IsEmptySetA(P) || !ContSetA(P, attr))
+ return 0;
+ if (IsEmptySetA(redQ))
+ return 1;
+ pos1 = InitFullSetO();
+ pos2 = InitFullSetO();
+ for (i = _mainsys->objects_num - 1; i > 0; i--)
+ for (j = i - 1; j >= 0; j--)
+ {
+ elem = GetD(i, j);
+ if (!InterSetA(elem, redQ))
+ continue;
+ if (!InterSetA(elem, P))
+ {
+ DelSetO(pos1, i);
+ DelSetO(pos2, i);
+ DelSetO(pos1, j);
+ DelSetO(pos2, j);
+ continue;
+ }
+ if (!InterSetA(elem, Pprim))
+ {
+ DelSetO(pos2, i);
+ DelSetO(pos2, j);
+ }
+ }
+ DifSetO(pos1, pos1, pos2);
+ coef = (float)CardSetO(pos1) / (float)_mainsys->objects_num;
+ CloseSetO(pos1);
+ CloseSetO(pos2);
+ CloseSetA(Pprim);
+ CloseSetA(redQ);
+ return coef;
+}
+
+
+int CardCoef(setA P, int matrix_type)
+{
+ int res = 0;
+
+ for (start_of_tab(matrix_type); end_of_tab(); next_of_tab())
+ if (InterSetA(P, _table_element))
+ res++;
+ if (_rerror > 0)
+ return -_rerror;
+ return res;
+}
Added: grass-addons/grass6/raster/r.roughset/rbasic.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rbasic.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rbasic.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,99 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** BASIC QUERRIES FOR SYSTEM ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+/* If a function from this section value a set, this set should be */
+/* first initialized and then given to the function as a first */
+/* argument. Functions from this group does not work on matrix X. */
+
+int LowAppr(setO appr, setO X, setA P, int matrix_type);
+int LowApprA(setO appr, setO X, setA P);
+int LowApprD(setO appr, setO X, setA P);
+
+ /* P-lower approximation of X */
+
+int UppAppr(setO appr, setO X, setA P, int matrix_type);
+int UppApprA(setO appr, setO X, setA P);
+int UppApprD(setO appr, setO X, setA P);
+
+ /* P-upper appriximation of X */
+
+int Bound(setO bound, setO X, setA P, int matrix_type);
+int BoundA(setO bound, setO X, setA P);
+int BoundD(setO bound, setO X, setA P);
+
+ /* P-boundary of X */
+
+float AccurCoef(setO X, setA P, int matrix_type);
+float AccurCoefA(setO X, setA P);
+float AccurCoefD(setO X, setA P);
+
+ /* Accuracy coefficient of X with respect to P */
+
+float ClassCoef(setO X, setA P, int matrix_type);
+float ClassCoefA(setO X, setA P);
+float ClassCoefD(setO X, setA P);
+
+ /* quality of classification (X,not X) */
+ /* with respect to P */
+
+int Pos(setO pos, setA P, setA Q, int matrix_type);
+int PosA(setO pos, setA P, setA Q);
+int PosD(setO pos, setA P, setA Q);
+
+ /* P-positive region of Q */
+
+float DependCoef(setA P, setA Q, int matrix_type);
+float DependCoefA(setA P, setA Q);
+float DependCoefD(setA P, setA Q);
+
+ /* degree of dependency Q from P */
+
+float SignifCoef(int attr, setA P, int matrix_type);
+float SignifCoefA(int attr, setA P);
+float SignifCoefD(int attr, setA P);
+
+ /* significance of attribute attr */
+ /* in the set P */
+
+float SignifRelCoef(int attr, setA P, setA Q, int matrix_type);
+float SignifRelCoefA(int attr, setA P, setA Q);
+float SignifRelCoefD(int attr, setA P, setA Q);
+
+ /* significance of attribute attr */
+ /* in the set P, relatively to Q */
+
+
+int CardCoef(setA P, int matrix_type);
+
+ /* returns number of matrix fields */
+ /* covered by P (MATA is treated as MATD) */
+ /* but elements are evaluated "on-line" */
Added: grass-addons/grass6/raster/r.roughset/rclass.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rclass.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rclass.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,248 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+/*** ( CLASSIFICATION ) ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include <stdlib.h>
+#include "rough.h"
+
+
+int DecisionEQ(value_type * rule1, value_type * rule2, setA Q)
+{
+ int i;
+
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (ContSetA(Q, i))
+ if (rule1[i] != rule2[i])
+ return 0;
+ return 1;
+}
+
+
+int StrengthOfRule(value_type * rule)
+{
+ int obj, attr, find, result = 0, size = _mainsys->attributes_num;
+
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ {
+ find = 1;
+ for (attr = 0; attr < size; attr++)
+ if (rule[attr] != GetA(obj, attr))
+ if (rule[attr] != MINUS && GetA(obj, attr) != MINUS)
+ {
+ find = 0;
+ break;
+ }
+ if (find)
+ result++;
+ }
+ return result;
+}
+
+int *StrengthOfRules(value_type * rules, int N)
+{
+ int *result;
+ int i, size = _mainsys->attributes_num;
+
+ if (N <= 0)
+ return NULL;
+ result = (int *)calloc(N, sizeof(int));
+ for (i = 0; i < N; i++)
+ result[i] = StrengthOfRule(rules + i * size);
+ return result;
+}
+
+
+int ObjectsForRule(setO set, value_type * rule)
+{
+ int i, j, find, size = _mainsys->attributes_num;
+
+ for (i = 0; i < _mainsys->objects_num; i++)
+ {
+ find = 1;
+ for (j = 0; j < size; j++)
+ if (rule[j] != GetA(i, j))
+ if (rule[j] != MINUS && GetA(i, j) != MINUS)
+ {
+ find = 0;
+ break;
+ }
+ if (find)
+ AddSetO(set, i);
+ }
+ return CardSetO(set);
+}
+
+
+int CompareToRule(value_type * sample, value_type * rule, setA P)
+{
+ int j, result = 0, size = _mainsys->attributes_num;
+
+ for (j = 0; j < size; j++)
+ {
+ if (!ContSetA(P, j))
+ continue;
+ if (rule[j] != sample[j])
+ if (rule[j] != MINUS && sample[j] != MINUS)
+ result++;
+ }
+ return result;
+}
+
+
+int *CompareToRules(value_type * sample, value_type * rules, int N, setA P)
+{
+ int *result;
+ int i, size = _mainsys->attributes_num;
+
+ if (N <= 0)
+ return NULL;
+ result = (int *)calloc(N, sizeof(int));
+ for (i = 0; i < N; i++)
+ result[i] = CompareToRule(sample, rules + i * size, P);
+ return result;
+}
+
+
+int Classify1(value_type * sample, value_type * rules, int N, setA P, setA Q)
+{
+ int done = 0,
+ result = -1,
+ count = 0,
+ *diff, minDiff = _mainsys->attributes_num,
+ i, j = 0, max = 0, size = _mainsys->attributes_num;
+ diff = CompareToRules(sample, rules, N, P);
+ for (i = 0; i < N; i++)
+ if (diff[i] < minDiff)
+ minDiff = diff[i];
+ while (!done) /* while there is an unprocessed rule */
+ {
+ while (diff[j] != minDiff)
+ j++; /* to find the rule with new decision */
+ done = 1;
+ diff[j]++;
+ count = 1;
+ for (i = j; i < N; i++)
+ if (diff[i] == minDiff)
+ if (DecisionEQ(rules + i * size, rules + j * size, Q))
+ {
+ diff[j]++; /* to protect from next use */
+ count++;
+ }
+ else
+ done = 0;
+ if (count > max)
+ max = count, result = j;
+ }
+ free(diff);
+ return result;
+}
+
+
+int Classify2(value_type * sample, value_type * rules, int N, setA P, setA Q)
+{
+ int i,
+ *diff,
+ first = 0, maxindex,
+ size = _mainsys->attributes_num,
+ minDiff = _mainsys->attributes_num, done = 0;
+ setO cover, maxcover, rulecover;
+
+ cover = InitEmptySetO();
+ maxcover = InitEmptySetO();
+ rulecover = InitEmptySetO();
+ diff = CompareToRules(sample, rules, N, P);
+ for (i = 0; i < N; i++)
+ if (diff[i] < minDiff)
+ minDiff = diff[i];
+ while (!done) /* while there is an unprocessed rule */
+ {
+ while (diff[first] != minDiff)
+ first++; /* to find the rule with new decision */
+ done = 1;
+ diff[first]++; /* to protect from next use */
+ ObjectsForRule(cover, rules + first * size);
+ for (i = first; i < N; i++)
+ if (diff[i] == minDiff)
+ if (DecisionEQ(rules + first * size, rules + i * size, Q))
+ {
+ diff[i]++;
+ ObjectsForRule(rulecover, rules + i * size);
+ OrSetO(cover, cover, rulecover);
+ }
+ else
+ done = 0;
+ if (CardSetO(maxcover) < CardSetO(cover))
+ {
+ CopySetO(maxcover, cover);
+ maxindex = first;
+ }
+ }
+ free(diff);
+ CloseSetO(cover);
+ CloseSetO(maxcover);
+ CloseSetO(rulecover);
+ return maxindex;
+}
+
+int Classify3(value_type * sample, value_type * rules, int N,
+ int *strength, setA P, setA Q)
+{
+ int i,
+ *diff,
+ first = 0, maxindex,
+ size = _mainsys->attributes_num,
+ minDiff = _mainsys->attributes_num, done = 0;
+ int cover, maxcover = 0;
+
+ diff = CompareToRules(sample, rules, N, P);
+ for (i = 0; i < N; i++)
+ if (diff[i] < minDiff)
+ minDiff = diff[i];
+ while (!done) /* while there is an unprocessed rule */
+ {
+ while (diff[first] != minDiff)
+ first++; /* to find the rule with new decision */
+ done = 1;
+ diff[first]++; /* to protect from next use */
+ cover = strength[first];
+ for (i = first; i < N; i++)
+ if (diff[i] == minDiff)
+ if (DecisionEQ(rules + first * size, rules + i * size, Q))
+ {
+ diff[i]++;
+ cover += strength[i];
+ }
+ else
+ done = 0;
+ if (maxcover < cover)
+ {
+ maxcover = cover;
+ maxindex = first;
+ }
+ }
+ free(diff);
+ return maxindex;
+}
Added: grass-addons/grass6/raster/r.roughset/rclass.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rclass.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rclass.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,82 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+
+/*** ( CLASSIFICATION ) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+int DecisionEQ(value_type * rule1, value_type * rule2, setA Q);
+
+ /* compares decisions of two rules */
+ /* returns 1 if equal */
+
+int StrengthOfRule(value_type * rule);
+
+ /* return a number of objects covered by rule (strength) */
+
+int *StrengthOfRules(value_type * rules, int N);
+
+ /* creates a table of rules strengths */
+ /* allocates memory, size = N * sizeof(int) */
+
+int ObjectsForRule(setO set, value_type * rule);
+
+ /* finds a set of objects covered by rule */
+
+int CompareToRule(value_type * sample, value_type * rule, setA P);
+
+ /* compares a sample with rule on a set of attributes */
+ /* returns a number of dissimilar attributes (distance) */
+
+int *CompareToRules(value_type * sample, value_type * rules, int N, setA P);
+
+ /* compares a sample with rules on a set of attributes */
+ /* creates a table of distances (number of dissimilar attributes) */
+ /* allocates memory, size = N * sizeof(int) */
+
+int Classify1(value_type * sample, value_type * rules, int N, setA P, setA Q);
+
+ /* chooses the best rule to cover sample, strategy no. 1 */
+ /* finds the most frequent decision among rules with minimal */
+ /* distances from a given sample, returns index of a proper rule */
+
+int Classify2(value_type * sample, value_type * rules, int N, setA P, setA Q);
+
+ /* chooses the best rule to cover sample, strategy no. 2 */
+ /* finds a decision that covers the largerst number of objects */
+ /* among rules with minimal distances from a given sample */
+ /* returns index of a proper rule */
+
+int Classify3(value_type * sample, value_type * rules, int N,
+ int *strength, setA P, setA Q);
+ /* chooses the best rule to cover sample, strategy no. 3 */
+ /* finds a decision with the largest total strength among */
+ /* rules with minimal distances from a given sample */
+ /* returns index of a proper rule */
Added: grass-addons/grass6/raster/r.roughset/rcore.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rcore.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rcore.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,364 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+/*** ( FINDING CORES AND CHECKING REDUCTS ) ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include "rough.h"
+#include <stdlib.h>
+
+
+int Core(setA core, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return CoreA(core);
+ case MATD:
+ return CoreDX(core, matrix_type);
+ case MATX:
+ return CoreDX(core, matrix_type);
+ default:
+ ERROR(8)
+ }
+}
+
+int CoreA(setA core)
+{
+ int ob1, ob2, attr, no1, pattr;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5)
+ ClearSetA(core);
+ for (ob1 = _mainsys->objects_num - 1; ob1 > 0; ob1--)
+ for (ob2 = ob1 - 1; ob2 >= 0; ob2--)
+ {
+ no1 = 0;
+ for (attr = _mainsys->attributes_num - 1; attr >= 0; attr--)
+ if (!SingCompA(ob1, ob2, attr))
+ {
+ no1++;
+ if (no1 > 1)
+ break;
+ else
+ pattr = attr;
+ }
+ if (no1 == 1)
+ AddSetA(core, pattr);
+ }
+ return 0;
+}
+
+int CoreDX(setA core, int matrix_type)
+{
+ int cluster, bit, flag, no1, pcluster;
+
+ ClearSetA(core);
+ if (matrix_type == MATD)
+ {
+ if (_mainsys->matD == NULL)
+ ERROR(6)
+ START_OF_D;
+ }
+ else
+ {
+ if (_mainsys->matX == NULL)
+ ERROR(7)
+ START_OF_X;
+ }
+ for (; END_OF_MAT; NEXT_OF_MAT)
+ {
+ flag = 0;
+ no1 = 0;
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ {
+ for (bit = 0; bit < _cluster_bits; bit++)
+ if (_mask[bit] & ELEM_OF_MAT[cluster])
+ {
+ no1++;
+ if (no1 > 1)
+ {
+ flag = 1;
+ break;
+ }
+ else
+ pcluster = cluster;
+ }
+ if (flag)
+ break;
+ }
+ if (no1 == 1)
+ core[pcluster] |= ELEM_OF_MAT[pcluster];
+ }
+ return 0;
+}
+
+int CoreRel(setA core, setA P, setA Q, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return CoreRelA(core, P, Q);
+ case MATD:
+ return CoreRelD(core, P, Q);
+ default:
+ ERROR(8)
+ }
+}
+
+int CoreRelA(setA core, setA P, setA Q)
+{
+ int ob1, ob2, attr, no1, pattr;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5)
+ ClearSetA(core);
+ for (ob1 = _mainsys->objects_num - 1; ob1 > 0; ob1--)
+ for (ob2 = ob1 - 1; ob2 >= 0; ob2--)
+ if (!CompareA(ob1, ob2, Q))
+ {
+ no1 = 0;
+ for (attr = _mainsys->attributes_num - 1; attr >= 0; attr--)
+ if (ContSetA(P, attr))
+ if (!SingCompA(ob1, ob2, attr))
+ {
+ no1++;
+ if (no1 > 1)
+ break;
+ else
+ pattr = attr;
+ }
+ if (no1 == 1)
+ AddSetA(core, pattr);
+ }
+ return 0;
+}
+
+int CoreRelD(setA core, setA P, setA Q)
+{
+ int cluster, bit, flag, no1, pcluster;
+ setA elem;
+
+ if (_mainsys->matD == NULL)
+ ERROR(6)
+ ClearSetA(core);
+ elem = InitEmptySetA();
+ for (START_OF_D; END_OF_MAT; NEXT_OF_MAT)
+ {
+ AndSetA(elem, ELEM_OF_MAT, P);
+ flag = 0;
+ no1 = 0;
+ if (!InterSetA(Q, ELEM_OF_MAT))
+ continue;
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ {
+ for (bit = 0; bit < _cluster_bits; bit++)
+ if (_mask[bit] & elem[cluster])
+ {
+ no1++;
+ if (no1 > 1)
+ {
+ flag = 1;
+ break;
+ }
+ else
+ pcluster = cluster;
+ }
+ if (flag)
+ break;
+ }
+ if (no1 == 1)
+ core[pcluster] |= elem[pcluster];
+ }
+ CloseSetA(elem);
+ return 0;
+}
+
+int IsOrtho(setA red, setA over, int matrix_type)
+{
+ setA and, redprim;
+
+ and = InitEmptySetA();
+ redprim = InitEmptySetA();
+ for (start_of_tab(matrix_type); end_of_tab(); next_of_tab())
+ {
+ AndSetA(and, _table_element, red);
+ if (CardSetA(and) == 1)
+ OrSetA(redprim, redprim, and);
+ }
+ CloseSetA(and);
+ DifSetA(over, red, redprim);
+ CloseSetA(redprim);
+ return IsEmptySetA(over);
+}
+
+int IsOrthoRel(setA red, setA over, setA P, setA Q, int matrix_type)
+{
+ setA and, redprim;
+
+ and = InitEmptySetA();
+ redprim = InitEmptySetA();
+ for (start_of_tab(matrix_type); end_of_tab(); next_of_tab())
+ if (InterSetA(_table_element, Q))
+ {
+ AndSetA(and, _table_element, red);
+ AndSetA(and, and, P);
+ if (CardSetA(and) == 1)
+ OrSetA(redprim, redprim, and);
+ }
+ CloseSetA(and);
+ DifSetA(over, red, redprim);
+ CloseSetA(redprim);
+ return IsEmptySetA(over);
+}
+
+int IsCover(setA red, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return IsCoverA(red);
+ case MATD:
+ return IsCoverDX(red, matrix_type);
+ case MATX:
+ return IsCoverDX(red, matrix_type);
+ default:
+ ERROR(8)
+ }
+}
+
+int IsCoverA(setA red)
+{
+ int ob1, ob2;
+ setA P = InitFullSetA();
+
+ DifSetA(P, P, red);
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ for (ob1 = _mainsys->objects_num - 1; ob1 > 0; ob1--)
+ for (ob2 = ob1 - 1; ob2 >= 0; ob2--)
+ if (CompareA(ob1, ob2, red))
+ if (!CompareA(ob1, ob2, P))
+ {
+ CloseSetA(P);
+ return 0;
+ }
+ CloseSetA(P);
+ return 1;
+}
+
+int IsCoverDX(setA red, int matrix_type)
+{
+ if (matrix_type == MATD)
+ {
+ if (_mainsys->matD == NULL)
+ ERROR(6)
+ START_OF_D;
+ }
+ else
+ {
+ if (_mainsys->matX == NULL)
+ ERROR(7)
+ START_OF_X;
+ }
+ for (; END_OF_MAT; NEXT_OF_MAT)
+ if (!IsEmptySetA(ELEM_OF_MAT))
+ if (!InterSetA(ELEM_OF_MAT, red))
+ return 0;
+ return 1;
+}
+
+int IsCoverRel(setA red, setA P, setA Q, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case MATA:
+ return IsCoverRelA(red, P, Q);
+ case MATD:
+ return IsCoverRelD(red, P, Q);
+ default:
+ ERROR(8)
+ }
+}
+
+int IsCoverRelA(setA red, setA P, setA Q)
+{
+ int ob1, ob2;
+ setA Pprim;
+
+ if (_mainsys->matA == NULL)
+ ERROR(5);
+ Pprim = InitEmptySetA();
+ DifSetA(Pprim, P, red);
+ for (ob1 = _mainsys->objects_num - 1; ob1 > 0; ob1--)
+ for (ob2 = ob1 - 1; ob2 >= 0; ob2--)
+ if (!CompareA(ob1, ob2, Q))
+ if (CompareA(ob1, ob2, red))
+ if (!CompareA(ob1, ob2, Pprim))
+ {
+ CloseSetA(Pprim);
+ return 0;
+ }
+ CloseSetA(Pprim);
+ return 1;
+}
+
+int IsCoverRelD(setA red, setA P, setA Q)
+{
+ if (_mainsys->matD == NULL)
+ ERROR(6);
+ for (START_OF_D; END_OF_MAT; NEXT_OF_MAT)
+ if (InterSetA(ELEM_OF_MAT, Q))
+ if (!InterSetA(ELEM_OF_MAT, red))
+ if (InterSetA(ELEM_OF_MAT, P))
+ return 0;
+ return 1;
+}
+
+int IsRed(setA red, int matrix_type)
+{
+ int result;
+ setA over;
+
+ if (!IsCover(red, matrix_type))
+ return 0;
+ over = InitEmptySetA();
+ result = IsOrtho(red, over, matrix_type);
+ CloseSetA(over);
+ return result;
+}
+
+
+int IsRedRel(setA red, setA P, setA Q, int matrix_type)
+{
+ int result;
+ setA over;
+
+ if (!IsCoverRel(red, P, Q, matrix_type))
+ return 0;
+ over = InitEmptySetA();
+ result = IsOrthoRel(red, over, P, Q, matrix_type);
+ CloseSetA(over);
+ return result;
+}
Added: grass-addons/grass6/raster/r.roughset/rcore.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rcore.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rcore.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,80 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+
+/*** ( FINDING CORES AND CHECKING REDUCTS ) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+
+int Core(setA core, int matrix_type);
+int CoreA(setA core);
+int CoreDX(setA core, int matrix_type);
+
+ /* finds a core of all attributes */
+
+int CoreRel(setA core, setA P, setA Q, int matrix_type);
+int CoreRelA(setA core, setA P, setA Q);
+int CoreRelD(setA core, setA P, setA Q);
+
+ /* finds a core of P relativly to Q */
+
+int IsOrtho(setA red, setA over, int matrix_type);
+
+ /* return 1 if red is orthogonal */
+ /* otherwise returns 0 */
+
+int IsOrthoRel(setA red, setA over, setA P, setA Q, int matrix_type);
+
+ /* return 1 if red is Q-orthogonal in P */
+ /* otherwise returns 0 */
+
+int IsCover(setA red, int matrix_type);
+int IsCoverA(setA red);
+int IsCoverDX(setA red, int matrix_type);
+
+ /* return 1 if red is a cover */
+ /* otherwise returns 0 */
+
+int IsCoverRel(setA red, setA P, setA Q, int matrix_type);
+int IsCoverRelA(setA red, setA P, setA Q);
+int IsCoverRelD(setA red, setA P, setA Q);
+
+ /* return 1 if red is Q-cover in P */
+ /* otherwise returns 0 */
+
+int IsRed(setA red, int matrix_type);
+
+ /* returns 1 if red is a reduct */
+ /* otherwise returns 0 */
+
+int IsRedRel(setA red, setA P, setA Q, int matrix_type);
+
+ /* returns 1 if red is a Q-relative reduct of P */
+ /* otherwise returns 0 */
Added: grass-addons/grass6/raster/r.roughset/reduct1.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/reduct1.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/reduct1.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,1305 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+/*** ( FINDING REDUCTS ) ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include "rough.h"
+#include <stdlib.h>
+
+
+int RedRel(setA * reducts, setA P, setA Q, int matrix_type)
+{
+ int MEMOKWANT = _mainsys->attributes_num * _mainsys->attributes_num, j, atr, over, size = _mainsys->setAsize, memoryN = 1, memoryO = 1, /* size of the allocated memory */
+ newcount = 0, oldcount; /* number of new/old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA arg, /* arguments are elements of matrix */
+ oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE, keep;
+
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ CoreRel(CORE, P, Q, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ if ((oldhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ arg = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type); /* initializing the reducts list */
+ if (!IsEmptySetA(CORE))
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size, oldcount = 1;
+ }
+ else
+ do
+ {
+ AndSetA(arg, _table_element, P);
+ oldcount = 0;
+ if (InterSetA(_table_element, Q))
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(arg, atr))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matD do */
+ {
+ AndSetA(arg, _table_element, P); /* take next element */
+ over = 0;
+ if (InterSetA(CORE, arg))
+ continue;
+ if (!InterSetA(_table_element, Q))
+ continue;
+ if (IsEmptySetA(arg))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare arg to all the old reducts */
+ {
+ if (!InterSetA(el, arg))
+ {
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of arg */
+ if (ContSetA(arg, atr))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ while ((el1 != el) && !over) /* comparing new reduct to old reducts */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over) /* appending new reduct */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes >
+ memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT *
+ (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(arg);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, new_el);
+ newtail += size;
+ }
+ }
+ }
+ else /* if reduct covers arg - rewrite it */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes > memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT * (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(arg);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, el);
+ newtail += size;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new reducts list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ j = memoryO;
+ memoryO = memoryN;
+ memoryN = j;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(arg);
+ CloseSetA(new_el);
+ return (oldcount);
+}
+
+int Red(setA * reducts, int matrix_type)
+{
+ int j, MEMOKWANT = _mainsys->attributes_num * _mainsys->attributes_num, atr, over, size = _mainsys->setAsize, memoryN = 1, memoryO = 1, /* size of the allocated memory */
+ newcount = 0, oldcount; /* number of new/old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE, keep, and, redprim;
+
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ Core(CORE, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ if ((oldhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ CloseSetA(CORE);
+ keep = NULL;
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ and = InitEmptySetA();
+ redprim = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE)) /* initializing of the reducts list */
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size, oldcount = 1;
+ }
+ else
+ do
+ {
+ oldcount = 0;
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(_table_element, atr))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matrix */
+ {
+ over = 0;
+ if (InterSetA(CORE, _table_element))
+ continue;
+ if (IsEmptySetA(_table_element))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare _table_element to all the old reducts */
+ {
+ if (!InterSetA(el, _table_element)) /* old reduct does not cover new element */
+ {
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of _table_element */
+ if (ContSetA(_table_element, atr))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ /* if ( matrix_type==MATX && (_mainsys->matXsize<(oldtail-oldhead)))
+ { ClearSetA( redprim );
+ for(el1=_mainsys->matX;el1<_table_end;el1+=size)
+ { AndSetA( and, el1, new_el );
+ if ( CardSetA( and ) == 1 ) OrSetA( redprim, redprim, and );
+ }
+ over = !CompSetA( redprim, new_el );
+ }
+ else
+ */
+ {
+ while ((el1 != el) && !over) /* comparing new reduct to all the old reducts */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ }
+ if (!over) /* appending new reduct */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes >
+ memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT *
+ (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, new_el);
+ newtail += size;
+ }
+ }
+ }
+ else /* old reduct covers new element */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes > memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT * (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, el);
+ newtail += size;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new reducts list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ j = memoryO;
+ memoryO = memoryN;
+ memoryN = j;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(new_el);
+ CloseSetA(redprim);
+ CloseSetA(and);
+ return (oldcount);
+}
+
+
+int RedRelLess(setA * reducts, setA P, setA Q, int N, int matrix_type)
+{
+ int j, MEMOKWANT = _mainsys->attributes_num * _mainsys->attributes_num, atr, over, size = _mainsys->setAsize, memoryN = 1, memoryO = 1, /* size of the allocated memory */
+ newcount = 0, oldcount; /* number of new/old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA arg, /* arguments are elements of matrix */
+ oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE, keep;
+
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ CoreRel(CORE, P, Q, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ if (CardSetA(CORE) > N)
+ {
+ free(CORE);
+ return (0);
+ }
+ if ((oldhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ arg = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE))
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size, oldcount = 1;
+ }
+ else
+ do
+ {
+ AndSetA(arg, _table_element, P);
+ oldcount = 0;
+ if (InterSetA(_table_element, Q))
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(arg, atr))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matrix do */
+ {
+ AndSetA(arg, _table_element, P); /* take next element */
+ over = 0;
+ if (InterSetA(CORE, arg))
+ continue;
+ if (!InterSetA(_table_element, Q))
+ continue;
+ if (IsEmptySetA(arg))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare arg to all old reducts */
+ {
+ if (!InterSetA(el, arg)) /* old reduct does not cover new element */
+ {
+ if (CardSetA(el) < N) /* shorter elements should be corected */
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of arg */
+ if (ContSetA(arg, atr))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ while ((el1 != el) && !over) /* comparing new reduct to all the old list */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over) /* appending to new list */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes >
+ memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT *
+ (++memoryN))) ==
+ NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(arg);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail =
+ newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, new_el);
+ newtail += size;
+ }
+ }
+ }
+ else /* old reduct covers new element */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes > memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT * (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(arg);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, el);
+ newtail += size;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ j = memoryO;
+ memoryO = memoryN;
+ memoryN = j;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(arg);
+ CloseSetA(new_el);
+ return (oldcount);
+}
+
+
+int RedLess(setA * reducts, int N, int matrix_type)
+{
+ int j, MEMOKWANT = _mainsys->attributes_num * _mainsys->attributes_num, atr, over, size = _mainsys->setAsize, memoryN = 1, memoryO = 1, /* size of the allocated memory */
+ newcount = 0, oldcount; /* number of new/old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE, keep;
+
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ Core(CORE, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ if (CardSetA(CORE) > N)
+ {
+ free(CORE);
+ return (0);
+ }
+ if ((oldhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE)) /*initializing the reducts list */
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size;
+ oldcount = 1;
+ }
+ else
+ do
+ {
+ oldcount = 0;
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(_table_element, atr))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matD do */
+ {
+ over = 0;
+ if (InterSetA(_table_element, CORE))
+ continue;
+ if (IsEmptySetA(_table_element))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare _table_element to all the old reducts */
+ {
+ if (!InterSetA(el, _table_element)) /* old reduct does not cover new element */
+ {
+ if (CardSetA(el) < N)
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of element */
+ if (ContSetA(_table_element, atr))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead; /* comparing new reduct to the old reducts */
+ while ((el1 != el) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over) /* appending new reduct to list */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes >
+ memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT *
+ (++memoryN))) ==
+ NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail =
+ newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, new_el);
+ newtail += size;
+ }
+ }
+ }
+ else /* old reduct covers new element */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes > memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT * (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, el);
+ newtail += size;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ j = memoryO;
+ memoryO = memoryN;
+ memoryN = j;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(new_el);
+ return (oldcount);
+}
+
+
+int RedRelSetA(setA * reducts, setA quasicore, setA P, setA Q,
+ int matrix_type)
+{
+ int j, MEMOKWANT = _mainsys->attributes_num * _mainsys->attributes_num, atr, over, size = _mainsys->setAsize, memoryN = 1, memoryO = 1, /* size of the allocated memory */
+ newcount = 0, oldcount; /* number of new/old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA arg, /* arguments are elements of matrix */
+ oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE, keep;
+
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ CoreRel(CORE, P, Q, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ OrSetA(CORE, CORE, quasicore);
+ if ((oldhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ arg = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE)) /*initializing the reducts list */
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size;
+ oldcount = 1;
+ }
+ else
+ do
+ {
+ AndSetA(arg, _table_element, P);
+ oldcount = 0;
+ if (InterSetA(_table_element, Q))
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(arg, atr))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matrix do */
+ {
+ AndSetA(arg, _table_element, P); /* take next element */
+ over = 0;
+ if (InterSetA(CORE, arg))
+ continue;
+ if (!InterSetA(_table_element, Q))
+ continue;
+ if (IsEmptySetA(arg))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare element to all the old reducts */
+ {
+ if (!InterSetA(el, arg)) /* old reduct does not cover new element */
+ {
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of element */
+ if (ContSetA(arg, atr))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ while ((el1 != el) && !over) /* comparing new reduct to all the old reducts */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over) /* appending new reduct to list */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes >
+ memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT *
+ (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(arg);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, new_el);
+ newtail += size;
+ }
+ }
+ }
+ else /* old reduct covers new elemet */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes > memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT * (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(arg);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, el);
+ newtail += size;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ j = memoryO;
+ memoryO = memoryN;
+ memoryN = j;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(arg);
+ CloseSetA(new_el);
+ return (oldcount);
+}
+
+int RedSetA(setA * reducts, setA quasicore, int matrix_type)
+{
+ int j, MEMOKWANT = _mainsys->attributes_num * _mainsys->attributes_num, atr, over, size = _mainsys->setAsize, memoryN = 1, memoryO = 1, /* size of the allocated memory */
+ newcount = 0, oldcount; /* number of new/old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE, keep;
+
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ Core(CORE, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ OrSetA(CORE, CORE, quasicore);
+ if ((oldhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(MEMOKWANT)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE)) /*initializing the reducts list */
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size, oldcount = 1;
+ }
+ else
+ do
+ {
+ oldcount = 0;
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(_table_element, atr))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matrix do */
+ {
+ over = 0;
+ if (InterSetA(_table_element, CORE))
+ continue;
+ if (IsEmptySetA(_table_element))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare elment to all the old reducts */
+ {
+ if (!InterSetA(el, _table_element)) /* old reduct does not cover element */
+ {
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of element */
+ if (ContSetA(_table_element, atr))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ while ((el1 != el) && !over) /* comparing new reduct to all the old reducts */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over) /* appending new reduct to list */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes >
+ memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT *
+ (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, new_el);
+ newtail += size;
+ }
+ }
+ }
+ else /* old reduct covers new element */
+ {
+ newcount++;
+ if (newcount * size * _cluster_bytes > memoryN * MEMOKWANT)
+ {
+ keep = newhead;
+ if ((newhead =
+ (setA) realloc(keep,
+ MEMOKWANT * (++memoryN))) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(new_el);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if (keep != newhead)
+ newtail = newhead + (newcount - 1) * size;
+ }
+ CopySetA(newtail, el);
+ newtail += size;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ j = memoryO;
+ memoryO = memoryN;
+ memoryN = j;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(new_el);
+ return (oldcount);
+}
+
+int RedRelFirst(setA * reducts, setA P, setA Q, int N, int matrix_type)
+{
+ int atr, over, size = _mainsys->setAsize, newcount = 0, oldcount; /* number of new,old reducts */
+ int no_A = _mainsys->attributes_num;
+ setA arg, /* arguments are elements of matrix */
+ oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE;
+
+ if (N == 0)
+ return 0;
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ CoreRel(CORE, P, Q, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ if ((oldhead = (setA) malloc(N * size * _cluster_bytes)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(N * size * _cluster_bytes)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ arg = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE)) /* initializing the reducts list */
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size, oldcount = 1;
+ }
+ else
+ do
+ {
+ AndSetA(arg, _table_element, P);
+ oldcount = 0;
+ if (InterSetA(_table_element, Q))
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(arg, atr) && (oldcount < N))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matrix do */
+ {
+ AndSetA(arg, _table_element, P); /* take next element */
+ over = 0;
+ if (InterSetA(CORE, arg))
+ continue;
+ if (!InterSetA(_table_element, Q))
+ continue;
+ if (IsEmptySetA(arg))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare element to all the old reducts */
+ {
+ if (!InterSetA(el, arg)) /* old reduct does not cover element */
+ {
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of elememt */
+ if (ContSetA(arg, atr) && (newcount < N))
+ {
+ CopySetA(new_el, el); /* creating potentialy new reduct */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ while ((el1 != el) && !over) /* comparing new reduct to all the old reducts */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over) /* appending new reduct to list */
+ {
+ CopySetA(newtail, new_el);
+ newtail += size;
+ newcount++;
+ }
+ }
+ }
+ else if (newcount < N)
+ {
+ CopySetA(newtail, el);
+ newtail += size;
+ newcount++;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(arg);
+ CloseSetA(new_el);
+ return (oldcount);
+}
+
+int RedFirst(setA * reducts, int N, int matrix_type)
+{
+ int atr, over, size = _mainsys->setAsize, newcount = 0, oldcount; /* sizes of reduct lists */
+ int no_A = _mainsys->attributes_num;
+ setA oldhead, /* head of static list of reducts */
+ oldtail, /* tail ends the old reducts list */
+ el, el1, /* elements of this list */
+ new_el, /* probably new element of the list */
+ newhead, /* begin of new reducts */
+ newtail; /* tail of the list of new reducts */
+ setA CORE;
+
+ if (N == 0)
+ return 0;
+ CORE = InitEmptySetA();
+ _rerror = 0;
+ Core(CORE, matrix_type);
+ if (_rerror != 0)
+ {
+ CloseSetA(CORE);
+ return (-_rerror);
+ }
+ if ((oldhead = (setA) malloc(N * size * _cluster_bytes)) == NULL)
+ {
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ if ((newhead = (setA) malloc(N * size * _cluster_bytes)) == NULL)
+ {
+ free(oldhead);
+ CloseSetA(CORE);
+ ERROR(3);
+ }
+ new_el = InitEmptySetA();
+ oldtail = oldhead;
+ newtail = newhead;
+ start_of_tab(matrix_type);
+ if (!IsEmptySetA(CORE)) /*initializing the reducts list */
+ {
+ CopySetA(oldtail, CORE);
+ oldtail += size, oldcount = 1;
+ }
+ else
+ do
+ {
+ oldcount = 0;
+ for (atr = 0; atr < no_A; atr++)
+ if (ContSetA(_table_element, atr) && (oldcount < N))
+ {
+ ClearSetA(oldtail);
+ AddSetA(oldtail, atr);
+ oldtail += size;
+ oldcount++;
+ }
+ next_of_tab();
+ }
+ while ((oldcount == 0) && end_of_tab());
+ for (; end_of_tab(); next_of_tab()) /* for each element of matrix do */
+ {
+ over = 0;
+ if (InterSetA(_table_element, CORE))
+ continue;
+ if (IsEmptySetA(_table_element))
+ continue;
+ el = oldhead;
+ while (el < oldtail) /* compare element to all the old reducts */
+ {
+ if (!InterSetA(el, _table_element)) /* old reduct does not cover element */
+ {
+ for (atr = 0; atr < no_A; atr++) /* for each atribute of element */
+ if (ContSetA(_table_element, atr) && (newcount < N))
+ {
+ CopySetA(new_el, el); /* creating potentialy new element */
+ AddSetA(new_el, atr);
+ over = 0;
+ el1 = oldhead;
+ while ((el1 != el) && !over) /* comparing new reduct to old list */
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ el1 = el + size;
+ while ((el1 != oldtail) && !over)
+ {
+ over = InSetA(new_el, el1);
+ el1 += size;
+ }
+ if (!over)
+ {
+ CopySetA(newtail, new_el);
+ newtail += size;
+ newcount++;
+ }
+ }
+ }
+ else if (newcount < N)
+ {
+ CopySetA(newtail, el); /* rewriting reduct from old list */
+ newtail += size;
+ newcount++;
+ }
+ el += size;
+ }
+ oldtail = newhead; /* new list becomes old */
+ newhead = oldhead;
+ oldhead = oldtail;
+ oldtail = newtail;
+ newtail = newhead;
+ oldcount = newcount;
+ newcount = 0;
+ }
+ if (oldcount == 0)
+ {
+ free(oldhead);
+ *reducts = NULL;
+ }
+ else
+ *reducts = oldhead;
+ free(newhead);
+ CloseSetA(CORE);
+ CloseSetA(new_el);
+ return (oldcount);
+}
Added: grass-addons/grass6/raster/r.roughset/reduct1.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/reduct1.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/reduct1.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,78 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+
+/*** ( FINDING REDUCTS ) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+
+int Red(setA * red, int matrix);
+
+ /* finds all reducts for information system, */
+ /* sets red to yhe allocated reducts table */
+ /* function returns number of reducts */
+
+
+int RedRel(setA * red, setA P, setA Q, int matrix_type);
+
+ /* like Red, but reducts are Q-relative and */
+ /* computed from set of attributes P */
+
+int RedLess(setA * red, int N, int matrix_type);
+
+ /* finds all reducts which contain less than */
+ /* N attributes, other parameters and result */
+ /* like Red */
+
+int RedRelLess(setA * red, setA P, setA Q, int N, int matrix_type);
+
+ /* like RedLess, but reducts are Q-relative of P */
+
+
+int RedSetA(setA * red, setA quasicore, int matrix_type);
+
+ /* finds only reducts including quasicore , */
+ /* some reducts can be not minimal ( formally */
+ /* they are not reducts ) , other parameters and */
+ /* result like Red */
+
+int RedRelSetA(setA * red, setA quasicore, setA P, setA Q, int matrix_type);
+
+ /* like RedSetA, but reducts are Q-relative of P */
+
+
+int RedFirst(setA * red, int N, int matrix_type);
+
+ /* finds first N reducts ( only quasi-reduct ), */
+ /* other parameters and result like Red */
+
+int RedRelFirst(setA * red, setA P, setA Q, int N, int matrix_type);
+
+ /* like RedFirst, but reducts are Q-relative of P */
Added: grass-addons/grass6/raster/r.roughset/reduct2.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/reduct2.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/reduct2.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,236 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+/*** ( AUXILIARY REDUCTS ROUTINES ) ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include <stdlib.h>
+#include "rough.h"
+
+int RedSingle(setA red, int matrix_type)
+{
+ int i;
+ int cont = 1;
+ setA over;
+
+ over = InitEmptySetA();
+ Core(red, matrix_type);
+ for (i = 0; (i < _mainsys->attributes_num) && cont; i++)
+ if (IsCover(red, matrix_type))
+ cont = 0;
+ else
+ AddSetA(red, i);
+ cont = 1;
+ while (cont)
+ if (IsOrtho(red, over, matrix_type))
+ cont = 0;
+ else
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (ContSetA(over, i))
+ {
+ DelSetA(red, i);
+ break;
+ }
+ CloseSetA(over);
+ return 1;
+}
+
+int RedRelSingle(setA red, setA P, setA Q, int matrix_type)
+{
+ int i;
+ int cont = 1;
+ setA over;
+
+ over = InitEmptySetA();
+ CoreRel(red, P, Q, matrix_type);
+ for (i = 0; (i < _mainsys->attributes_num) && cont; i++)
+ {
+ if (IsCoverRel(red, P, Q, matrix_type))
+ cont = 0;
+ else
+ AddSetA(red, i);
+ }
+ cont = 1;
+ while (cont)
+ {
+ if (IsOrthoRel(red, over, P, Q, matrix_type))
+ cont = 0;
+ else
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (ContSetA(over, i))
+ {
+ DelSetA(red, i);
+ break;
+ }
+ }
+ CloseSetA(over);
+ return 1;
+}
+
+
+
+int RedOptim(setA red, int matrix_type)
+{
+ int max, f, fmax;
+ int i, atr;
+ int cont = 1;
+ setA over;
+
+ over = InitFullSetA();
+ Core(red, matrix_type);
+ fmax = CardCoef(over, matrix_type);
+ max = CardCoef(red, matrix_type);
+ while (max < fmax)
+ {
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (!ContSetA(red, i))
+ {
+ AddSetA(red, i);
+ f = CardCoef(red, matrix_type);
+ if (f >= max)
+ max = f, atr = i;
+ DelSetA(red, i);
+ }
+ AddSetA(red, atr);
+ }
+ cont = 1;
+ while (cont)
+ {
+ if (IsOrtho(red, over, matrix_type))
+ cont = 0;
+ else
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (ContSetA(over, i))
+ {
+ DelSetA(red, i);
+ break;
+ }
+ }
+ CloseSetA(over);
+ return 1;
+}
+
+int RedRelOptim(setA red, setA P, setA Q, int matrix_type)
+{
+ float max, f, fmax;
+ int i, atr;
+ int cont = 1;
+ setA over;
+
+ over = InitEmptySetA();
+ CoreRel(red, P, Q, matrix_type);
+ fmax = DependCoef(P, Q, matrix_type);
+ while (cont)
+ {
+ max = 0;
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (ContSetA(P, i) && !ContSetA(red, i))
+ {
+ AddSetA(red, i);
+ f = DependCoef(red, Q, matrix_type);
+ if (f >= max)
+ max = f, atr = i;
+ DelSetA(red, i);
+ }
+ AddSetA(red, atr);
+ if (max == fmax)
+ cont = 0;
+ }
+ cont = 1;
+ while (cont)
+ {
+ if (IsOrthoRel(red, over, P, Q, matrix_type))
+ cont = 0;
+ else
+ for (i = 0; i < _mainsys->attributes_num; i++)
+ if (ContSetA(over, i))
+ {
+ DelSetA(red, i);
+ break;
+ }
+ }
+ CloseSetA(over);
+ return 1;
+}
+
+int RedFew(setA * reds, int matrix_type)
+{
+ setA r;
+ int n;
+
+ r = InitEmptySetA();
+ RedSingle(r, matrix_type);
+ n = RedLess(reds, CardSetA(r), matrix_type);
+ CloseSetA(r);
+ return n;
+}
+
+int RedRelFew(setA * reds, setA P, setA Q, int matrix_type)
+{
+ setA r;
+ int n;
+
+ r = InitEmptySetA();
+ RedRelSingle(r, P, Q, matrix_type);
+ n = RedRelLess(reds, P, Q, CardSetA(r), matrix_type);
+ CloseSetA(r);
+ return n;
+}
+
+int SelectOneShort(setA reds, int N)
+{
+ int min, m, i, num = 0;
+
+ min = _mainsys->attributes_num;
+ for (i = 0, START_OF_MAT(reds, N); END_OF_MAT; i++, NEXT_OF_MAT)
+ if (min > (m = CardSetA(ELEM_OF_MAT)))
+ min = m, num = i;
+ return num;
+}
+
+int SelectAllShort(setA * newreds, setA reds, int N)
+{
+ int num, m, min;
+ int size = _mainsys->setAsize;
+
+ min = _mainsys->attributes_num + 1;
+ *newreds = (cluster_type *) malloc(N * size * _cluster_bytes);
+ for (START_OF_MAT(reds, N); END_OF_MAT; NEXT_OF_MAT)
+ if (min > (m = CardSetA(ELEM_OF_MAT)))
+ {
+ min = m;
+ num = 0;
+ CopySetA(*newreds + num * size, ELEM_OF_MAT);
+ }
+ else if (min == m)
+ {
+ num++;
+ CopySetA(*newreds + num * size, ELEM_OF_MAT);
+ }
+ *newreds =
+ (cluster_type *) realloc(*newreds, (num + 1) * size * _cluster_bytes);
+ if (*newreds == 0)
+ num = -1;
+ return num + 1;
+}
Added: grass-addons/grass6/raster/r.roughset/reduct2.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/reduct2.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/reduct2.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,74 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+
+/*** (AUXILIARY REDUCTS ROUTINES) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+int RedSingle(setA red, int matrix_type);
+
+ /* heuristicly searches for a single reduct and stores it in red */
+
+int RedRelSingle(setA red, setA P, setA Q, int matrix_type);
+
+ /* heuristicly searches for a single Q-relative reduct of P */
+ /* and stores it in red */
+
+int RedOptim(setA red, int matrix_type);
+
+ /* heuristicly searches for a single reduct and stores it in red */
+ /* dependency coefficient is used to select */
+ /* attributes for search optimization */
+
+int RedRelOptim(setA red, setA P, setA Q, int matrix_type);
+
+ /* heuristicly searches for a single Q-relative reduct of P */
+ /* and stores it in red; dependency coefficient is used to select */
+ /* attributes for search optimization */
+
+int RedFew(setA * reds, int matrix_type);
+
+ /* finds all reducts shortesest or equal in size to the first */
+ /* reduct found by the heuristic algorithm */
+
+int RedRelFew(setA * reds, setA P, setA Q, int matrix_type);
+
+ /* finds all Q-relative P-reducts shortesest or equal in size */
+ /* to the first reduct found by the heuristic algorithm */
+
+int SelectOneShort(setA reds, int num);
+
+ /* returns index of the single shortest reduct */
+ /* from the table of num reducts pointed by reds */
+
+int SelectAllShort(setA * newreds, setA reds, int num);
+
+ /* copies all the shortest reducts */
+ /* from the table of num reducts pointed by reds */
Added: grass-addons/grass6/raster/r.roughset/rerror.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rerror.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rerror.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,45 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** OPTIONAL ERROR HANDLING AND ERROR MESSAGES ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J. Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+char *errcodes[10] = { "everything O.K.",
+ "Cannot open file",
+ "Wrong format of file",
+ "Not enough memory",
+ "Cannot write to file",
+ "Matrix A not initialized",
+ "Matrix D not initialized",
+ "Matrix X not initialized",
+ "Wrong matrix type",
+ "Set element out of domain"
+};
+
+#define ErrorPrint() {printf("ERROR no: %i: %s\n",_rerror,errcodes[_rerror]);_rerror=0;}
Added: grass-addons/grass6/raster/r.roughset/rough.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rough.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rough.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,73 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** OBLIGATORY FILE TO INCLUDE IN APPLICATION SOURCE ***/
+
+/*** (DATA TYPES, CONSTANTS AND HEADERS) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+/********************* CONSTANTS AND TYPES ***********************************/
+
+#define MATA 0
+#define MATD 1
+#define MATX 2
+
+#define ERROR(a) { _rerror=a; return -a; }
+
+typedef unsigned int cluster_type;
+typedef unsigned int value_type;
+typedef cluster_type *setO;
+typedef cluster_type *setA;
+
+typedef struct
+{
+ char name[50];
+ int objects_num;
+ int attributes_num;
+ int descr_size;
+ void *description;
+ int setAsize;
+ int setOsize;
+ value_type *matA;
+ cluster_type *matD;
+ cluster_type *matX;
+ unsigned int matXsize;
+} SYSTEM;
+
+/************** H-FILES CONTAINING DECLARATIONS OF FUNCTIONS *********/
+
+#include "rset.h"
+#include "rsystem.h"
+#include "raccess.h"
+#include "rbasic.h"
+#include "rcore.h"
+#include "reduct1.h"
+#include "reduct2.h"
+#include "rule1.h"
+#include "rule2.h"
+#include "rclass.h"
Added: grass-addons/grass6/raster/r.roughset/rset.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rset.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rset.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,500 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** OPERATIONS ON SETS OF ATTRIBUTES AND OBJECTS ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J. Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include "rough.h"
+#include <stdlib.h>
+
+
+int _cluster_bytes = sizeof(cluster_type);
+int _cluster_bits = 8 * sizeof(cluster_type);
+cluster_type _mask[8 * sizeof(cluster_type)];
+
+setA InitEmptySetA(void)
+{
+ int cluster;
+ setA set;
+
+ set = (setA) malloc(_mainsys->setAsize * _cluster_bytes);
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ set[cluster] = 0;
+ return set;
+}
+
+setO InitEmptySetO(void)
+{
+ int cluster;
+ setO set;
+
+ set = (setA) malloc(_mainsys->setOsize * _cluster_bytes);
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ set[cluster] = 0;
+ return set;
+}
+
+setA InitFullSetA(void)
+{
+ int attr, cluster;
+ setA set;
+
+ set = (setA) malloc(_mainsys->setAsize * _cluster_bytes);
+ for (cluster = _mainsys->setAsize - 2; cluster >= 0; cluster--)
+ set[cluster] = ~(cluster_type) 0;
+ set[_mainsys->setAsize - 1] = 0;
+ for (attr = _mainsys->attributes_num - 1;
+ attr >= _cluster_bits * (_mainsys->setAsize - 1); attr--)
+ AddSetA(set, attr);
+ return set;
+}
+
+setO InitFullSetO(void)
+{
+ int cluster, attr;
+ setO set;
+
+ set = (setA) malloc(_mainsys->setOsize * _cluster_bytes);
+ for (cluster = _mainsys->setOsize - 2; cluster >= 0; cluster--)
+ set[cluster] = ~(cluster_type) 0;
+ set[_mainsys->setOsize - 1] = 0;
+ for (attr = _mainsys->objects_num - 1;
+ attr >= _cluster_bits * (_mainsys->setOsize - 1); attr--)
+ AddSetO(set, attr);
+ return set;
+}
+
+void TabToSetA(setA set, int num, int tab[])
+{
+ int atr;
+
+ ClearSetA(set);
+ for (atr = 0; atr < num; atr++)
+ AddSetA(set, tab[atr]);
+}
+
+void TabToSetO(setO set, int num, int tab[])
+{
+ int obj;
+
+ ClearSetO(set);
+ for (obj = 0; obj < num; obj++)
+ AddSetO(set, tab[obj]);
+}
+
+void ArgToSetA(setA set, int num, ...)
+{
+ int atr;
+ va_list list;
+
+ ClearSetA(set);
+ va_start(list, num);
+ for (atr = 0; atr < num; atr++)
+ AddSetA(set, va_arg(list, int));
+
+ va_end(list);
+}
+
+void ArgToSetO(setO set, int num, ...)
+{
+ int obj;
+ va_list list;
+
+ ClearSetO(set);
+ va_start(list, num);
+ for (obj = 0; obj < num; obj++)
+ AddSetO(set, va_arg(list, int));
+
+ va_end(list);
+}
+
+void CloseSetA(setA set)
+{
+ free(set);
+}
+
+void CloseSetO(setA set)
+{
+ free(set);
+}
+
+void OrSetO(setO or, setO s1, setO s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ or[cluster] = s1[cluster] | s2[cluster];
+ return;
+}
+
+void OrSetA(setA or, setA s1, setA s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ or[cluster] = s1[cluster] | s2[cluster];
+ return;
+}
+
+void AndSetO(setO and, setO s1, setO s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ and[cluster] = s1[cluster] & s2[cluster];
+ return;
+}
+
+void AndSetA(setA and, setA s1, setA s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ and[cluster] = s1[cluster] & s2[cluster];
+ return;
+}
+
+void DifSetO(setO dif, setO s1, setO s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ dif[cluster] = s1[cluster] & ~s2[cluster];
+ return;
+}
+
+void DifSetA(setA dif, setA s1, setA s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ dif[cluster] = s1[cluster] & ~s2[cluster];
+ return;
+}
+
+
+void NotSetO(setO not, setO set)
+{
+ int cluster, obj;
+
+ for (cluster = _mainsys->setOsize - 2; cluster >= 0; cluster--)
+ not[cluster] = ~set[cluster];
+ cluster = _mainsys->setOsize - 1;
+ for (obj =
+ _mainsys->objects_num - 1 - _cluster_bits * (_mainsys->setOsize - 1);
+ obj >= 0; obj--)
+ if (set[cluster] & _mask[obj])
+ not[cluster] &= ~_mask[obj];
+ else
+ not[cluster] |= _mask[obj];
+ return;
+}
+
+void NotSetA(setA not, setA set)
+{
+ int cluster, attr;
+
+ for (cluster = _mainsys->setAsize - 2; cluster >= 0; cluster--)
+ not[cluster] = ~set[cluster];
+ cluster = _mainsys->setAsize - 1;
+ for (attr =
+ _mainsys->attributes_num - 1 - _cluster_bits * (_mainsys->setAsize -
+ 1); attr >= 0;
+ attr--)
+ if (set[cluster] & _mask[attr])
+ not[cluster] &= ~_mask[attr];
+ else
+ not[cluster] |= _mask[attr];
+ return;
+}
+
+void ClearSetO(setO set)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ set[cluster] = 0;
+ return;
+}
+
+void ClearSetA(setA set)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ set[cluster] = 0;
+ return;
+}
+
+void FillSetO(setO set)
+{
+ int cluster, obj;
+
+ for (cluster = _mainsys->setOsize - 2; cluster >= 0; cluster--)
+ set[cluster] = ~(cluster_type) 0;
+ set[_mainsys->setOsize - 1] = 0;
+ for (obj = _mainsys->objects_num - 1;
+ obj >= _cluster_bits * (_mainsys->setOsize - 1); obj--)
+ AddSetO(set, obj);
+ return;
+}
+
+void FillSetA(setA set)
+{
+ int cluster, attr;
+
+ for (cluster = _mainsys->setAsize - 2; cluster >= 0; cluster--)
+ set[cluster] = ~(cluster_type) 0;
+ set[_mainsys->setAsize - 1] = 0;
+ for (attr = _mainsys->attributes_num - 1;
+ attr >= _cluster_bits * (_mainsys->setAsize - 1); attr--)
+ AddSetA(set, attr);
+ return;
+}
+
+int AddSetO(setO set, int obj)
+{
+ if (obj >= _mainsys->objects_num)
+ ERROR(9)
+ set[obj / _cluster_bits] |= _mask[obj % _cluster_bits];
+ return 0;
+}
+
+int AddSetA(setA set, int attr)
+{
+ if (attr >= _mainsys->attributes_num)
+ ERROR(9)
+ set[attr / _cluster_bits] |= _mask[attr % _cluster_bits];
+ return 0;
+}
+
+int DelSetO(setO set, int obj)
+{
+ if (obj >= _mainsys->objects_num)
+ ERROR(9)
+ set[obj / _cluster_bits] &= ~_mask[obj % _cluster_bits];
+ return 0;
+}
+
+int DelSetA(setA set, int attr)
+{
+ if (attr >= _mainsys->attributes_num)
+ ERROR(9)
+ set[attr / _cluster_bits] &= ~_mask[attr % _cluster_bits];
+ return 0;
+}
+
+int InSetO(setO big, setO small)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ if (big[cluster] != (big[cluster] | small[cluster]))
+ return 0;
+ return 1;
+}
+
+int InSetA(setA big, setA small)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ if (big[cluster] != (big[cluster] | small[cluster]))
+ return 0;
+ return 1;
+}
+
+int ContSetA(setA set, int attr)
+{
+ if (attr >= _mainsys->attributes_num)
+ ERROR(9)
+ return (_mask[attr % _cluster_bits] & set[attr / _cluster_bits]);
+}
+
+int ContSetO(setO set, int obj)
+{
+ if (obj >= _mainsys->objects_num)
+ ERROR(9)
+ return (_mask[obj % _cluster_bits] & set[obj / _cluster_bits]);
+}
+
+int InterSetO(setO s1, setO s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ if (s1[cluster] & s2[cluster])
+ return 1;
+ return 0;
+}
+
+int InterSetA(setA s1, setA s2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ if (s1[cluster] & s2[cluster])
+ return 1;
+ return 0;
+}
+
+
+int IsEmptySetO(setO set)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ if (set[cluster])
+ return 0;
+ return 1;
+}
+
+int IsEmptySetA(setA set)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ if (set[cluster])
+ return 0;
+ return 1;
+}
+
+int CardSetO(setO set)
+{
+ int obj, card = 0;
+
+ for (obj = _mainsys->objects_num - 1; obj >= 0; obj--)
+ if (ContSetO(set, obj))
+ card++;
+ return card;
+}
+
+int CardSetA(setA set)
+{
+ int cluster, attr, card = 0;
+
+ for (cluster = _mainsys->setAsize - 2; cluster >= 0; cluster--)
+ for (attr = _cluster_bits - 1; attr >= 0; attr--)
+ if (_mask[attr] & set[cluster])
+ card++;
+ cluster = _mainsys->setAsize - 1;
+ for (attr =
+ _mainsys->attributes_num - 1 - _cluster_bits * (_mainsys->setAsize -
+ 1); attr >= 0;
+ attr--)
+ if (_mask[attr] & set[cluster])
+ card++;
+ return card;
+}
+
+void CopySetO(setO dest, setO source)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ dest[cluster] = source[cluster];
+ return;
+}
+
+void CopySetA(setA dest, setA source)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ dest[cluster] = source[cluster];
+ return;
+}
+
+int CompSetO(setO set1, setO set2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setOsize - 1; cluster >= 0; cluster--)
+ if (set1[cluster] != set2[cluster])
+ return 0;
+ return 1;
+}
+
+int CompSetA(setA set1, setA set2)
+{
+ int cluster;
+
+ for (cluster = _mainsys->setAsize - 1; cluster >= 0; cluster--)
+ if (set1[cluster] != set2[cluster])
+ return 0;
+ return 1;
+}
+
+int SizeSetO(void)
+{
+ return _mainsys->setOsize;
+}
+
+int SizeSetA(void)
+{
+ return _mainsys->setAsize;
+}
+
+void AttrValSetO(setO set, int attr, value_type val)
+{
+ int obj;
+
+ ClearSetO(set);
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (val == GetA(obj, attr))
+ AddSetO(set, obj);
+}
+
+int ClassSetO(setO aclass, int obj, setA Q)
+{
+ int i;
+
+ ClearSetO(aclass);
+ for (i = 0; i < _mainsys->objects_num; i++)
+ if (CompareA(i, obj, Q))
+ AddSetO(aclass, i);
+ return 0;
+}
+
+
+void PrintSetO(setO set)
+{
+ int obj, i = 0;
+
+ printf("{");
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (ContSetO(set, obj))
+ printf("%c%i", (i++ > 0 ? ',' : ' '), obj);
+ printf(" }\n");
+}
+
+void PrintSetA(setA set)
+{
+ int attr, i = 0;
+
+ printf("{");
+ for (attr = 0; attr < _mainsys->attributes_num; attr++)
+ if (ContSetA(set, attr))
+ printf("%c%i", (i++ > 0 ? ',' : ' '), attr);
+ printf(" }\n");
+}
Added: grass-addons/grass6/raster/r.roughset/rset.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rset.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rset.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,165 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** OPERATIONS ON SETS OF ATTRIBUTES AND OBJECTS ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J. Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+#include <stdarg.h>
+
+extern cluster_type _mask[];
+extern int _cluster_bytes;
+extern int _cluster_bits;
+
+setO InitEmptySetO(void);
+setA InitEmptySetA(void);
+
+ /* allocates memory for set and initializes it */
+ /* as empty, returns allocated set */
+
+setO InitFullSetO(void);
+setO InitFullSetA(void);
+
+ /* allocates memory for set and initializes it */
+ /* as full, returns newly allocated set */
+
+void TabToSetO(setO set, int num, int tab[]);
+void TabToSetA(setA set, int num, int tab[]);
+
+ /* sets a set with num elements of table tab */
+
+void ArgToSetO(setO set, int num, ...);
+void ArgToSetA(setA set, int num, ...);
+
+ /* sets a set with num arguments of function */
+
+void CloseSetO(setO set);
+void CloseSetA(setA set);
+
+ /* free memory used by set */
+
+void OrSetO(setO or, setO s1, setO s2);
+void OrSetA(setA or, setA s1, setA s2);
+
+ /* puts union of s1 and s2 to or */
+
+void AndSetO(setO and, setO s1, setO s2);
+void AndSetA(setA and, setA s1, setA s2);
+
+ /* puts product of s1 and s2 to and */
+
+void DifSetO(setO dif, setO s1, setO s2);
+void DifSetA(setA dif, setA s1, setA s2);
+
+ /* puts complement of s1 in s2 to dif */
+
+void NotSetO(setO not, setO set);
+void NotSetA(setO not, setA set);
+
+ /* puts a complement of set to not */
+
+void ClearSetO(setO set);
+void ClearSetA(setA set);
+
+ /* clears set */
+
+void FillSetO(setO set);
+void FillSetA(setA set);
+
+ /* fills set with domain */
+
+int AddSetO(setO set, int obj);
+int AddSetA(setA set, int attr);
+
+ /* adds element to set */
+
+int DelSetO(setO set, int obj);
+int DelSetA(setA set, int attr);
+
+ /* deletes element from set */
+
+int InSetO(setO big, setO small);
+int InSetA(setA big, setA small);
+
+ /* return 1 if set big contains set small */
+ /* otherwise returns 0 */
+
+int ContSetO(setO set, int obj);
+int ContSetA(setA set, int attr);
+
+ /* returns 1 if set contains element */
+ /* otherwise returns 0 */
+
+int InterSetO(setO s1, setO s2);
+int InterSetA(setA s1, setA s2);
+
+ /* returns 1 if s1 and s2 have nonempty product */
+ /* otherwise returns 0 */
+
+int IsEmptySetO(setO set);
+int IsEmptySetA(setA set);
+
+ /* returns 1 if set is empty */
+ /* otherwise returns 0 */
+
+int CardSetO(setO set);
+int CardSetA(setA set);
+
+ /* returns cardinality of set */
+
+void CopySetO(setO dest, setO source);
+void CopySetA(setA dest, setO source);
+
+ /* copy source to dest */
+
+int CompSetO(setO set1, setO set2);
+int CompSetA(setA set1, setA set2);
+
+ /* returns 1 if set1 and set2 are identical */
+ /* otherwise returns 0 */
+
+int SizeSetO(void);
+int SizeSetA(void);
+
+ /* returns number of clusters in set representation */
+ /* in the active system sizeof(cluster_type)=_cluster_bytes */
+
+void AttrValSetO(setO set, int attr, value_type val);
+
+ /* puts into set all object that have value val */
+ /* on attribute attr */
+
+int ClassSetO(setO aclass, int obj, setA Q);
+
+ /* fills class with all object that have the same values */
+ /* on all attributes from Q as the object obj, uses MATA */
+
+void PrintSetO(setO set);
+void PrintSetA(setA set);
+
+ /* outputs set to screen */
Added: grass-addons/grass6/raster/r.roughset/rsystem.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rsystem.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rsystem.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,564 @@
+/****************************************************************************
+ *
+ * MODULE: r.roughset
+ * AUTHOR(S): GRASS module authors and 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SYSTEM HANDLING FUNCTIONS ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "rough.h"
+
+SYSTEM *_mainsys=NULL;
+int _rerror=0;
+
+SYSTEM *InitEmptySys(void)
+{
+ SYSTEM *sys;
+ int i;
+ cluster_type mask=1;
+ if ((sys=(SYSTEM *)malloc(sizeof(SYSTEM)))==NULL)
+ {
+ _rerror=3;
+ return NULL;
+ }
+ for (i=0; i<_cluster_bits; i++)
+ {
+ _mask[i]=mask;
+ mask <<= 1;
+ }
+ sys->attributes_num=0;
+ sys->objects_num=0;
+ sys->description=NULL;
+ sys->descr_size=0;
+ sys->setAsize=0;
+ sys->setOsize=0;
+ sys->matA=NULL;
+ sys->matD=NULL;
+ sys->matX=NULL;
+ sys->matXsize=0;
+ return sys;
+}
+
+void SetParameters(SYSTEM *sys,int obj_num,int attr_num)
+{
+ sys->attributes_num=attr_num;
+ sys->objects_num=obj_num;
+ sys->setAsize=1+(attr_num-1)/_cluster_bits;
+ sys->setOsize=1+(obj_num-1)/_cluster_bits;
+}
+
+void ConnectDescr(SYSTEM *sys,void *descr,int size)
+{
+ sys->description=descr;
+ sys->descr_size=size;
+}
+
+void SetName(SYSTEM *sys,char *name)
+{
+ strncpy(sys->name,name,50);
+}
+
+int FileToSys(SYSTEM *sys,char *filename)
+{
+ FILE *source;
+ value_type *buf;
+ int descr_size,matAsize;
+ long len1,len2;
+ char string[50];
+ int c,n=0;
+ if (sys->matA!=NULL)
+ {
+ free(sys->matA);
+ sys->matA=NULL;
+ }
+ if (sys->matD!=NULL)
+ {
+ free(sys->matD);
+ sys->matD=NULL;
+ }
+ if (sys->matX!=NULL)
+ {
+ free(sys->matX);
+ sys->matX=NULL;
+ }
+ if (sys->description!=NULL)
+ {
+ free(sys->description);
+ sys->description=NULL;
+ }
+ if ((source=fopen(filename,"r"))==NULL)
+ ERROR(1)
+ if (!fscanf(source,"NAME%c",string))
+ ERROR(2)
+ if (string[0]!=':') ERROR(2)
+ if (!fgets(sys->name,48,source))
+ ERROR(2)
+ if (sys->name[strlen(sys->name)-1]=='\n')
+ sys->name[strlen(sys->name)-1]='\0';
+ else
+ while (fgetc(source)!='\n' && !feof(source));
+ if (!fscanf(source,"ATTRIBUTES:%i\n",&(sys->attributes_num)))
+ ERROR(2)
+ if (!fscanf(source,"OBJECTS:%i\n",&(sys->objects_num)))
+ ERROR(2)
+ sys->setAsize=1+(sys->attributes_num-1)/_cluster_bits;
+ sys->setOsize=1+(sys->objects_num-1)/_cluster_bits;
+ matAsize=(sys->objects_num)*(sys->attributes_num);
+ if ((buf=(value_type*)(setA)malloc(sizeof(value_type)*matAsize))==NULL)
+ ERROR(3)
+ while (!feof(source) && n<matAsize)
+ if (fscanf(source,"%s",string)==NULL)
+ ERROR(2)
+ else if (string[0]=='?' || string[0]=='*')
+ buf[n++]=(value_type)-1;
+ else if (sscanf(string,"%d",&c)==NULL)
+ ERROR(2)
+ else buf[n++]=(value_type)c;
+ sys->matA=buf;
+ while (isspace(fgetc(source)));
+ if (!feof(source))
+ {
+ len1=ftell(source)-1;
+ fseek(source,0l,SEEK_END);
+ len2=ftell(source);
+ fseek(source,len1,SEEK_SET);
+ descr_size=(int)(len2-len1);
+ buf=(value_type *)(setA)malloc(descr_size);
+ fread(buf,sizeof(char),descr_size,source);
+ sys->description=buf;
+ sys->descr_size=descr_size;
+ }
+ fclose(source);
+ return 1;
+}
+
+void ConnectA(SYSTEM *sys,value_type *buf)
+{
+ sys->matA=buf;
+}
+
+void PutA(SYSTEM *sys,int obj,int atr,value_type val)
+{
+ (sys->matA)[obj*(sys->attributes_num)+atr]=val;
+}
+
+int FillAfromAscii(SYSTEM *sys,FILE *file)
+{
+ int k=0,j;
+ while (!feof(file)&&(k<Asize(sys)))
+ {
+ if (fscanf(file,"%d",&j)==NULL) ERROR(2);
+ *(sys->matA+k++)=j;
+ }
+ return 0;
+}
+
+int InitD(SYSTEM *sys)
+{
+ cluster_type *buf;
+ int nby=(sys->attributes_num)/_cluster_bits; /* number of full clusters in element of tableD */
+ int nbi=sys->attributes_num%_cluster_bits; /* number of bits in the last cluster */
+ int atrnum=sys->attributes_num; /* number of attributes */
+ int row,column,cluster,bit;
+ value_type *A=sys->matA;
+ cluster_type val,*ptr;
+ if (A==NULL)
+ ERROR(5);
+ if ((buf=(setA)malloc(MatMemSize(sys,MATD)))==NULL)
+ ERROR(3);
+ ptr=buf;
+ for (row=1;row<sys->objects_num;row++)
+ for (column=0;column<row;column++)
+ {
+ for (cluster=0;cluster<nby;cluster++)
+ {
+ val=0;
+ for (bit=0;bit<_cluster_bits;bit++)
+ if (A[row*atrnum+_cluster_bits*cluster+bit]!=A[column*atrnum+_cluster_bits*cluster+bit] &&
+ A[row*atrnum+_cluster_bits*cluster+bit]!=(value_type)-1 &&
+ A[column*atrnum+_cluster_bits*cluster+bit]!=(value_type)-1 )
+ val=val|_mask[bit];
+ *ptr=val;
+ ptr++;
+ }
+ if (nbi)
+ {
+ val=0;
+ for (bit=0;bit<nbi;bit++)
+ if (A[row*atrnum+_cluster_bits*cluster+bit]!=A[column*atrnum+_cluster_bits*cluster+bit] &&
+ A[row*atrnum+_cluster_bits*cluster+bit]!=(value_type)-1 &&
+ A[column*atrnum+_cluster_bits*cluster+bit]!=(value_type)-1 )
+ val=val|_mask[bit];
+ *ptr=val;
+ ptr++;
+ }
+ }
+ sys->matD=buf;
+ return MatMemSize(sys,MATD);
+}
+
+int InitX(SYSTEM* sys,setA P,setA Q,int matrix_type)
+{
+ setA head,tail,el,el1;
+ int memo=1,count=0,no,dif,size,MEMOKWANT=Asize(_mainsys);
+ size=_mainsys->setAsize;
+ if ((head=(setA)malloc(MEMOKWANT))==NULL) ERROR(3);
+ tail=head;
+ el=InitEmptySetA();
+ for (start_of_tab(matrix_type);end_of_tab();next_of_tab())
+ {
+ if ( !InterSetA(_table_element,Q) ) continue;
+ AndSetA(el,P,_table_element);
+ if ( IsEmptySetA(el) ) continue;
+ no=0;
+ dif=0;
+ for (el1=head;el1<tail;el1+=size)
+ if (InSetA(el,el1))
+ {
+ no=1;
+ break;
+ }
+ else
+ {
+ if (InSetA(el1,el)) dif+=size,count--;
+ else if (dif) CopySetA(el1-dif,el1);
+ }
+ if (!no)
+ {
+ if ((memo*MEMOKWANT)<((count+1)*size*_cluster_bytes))
+ if ((head=(setA)realloc(head,++memo*MEMOKWANT))==NULL)
+ {
+ CloseSetA(el);
+ ERROR(3);
+ }
+ CopySetA(head+count*size,el);
+ count++;
+ tail=head+count*size;
+ }
+ }
+ CloseSetA(el);
+ if ((head=(setA)realloc(head,count*size*_cluster_bytes))==NULL) ERROR(3);
+ sys->matX=head;
+ sys->matXsize=count*size;
+ return(count);
+}
+
+int InitXforObject(SYSTEM* sys,int obj,setA P,setA Q,int matrix_type)
+{
+ setA head,tail,el,el1;
+ int memo=1,count=0,no,dif,MEMOKWANT=Asize(_mainsys);
+ int size=_mainsys->setAsize;
+ int obj2;
+ if ((head=(setA)malloc(MEMOKWANT))==NULL)
+ ERROR(3);
+ tail=head;
+ el=InitEmptySetA();
+ for (obj2=0; obj2<_mainsys->objects_num; obj2++)
+ {
+ if ( obj==obj2 ) continue;
+ if ( matrix_type==MATA ) GetDfromA( el, obj, obj2 );
+ else
+ {
+ _table_element=GetD( obj, obj2 );
+ CopySetA( el, _table_element );
+ }
+ if ( !InterSetA(el,Q) ) continue;
+ AndSetA(el,P,el);
+ if ( IsEmptySetA(el) ) continue;
+ no=0;
+ dif=0;
+ for (el1=head;el1<tail;el1+=size)
+ if (InSetA(el,el1))
+ {
+ no=1;
+ break;
+ }
+ else
+ {
+ if (InSetA(el1,el)) dif+=size,count--;
+ else if (dif) CopySetA(el1-dif,el1);
+ }
+ if (!no)
+ {
+ if ((memo*MEMOKWANT)<((count+1)*size*_cluster_bytes))
+ if ((head=(setA)realloc(head,++memo*MEMOKWANT))==NULL)
+ {
+ CloseSetA(el);
+ ERROR(3);
+ }
+ CopySetA(head+count*size,el);
+ count++;
+ tail=head+count*size;
+ }
+ }
+ CloseSetA(el);
+ if ((head=(setA)realloc(head,count*size*_cluster_bytes))==NULL)
+ if (count>0) ERROR(3);
+ sys->matX=head;
+ sys->matXsize=count*size;
+ return(count);
+}
+
+int InitXforObjects(SYSTEM* sys,setO objects,setA P,setA Q,int matrix_type)
+{
+ setA head,tail,el,el1;
+ int memo=1,count=0,no,dif,size,MEMOKWANT=Asize(_mainsys);
+ size=_mainsys->setAsize;
+ if ((head=(setA)malloc(MEMOKWANT))==NULL) ERROR(3);
+ tail=head;
+ el=InitEmptySetA();
+ for (start_of_tab(matrix_type);end_of_tab();next_of_tab())
+ {
+ if ( !ContSetO(objects,_table_row) &&
+ !ContSetO(objects,_table_column) ) continue;
+ if ( !InterSetA(_table_element,Q) ) continue;
+ AndSetA(el,P,_table_element);
+ if ( IsEmptySetA(el) ) continue;
+ no=0;
+ dif=0;
+ for (el1=head;el1<tail;el1+=size)
+ if (InSetA(el,el1))
+ {
+ no=1;
+ break;
+ }
+ else
+ {
+ if (InSetA(el1,el)) dif+=size,count--;
+ else if (dif) CopySetA(el1-dif,el1);
+ }
+ if (!no)
+ {
+ if ((memo*MEMOKWANT)<((count+1)*size*_cluster_bytes))
+ if ((head=(setA)realloc(head,++memo*MEMOKWANT))==NULL)
+ {
+ CloseSetA(el);
+ ERROR(3);
+ }
+ CopySetA(head+count*size,el);
+ count++;
+ tail=head+count*size;
+ }
+ }
+ CloseSetA(el);
+ if ((head=(setA)realloc(head,count*size*_cluster_bytes))==NULL) ERROR(3);
+ sys->matX=head;
+ sys->matXsize=count*size;
+ return(count);
+}
+
+int InitXforObjectFromClass(SYSTEM* sys,int obj,setA P,setO aclass,int matrix_type)
+{
+ setA head,tail,el,el1;
+ int memo=1,count=0,no,dif,MEMOKWANT=Asize(_mainsys);
+ int size=_mainsys->setAsize;
+ int obj2;
+ if ((head=(setA)malloc(MEMOKWANT))==NULL) ERROR(3);
+ tail=head;
+ el=InitEmptySetA();
+ for (obj2=0; obj2<_mainsys->objects_num; obj2++)
+ {
+ if ( ContSetO( aclass, obj2 ) ) continue;
+ if ( matrix_type==MATA ) GetDfromA( el, obj, obj2 );
+ else
+ {
+ _table_element=GetD( obj, obj2 );
+ CopySetA( el, _table_element );
+ }
+ AndSetA(el,P,el);
+ if ( IsEmptySetA(el) ) continue;
+ no=0;
+ dif=0;
+ for (el1=head;el1<tail;el1+=size)
+ if (InSetA(el,el1))
+ {
+ no=1;
+ break;
+ }
+ else
+ {
+ if (InSetA(el1,el)) dif+=size,count--;
+ else if (dif) CopySetA(el1-dif,el1);
+ }
+ if (!no)
+ {
+ if ((memo*MEMOKWANT)<((count+1)*size*_cluster_bytes))
+ if ((head=(setA)realloc(head,++memo*MEMOKWANT))==NULL)
+ {
+ CloseSetA(el);
+ ERROR(3);
+ }
+ CopySetA(head+count*size,el);
+ count++;
+ tail=head+count*size;
+ }
+ }
+ CloseSetA(el);
+ if ((head=(setA)realloc(head,count*size*_cluster_bytes))==NULL) ERROR(3);
+ sys->matX=head;
+ sys->matXsize=count*size;
+ return(count);
+}
+
+
+
+void UseSys(SYSTEM *sys)
+{
+ _mainsys=sys;
+}
+
+int SysToFile(SYSTEM *sys,char *filename)
+{
+ FILE *output;
+ int attr,obj;
+ if ((output=fopen(filename,"wb"))==NULL)
+ ERROR(1)
+ if (!fprintf(output,"NAME: %s\n",sys->name))
+ ERROR(4)
+ if (!fprintf(output,"ATTRIBUTES: %i\n",sys->attributes_num))
+ ERROR(4);
+ if (!fprintf(output,"OBJECTS: %i\n\n",sys->objects_num))
+ ERROR(4);
+ for (obj=0;obj<sys->objects_num;obj++)
+ {
+ for (attr=0;attr<sys->attributes_num;attr++)
+ if (sys->matA[obj*sys->attributes_num+attr]==(value_type)-1 )
+ {
+ if (!fprintf(output,"%c ",'?'))
+ ERROR(4)
+ }
+ else if (!fprintf(output,"%i ",sys->matA[obj*sys->attributes_num+attr]))
+ ERROR(4)
+ fprintf(output,"\n");
+ }
+ if (sys->descr_size!=0)
+ {
+ fprintf(output,"\n");
+ fwrite(sys->description,sizeof(char),sys->descr_size,output);
+ }
+ fclose(output);
+ return 1;
+}
+
+void CloseSys(SYSTEM *sys)
+{
+ if (sys->matA) free(sys->matA);
+ if (sys->matD) free(sys->matD);
+ if (sys->matX) free(sys->matX);
+ if (sys->description) free(sys->description);
+ free(sys);
+}
+
+void CloseMat(SYSTEM *sys, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case (MATA): if (sys->matA) free(sys->matA);
+ sys->matA=NULL;
+ return;
+ case (MATD): if (sys->matD) free(sys->matD);
+ sys->matD=NULL;
+ return;
+ case (MATX): if (sys->matX) free(sys->matX);
+ sys->matX=NULL;
+ return;
+ default:
+ _rerror=8;
+ }
+}
+
+void DisconMat(SYSTEM *sys, int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case (MATA): sys->matA=NULL;
+ return;
+ case (MATD): sys->matD=NULL;
+ return;
+ case (MATX): sys->matX=NULL;
+ return;
+ default:
+ _rerror=8;
+ }
+}
+
+unsigned int Asize(SYSTEM *sys)
+{
+ return (unsigned int)(sys->attributes_num)
+ *(unsigned int)(sys->objects_num);
+}
+
+unsigned int Dsize(SYSTEM *sys)
+{
+ return (unsigned int)(sys->objects_num-1)
+ *(unsigned int)(sys->objects_num)/2
+ *(unsigned int)(sys->setAsize);
+}
+
+unsigned int Xsize(SYSTEM *sys)
+{
+ return sys->matXsize;
+}
+
+unsigned int MatMemSize(SYSTEM *sys,int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case (MATA): return Asize(sys)*sizeof(value_type);
+ case (MATD): return Dsize(sys)*sizeof(cluster_type);
+ case (MATX): return Xsize(sys)*sizeof(cluster_type);
+ }
+ ERROR(8)
+}
+
+void *MatExist(SYSTEM *sys,int matrix_type)
+{
+ switch (matrix_type)
+ {
+ case (MATA): return (void *)sys->matA;
+ case (MATD): return (void *)sys->matD;
+ case (MATX): return (void *)sys->matX;
+ }
+ _rerror=8;
+ return NULL;
+}
+
+int ObjectsNum(SYSTEM *sys)
+{
+ return sys->objects_num ;
+}
+
+int AttributesNum(SYSTEM *sys)
+{
+ return sys->attributes_num;
+}
+
+void *Description(SYSTEM *sys)
+{
+ return sys->description;
+}
+
+char *SysName(SYSTEM *sys)
+{
+ return sys->name;
+}
Added: grass-addons/grass6/raster/r.roughset/rsystem.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rsystem.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rsystem.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,172 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SYSTEM HANDLING FUNCTIONS ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+#include <stdio.h>
+
+extern SYSTEM *_mainsys;
+
+ /* active information system */
+
+extern int _rerror;
+
+ /* error code */
+
+SYSTEM *InitEmptySys(void);
+
+ /* allocates memory for a new system descriptor */
+ /* and clears all parameters, no matrices connected */
+
+void SetParameters(SYSTEM * sys, int objects_num, int attributes_num);
+
+ /* puts system parameters to the system descriptor */
+
+void ConnectDescr(SYSTEM * sys, void *descr, int size);
+
+ /* connects additional description to the system descriptor */
+
+void SetName(SYSTEM * sys, char *name);
+
+ /* puts system name to the system descriptor */
+
+int FileToSys(SYSTEM * sys, char *filename);
+
+ /* imports system from the special format file */
+ /* including parameters, desicripton and matrix A */
+ /* other matrices are left uninitialed */
+
+int SysToFile(SYSTEM * sys, char *filename);
+
+ /* exports system descriptor, additional description */
+ /* and matrix A to a special format file */
+
+void ConnectA(SYSTEM * sys, value_type * buf);
+
+ /* connects matrix A (it may be empty) with */
+ /* the system descriptor */
+
+void PutToA(SYSTEM * sys, int object, int attribute, value_type value);
+
+ /* sets value to element of matrix A */
+
+int FillAfromAscii(SYSTEM * sys, FILE * file);
+
+ /* fills matrix A with the values from Ascii file */
+
+int InitD(SYSTEM * sys);
+
+ /* fills matrix D from A, allocates memory */
+ /* and connects it with the system descriptor */
+ /* matrix A must already exist */
+
+int InitX(SYSTEM * sys, setA P, setA Q, int matrix_type);
+
+ /* fills matrix X, allocates memory for it and */
+ /* connects it with the system descriptor */
+ /* matrix_type indicates the source matrix */
+ /* initialization is done with respect to Q from P */
+
+int InitXforObject(SYSTEM * sys, int obj, setA P, setA Q, int matrix_type);
+
+ /* works like InitX but considers only single column of MATD */
+
+int InitXforObjects(SYSTEM * sys, setO objects, setA P, setA Q,
+ int matrix_type);
+ /* works like InitX but concider only columns of MATD */
+ /* belonging to objects */
+
+int InitXforObjectFromClass(SYSTEM * sys, int obj, setA P, setO aclass,
+ int matrix_type);
+ /* works like InitX, conciders only specified objects */
+ /* initialization is done with respect to objects from */
+ /* outside of aclass */
+
+void UseSys(SYSTEM * sys);
+
+ /* makes indicated system active, from now on */
+ /* all query routines will operate only on it */
+
+void CloseSys(SYSTEM * sys);
+
+ /* frees memory allocated for all matrixes, */
+ /* system descriptor and additional description */
+
+void CloseMat(SYSTEM * sys, int matrix_type);
+
+ /* disconnects matrix X from the system descriptor */
+ /* and frees memory */
+
+void DisconMat(SYSTEM * sys, int matrix_type);
+
+ /* disconnects matrix from the system descriptor */
+ /* does not free memory */
+
+unsigned int Asize(SYSTEM * sys);
+
+ /* returns number of elements in matrix A */
+ /* memory size = Asize * sizeof(value_type) */
+ /* system parameters should be valued */
+
+unsigned int Dsize(SYSTEM * sys);
+
+ /* returns number of memory clusters for matrix D */
+ /* memory size = Dsize * sizeof(cluster_type) */
+ /* system parameters should be valued */
+
+unsigned int Xsize(SYSTEM * sys);
+
+ /* returns number of memory cluster used by matrix X */
+ /* memory size = Xsize * sizeof(cluster_type) */
+ /* matrix X should be generated */
+
+unsigned int MatMemSize(SYSTEM * sys, int matrix_type);
+
+ /* returns the size of memory used by specified matrix */
+
+void *MatExist(SYSTEM * sys, int matrix_type);
+
+ /* return specified matrix if exist */
+ /* otherwise returns NULL */
+
+int ObjectsNum(SYSTEM * sys);
+
+ /* returns number of objects in the system */
+
+int AttributesNum(SYSTEM * sys);
+
+ /* returns number of attributes in the system */
+
+void *Description(SYSTEM * sys);
+
+ /* returns pointer to additional description */
+
+char *SysName(SYSTEM * sys);
+
+ /* returns system name */
Added: grass-addons/grass6/raster/r.roughset/rule1.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rule1.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rule1.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,55 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+/*** ( MENAGING RULES ) ***/
+/*** ***/
+/*** part of the ROUGH system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include <stdlib.h>
+#include <string.h>
+#include "rough.h"
+
+void RuleCopy(value_type * dest, value_type * source)
+{
+ memcpy(dest, source, _mainsys->attributes_num * sizeof(value_type));
+}
+
+int RuleEQ(value_type * first, value_type * second)
+{
+ return !memcmp(first, second,
+ _mainsys->attributes_num * sizeof(value_type));
+}
+
+
+void AddRule(value_type * rules, int *count, value_type * rule)
+{
+ int i;
+ int size = _mainsys->attributes_num;
+
+ for (i = 0; i < *count; i++)
+ if (RuleEQ(rules + i * size, rule))
+ return;
+ RuleCopy(rules + (*count) * size, rule);
+ *count += 1;
+ return;
+}
Added: grass-addons/grass6/raster/r.roughset/rule1.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rule1.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rule1.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,48 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+
+/*** ( MENAGING RULES ) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+
+#define MINUS ((value_type)-1)
+
+void RuleCopy(value_type * dest, value_type * source);
+
+ /* copies a rule from source to dest */
+
+int RuleEQ(value_type * first, value_type * second);
+
+ /* returns 1 if first rule is equal to second */
+
+void AddRule(value_type * rules, int *size, value_type * rule);
+
+ /* adds new rule to array of rules */
+ /* if the rule is unique size is incremented */
Added: grass-addons/grass6/raster/r.roughset/rule2.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rule2.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rule2.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,666 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (2008)
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+/*** ***/
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+/*** ( FINDING RULES ) ***/
+/*** ***/
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+/*** ***/
+/*****************************************************************************/
+
+
+#include "rough.h"
+#include <stdlib.h>
+#include <string.h>
+
+int AllRules(value_type ** rules, setA P, setA Q, int matrix_type)
+{
+ int obj, attr, n, red;
+ int memo = _mainsys->objects_num;
+ int count = 0;
+ int size = _mainsys->setAsize;
+ setA reducts = NULL;
+ value_type *rule = NULL;
+
+ *rules =
+ (value_type *) malloc(memo * _mainsys->attributes_num *
+ sizeof(value_type));
+ if (*rules == NULL)
+ ERROR(3);
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ {
+ if (InitXforObject(_mainsys, obj, P, Q, matrix_type) < 0)
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ else if (!MatExist(_mainsys, MATX))
+ continue;
+ n = Red(&reducts, MATX);
+ CloseMat(_mainsys, MATX);
+ if (memo < count + n)
+ {
+ memo = count + n;
+ if ((*rules = (value_type *) realloc(*rules,
+ memo *
+ _mainsys->attributes_num *
+ sizeof(value_type))) ==
+ NULL)
+ {
+ free(reducts);
+ free(rule);
+ ERROR(3)
+ }
+ }
+ for (red = 0; red < n; red++)
+ {
+ for (attr = _mainsys->attributes_num - 1; attr >= 0; attr--)
+ if (ContSetA(reducts + red * size, attr) || ContSetA(Q, attr))
+ rule[attr] = GetA(obj, attr);
+ else
+ rule[attr] = MINUS;
+ AddRule(*rules, &count, rule);
+ }
+ if (n > 0)
+ free(reducts);
+ reducts = NULL;
+ }
+ free(rule);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ count * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return count;
+}
+
+int AllRulesForReducts(value_type ** rules, cluster_type * reducts,
+ int N, setA Q, int matrix_type)
+{
+ int red, num, i, count = 0;
+ value_type *newrules = NULL; /* rules for a single reduct */
+
+ *rules = NULL;
+ for (red = 0; red < N; red++)
+ {
+ num =
+ AllRules(&newrules, reducts + red * _mainsys->setAsize, Q,
+ matrix_type);
+ if ((*rules =
+ (value_type *) realloc(*rules,
+ (count +
+ num) * _mainsys->attributes_num *
+ sizeof(value_type))) == NULL)
+ {
+ free(newrules);
+ ERROR(3)
+ }
+ for (i = 0; i < num; i++)
+ AddRule(*rules, &count, newrules + i * _mainsys->attributes_num);
+ if (num > 0)
+ free(newrules);
+ newrules = NULL;
+ }
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ count * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return count;
+}
+
+
+int SelectRules(value_type ** rules, int *N, setO set, setA P, int option)
+{
+ int i, j, obj, find, newXsize,
+ newN = 0, next = -1, size = _mainsys->attributes_num;
+ setA red, newfull;
+ SYSTEM *oldsys = _mainsys, *newsys = InitEmptySys();
+ cluster_type *newX;
+ value_type *newrules;
+
+ SetParameters(newsys, *N, *N);
+ newXsize = CardSetO(set) * (newsys->setAsize);
+ newX = (cluster_type *) calloc(newXsize, _cluster_bytes);
+ if (newX == NULL)
+ {
+ CloseSys(newsys);
+ ERROR(3);
+ }
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (ContSetO(set, obj))
+ for (i = 0, next++; i < *N; i++)
+ {
+ find = 1;
+ for (j = 0; j < _mainsys->attributes_num; j++)
+ {
+ if (!ContSetA(P, j))
+ continue;
+ if ((*rules + i * size)[j] != GetA(obj, j))
+ if ((*rules + i * size)[j] != MINUS)
+ {
+ find = 0;
+ break;
+ }
+ }
+ if (find)
+ newX[next * newsys->setAsize + i / _cluster_bits] |=
+ _mask[i % _cluster_bits];
+ }
+ UseSys(newsys);
+ newsys->matX = newX;
+ newsys->matXsize = newXsize;
+ newfull = InitFullSetA();
+ InitX(newsys, newfull, newfull, MATX);
+ free(newX);
+ CloseSetA(newfull);
+ if (_rerror != 0)
+ {
+ free(newsys);
+ UseSys(oldsys);
+ ERROR(3);
+ }
+ if (option == BESTOPT)
+ {
+ i = Red(&red, MATX);
+ j = SelectOneShort(red, i);
+ }
+ else
+ {
+ red = InitEmptySetA();
+ RedOptim(red, MATX);
+ j = 0;
+ }
+ newN = CardSetA(red + j * newsys->setAsize);
+ newrules = (value_type *) malloc(newN * oldsys->attributes_num *
+ sizeof(value_type));
+ if (newrules == NULL)
+ {
+ CloseSys(newsys);
+ UseSys(oldsys);
+ ERROR(3);
+ }
+ for (i = 0, obj = 0; i < *N; i++)
+ if (ContSetA(red + j * newsys->setAsize, i))
+ memcpy(newrules + obj++ * oldsys->attributes_num,
+ *rules + i * oldsys->attributes_num,
+ oldsys->attributes_num * sizeof(value_type));
+ if (option == BESTOPT)
+ free(red);
+ else
+ CloseSetA(red);
+ if (*N > 0)
+ free(*rules);
+ *N = newN;
+ *rules = newrules;
+ UseSys(oldsys);
+ CloseSys(newsys);
+ return 0;
+}
+
+int BestRules(value_type ** rules, setA P, setA Q, int matrix_type)
+{
+ int i, obj, num = 0, /* total number of rules */
+ classnum = 0; /* number of rules for class */
+ setO processed, /* already covered objects */
+ newclass; /* class */
+ value_type *classrules = NULL; /* rules for a single class */
+
+ *rules = (value_type *) malloc(Asize(_mainsys) * sizeof(value_type));
+ if (*rules == NULL)
+ ERROR(3);
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ processed = InitEmptySetO();
+ newclass = InitEmptySetO();
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (!ContSetO(processed, obj))
+ {
+ ClassSetO(newclass, obj, Q);
+ classnum = 0;
+ OrSetO(processed, processed, newclass);
+ classnum =
+ BestRulesForClass(&classrules, newclass, P, Q, matrix_type);
+ for (i = 0; i < classnum; i++)
+ AddRule(*rules, &num,
+ classrules + i * _mainsys->attributes_num);
+ if (classnum > 0)
+ free(classrules);
+ classrules = NULL;
+ }
+ CloseSetO(processed);
+ CloseSetO(newclass);
+ if ((*rules = (value_type *) realloc(*rules,
+ num * _mainsys->attributes_num *
+ sizeof(value_type))) == NULL)
+ ERROR(3);
+ return num;
+}
+
+
+int BestRulesForClass(value_type ** rules, setO set, setA P, setA Q,
+ int matrix_type)
+{
+ int n, attr, obj, red, num = 0; /* total number of rules */
+ int size = _mainsys->setAsize;
+ setA fewred = NULL, /* selected reducts */
+ allred = NULL; /* all reducts */
+ value_type *rule = NULL; /* single rule */
+
+ *rules = NULL;
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (ContSetO(set, obj))
+ {
+ if (InitXforObject(_mainsys, obj, P, Q, matrix_type) < 0)
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ else if (!MatExist(_mainsys, MATX))
+ continue;
+ n = Red(&allred, MATX);
+ CloseMat(_mainsys, MATX);
+ if (n > 0)
+ {
+ n = SelectAllShort(&fewred, allred, n);
+ free(allred);
+ }
+ else
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ (n +
+ num) * _mainsys->attributes_num * sizeof(value_type))) ==
+ NULL)
+ {
+ free(rule);
+ free(fewred);
+ return (-_rerror);
+ }
+ for (red = 0; red < n; red++)
+ {
+ for (attr = _mainsys->attributes_num - 1; attr >= 0; attr--)
+ if (ContSetA(fewred + red * size, attr) ||
+ ContSetA(Q, attr))
+ rule[attr] = GetA(obj, attr);
+ else
+ rule[attr] = MINUS;
+ AddRule(*rules, &num, rule);
+ }
+ }
+ if (n > 0)
+ free(fewred);
+ free(rule);
+ if (SelectRules(rules, &num, set, P, BESTOPT) < 0)
+ return (-_rerror);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ num * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return num;
+}
+
+
+int Rules(value_type ** rules, setA P, setA Q, int matrix_type)
+{
+ int i, obj, num = 0, /* total number of rules */
+ classnum = 0; /* number of rules for class */
+ setO processed, /* already covered objects */
+ newclass; /* class */
+ value_type *rule = NULL, /* single rule */
+ *classrules = NULL; /* rules for class */
+
+ *rules = (value_type *) malloc(Asize(_mainsys) * sizeof(value_type));
+ if (*rules == NULL)
+ ERROR(3);
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ processed = InitEmptySetO();
+ newclass = InitEmptySetO();
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (!ContSetO(processed, obj))
+ {
+ ClassSetO(newclass, obj, Q);
+ classnum = 0;
+ OrSetO(processed, processed, newclass);
+ classnum =
+ RulesForClass(&classrules, newclass, P, Q, matrix_type);
+ for (i = 0; i < classnum; i++)
+ AddRule(*rules, &num,
+ classrules + i * _mainsys->attributes_num);
+ if (classnum > 0)
+ free(classrules);
+ classrules = NULL;
+ }
+ free(rule);
+ CloseSetO(processed);
+ CloseSetO(newclass);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ num * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return num;
+}
+
+
+int RulesForClass(value_type ** rules, setO set, setA P, setA Q,
+ int matrix_type)
+{
+ int j, n, obj, red, num = 0; /* total number of rules */
+ int size = _mainsys->setAsize;
+ setA fewred = NULL, /* selected reducts */
+ allred = NULL; /* all reducts */
+ value_type *rule = NULL; /* single rule */
+
+ *rules = NULL;
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (ContSetO(set, obj))
+ {
+ if (InitXforObject(_mainsys, obj, P, Q, matrix_type) < 0)
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ else if (!MatExist(_mainsys, MATX))
+ continue;
+ n = Red(&allred, MATX);
+ CloseMat(_mainsys, MATX);
+ if (n > 0)
+ {
+ n = SelectAllShort(&fewred, allred, n);
+ free(allred);
+ }
+ else
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ (n +
+ num) * _mainsys->attributes_num * sizeof(value_type))) ==
+ NULL)
+ {
+ free(rule);
+ free(fewred);
+ return (-_rerror);
+ }
+ for (red = 0; red < n; red++)
+ {
+ for (j = _mainsys->attributes_num - 1; j >= 0; j--)
+ if (ContSetA(fewred + red * size, j) || ContSetA(Q, j))
+ rule[j] = GetA(obj, j);
+ else
+ rule[j] = MINUS;
+ AddRule(*rules, &num, rule);
+ }
+ }
+ if (n > 0)
+ free(fewred);
+ free(rule);
+ if (SelectRules(rules, &num, set, P, FASTOPT) < 0)
+ return (-_rerror);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ num * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return num;
+}
+
+
+int FastRules(value_type ** rules, setA P, setA Q, int matrix_type)
+{
+ int obj, attr, n, red, count = 0;
+ int size = _mainsys->setAsize;
+ setA reducts = NULL;
+ value_type *rule = NULL;
+
+ *rules = (value_type *) malloc(Asize(_mainsys) * sizeof(value_type));
+ if (*rules == NULL)
+ ERROR(3);
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ {
+ if (InitXforObject(_mainsys, obj, P, Q, matrix_type) < 0)
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ else if (!MatExist(_mainsys, MATX))
+ continue;
+ n = Red(&reducts, MATX);
+ CloseMat(_mainsys, MATX);
+ red = SelectOneShort(reducts, n);
+ for (attr = _mainsys->attributes_num - 1; attr >= 0; attr--)
+ if (ContSetA(reducts + red * size, attr) || ContSetA(Q, attr))
+ rule[attr] = GetA(obj, attr);
+ else
+ rule[attr] = MINUS;
+ AddRule(*rules, &count, rule);
+ if (n > 0)
+ free(reducts);
+ reducts = NULL;
+ }
+ free(rule);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ count * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return count;
+}
+
+
+int VeryFastRules(value_type ** rules, setA P, setA Q, int matrix_type)
+{
+ int obj, attr, count = 0;
+ setA reduct;
+ value_type *rule = NULL;
+
+ *rules = (value_type *) malloc(Asize(_mainsys) * sizeof(value_type));
+ if (*rules == NULL)
+ ERROR(3);
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ reduct = InitEmptySetA();
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ {
+ if (InitXforObject(_mainsys, obj, P, Q, matrix_type) < 0)
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ else if (!MatExist(_mainsys, MATX))
+ continue;
+ RedOptim(reduct, MATX);
+ CloseMat(_mainsys, MATX);
+ for (attr = _mainsys->attributes_num - 1; attr >= 0; attr--)
+ if (ContSetA(reduct, attr) || ContSetA(Q, attr))
+ rule[attr] = GetA(obj, attr);
+ else
+ rule[attr] = MINUS;
+ AddRule(*rules, &count, rule);
+ }
+ free(rule);
+ CloseSetA(reduct);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ count * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return count;
+}
+
+
+int ApprRules(value_type ** rules, setA P, setA Q, int option,
+ int matrix_type)
+{
+ int i, obj, num = 0, /* total number of rules */
+ classnum = 0; /* number of rules for class */
+ setO processed, /* already covered objects */
+ newclass; /* class */
+ value_type *rule = NULL, /* single rule */
+ *classrules = NULL; /* rules for class */
+
+ *rules = (value_type *) malloc(Asize(_mainsys) * sizeof(value_type));
+ if (*rules == NULL)
+ ERROR(3);
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ processed = InitEmptySetO();
+ newclass = InitEmptySetO();
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (!ContSetO(processed, obj))
+ {
+ ClassSetO(newclass, obj, Q);
+ classnum = 0;
+ OrSetO(processed, processed, newclass);
+ classnum = ApprRulesForClass(&classrules, newclass, P, Q,
+ option, matrix_type);
+ for (i = 0; i < classnum; i++)
+ AddRule(*rules, &num,
+ classrules + i * _mainsys->attributes_num);
+ if (classnum > 0)
+ free(classrules);
+ classrules = NULL;
+ }
+ free(rule);
+ CloseSetO(processed);
+ CloseSetO(newclass);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ num * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return num;
+}
+
+
+
+int ApprRulesForClass(value_type ** rules, setO set, setA P, setA Q,
+ int option, int matrix_type)
+{
+ int j, n, obj, red, num = 0; /* total number of rules */
+ int size = _mainsys->setAsize;
+ setA fewred = NULL, /* selected reducts */
+ allred = NULL; /* all reducts */
+ value_type *rule = NULL; /* single rule */
+ setO aclass;
+
+ *rules = NULL;
+ if (!MatExist(_mainsys, MATA))
+ ERROR(5);
+ rule =
+ (value_type *) malloc(_mainsys->attributes_num * sizeof(value_type));
+ if (rule == NULL)
+ ERROR(3);
+ aclass = InitEmptySetO();
+ if (option == LOWER)
+ LowAppr(aclass, set, P, matrix_type);
+ else if (option == UPPER)
+ UppAppr(aclass, set, P, matrix_type);
+ else
+ CopySetO(aclass, set);
+ for (obj = 0; obj < _mainsys->objects_num; obj++)
+ if (ContSetO(aclass, obj))
+ {
+ if (InitXforObjectFromClass(_mainsys, obj, P, aclass, matrix_type)
+ < 0)
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ else if (!MatExist(_mainsys, MATX))
+ continue;
+ n = Red(&allred, MATX);
+ CloseMat(_mainsys, MATX);
+ if (n > 0)
+ {
+ n = SelectAllShort(&fewred, allred, n);
+ free(allred);
+ }
+ else
+ {
+ free(rule);
+ return (-_rerror);
+ }
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ (n +
+ num) * _mainsys->attributes_num * sizeof(value_type))) ==
+ NULL)
+ {
+ free(rule);
+ free(fewred);
+ return (-_rerror);
+ }
+ for (red = 0; red < n; red++)
+ {
+ for (j = _mainsys->attributes_num - 1; j >= 0; j--)
+ if (ContSetA(fewred + red * size, j) || ContSetA(Q, j))
+ rule[j] = GetA(obj, j);
+ else
+ rule[j] = MINUS;
+ AddRule(*rules, &num, rule);
+ }
+ }
+ if (n > 0)
+ free(fewred);
+ free(rule);
+ if (SelectRules(rules, &num, set, P, FASTOPT) < 0)
+ return (-_rerror);
+ if ((*rules = (value_type *) realloc
+ (*rules,
+ num * _mainsys->attributes_num * sizeof(value_type))) == NULL)
+ ERROR(3);
+ return num;
+}
Added: grass-addons/grass6/raster/r.roughset/rule2.h
===================================================================
--- grass-addons/grass6/raster/r.roughset/rule2.h (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rule2.h 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,110 @@
+
+/****************************************************************************
+ *
+ * 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.
+ *
+ *****************************************************************************/
+
+/*** ***/
+
+/*** SOME MORE QUERIES FOR SYSTEM ***/
+
+/*** ( FINDING RULES ) ***/
+
+/*** ***/
+
+/*** part of the RSL system written by M.Gawrys J.Sienkiewicz ***/
+
+/*** ***/
+
+/*****************************************************************************/
+
+#define BESTOPT 0
+#define FASTOPT 1
+
+#define NORMAL 0
+#define LOWER 1
+#define UPPER 2
+
+int AllRules(value_type ** rules, setA P, setA Q, int matrix_type);
+
+ /* finds all possible rules for condition attributes P */
+ /* and decision Q, allocates memory, returns number of rules */
+
+int AllRulesForReducts(value_type ** rules, cluster_type * reducts,
+ int N, setA Q, int matrix_type);
+ /* finds all possible rules for each reduct separatly */
+ /* allocates memory, returns number of rules */
+
+int SelectRules(value_type ** rules, int *N, setO set, setA P, int option);
+
+ /* reduce set of rules to cover only some objects */
+ /* on attributes P, reallocates memory and decrease N */
+ /* option: FASTOPT for shortcomings */
+ /* BESTOPT for optimal computing */
+
+int BestRules(value_type ** rules, setA P, setA Q, int matrix_type);
+
+ /* finds minimal set of rules, allocates memory */
+ /* P - condition attributes; Q - decision attributes */
+ /* returns number of rules */
+
+int BestRulesForClass(value_type ** rules, setO set, setA P, setA Q,
+ int matrix_type);
+ /* finds minimal set of rules to cover objects from set */
+ /* P - condition attributes; Q - decision attributes */
+ /* allocates memory, returns number of rules */
+
+int Rules(value_type ** rules, setA P, setA Q, int matrix_type);
+
+ /* finds set of rules, allocates memory */
+ /* P - condition attributes; Q - decision attributes */
+ /* returns number of rules */
+
+int RulesForClass(value_type ** rules, setO set, setA P, setA Q,
+ int matrix_type);
+ /* finds set of rules to cover objects from set */
+ /* P - condition attributes; Q - decision attributes */
+ /* allocates memory, returns number of rules */
+
+int FastRules(value_type ** rules, setA P, setA Q, int matrix_type);
+
+ /* finds quickly set of rules, allocates memory */
+ /* P - condition attributes; Q - decision attributes */
+ /* returns number of rules */
+
+int VeryFastRules(value_type ** rules, setA P, setA Q, int matrix_type);
+
+ /* finds very quickly set of rules, allocates memory */
+ /* P - condition attributes; Q - decision attributes */
+ /* returns number of rules */
+
+int ApprRules(value_type ** rules, setA P, setA Q, int option,
+ int matrix_type);
+ /* finds set of rules for approximated classes */
+ /* P - condition attributes; Q - decision attributes */
+ /* option: LOWER - lower approximation - certain rules */
+ /* UPPER - upper approximation - possible rules */
+ /* NORMAL - no approximation - normal rules */
+ /* allocates memory, returns number of rules */
+
+int ApprRulesForClass(value_type ** rules, setO set, setA P, setA Q,
+ int option, int matrix_type);
+ /* finds set of rules for approximated class (set) */
+ /* P - condition attributes; Q - decision attributes */
+ /* option: LOWER - lower approximation - certain rules */
+ /* UPPER - upper approximation - possible rules */
+ /* NORMAL - no approximation - normal rules */
+ /* allocates memory, returns number of rules */
Added: grass-addons/grass6/raster/r.roughset/rules_extr.c
===================================================================
--- grass-addons/grass6/raster/r.roughset/rules_extr.c (rev 0)
+++ grass-addons/grass6/raster/r.roughset/rules_extr.c 2012-11-19 13:37:34 UTC (rev 53911)
@@ -0,0 +1,489 @@
+
+/****************************************************************************
+ *
+ * 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) A.Boggia - G.Massei (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, 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);
+
+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, 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. */
+ 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_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");
+
+ 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", sys1->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. */
+ 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);
+ }
+
+ 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_c_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);
+ G_percent(j, _mainsys->objects_num, 1);
+ }
+ }
+ 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);
+ G_percent(j, _mainsys->objects_num, 1);
+ }
+ }
+ 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);
+ G_percent(j, _mainsys->objects_num, 1);
+ }
+ }
+ break;
+
+ default:
+ 0;
+ 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 in %s",
+ file_out_sys);
+
+ nobject = 0;
+
+ for (row = 0; row < nrows; row++)
+ {
+ for (i = 0; i <= nattribute; i++)
+ {
+ G_get_c_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++;
+ }
+ }
+ G_percent(row, nrows, 1);
+ }
+
+ /************** write code file*************/
+
+ for (i = 0; i <= nattribute; i++)
+ {
+ fprintf(fp, "\n%s", attributes[i].name);
+ }
+
+ /************** write header file*************/
+
+ rewind(fp); /*move file pointer to header file */
+ /* TODO: make a system to detect errors like: G_fatal_error("Not able to write file [%s]",file_out_sys); */
+
+ fprintf(fp, "NAME: %s\nATTRIBUTES: %d\nOBJECTS: %d", 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 */
More information about the grass-commit
mailing list