Advertisement
arsaraiva

Sim_Num

May 26th, 2020 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <time.h>
  6. #include <math.h>
  7. #include <gsl/gsl_rng.h>
  8. #include <gsl/gsl_randist.h>
  9.  
  10.  
  11. const gsl_rng_type * T;
  12. gsl_rng * r;
  13.  
  14. typedef struct {
  15.  
  16.     int v;
  17.     int k;
  18.     int * onSlots;
  19. } t_bd;
  20.  
  21.  
  22. typedef struct {
  23.  
  24.     int v;
  25.     int k;
  26.     int * coincidences;
  27.     int * index;
  28. } t_pp_intersection;
  29.  
  30. typedef struct {
  31.  
  32.     int v;
  33.     int k;
  34.     t_pp_intersection offsetZero;
  35.     t_pp_intersection others;
  36. } t_pp_bd;
  37.  
  38. t_pp_bd * genPP(t_bd * pattern, t_bd * pattern2, unsigned int mmc, unsigned int offset) {
  39.  
  40.     t_pp_bd * pp;
  41.     int i, j, z, w, a, b, c, d, e, f, h, k, t, q, u, r, y, x, last, index;
  42.  
  43.     /*
  44.      * Allocate structures.
  45.      */
  46.  
  47.     pp = (t_pp_bd *) malloc(sizeof(t_pp_bd));
  48.     pp->offsetZero.coincidences = (int *) malloc(pattern->k * pattern2->k * sizeof(double)); //int
  49.     pp->offsetZero.index = (int *) malloc(mmc * sizeof(double));
  50.     pp->others.coincidences = (int *) malloc(pattern->k * pattern2->k * sizeof(double));
  51.     pp->others.index = (int *) malloc(mmc * sizeof(double));
  52.     pp->v = pattern->v;
  53.     pp->k = pattern->k;
  54.     pp->v = pattern2->v;
  55.     pp->k = pattern2->k;
  56.  
  57.     /*
  58.      * Creation of the MMC index for offset 0
  59.      */
  60.    
  61.     // First pattern
  62.        
  63.        
  64.         a = 0;
  65.         b = 0;
  66.         int menor[mmc];
  67.         memset(menor, 0, mmc*sizeof(int));
  68.         while (a <= mmc/pattern->v) {
  69.             for (c=0; c < pattern->k; c++){
  70.                 menor[((pattern->onSlots[c] + a*pattern->v) % mmc)] = 1;               
  71.                 }
  72.             a++;
  73.         }
  74.  
  75.  
  76.     /*  for (int p=0; p<mmc; p++){
  77.             printf("%d",menor[p]);
  78.             }
  79.         printf("\n"); */
  80.  
  81.     // Second pattern
  82.    
  83.         d = 0;
  84.         e = 0;
  85.         int maior[mmc];
  86.         memset(maior, 0, mmc*sizeof(int));
  87.         while (d < mmc/pattern2->v) {
  88.             for (f=0; f < pattern2->k; f++){
  89.                 maior[((pattern2->onSlots[f] + d*pattern2->v) % mmc)] = 1;             
  90.                 e++;
  91.                 }
  92.             d++;
  93.         }
  94.        
  95.     /*  for (int p=0; p<mmc; p++){
  96.             printf("%d",maior[p]);
  97.             }
  98.         printf("\n"); */
  99.        
  100.        
  101.  
  102.     // Comparation of pattern of offset 0
  103.     if (offset == 0){  
  104.         w=0;
  105.         for (z=0; z < mmc; z++){
  106.             if ((menor[z] == 1) && (maior[z]==1)){
  107.                 pp->offsetZero.coincidences[w] = z;            
  108.                 pp->offsetZero.k++;
  109.                 w++;
  110.                 }
  111.             }
  112.        
  113.         /*for (int p=0; p<pp->offsetZero.k; p++){
  114.             printf("offsetZero.coincidences %d= %d\n",p,pp->offsetZero.coincidences[p]);
  115.             }
  116.         printf("\n");*/
  117.        
  118.         j = 0; 
  119.         pp->offsetZero.v = pp->v;
  120.         for (i = 0; i < pp->offsetZero.k; i++) {
  121.             while(j <= pp->offsetZero.coincidences[i]) {
  122.                 pp->offsetZero.index[j++] = pp->offsetZero.coincidences[i];
  123.                 }
  124.             }
  125.        
  126.         while(j < pp->v) {
  127.             pp->offsetZero.index[j++] = 0;
  128.             }
  129.  
  130.         /*for (int p=0; p<mmc; p++){
  131.             printf("offsetZero.index %d = %d\n",p,pp->offsetZero.index[p]);
  132.             }
  133.             printf("\n");*/
  134.     } else {
  135.         // Second pattern offset for different offset 0
  136.         int size = sizeof(maior)/sizeof(maior[0]);
  137.         for (int q=0; q < offset; q++){    
  138.             last = maior[size -1];
  139.             for (r = size-1; r >0; r--){
  140.                 maior[r] = maior[r-1];
  141.                 }
  142.             maior[0] = last;
  143.             }
  144.            
  145.            
  146.     /*  for (int p=0; p<mmc; p++){
  147.             printf("%d",menor[p]);
  148.             }
  149.         printf("\n");
  150.    
  151.         for (int p=0; p<mmc; p++){
  152.             printf("%d",maior[p]);
  153.             }
  154.         printf("\n");*/
  155.            
  156.         k=0;
  157.         for (t=0; t < mmc; t++){
  158.             if ((menor[t] == 1) && (maior[t]==1)){
  159.                 pp->others.coincidences[k] = t;            
  160.                 pp->others.k++;
  161.                 k++;
  162.                 }
  163.             }
  164.            
  165.     /*  for (int p=0; p<pp->others.k; p++){
  166.             printf("others.coincidences %d= %d\n",p,pp->others.coincidences[p]);
  167.             }
  168.         printf("\n");*/
  169.    
  170.         y = 0; 
  171.         pp->others.v = pp->v;
  172.         x = pp->others.coincidences[pp->others.k-1];
  173.         for (u = 0; u <= pp->others.k; u++) {
  174.             if (y > pp->others.coincidences[pp->others.k-1]) {
  175.                 while (x < mmc-1){
  176.                 pp->others.index[y++] = pp->others.coincidences[0];
  177.                 x++;
  178.                     }
  179.                 } else {               
  180.             while(y <= pp->others.coincidences[u]) {
  181.                 pp->others.index[y++] = pp->others.coincidences[u];
  182.                 }
  183.             }
  184.         }
  185.        
  186.        
  187.         while(y < pp->v) {
  188.             pp->others.index[y++] = 0;
  189.             }
  190.  
  191.     /*  for (int p=0; p<mmc; p++){
  192.             printf("others.index %d = %d\n",p,pp->others.index[p]);
  193.             }
  194.             printf("\n");*/
  195.         };     
  196.  
  197.     return(pp);
  198. }
  199.  
  200.  
  201. uint64_t simulateEncounter(t_pp_bd * pp, double p, unsigned int start, unsigned int offset, unsigned int mmc ) {
  202.  
  203.     uint64_t t;
  204.     unsigned int attempts;
  205. //  unsigned int offset;
  206. //  unsigned int start;
  207.     int i, j, next_i;
  208.     t_pp_intersection * pp_intersection;
  209.  
  210.     attempts = gsl_ran_geometric(r, p);
  211. //  offset = floor(gsl_ran_flat(r, 0.0, (double) pp->v));
  212. //  start = floor(gsl_ran_flat(r, 0.0, (double) pp->v));
  213. //  attempts = 2; offset = 47; start = 44;
  214. //  printf("attempts = %u, start = %u, offset = %u\n", attempts, start, offset);
  215.     /*
  216.      * Test in which of the four cases the offset falls.
  217.      */
  218.     //printf("start = %d\n", start);
  219.     if (offset == 0) {
  220.         pp_intersection = & (pp->offsetZero); //aqui muda para a posição do pré processamento, tirar o if
  221.     }
  222.     else {
  223.         pp_intersection = & (pp->others);
  224.     }
  225.  
  226.     /*
  227.      * Account for the number of slots until
  228.      * the first opportunity.
  229.      */
  230.  
  231.     if (pp_intersection->coincidences[pp_intersection->index[start]] >= start){
  232.         t = pp_intersection->coincidences[pp_intersection->index[start]] - start;
  233.     } else {
  234.         //t = pp_intersection->v - (start - pp_intersection->coincidences[pp_intersection->index[start]]); //gdb aqui
  235.         t = mmc - (start - pp_intersection->coincidences[pp_intersection->index[start]]);
  236.     }
  237.     attempts--;
  238.     printf("t1 = %lu\n", t);
  239.     /*
  240.      * Account for the number of cycles.
  241.      */
  242.  
  243.     //t += (attempts / pp_intersection->k) * pp->v;
  244.     t += (attempts / pp_intersection->k) * mmc;
  245.     attempts = (attempts % pp_intersection->k);
  246.  
  247.     printf("t2 = %lu\n", t);
  248.     /*
  249.      * Account for the remainder of the attempts.
  250.      */
  251.  
  252.     i = pp_intersection->index[start];
  253.     while(attempts--) {
  254.         next_i = (i + 1) % pp_intersection->k;
  255. //      printf("attempts = %u, i = %d, next_i = %d\n", attempts, i, next_i);
  256.         if (next_i > i) {
  257.  
  258.             t += (pp_intersection->coincidences[next_i] - pp_intersection->coincidences[i]);
  259.             printf("t3 = %lu\n", t);
  260.         }
  261.         else {
  262.  
  263.             t += (mmc - (pp_intersection->coincidences[i] - pp_intersection->coincidences[next_i]));
  264.             printf("t4 = %lu\n", t);
  265.         }
  266.         i = next_i;
  267. //      printf("tn = %lu\n", t);
  268.     }
  269.  
  270.     return(t);
  271. }
  272.  
  273. int main(int argc, char ** argv) {
  274.  
  275.     t_bd pattern;
  276.     t_bd pattern2;
  277.     t_pp_bd * pp;
  278.     char * s_pattern;
  279.     char * s_pattern2;
  280.     int i, j, k;
  281.     double start_p, by_p;
  282.     int n_p;
  283.     double p;
  284.     unsigned int reps;
  285.     unsigned int t;
  286.     long unsigned int t_total;
  287.     unsigned int offset;
  288.     unsigned int start;
  289.     unsigned int hops;
  290.     unsigned int mmc;
  291.     FILE * output;
  292.     char filename[255], * outputdir;
  293.  
  294.     gsl_rng_env_setup();
  295.     T = gsl_rng_default;
  296.     r = gsl_rng_alloc(T);
  297.     gsl_rng_set(r, time(NULL));
  298.  
  299.     s_pattern = argv[1];
  300.     s_pattern2 = argv[2];
  301.     start_p = atof(strtok(argv[3], ","));
  302.     by_p = atof(strtok(NULL, ","));
  303.     n_p = atoi(strtok(NULL, ","));
  304.     reps = atoi(argv[4]);
  305.     outputdir = argv[5];
  306.     hops = atoi(argv[6]);
  307.     mmc = atoi(argv[7]);
  308.  
  309.     // Parse pattern.
  310.     pattern.v = atoi(strtok(s_pattern, ","));
  311.     pattern.k = atoi(strtok(NULL, ","));
  312.     pattern.onSlots = (int *) malloc(sizeof(unsigned int) * pattern.k);
  313.     strtok(NULL, ",");  // We dont use l.
  314.  
  315.     for (i = 0; i < pattern.k; i++) {
  316.  
  317.         pattern.onSlots[i] = atoi(strtok(NULL, ","));
  318.     }
  319.  
  320.  
  321.     pattern2.v = atoi(strtok(s_pattern2, ","));
  322.     pattern2.k = atoi(strtok(NULL, ","));
  323.     pattern2.onSlots = (int *) malloc(sizeof(unsigned int) * pattern2.k);
  324.     strtok(NULL, ",");  // We dont use l.
  325.  
  326.     for (i = 0; i < pattern2.k; i++) {
  327.  
  328.         pattern2.onSlots[i] = atoi(strtok(NULL, ","));
  329.     }
  330.  
  331.     printf("start_p %f", start_p);
  332.     printf("n_p %d", n_p);
  333.     printf("by_p %f", by_p);
  334.     p = start_p;
  335.  
  336.     for (j = 0; j < n_p; j++) {
  337.         sprintf(filename, "%sbd_%.6f.txt", outputdir, p);
  338.         output = fopen(filename, "w");
  339.         for (i = 0; i < reps; i++) {
  340.             start = floor(gsl_ran_flat(r, 0.0, (double) mmc));
  341.             t_total = 0;
  342.             for (k = 0; k < hops; k++) {
  343.                 offset = floor(gsl_ran_flat(r, 0.0, (double) mmc)); //PARA OFFSET RANDOM
  344.                 //offset = pp-> v -1; PARA OFFSET 1 NO BD E offset = v-1; NOS DEMAIS
  345.                 pp = genPP(& pattern, & pattern2, mmc, offset);
  346.                 t = simulateEncounter(pp, p, start, offset, mmc);
  347.                 t_total += t;
  348.                 start = (start + offset + t + 1) % mmc;
  349.                 fprintf(output, "%u\t", t);
  350.             }
  351.             fprintf(output, "%lu\n", t_total);
  352.         }
  353.         p += by_p;
  354.         if (p > 1.0) p = 1.0;
  355.         fclose(output);
  356.         free(pattern.onSlots);
  357.         free(pattern2.onSlots);
  358.         free(pp);
  359.  
  360.     }
  361.     free(pattern.onSlots);
  362.     free(pattern2.onSlots);
  363.     free(pp);
  364.     gsl_rng_free(r);
  365.  
  366.     return(0);
  367. }
  368.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement