dooooooduo

CI_ASS3_Vector Quantilization_C

Feb 17th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. #define VECTORSIZE 2
  7.  
  8. int randInt (int min, int max) {
  9.     return (rand() % (max - min + 1)) + min;
  10. }
  11.  
  12. struct vec2 {
  13.     double x;
  14.     double y;
  15. };
  16.  
  17. struct vec2 vectorPlus (struct vec2 a, struct vec2 b) {
  18.     a.x += b.x;
  19.     a.y += b.y;
  20.     return a;
  21. }
  22.  
  23. struct vec2 vectorSubtract (struct vec2 a, struct vec2 b) {
  24.     a.x -= b.x;
  25.     a.y -= b.y;
  26.     return a;
  27. }
  28.  
  29. struct vec2 vectorScalarMultiply (double scalar, struct vec2 vector) {
  30.     vector.x *= scalar;
  31.     vector.y *= scalar;
  32.     return vector;
  33. }
  34.  
  35. double calculateDistance (struct vec2 a, struct vec2 b) {
  36.     struct vec2 c = vectorSubtract(a, b);
  37.     return sqrt(pow(c.x, 2) + pow(c.y, 2));
  38. }
  39.  
  40. int main() {
  41.  
  42.     srand(time(NULL));
  43.  
  44.     double learning_rate = 0.001;
  45.  
  46.     // Reading the inputs
  47.     struct vec2 inputs[1000];
  48.     struct vec2 input;
  49.     int input_num = 0;
  50.     while (scanf("%lf,%lf\n", &input.x, &input.y) != EOF) {
  51.         inputs[input_num] = input;
  52.         input_num++;
  53.     }
  54.  
  55.     int cluster_size;
  56.     int i = 0;
  57.     double xmin_cost = 99999999;
  58.     double previous_cost = 999999999;
  59.     struct vec2 *xbest_centroids = NULL;
  60.     for (cluster_size = 2; cluster_size < input_num; cluster_size++) {
  61.  
  62.         double min_cost = 9999999;
  63.         struct vec2 best_centroids[cluster_size];
  64.         for (i = 0; i < 100; i++) {
  65.  
  66.             // randIntizing centroids by picking non repeating inputs
  67.             struct vec2 centroids[cluster_size];
  68.             int cluster_index = 0;
  69.             int lookup[input_num];
  70.             int j;
  71.             for (j = 0; j < input_num; j++) lookup[j] = 0;
  72.             for (; cluster_index < cluster_size; ) {
  73.                 int rand_num = randInt(0, input_num);
  74.                 if (lookup[rand_num] == 1) continue;
  75.                 lookup[rand_num] = 1;
  76.                 centroids[cluster_index] = inputs[rand_num];
  77.                 cluster_index++;
  78.             }
  79.  
  80.             // Starting the training process by iterating N times
  81.             double cost = 0;
  82.             for (j = 0; j < 500; j++) {
  83.                 int k;
  84.                 for (k = 0; k < input_num; k++) {
  85.                     // randIntizing an input
  86.                     //struct vec2 rand_input = inputs[randInt(0, input_num)];
  87.                     struct vec2 rand_input = inputs[k];
  88.  
  89.                     // Looking for the nearest centroid
  90.                     double min_distance = 999999;
  91.                     struct vec2 *min_centroid = NULL;
  92.                     for (cluster_index = 0; cluster_index < cluster_size; cluster_index++) {
  93.                         double distance = calculateDistance(rand_input, centroids[cluster_index]);
  94.                         if (distance < min_distance) {
  95.                             min_distance = distance;
  96.                             min_centroid = &centroids[cluster_index];
  97.                         }
  98.                     }
  99.  
  100.                     // Updating the centroids coordinates
  101.                     *min_centroid = vectorPlus(
  102.                         *min_centroid,
  103.                         vectorScalarMultiply(
  104.                             learning_rate,
  105.                             vectorSubtract(rand_input, *min_centroid)
  106.                         )
  107.                     );
  108.  
  109.                     cost += pow(min_distance, 2);
  110.                 }
  111.             }
  112.  
  113.             cost /= input_num * 500.0;
  114.             if (cost < min_cost) {
  115.                 min_cost = cost;
  116.                 for (j = 0; j < cluster_size; j++) {
  117.                     best_centroids[j] = centroids[j];
  118.                 }
  119.             }
  120.         }
  121.  
  122.         printf("%lf\n", previous_cost - min_cost);
  123.         if ((input_num * 500.0) * (previous_cost - min_cost) < 500) break;
  124.         previous_cost = min_cost;
  125.         if (min_cost < xmin_cost) {
  126.             xmin_cost = min_cost;
  127.             xbest_centroids = malloc(cluster_size * VECTORSIZE * sizeof(struct vec2));
  128.             for (i = 0; i < cluster_size; i++) {
  129.                 xbest_centroids[i] = best_centroids[i];
  130.             }
  131.         }
  132.     }
  133.  
  134.     cluster_size--;
  135.     for (i = 0; i < cluster_size; i++) {
  136.         printf("%lf,%lf\n", xbest_centroids[i].x, xbest_centroids[i].y);
  137.     }
  138.  
  139.     return 0;
  140. }
Add Comment
Please, Sign In to add comment