lgradual_rules_learning.c

Go to the documentation of this file.
00001 /*
00002 *    Copyright (c) 2007. The BATI team. All right reserved.
00003 *
00004 *    This file is part of BATI library.
00005 *
00006 *    BATI library is free software: you can redistribute it and/or modify
00007 *    it under the terms of the GNU General Public License as published by
00008 *    the Free Software Foundation, either version 3 of the License, or
00009 *    (at your option) any later version.
00010 *
00011 *    BATI library  is distributed in the hope that it will be useful,
00012 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *    GNU General Public License for more details.
00015 *
00016 *    You should have received a copy of the GNU General Public License
00017 *    along with BATI library.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019  
00020 
00021 #include <stdio.h>
00022 #include <stdarg.h>
00023 #include <math.h>
00024 #include "image.h"
00025 #include "proto2D.h"
00026 #include "gradual_rules_learning.h"
00027 //#include "ff_symb_copy.h"
00028 #include "classifier.h"
00029 
00030 double minim (double a, double b)
00031 {
00032   if ((a < b) || (a <= b))
00033     return a;
00034   return b;
00035 }
00036 
00037 double maxim (double a, double b)
00038 {
00039   if ((a > b) || (a >= b))
00040     return a;
00041   return b;
00042 }
00043 
00044 int sum (int i, lgr_d id)
00045 {
00046   int j, a = 0;
00047   for (j = 0; j < i; j++)
00048     a = a + id.nb_pts[j];
00049   return (a);
00050 }
00051 
00052 int intersch (double *a, double *b)
00053 {double x;
00054   x = *a;
00055   *a = *b;
00056   *b = x;
00057   return(0);
00058 }
00059 
00060 double semn(double a)
00061 {
00062   if (a == 0)
00063     return 0.0;
00064   if (a < 0)
00065     return -1.0;
00066   return 1.0;
00067 }
00068 
00069 
00103 param *learning_gradual_rules_lect(lgr_d *a, param *ptp, char *debq)
00104 {
00105   char question[500];
00106   int i, j;
00107 
00108     sprintf(question, "%s Data file containing the learning points", debq);
00109     lec_param(question, ptp);
00110     a->fisin = ptp->rep;
00111     ptp = ptp->next;
00112 
00113     sprintf(question, "%s Does it come from the preprocessing stage? (0/1)", debq);
00114     lec_param(question, ptp);
00115     a->isProcessed = atoi(ptp->rep);
00116     ptp = ptp->next;
00117 
00118     sprintf(question, "%s File for the coordinates of the corners", debq);
00119     lec_param(question, ptp);
00120     a->fisout1 = ptp->rep;
00121     ptp = ptp->next;
00122 
00123     sprintf(question, "%s File for the obtained rules", debq);
00124     lec_param(question, ptp);
00125     a->fisout2 = ptp->rep;
00126     ptp = ptp->next;
00127 
00128     sprintf(question, "%s Number of attributes in the curent file", debq); 
00129     lec_param(question, ptp);
00130     a->nb_attr = atoi(ptp->rep);
00131     ptp = ptp->next;
00132 
00133     if (a->isProcessed == 0)
00134       {
00135         sprintf(question, "%s Number of classes in the current file", debq);
00136         lec_param(question, ptp);
00137         a->nb_class = atoi(ptp->rep);
00138         ptp = ptp->next;
00139 
00140         a->nb_pts = (int*)malloc(a->nb_class*sizeof(int));
00141 
00142         for (i = 0; i < a->nb_class; i++){
00143           sprintf(question, "%s Number of learning points in class %d", debq, i); 
00144           lec_param(question, ptp);
00145           a->nb_pts[i] = atoi(ptp->rep);
00146           ptp = ptp->next;
00147         }
00148 
00149       }
00150     else 
00151       if (a->isProcessed != 1)
00152         {
00153           printf("Please confirm that the data file comes from the preprocessing stage or not\n");
00154           exit(1);
00155         }
00156       else a->nb_class = 0;
00157 
00158 //    printf("Am terminat lect\n");
00159 
00160     return(ptp);
00161 }
00162 
00163 
00165 int learning_gradual_rules_init (lgr_d *id)
00166 {
00167   int i;
00168 
00169   if (id->isProcessed)
00170     id->nb_pts = (int*)malloc(id->nb_class*sizeof(int));
00171 
00172  return(0);
00173 }
00174 
00175 
00176 
00177 double isleft(elem_contour *p, elem_contour *q)
00178 {
00179 
00180   return ( - (q->x - p->x)*(q->next->y - p->y) + (q->next->x - p->x)*(q->y - p->y));
00181 
00182 }
00183 
00187 elem_contour *conv_hull(elem_contour *first)
00188 {
00189 
00190   elem_contour *current, *interm, *firstf;
00191   int k = 0, m, f;
00192   
00193 
00194   interm = first;
00195   current = first->next;
00196 
00197   while (current != first)
00198     {
00199       if (current->y < interm->y)
00200         interm = current;
00201       else
00202         if ((current->y == interm->y) && (current->x > interm->x))
00203           interm = current;
00204       current = current->next;
00205     }
00206 
00207   first = interm;
00208 
00209   while (!k)
00210     {
00211       k = 1;
00212       current = first->next;
00213       while (current->next != first)
00214         {
00215           if (isleft(first, current) < 0)
00216             {
00217               intersch(&current->x, &current->next->x);
00218               intersch(&current->y, &current->next->y);
00219               k = 0;
00220             }
00221           if (!isleft(first, current))
00222             if ((current->y >= current->next->y) || ((current->y == current->next->y) && (current->x <= current->next->x)))
00223               if (current != current->next)
00224               {
00225                 interm = current->next;
00226                 current->next = interm->next;
00227                 current->next->ant = current;
00228                 if (interm == first)
00229                   first = current;
00230                 free(interm);
00231               }
00232             else 
00233               if ((current->y <= current->next->y) || ((current->y == current->next->y) && (current->x >= current->next->x)))
00234                 if (current != current->next)
00235                 {
00236                   interm = current;
00237                   current = current->next;
00238                   current->ant = interm->ant;
00239                   interm->ant->next = current;
00240                   if (interm == first)
00241                     first = current;
00242                   free(interm);
00243                 }
00244           current = current->next;
00245         }
00246     }
00247 
00248 
00249   current = first->next->next;
00250   k = 1;
00251   while (k)
00252     {
00253       if (current == first)
00254         k = 0;
00255       if (isleft(current->ant->ant, current->ant) <= 0)
00256         {
00257           interm = current->ant;
00258           current->ant = interm->ant;
00259 //        printf("The erased point: %f %f\n", interm->x, interm->y);
00260           interm->ant->next = current;
00261           free(interm);
00262         }
00263       else
00264         current = current->next;
00265     }
00266 
00267   firstf = first;
00268   f = 1;
00269   while (f)
00270     {
00271       current = first;
00272       k = 1;
00273       while (k)
00274         {
00275           m = 1;
00276           if (current->x == first->x && current->y == first->y)
00277             if (current != first)
00278               {
00279                 interm = current;
00280                 current = current->next;
00281                 current->ant = interm->ant;
00282                 interm->ant->next = current;
00283                 free(interm);
00284                 m = 0;
00285               }
00286           current = current->next;
00287           if (current == first)
00288             k = 0;
00289         }
00290       first = first->next;
00291       if (first == firstf)
00292         f = 0;
00293     }
00294 
00295   return first;
00296 
00297 }
00298 
00303 elem_contour *contour_extraction(learning_set iris, int start, int stop, int att1, int att2)
00304 {
00305   elem_contour *first, *current, *new;
00306   elem_contour *firstm;
00307   int i, k, j, m, cont = 1;
00308 
00309 
00310 
00311   current = (elem_contour*)malloc(sizeof(elem_contour));
00312   current->x = iris.input_ref.attributes[att1][start];
00313   current->y = iris.input_ref.attributes[att2][start];
00314   current->next = current;
00315   current->ant = current;
00316   first = current;
00317 
00318   cont = 1;
00319 
00320 
00321   for (j = start; j < stop; j++)
00322     {
00323       k = 1; m = 0;
00324       current = first->next;
00325       while (k)
00326         {
00327           if (current == first)
00328           k = 0;
00329           if (current->y == iris.input_ref.attributes[att2][j])
00330             {
00331               m = 1;
00332               if (current->x < iris.input_ref.attributes[att1][j])
00333                 current->x = iris.input_ref.attributes[att1][j];
00334             }
00335           current = current->next;
00336         }
00337       if(!m)
00338         {
00339           cont++;
00340           new = (elem_contour*)malloc(sizeof(elem_contour));
00341           new->x = iris.input_ref.attributes[att1][j];
00342           new->y = iris.input_ref.attributes[att2][j];
00343           new->next = current;
00344           new->ant = current->ant;
00345           current->ant->next = new;
00346           current->ant = new;
00347         }
00348     }
00349 
00350 
00351   current = (elem_contour*)malloc(sizeof(elem_contour));
00352   current->x = iris.input_ref.attributes[att1][start];
00353   current->y = iris.input_ref.attributes[att2][start];
00354   current->next = current;
00355   current->ant = current;
00356   firstm = current;
00357 
00358   for (j = start; j < stop; j++)
00359     {
00360       k = 1; m = 0;
00361       current = firstm->next;
00362       while (k)
00363         {
00364           if (current == firstm)
00365             k = 0;
00366           if (current->y == iris.input_ref.attributes[att2][j])
00367             {
00368               m = 1;
00369               if (current->x > iris.input_ref.attributes[att1][j])
00370                 current->x = iris.input_ref.attributes[att1][j];
00371             }
00372           current = current->next;
00373         }
00374       if(!m)
00375         {
00376           new = (elem_contour*)malloc(sizeof(elem_contour));
00377           new->x = iris.input_ref.attributes[att1][j];
00378           new->y = iris.input_ref.attributes[att2][j];
00379           new->next = current;
00380           new->ant = current->ant;
00381           current->ant->next = new;
00382           current->ant = new;
00383         }
00384     }
00385 
00386 
00387   current = first->ant;
00388   new = firstm->ant;
00389   first->ant = new;
00390   firstm->ant = current;
00391   current->next = firstm;
00392   new->next = first;
00393 
00394   firstm = conv_hull(firstm);
00395 
00396 
00397   return(firstm);
00398 }
00399 
00404 int rules_calc(elem_contour *first, lgr_d *id)
00405 {
00406 
00407   elem_contour *current, *interm;
00408   int k, c;
00409   FILE *myfile, *myfilec;
00410   float mx, Mx, my, My;
00411   
00412  if ((myfilec=fopen(id->fisout1, "a")) == NULL) {
00413    printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisout1);
00414         exit(1);
00415  }
00416 
00417  if ((myfile=fopen(id->fisout2, "a")) == NULL) {
00418    printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisout2);
00419         exit(1);
00420  }
00421 
00422  mx = Mx = first->x;
00423  my = My = first->y;
00424  k = 1; c = 0;
00425   current = first;
00426 
00427 
00428   if (isleft(current, current->next) < 0)
00429     while (k)
00430       {
00431         fprintf(myfilec, "%f %f\n", current->x, current->y);
00432         if (mx > current->x)
00433           mx = current->x;
00434         if (Mx < current->x)
00435           Mx = current->x;
00436         if (my > current->y)
00437           my = current->y;
00438         if (My < current->y)
00439           My = current->y;
00440         if (current->x != current->next->x && current->y != current->next->y)
00441           {
00442             if (current->x > current->next->x)
00443               if (current->y > current->next->y)
00444                 fprintf(myfile, "A%d 3 %f %f\nB%d 3 %f %f\n", c, current->next->x, current->x, c, current->next->y, current->y);
00445               else
00446                 fprintf(myfile, "A%d 2 %f %f\nB%d 3 %f %f\n", c, current->next->x, current->x, c, current->y, current->next->y);
00447             else
00448               if (current->y > current->next->y)
00449                 fprintf(myfile, "A%d 3 %f %f\nB%d 2 %f %f\n", c, current->x, current->next->x, c, current->next->y, current->y);
00450               else
00451                 fprintf(myfile, "A%d 2 %f %f\nB%d 2 %f %f\n", c, current->x, current->next->x, c, current->y, current->next->y);
00452             c++;
00453           }
00454         current = current->next;
00455         if (current == first)
00456           k = 0;
00457       }
00458   else
00459     while (k)
00460       {
00461         fprintf(myfilec, "%f %f\n", current->x, current->y);
00462         if (mx > current->x)
00463           mx = current->x;
00464         if (Mx < current->x)
00465           Mx = current->x;
00466         if (my > current->y)
00467           my = current->y;
00468         if (My < current->y)
00469           My = current->y;
00470         if (current->x != current->next->x && current->y != current->next->y)
00471           {
00472             if (current->x > current->next->x)
00473               if (current->y > current->next->y)
00474                 fprintf(myfile, "A%d 2 %f %f\nB%d 2 %f %f\n", c, current->next->x, current->x, c, current->next->y, current->y);
00475               else
00476                 fprintf(myfile, "A%d 3 %f %f\nB%d 2 %f %f\n", c, current->next->x, current->x, c, current->y, current->next->y);
00477             else
00478               if (current->y > current->next->y)
00479                 fprintf(myfile, "A%d 2 %f %f\nB%d 3 %f %f\n", c, current->x, current->next->x, c, current->next->y, current->y);
00480               else
00481                 fprintf(myfile, "A%d 3 %f %f\nB%d 3 %f %f\n", c, current->x, current->next->x, c, current->y, current->next->y);
00482             c++;
00483           }
00484         current = current->next;
00485         if (current == first)
00486           k = 0;
00487       }
00488 
00489   fprintf(myfile, "A%d 5 %f %f\nB%d 0\n", c, mx, Mx, c);
00490   c++;
00491   fprintf(myfile, "A%d 1\nB%d 4 %f %f\n", c, c, my, My);
00492 
00493   fprintf(myfile, "*************************\n");
00494   fprintf(myfilec, "*************************\n");
00495 
00496   fclose(myfile);
00497   fclose(myfilec);
00498 
00499 
00500 
00501   return 0;
00502 }
00503 
00504 int learning_set_process(learning_set ls, lgr_d *id, int class)
00505 {
00506   int atr1, atr2, i, k, m;
00507   int nb_points, a;
00508   elem_contour *conv, *current, *interm;
00509   float p, n;
00510   FILE *myfile;
00511 
00512 
00513 
00514 
00515 
00516 //this file will contain the obtained rules: for each pair of attributes the rules for 1st attribute 1st class, 2nd attribute 1st class, 1st attribute 2nd class and so on
00517  if ((myfile=fopen(id->fisout2, "a")) == NULL) {
00518    printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisout2);
00519         exit(1);
00520  }
00521 
00522  fprintf(myfile, "\n****%d****\n", class);
00523 
00524  fclose(myfile);
00525 
00526 //in this file will be the points that define the surface corresponding to one class - for all the existing classes
00527  if ((myfile=fopen(id->fisout1, "a")) == NULL) {
00528    printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisout1);
00529         exit(1);
00530  }
00531 
00532  fprintf(myfile, "\n****%d****\n", class);
00533 
00534  fclose(myfile);
00535 
00536   for (atr1 = 0; atr1 < (ls.input_ref.nb_attr)-1; atr1++)
00537     for (atr2 = atr1+1; atr2 < ls.input_ref.nb_attr; atr2++)
00538       {
00539         nb_points = 0;
00540         for (i = 0; i < ls.output_ref.nb_class; i++)
00541           {
00542            /*  printf("Setul pe care lucrez:\n"); */
00543 /*          for (k = nb_points; k < nb_points + id->nb_pts[i]; k++) */
00544 /*            printf("%f %f\n", ls.input_ref.attributes[atr1][k], ls.input_ref.attributes[atr2][k]); */
00545 //          printf("Deci convex hull:\n");
00546             conv = contour_extraction(ls, nb_points, nb_points+id->nb_pts[i], atr1, atr2);
00547             if (rules_calc(conv, id))
00548               {
00549                 printf("Erreur dans la fonction d'extraction des regles\n");
00550                 exit(1);
00551               }
00552             current = conv;
00553             k = 1;
00554             m = 0;
00555             while (k)
00556               {
00557                 ls.input_ref.attributes[atr1][nb_points+m] = current->x;
00558                 ls.input_ref.attributes[atr2][nb_points+m] = current->y;
00559                 current = current->next;
00560                 //      printf("%f %f\n", current->x, current->y);
00561                 m++;
00562                 if (current == conv)
00563                     k = 0;
00564               }
00565 
00566             nb_points = nb_points + id->nb_pts[i];
00567           }
00568       }
00569 
00570 
00571   return(0);
00572 }
00573 
00574 
00575 int learning_gradual_rules_calc(lgr_d *id)
00576 {
00577   FILE *myfile, *myfile1;
00578   int a, i, j, atr1, atr2, nb_points, k;
00579   learning_set iris;
00580   connex_comp comp;
00581   elem_contour *cont, *conv_h, *current;
00582   double P1, P2, P_conv, convexity;
00583 
00584 
00585 //this file will contain the obtained rules: for each pair of attributes the rules for 1st attribute 1st class, 2nd attribute 1st class, 1st attribute 2nd class and so on
00586  if ((myfile=fopen(id->fisout2, "w")) == NULL) {
00587    printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisout2);
00588         exit(1);
00589  }
00590 
00591  fclose(myfile);
00592 
00593 //in this file will be the 4 points that define the surface corresponding to one class - for all the existing classes
00594  if ((myfile=fopen(id->fisout1, "w")) == NULL) {
00595    printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisout1);
00596         exit(1);
00597  }
00598 
00599  fclose(myfile);
00600 
00601  if (id->nb_class > 0)
00602    {
00603      nb_points = 0;
00604      for (i = 0; i < id->nb_class; i++)
00605        nb_points = nb_points + id->nb_pts[i];
00606 
00607      iris.input_ref.nb_attr = id->nb_attr;
00608      iris.input_ref.nb_pts = nb_points;
00609      iris.output_ref.nb_class = id->nb_class;
00610      iris.output_ref.nb_pts = nb_points;
00611      iris.output_ref.type = CRISP_CHOICE;
00612 
00613 
00614      if ((myfile=fopen(id->fisin, "r")) == NULL) {
00615        printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisin);
00616        exit(1);
00617      }
00618 
00619      if ((a = alloc_classifier_data_input_completely(&iris.input_ref)) != 0)
00620        printf("Probleme d'allocation memoire pour les donnees d'entree\n");
00621 
00622      a = alloc_classifier_data_output(&iris.output_ref);
00623 
00624      for (i = 0; i < nb_points; i++)
00625        {
00626          for (j = 0; j < id->nb_attr; j++)
00627            {
00628              fscanf(myfile, "%f", &iris.input_ref.attributes[j][i]);
00629              //    printf("%f ", iris.input_ref.attributes[j][i]); 
00630            }
00631          fscanf(myfile, "%d", &a);
00632          //      printf("\n");
00633        }
00634 
00635      fclose(myfile);
00636      myfile = fopen(id->fisout1, "a");
00637      fprintf(myfile, "0 1\n");
00638      fclose(myfile);
00639      myfile = fopen(id->fisout2, "a");
00640      fprintf(myfile, "0 1\n");
00641      fclose(myfile);
00642 
00643      learning_set_process(iris, id, -1);
00644 
00645    }
00646  else
00647    {
00648      if((myfile = fopen(id->fisin, "r")) == NULL)
00649        {
00650          printf("ERREUR: impossible d'ouvrir le fichier %s\n", id->fisin);
00651          exit(1);
00652        }
00653      fscanf(myfile, "%d", &id->nb_class);
00654      for (atr1 = 0; atr1 < id->nb_attr - 1; atr1++)
00655        for (atr2 = atr1+1; atr2 < id->nb_attr; atr2++)
00656          {
00657            fscanf(myfile, "%d %d", &i, &i);
00658            for (k = 0; k < id->nb_class; k++)
00659              {
00660                fscanf(myfile, "%d", &iris.output_ref.nb_class);
00661                iris.input_ref.nb_pts = iris.output_ref.nb_pts = 0;
00662                id->nb_pts = (int*)malloc(iris.output_ref.nb_class*sizeof(int));
00663                for (i = 0; i < iris.output_ref.nb_class; i++)
00664                  {
00665                    fscanf(myfile, "%d", &id->nb_pts[i]);
00666                    iris.input_ref.nb_pts = iris.output_ref.nb_pts = iris.output_ref.nb_pts + id->nb_pts[i];
00667                  }
00668                iris.input_ref.nb_attr = 2;
00669                iris.output_ref.type = CRISP_CHOICE;
00670                if ((a = alloc_classifier_data_input_completely(&iris.input_ref)) != 0)
00671                  printf("Probleme d'allocation memoire pour les donnees d'entree\n");
00672 
00673                if ((a = alloc_classifier_data_output(&iris.output_ref)) != 0)
00674                  printf("Probleme d'allocation memoire pour les donnees d'entree\n");
00675 
00676 //             printf("%d\n", iris.input_ref.nb_pts);
00677 
00678                for (i = 0; i < iris.input_ref.nb_pts; i++)
00679                  fscanf(myfile, "%f %f", &iris.input_ref.attributes[0][i], &iris.input_ref.attributes[1][i]);
00680 
00681                myfile1 = fopen(id->fisout1, "a");
00682                fprintf(myfile1, "\n%d %d", atr1, atr2);
00683                fclose(myfile1);
00684 
00685                myfile1 = fopen(id->fisout2, "a");
00686                fprintf(myfile1, "\n%d %d", atr1, atr2);
00687                fclose(myfile1);
00688 
00689                learning_set_process(iris, id, k);
00690                free(id->nb_pts);
00691                free_data_input(&iris.input_ref);
00692                free_data_output(&iris.output_ref);
00693              }
00694          }
00695      fclose(myfile);
00696    }
00697 
00698 
00699 
00700   return(0);
00701 }
00702 
00703 int main(int argc, char *argv[]){
00704   lgr_d id;
00705   param par0, *ptp;
00706 
00707 
00708 
00709   param_debut(argc, argv, &par0);
00710   ptp = &par0;
00711   ptp = learning_gradual_rules_lect(&id, ptp, "");
00712   param_fin(argc, argv, &par0);
00713 
00714   learning_gradual_rules_init(&id);
00715 
00716 
00717   if (learning_gradual_rules_calc(&id) != 0)
00718     {
00719       printf("Erreur dans la fonction de calcul\n");
00720       exit(1);
00721     }
00722 
00723  return(0);
00724 
00725 
00726 }

Generated on Tue Apr 22 13:31:02 2008 for donnee1D by  doxygen 1.5.3