Skip to content
Snippets Groups Projects
agg_fuss.cpp 6.13 KiB
Newer Older
Lukas Rapant's avatar
Lukas Rapant committed
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;

struct my_record {
    char *time;
    int session;
    int speed;
    int gid;
};

struct road_network {
    int gid;
    int type;
    int speed;
};

struct aggregates {
    int gid;
    double speed [100];
    int num;
    int sess [100];

};

float harmonicMean(double arr[], int n) {
    // Declare sum variables and initialize
    // with zero.
    double sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + (double) 1 / arr[i];

    return (double) n / sum;
}

int unique_elements(int arr[], int len) {

    int counted[len], j, n, count, flag;

    counted[0] = arr[0];

    count = 1; /*one element is counted*/

    for (j = 0; j <= len - 1; ++j) {
        flag = 1;
        ;
        /*the counted array will always have 'count' elements*/
        for (n = 0; n < count; ++n) {
            if (arr[j] == counted[n]) {
                flag = 0;
            }
        }
        if (flag == 1) {
            ++count;
            counted[count - 1] = arr[j];
        }
    }
    return count;
}

double r2()
{
    return (double)rand() / (double)RAND_MAX ;
}
double r3()
{
    return ((double)rand() / (double)RAND_MAX )*90;
}
Lukas Rapant's avatar
Lukas Rapant committed
int main(int argc, char *argv[]) {
   char mystring [100];
   char mystring1 [100];
   char *str;
   char *str1;
   printf("%s\n",argv[1]);
   printf("%s\n",argv[2]);

    FILE* my_file = fopen(argv[1], "r");
     if (my_file == NULL) {
        perror(argv[1]);
        exit(1);
    }
    struct my_record records[10000];
    size_t count = 0;
    while (fgets(mystring, 100, my_file)!= NULL) {
        str = strtok(mystring, ";");
        records[count].time = str;
        str = strtok(NULL, ";");
        records[count].gid = atoi(str);
        str = strtok(NULL, ";");
        records[count].session = atoi(str);
        str = strtok(NULL, ";");
        records[count].speed = atoi(str);
        str = strtok(NULL, ";");

        ++count;
       

    }
    

   
    fclose(my_file);

    FILE* my_file1 = fopen(argv[2], "r");
         if (my_file == NULL) {
        perror(argv[1]);
        exit(1);
    }
    struct road_network Trnava[30];
    size_t count1 = 0;
    while (fgets(mystring1, 100, my_file1)!= NULL) {
        str1 = strtok(mystring1, ";");;
        Trnava[count1].gid = atoi(str1);
        str1 = strtok(NULL, ";");
        Trnava[count1].type = atoi(str1);
        str1 = strtok(NULL, ";");
        Trnava[count1].speed = atoi(str1);
        str1 = strtok(NULL, ";");
        ++count1;
       
    }
    
   
    fclose(my_file1);



    struct aggregates Agg [30];

    for (int i = 0; i<count1; i++) {
        int temp = 0;
        Agg[i].gid = Trnava[i].gid;
        for (int j = 0; j<count; j++) {
            if (records[j].gid == Trnava[i].gid) {
                Agg[i].speed[temp] = records[j].speed;               
                Agg[i].sess[temp] = records[j].session;
            }
            temp++;
        }
        Agg[i].num = temp;
    }
    double means [count1];
    int rel [count1];

    for (int i = 0; i<count1; i++) {
        if (Agg[i].speed[0] != 0) {
            means[i] = harmonicMean(Agg[i].speed, Agg[i].num);           
            int tt = unique_elements(Agg[i].sess, Agg[i].num);
            rel[i] = tt / ((1 / Trnava[i].type + 1)*100);
        } else {
            means[i] = -1;
            rel[i] = -1;
        }
    }
    

    FILE* my_file3 = fopen(argv[3], "wa");
    if (my_file3 == NULL) {
        exit(1);
    }
    fprintf(my_file3, "%s;", records[0].time);
    fprintf(my_file3, "%f", rel[0]);
    for (int i = 1; i < count1; i++) {
        fprintf(my_file3, ";%f", rel[i]);
    }
    fprintf(my_file3, "\n");
    fclose(my_file3);


    FILE* my_file4 = fopen(argv[4], "wa");
    if (my_file4 == NULL) {
        exit(201);
    }
    fprintf(my_file4,"%s;", records[0].time);
    fprintf(my_file4,"%f", means[0]);
    for (int i = 0; i<count1; i++) {
        fprintf(my_file4,";%f", means[i]);
    }
    fprintf(my_file4,"\n");
    fclose(my_file4);
    double t;

    double sp_pr [2016][count1];
    
    for (int j=0;j<2016;j++){
        for (int i = 0; i<count1;i++) {
            t=r3();
            sp_pr[j][i]=t;
        }
    }
   double rel_pr [2016][count1];
    
    for (int j=0;j<2016;j++){
        for (int i = 0; i<count1;i++) {
            t=r2();
            rel_pr[j][i]=t;
        }
    }

    
     puts("sdsdsdsd");

    char st [1000];
    struct tm tm;





    double sp_p[count1];
    double rel_p[count1];

    

   strptime(records[0].time, "%Y-%m-%dT%H:%M:%S", &tm);


   //strptime("2018-02-26T15:11:59+01", "%Y-%m-%dT%H:%M:%S+01", &tm);
    int row = tm.tm_wday * 288 + tm.tm_hour * 12 + (tm.tm_min / 5);

    
  
              
    for (int j=0;j<2016;j++){
        if (j == row) {
            for (int i=0;i<count1;i++)
            {
                sp_p[i]=sp_pr[j][i];
            }
                    
            break;
        }
    }

    for (int j=0;j<2016;j++){
        if (j == row) {
            for (int i=0;i<count1;i++)
            {
                rel_p[i]=rel_pr[j][i];
            }
                    
            break;
        }
    }


    
    double fuss[2][count1];
    for (int i = 0; i < count1; i++) {
        printf("%f\n",sp_p[i]);
        if (rel[i] > rel_p[i]) {
            fuss[1][i] = means[i];
            fuss[2][i] = rel[i];
        } else {
            fuss[1][i] = sp_p[i];
            fuss[2][i] = rel_p[i];
        }
    }
  
//  //  tm.tm_min = tm.tm_min + 5;
//    
//    
Lukas Rapant's avatar
Lukas Rapant committed
      char buf [80];
   strftime(buf, sizeof (buf), "%Y-%m-%d_%H:%M:%S", &tm);
Lukas Rapant's avatar
Lukas Rapant committed
////    char tmpstr[] = ".csv";
//
//
//
Lukas Rapant's avatar
Lukas Rapant committed
   char af[]=".csv";
   char aff[]="output";

   char output[1000] = {0};
   snprintf(output, sizeof(output), "%s\\%s_%s%s", argv[5],aff, buf,af );
 
   printf("%s",output);
    FILE* my_file6 = fopen(output, "w");
Lukas Rapant's avatar
Lukas Rapant committed
    if (my_file6 == NULL) {
        exit(1);
    }
    for (int i = 0; i<count1; i++) {
        fprintf(my_file6,"%d; ", Trnava[i].gid);
        fprintf(my_file6,"%f; ", fuss[1][i]);
        fprintf(my_file6,"%f\n ", fuss[2][i]);
    }
    fclose(my_file6);

    return 0;
}