Advertisement
OMEGAHEAD_MonkoX

Untitled

Nov 23rd, 2022
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include <openssl/rand.h>
  7. #include <stdint.h>
  8. #include "array.h"
  9. #include "list.h"
  10. #include "errors.h"
  11. #include "menu.h"
  12.  
  13. //#define T1_0    0
  14. //#define T1_1    6
  15. //#define T2_0    1
  16. //#define T2_1    8
  17. #define CHANCE  0.7
  18. #define EPS     1e-5
  19.  
  20. struct timeval tm;
  21.  
  22. double random_double()
  23. {
  24.     union
  25.     {
  26.         uint64_t i;
  27.         unsigned char c[sizeof(uint64_t)];
  28.     } u;
  29.  
  30.     if (!RAND_bytes(u.c, sizeof(u.c)))
  31.         exit(1);
  32.     return (u.i >> 11) * (1.0/9007199254740992.0);
  33. }
  34.  
  35. double random_float(double a, double b)
  36. {
  37.     return random_double() * (b - a) + a;
  38. }
  39.  
  40. double min(double a, double b)
  41. {
  42.     return (a - b) > EPS ? b : a;
  43. }
  44.  
  45.  
  46. void array_task(bool print, double T1_0, double T1_1, double T2_0, double T2_1)
  47. {
  48.     array_queue *queue1 = array_init(), *queue2 = array_init();
  49.  
  50.     /*
  51.         downtime_1 - Время простоя второй очереди
  52.         downtime_2 - Время простоя второй очереди
  53.         total_time - Общее время работы
  54.         chance - P, вероятность перейти в следующую очередь
  55.         time1 - Время в ОА1
  56.         time2 - Время в ОА2
  57.         min_time - Минимальное из двух времен на итерации
  58.         avg_time - Среднее время пребывания заявок в очереди 2
  59.     */
  60.     double downtime_2 = 0, total_time = 0, chance, time_1 = 0, time_2 = 0, min_time = 0, avg_time_2 = 0;
  61.  
  62.     /*
  63.         total_out - Количество вышедших из ОА2
  64.         total_first - Текущее количество в 1 очереди
  65.         total_second - Текущее количество во 2 очереди
  66.         counter_1 - Количество изменений первой очереди
  67.         counter_2 - Количество изменений второй очереди
  68.         trigger_1 - Количество срабатываний ОА1
  69.     */
  70.     size_t total_out = 0, total_first = 100, total_second = 0, counter_1 = 1, counter_2 = 1, trigger_1 = 0;
  71.  
  72.     /*
  73.         sum_1 - Сумма размеров первой очереди каждого изменения
  74.         sum_2 - Сумма размеров второй очереди каждого изменения
  75.     */
  76.     double sum_1 = 100, sum_2 = 0;
  77.  
  78.     int *address, x;
  79.  
  80.     // Начальная инициализация 100 заявок
  81.     for (size_t i = 0; i < 100; ++i)
  82.         array_push(queue1, 1);
  83.  
  84.     // Основной цикл
  85.     while (total_out < 1000)
  86.     {
  87.         if (time_1 < EPS && !array_is_empty(queue1))
  88.             time_1 = random_float(T1_0, T1_1);
  89.  
  90.         if (time_2 < EPS && !array_is_empty(queue2))
  91.         {
  92.             time_2 = random_float(T2_0, T2_1);
  93.             avg_time_2 += time_2;
  94.         }
  95.  
  96.         //  printf("1. SIZES: %zu %zu, COUNTER: %zu %zu, TIME: %lf %lf\n", queue1->size, queue2->size, counter_1, counter_2, time_1, time_2);
  97.  
  98.         if (!array_is_empty(queue1) && array_is_empty(queue2))
  99.         {
  100.             downtime_2 += time_1;
  101.             min_time = time_1;
  102.         }
  103.         else if (!array_is_empty(queue2) && array_is_empty(queue1))
  104.             min_time = time_2;
  105.         else
  106.             min_time = min(time_1, time_2);
  107.        
  108.         total_time += min_time;
  109.         time_1 -= min_time;
  110.         time_2 -= min_time;
  111.  
  112.         //  printf("2. SIZES: %zu %zu, COUNTER: %zu %zu, TIME: %lf %lf\n", queue1->size, queue2->size, counter_1, counter_2, time_1, time_2);
  113.  
  114.         if (time_1 < EPS && queue1->size)
  115.         {
  116.             trigger_1++;
  117.             array_pop(queue1, &address, &x);
  118.             chance = random_float(0, 1);
  119.             if (chance - CHANCE > EPS)
  120.             {
  121.                 array_push(queue2, x);
  122.                 if (time_2 < EPS && queue2->size == 1)
  123.                 {
  124.                     time_2 = random_float(T2_0, T2_1);
  125.                     avg_time_2 += time_2;
  126.                 }
  127.                 sum_2 += queue2->size;
  128.                 counter_2++;
  129.                 sum_1 += queue1->size;
  130.                 counter_1++;
  131.                 total_second++;
  132.             }
  133.             else
  134.             {
  135.                 array_push(queue1, x);
  136.                 total_first++;
  137.             }
  138.         }
  139.         //  printf("3. SIZES: %zu %zu, COUNTER: %zu %zu, TIME: %lf %lf, CHANCE: %lf\n", queue1->size, queue2->size, counter_1, counter_2, time_1, time_2, chance);
  140.  
  141.  
  142.         if (time_2 < EPS && queue2->size)
  143.         {
  144.             array_pop(queue2, &address, &x);
  145.             array_push(queue1, x);
  146.             total_out++;
  147.             sum_1 += queue1->size;
  148.             counter_1++;
  149.             sum_2 += queue2->size;
  150.             counter_2++;
  151.  
  152.             if (total_out % 100 == 0 && print)
  153.                output_inter_result(queue1->size, queue2->size, sum_1 / (double)counter_1, sum_2 / (double)counter_2, total_out);
  154.         }
  155.             //  printf("4. SIZES: %zu %zu, COUNTER: %zu %zu, TIME: %lf %lf, CHANCE: %lf\n", queue1->size, queue2->size, counter_1, counter_2, time_1, time_2, chance);
  156.     }
  157.     avg_time_2 /= total_out;
  158.     if (print)
  159.         output_full_result(total_time, downtime_2, trigger_1, avg_time_2, T1_0, T1_1, T2_0, T2_1);
  160.     array_free(queue1);
  161.     array_free(queue2);
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement