Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <math.h>
  4. #define MAX 2500
  5. #define MAX_CENT 20
  6.  
  7. double eucladiana(double centroid[][MAX_CENT], int nCentroid, double objects[][MAX_CENT], int nObject, int nAtributos){
  8.     double resp = 0.0;
  9.     double resta = 0.0;
  10.     int i, j;
  11.     for(i = 0; i < nAtributos; i++){
  12.         resta = centroid[nCentroid][i] - objects[nObject][i];
  13.         resp += pow(resta, 2);
  14.     }
  15.     return sqrt(resp);
  16. }
  17.  
  18. void calculaDistancias(double distancias[][MAX], double objects[][MAX_CENT], int nObjects, double centroid[][MAX_CENT], int fil, int col){
  19.     for(int i=0; i<fil; i++)      
  20.         for(int j=0; j<nObjects; j++)
  21.             distancias[i][j] = eucladiana(centroid, i, objects, j, col);    
  22. }
  23.  
  24. void clustering(int newCluster[][MAX], int nClusters, int nObjects, double distancias[][MAX]){
  25.     bool menor = false;
  26.     int i, j, k, indice;
  27.     for(i=0; i<nObjects; i++){
  28.         for(j=0; j<nClusters-1; j++){
  29.             for(k=j+1; k<nClusters; k++){
  30.                 if (distancias[j][i] < distancias[k][i]){
  31.                     indice = j;                
  32.                     menor = true;
  33.                 }
  34.                 else{
  35.                     menor = false;
  36.                     break;
  37.                 }
  38.             }
  39.             if (menor == true) break;
  40.             else {
  41.                 indice = k;
  42.             }
  43.         }
  44.         newCluster[indice][i] = 1;
  45.     }
  46. }
  47.  
  48. void copyCluster(int copy[][MAX],int original[][MAX],int filas,int columnas){
  49.     for(int i=0;i<filas;i++){
  50.         for(int j=0; j<columnas;j++){
  51.             copy[i][j] = original[i][j];
  52.         }
  53.     }
  54. }
  55.  
  56. void asignaNuevoCentroide(double centroid[][MAX_CENT],double objects[][MAX_CENT],int oldCluster[][MAX],int nClusters,int nObjects){
  57.     double sumaX = 0,sumaY = 0,numDatos = 0;
  58.     int i, j;
  59.     for(i=0;i<nClusters;i++){
  60.         for(j=0;j<nObjects;j++){
  61.             if(oldCluster[i][j] == 1){
  62.                 numDatos++;
  63.                 sumaX += objects[j][0]; //X
  64.                 sumaY += objects[j][1]; //Y            
  65.             }
  66.         }
  67.         centroid[i][0]= sumaX/numDatos;
  68.         centroid[i][1]= sumaY/numDatos;
  69.         sumaX =0;sumaY=0;numDatos =0 ;
  70.     }
  71. }
  72.  
  73. int clusterDiferente(int newCluster[][MAX], int oldCluster[][MAX],int nClusters,int nObjects){
  74.     int diferentes = 0;
  75.     for(int i=0; i<nClusters;i++){
  76.         for(int j=0;j<nObjects;j++)
  77.             if(newCluster[i][j] != oldCluster[i][j])
  78.                 return 1;                    
  79.     }
  80.     return 0;
  81. }
  82.  
  83. void inicializaCluster(int cluster[][MAX], int nClusters, int nObjects){
  84.     for(int i=0; i < nClusters; i++)
  85.         for(int j=0; j < nObjects; j++)
  86.             cluster[i][j] = 0;
  87.    
  88. }
  89.  
  90. void imprimeCluster(int cluster[][MAX], int nClusters, int nObjects){
  91.     for (int a = 0; a < nClusters; a++){
  92.         for (int b = 0; b < nObjects; b++)
  93.             printf("%d ", cluster[a][b]);
  94.         printf("\n");
  95.     }
  96.     printf("\n");
  97.    
  98. }
  99.  
  100. void imprimeCentroid(double centroid[][MAX_CENT], int nClusters, int nAtributos){
  101.     for(int i=0; i < nClusters; i++){
  102.         for(int j=0; j < nAtributos; j++)
  103.             printf("%.2lf ", centroid[i][j]);
  104.         printf("\n");
  105.     }
  106.     printf("\n");
  107.    
  108. }
  109. int main(){
  110.     int n = 2;
  111.     int k = 2;
  112.     int i = 0, j = 0;
  113.     int atributo;        
  114.     char nombreArchivo[100];
  115.     FILE *archivo;
  116.     scanf("%s",nombreArchivo);
  117.     archivo = fopen(nombreArchivo , "r");
  118.     scanf("%d",&k);
  119.     scanf("%d",&n);
  120.     int oldCluster[MAX_CENT][MAX] = {0,0}, newCluster[MAX_CENT][MAX] = {0,0};
  121.     double objects[MAX][MAX_CENT] = {0,0}, centroid[MAX][MAX_CENT] = {0,0}, distancias[MAX_CENT][MAX] = {0.0,0.0};
  122.     while(fscanf(archivo,"%d,", &atributo) != EOF){
  123.         objects[i][j] = atributo;        
  124.         j++;
  125.         if (j == n){
  126.             i++;
  127.             j=0;
  128.         }
  129.     }
  130.     int nObjects = i;    
  131.     //Si el numero de centroides es mayor que el numero de objetos no se puede.      
  132.     for(int i=0; i<k; i++)
  133.         for(int j=0; j<n; j++)
  134.             centroid[i][j] = objects[i][j];
  135.     calculaDistancias(distancias, objects, nObjects, centroid, k, n);  
  136.     clustering(newCluster, k, nObjects, distancias);
  137.     while(clusterDiferente(newCluster, oldCluster, k, nObjects)){              
  138.         copyCluster(oldCluster, newCluster, k, nObjects);
  139.         imprimeCluster(oldCluster, k, nObjects);
  140.         asignaNuevoCentroide(centroid, objects, oldCluster, k, nObjects);                    
  141.         calculaDistancias(distancias, objects, nObjects, centroid, k, n);
  142.         inicializaCluster(newCluster, k, nObjects);
  143.         clustering(newCluster, k, nObjects, distancias);
  144.         imprimeCluster(newCluster, k, nObjects);
  145.     }
  146.     imprimeCentroid(centroid, k, n);
  147.     int centro;
  148. FILE *salida;
  149.     salida= fopen("out.csv","w");
  150.     for(int a = 0 ; a<nObjects;a++ ){
  151.         for(int b = 0;b<n;b++){
  152.             fprintf(salida,"%.0lf,",objects[a][b]);
  153.         }
  154.        
  155.        for(int c = 0 ; c<k;c++){
  156.            if(newCluster[c][a]==1){
  157.                centro = c;
  158.                break;
  159.            }
  160.        }
  161.         fprintf(salida,"%d\n",centro);
  162.     }
  163.     // Falta de Tume el Csv
  164.     /*for (int a = 0; a < k; a++){
  165.         for (int b = 0; b < n; b++)
  166.             printf("%.2lf ", centroid[a][b]);
  167.         printf("\n");
  168.     }
  169.     printf("\n");
  170.     for (int a = 0; a < k; a++){
  171.         for (int b = 0; b < nObjects; b++)
  172.             printf("%.2lf ", distancias[a][b]);
  173.         printf("\n");
  174.     }
  175.     printf("\n");
  176.     for (int a = 0; a < k; a++){
  177.         for (int b = 0; b < nObjects; b++)
  178.             printf("%d ", newCluster[a][b]);
  179.         printf("\n");
  180.     }*/
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement