Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <math.h>
- #define MIN(a, b)(a < b ? a : b)
- #define MAX(a, b)(a < b ? b : a)
- #define TRUE 1
- #define FULL_QUEUE 1
- #define SUCCESS 0
- #define VECTOR_LEN 100
- #define EPS 10E-4
- //(0 + rand() % (10 - 0));
- typedef struct
- {
- double *p_in;
- double *p_out;
- double *low_border;
- double *high_border;
- int is_full;
- int is_working;
- } vector_descriptor_t;
- typedef struct list_descriptor
- {
- double val;
- struct list_descriptor *next;
- } list_descriptor_t;
- // REWORK: PLUSS ALL ELEMENTS, EXCEPT P_OUT; CHECK IF THEY IN MACHINE LATER
- void elements_wait(vector_descriptor_t vector_descriptor_a,
- vector_descriptor_t vector_descriptor_b, double time) // INCORRECT WORK
- {
- for(double *ptr = vector_descriptor_a.low_border; ptr <= vector_descriptor_a.high_border; ptr++)
- {
- if (ptr != vector_descriptor_a.p_out &&
- ptr !=vector_descriptor_b.p_out)
- (*ptr) += time;
- }
- /*
- if (vector_descriptor.is_full)
- for(double *ptr = vector_descriptor.low_border; ptr <= vector_descriptor.high_border; ptr++)
- *ptr += time;
- else
- {
- if (vector_descriptor.p_in < vector_descriptor.p_out)
- for(double *ptr = vector_descriptor.p_in; ptr != vector_descriptor.p_out; ptr++)
- {
- *ptr += time;
- if (ptr == vector_descriptor.high_border)
- ptr = vector_descriptor.low_border - 1;
- }
- else
- for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
- {
- *ptr += time;
- if (ptr == vector_descriptor.high_border)
- ptr = vector_descriptor.low_border - 1;
- }
- }
- */
- return;
- }
- void vector_pop(vector_descriptor_t *vector_descriptor)
- {
- if (vector_descriptor -> p_out == vector_descriptor -> high_border)
- vector_descriptor -> p_out = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_out) += 1;
- if (vector_descriptor -> is_full)
- (vector_descriptor -> is_full) = 0;
- }
- void vector_push(vector_descriptor_t *vector_descriptor)
- {
- if (!(vector_descriptor -> is_full))
- {
- if ((vector_descriptor -> p_in) == (vector_descriptor -> high_border))
- vector_descriptor -> p_in = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_in)++;
- if ((vector_descriptor -> p_in) == (vector_descriptor -> p_out))
- (vector_descriptor -> is_full) = 1;
- }
- return;
- }
- /*
- double* vector_pop(vector_descriptor_t *vector_descriptor)
- {
- double* out = vector_descriptor -> p_out;
- if (vector_descriptor -> p_out == vector_descriptor -> high_border)
- vector_descriptor -> p_out = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_out) += 1;
- if (vector_descriptor -> is_full)
- (vector_descriptor -> is_full) = 0;
- return out;
- }
- void vector_push(vector_descriptor_t *vector_descriptor, double* element)
- {
- if (!(vector_descriptor -> is_full))
- {
- (vector_descriptor -> p_in) = element;
- if ((vector_descriptor -> p_in) == (vector_descriptor -> high_border))
- vector_descriptor -> p_in = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_in)++;
- }
- if ((vector_descriptor -> p_in) == (vector_descriptor -> p_out))
- (vector_descriptor -> is_full) = 1;
- return;
- }
- */
- long int queue_len(vector_descriptor_t vector_descriptor)
- {
- if (vector_descriptor.is_full)
- return (vector_descriptor.high_border - vector_descriptor.low_border) + 1;
- if (vector_descriptor.p_in == vector_descriptor.p_out)
- return 0;
- long int cnt = 0;
- for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
- {
- cnt++;
- if (ptr == vector_descriptor.high_border)
- ptr = vector_descriptor.low_border - 1;
- }
- return cnt;
- }
- void vector_imitate_queue(vector_descriptor_t vector_descriptor_a, vector_descriptor_t vector_descriptor_b,
- double min_process_a, double max_process_a,
- double min_process_b, double max_process_b,
- double *time_a, double *time_b, double *time_b_waiting, double *time_a_waiting,
- size_t *cnt_a_work, double p, long int elements_exit, size_t *append_in_b, int is_print)
- {
- /*
- double decr = (max_process_a - min_process_a) / 4;
- max_process_a -= decr;
- min_process_a += decr;
- decr = (max_process_b - min_process_b) / 4;
- max_process_b -= decr;
- min_process_b += decr;
- */
- int cnt_b_out = 0;
- double time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- //(*cnt_a_work)++;
- double time_b_work = 0;
- vector_descriptor_a.is_working = 1;
- while(cnt_b_out != elements_exit)
- {
- if (vector_descriptor_a.is_working) // A working
- {
- if (vector_descriptor_b.is_working) // Both apparats are working right now
- {
- double time_min = MIN(time_a_work, time_b_work);
- time_a_work -= time_min;
- time_b_work -= time_min;
- elements_wait(vector_descriptor_a, vector_descriptor_b, time_min);
- (*time_a) += time_min;
- (*time_b) += time_min;
- if (time_a_work < EPS)
- {
- vector_descriptor_a.is_working = 0;
- if(((rand() % 100) / 100.0) > p)
- {
- (*append_in_b)++;
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_b);
- //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
- }
- else
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
- }
- }
- if (time_b_work < EPS)
- {
- cnt_b_out++;
- vector_descriptor_b.is_working = 0;
- vector_pop(&vector_descriptor_b);
- vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
- }
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else // B is NOT working right now
- {
- (*time_a) += time_a_work;
- (*time_b_waiting) += time_a_work;
- elements_wait(vector_descriptor_a, vector_descriptor_b, time_a_work);
- if (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full)
- (*vector_descriptor_b.p_out) += time_a_work;
- time_a_work = 0;
- if (time_a_work < EPS)
- {
- vector_descriptor_a.is_working = 0;
- if(((rand() % 100) / 100.0) > p)
- {
- (*append_in_b)++;
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_b);
- //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
- }
- else
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
- }
- }
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- }
- else // A don`t work
- {
- if (vector_descriptor_b.is_working) // ONLY B works right now
- {
- (*time_b) += time_b_work;
- (*time_a_waiting) += time_b_work;
- elements_wait(vector_descriptor_a, vector_descriptor_b, time_b_work);
- if (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full)
- (*vector_descriptor_a.p_out) += time_b_work;
- time_b_work = 0;
- if (time_b_work < EPS)
- {
- cnt_b_out++;
- vector_descriptor_b.is_working = 0;
- vector_pop(&vector_descriptor_b);
- vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
- }
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else // NOTHING is working at the moment
- {
- if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
- {
- (*cnt_a_work)++;
- vector_descriptor_a.is_working = 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
- {
- vector_descriptor_b.is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- }
- /*
- if (cnt_b_out == 1)
- {
- printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
- printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
- }*/
- if (!(cnt_b_out % 100) && cnt_b_out != 0 && is_print)
- {
- printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
- printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
- double sum_time_wait = 0;
- for (double *ptr = vector_descriptor_a.low_border; ptr <= vector_descriptor_a.high_border; ptr++)
- sum_time_wait += *ptr;
- if (sum_time_wait / (vector_descriptor_a.high_border - vector_descriptor_a.low_border) > EPS)
- printf("Average waiting time %lf\n",
- sum_time_wait / (vector_descriptor_a.high_border - vector_descriptor_a.low_border));
- else
- printf("Elements almost don`t stand in queue\n");
- printf("-----------------------------------------------\n");
- }
- }
- return;
- }
- void elements_list_wait(list_descriptor_t *list_descriptor_a, list_descriptor_t *list_descriptor_b,
- double time_min)
- {
- for (list_descriptor_t *ptr = list_descriptor_a; ptr != NULL; ptr = ptr -> next)
- {
- ptr -> val += time_min;
- }
- for (list_descriptor_t *ptr = list_descriptor_b; ptr != NULL; ptr = ptr -> next)
- {
- ptr -> val += time_min;
- }
- return;
- }
- list_descriptor_t *list_pop(list_descriptor_t **list_descriptor)
- {
- list_descriptor_t *ret = *list_descriptor;
- *list_descriptor = (*list_descriptor)-> next;
- return ret;
- }
- list_descriptor_t *list_push(list_descriptor_t *list_descriptor, list_descriptor_t *list_append)
- {
- list_append -> next = NULL;
- if (list_descriptor == NULL)
- {
- list_descriptor = list_append;
- return list_descriptor;
- }
- list_descriptor_t *ptr = list_descriptor;
- while ((ptr -> next) != NULL)
- ptr = ptr -> next;
- ptr -> next = list_append;
- return list_descriptor;
- }
- long int queue_list_len(list_descriptor_t *list_descriptor)
- {
- long int cnt = 0;
- while (list_descriptor != NULL)
- {
- cnt++;
- list_descriptor = list_descriptor -> next;
- }
- return cnt;
- }
- void list_imitate_queue(list_descriptor_t *list_descriptor_a, list_descriptor_t *list_descriptor_b,
- double min_process_a, double max_process_a,
- double min_process_b, double max_process_b,
- double *time_a, double *time_b, double *time_b_waiting, double *time_a_waiting,
- size_t *cnt_a_work, double p, long int elements_exit, size_t *append_in_b , long int queue_size,
- double *sum_time_wait, int is_print)
- {
- int cnt_b_out = 0;
- double time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- //(*cnt_a_work)++;
- double time_b_work = 0;
- int a_is_working = 1;
- int b_is_working = 0;
- while(cnt_b_out != elements_exit)
- {
- if (a_is_working) // A working
- {
- if (b_is_working) // Both apparats are working right now
- {
- double time_min = MIN(time_a_work, time_b_work);
- time_a_work -= time_min;
- time_b_work -= time_min;
- elements_list_wait(list_descriptor_a, list_descriptor_b, time_min);
- (*time_a) += time_min;
- (*time_b) += time_min;
- if (time_a_work < EPS)
- {
- a_is_working = 0;
- if(((rand() % 100) / 100.0) > p)
- {
- (*append_in_b)++;
- list_descriptor_b = list_push(list_descriptor_b, list_pop(&list_descriptor_a));
- //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
- }
- else
- {
- //vector_pop(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a);
- list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_a));
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
- }
- }
- if (time_b_work < EPS)
- {
- cnt_b_out++;
- b_is_working = 0;
- list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_b));
- //vector_pop(&vector_descriptor_b);
- //vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
- }
- if ((time_a_work < EPS) && (list_descriptor_a != NULL))
- {
- (*cnt_a_work)++;
- a_is_working= 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (list_descriptor_b != NULL))
- {
- b_is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else // B is NOT working right now
- {
- (*time_a) += time_a_work;
- (*time_b_waiting) += time_a_work;
- elements_list_wait(list_descriptor_a, list_descriptor_b, time_a_work);
- //if (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full)
- // (*vector_descriptor_b.p_out) += time_a_work;
- time_a_work = 0;
- if (time_a_work < EPS)
- {
- a_is_working= 0;
- if(((rand() % 100) / 100.0) > p)
- {
- (*append_in_b)++;
- list_descriptor_b = list_push(list_descriptor_b, list_pop(&list_descriptor_a));
- //vector_pop(&vector_descriptor_a);
- //vector_push(&vector_descriptor_b);
- //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
- }
- else
- {
- list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_a));
- //vector_pop(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
- }
- }
- if ((time_a_work < EPS) && (list_descriptor_a != NULL))
- {
- (*cnt_a_work)++;
- a_is_working= 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (list_descriptor_b != NULL))
- {
- b_is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- }
- else // A don`t work
- {
- if (b_is_working) // ONLY B works right now
- {
- (*time_b) += time_b_work;
- (*time_a_waiting) += time_b_work;
- elements_list_wait(list_descriptor_a, list_descriptor_b, time_a_work);
- //elements_wait(vector_descriptor_a, vector_descriptor_b, time_b_work);
- //if (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full)
- // (*vector_descriptor_a.p_out) += time_b_work;
- time_b_work = 0;
- if (time_b_work < EPS)
- {
- cnt_b_out++;
- b_is_working = 0;
- list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_b));
- //vector_pop(&vector_descriptor_b);
- //vector_push(&vector_descriptor_a);
- //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
- }
- if ((time_a_work < EPS) && (list_descriptor_a != NULL))
- {
- (*cnt_a_work)++;
- a_is_working= 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (list_descriptor_b != NULL))
- {
- b_is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else // NOTHING is working at the moment
- {
- if ((time_a_work < EPS) && (list_descriptor_a != NULL))
- {
- (*cnt_a_work)++;
- a_is_working= 1;
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if ((time_b_work < EPS) && (list_descriptor_b != NULL))
- {
- b_is_working = 1;
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- }
- if (!(cnt_b_out % 100) && cnt_b_out != 0 && is_print)
- {
- printf("Current length of queue A: %li\n", queue_list_len(list_descriptor_a));
- printf("Current length of queue B: %li\n", queue_list_len(list_descriptor_b));
- double sum_time = 0;
- for (list_descriptor_t *ptr = list_descriptor_a; ptr != NULL; ptr = ptr -> next)
- sum_time += ptr -> val;
- for (list_descriptor_t *ptr = list_descriptor_b; ptr != NULL; ptr = ptr -> next)
- sum_time += ptr -> val;
- if (sum_time / (queue_size) > EPS)
- printf("Average waiting time %lf\n",
- sum_time / (queue_size));
- else
- printf("Elements almost don`t stand in queue\n");
- printf("-----------------------------------------------\n");
- }
- }
- for (list_descriptor_t *ptr = list_descriptor_a; ptr != NULL; ptr = ptr -> next)
- (*sum_time_wait) += ptr -> val;
- for (list_descriptor_t *ptr = list_descriptor_b; ptr != NULL; ptr = ptr -> next)
- (*sum_time_wait) += ptr -> val;
- return;
- }
- list_descriptor_t *full_list(list_descriptor_t *queue_a, long int queue_size)
- {
- queue_a = malloc(sizeof(list_descriptor_t));
- if (queue_a == NULL)
- return NULL;
- queue_a -> val = 0;
- queue_a -> next = NULL;
- list_descriptor_t *ptr = queue_a;
- for (int i = 1; i < queue_size; i++)
- {
- ptr -> next = malloc(sizeof(list_descriptor_t));
- if ((ptr -> next) == NULL)
- return NULL;
- (ptr -> next) -> val = 0;
- (ptr -> next) -> next = NULL;
- ptr = (ptr -> next);
- ptr -> val = 0;
- }
- return queue_a;
- }
- void print_list(list_descriptor_t *queue_a)
- {
- int i = 0;
- while (queue_a != NULL)
- {
- i++;
- printf("%lf", queue_a -> val);
- queue_a = queue_a -> next;
- }
- printf("AMOUNT IS %i", i);
- }
- int main(void)
- {
- srand(time(NULL));
- //int choice;
- float min_process_a = 0;
- float max_process_a = 6;
- float min_process_b = 1;
- float max_process_b = 8;
- long int queue_size = 100;
- long int elements_exit = 1000;
- double p = 0.7;
- char s[16];
- s[0] = '\0';
- while (TRUE)
- {
- printf("\n");
- printf(" _ __ ___ ___ _ __ _ _ \n");
- printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
- printf("| | | | | | __/ | | | |_| |\n");
- printf("|_| |_| |_|\\___|_| |_|\\____|\n");
- printf("Initial parameters:\n");
- printf("Minimum process time for first machine %f\n", min_process_a);
- printf("Maximum process time for first machine %f\n", max_process_a);
- printf("Minimum process time for second machine %f\n", min_process_b);
- printf("Maximum process time for second machine %f\n", max_process_b);
- printf("Amount of elements in first queue %li\n", queue_size);
- printf("Amount of elements that the second machine needs to complete the process %li\n", elements_exit);
- printf("P %lf\n", p);
- printf("\n1: Imitate queue on vector");
- printf("\n2: Imitate queue on list");
- printf("\n3: Change time work for first machine");
- printf("\n4: Change time work for second machine");
- printf("\n5: Change amount of elements in queue");
- printf("\n6: Change amount of elements that the second machine needs to complete the process");
- printf("\n7: Change p");
- printf("\n0: EXIT\n");
- printf("Choice: ");
- fgets(s, 15, stdin);
- if (strcmp(s, "0\n") == 0)
- break;
- if (strcmp(s, "1\n") == 0)
- {
- double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict *= 1000;
- wait_b_queue_predict /= ((1 - p) * 1000);
- wait_b_queue_predict *= elements_exit;
- // check in phys notebook
- //double work_a_queue_predict = (max_process_a + min_process_a) / 2;
- //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
- double *waiting_time;
- waiting_time = calloc(queue_size, sizeof(double));
- vector_descriptor_t queue_a;
- queue_a.p_in = waiting_time;
- queue_a.p_out = waiting_time;
- queue_a.low_border = waiting_time;
- queue_a.high_border = &(waiting_time[queue_size - 1]);
- queue_a.is_full = 1;
- queue_a.is_working = 0;
- vector_descriptor_t queue_b;
- queue_b.p_in = waiting_time;
- queue_b.p_out = waiting_time;
- queue_b.low_border = waiting_time;
- queue_b.high_border = &(waiting_time[queue_size - 1]);
- queue_b.is_full = 0;
- queue_b.is_working = 0;
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- double time_a_waiting = 0;
- size_t cnt_a_work = 0;
- size_t append_in_b = 0;
- vector_imitate_queue(queue_a, queue_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting,
- &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, 1);
- double sum_time_wait = 0;
- for (int i = 0; i < queue_size; i++)
- sum_time_wait += waiting_time[i];
- printf("Apparat a worked for %lf (t.u.)\n", time_a);
- if (fabs(time_a_waiting) > EPS)
- printf("Apparat a waited for %lf (t.u.)\n", time_a_waiting);
- else
- printf("Apparat a worked all time\n");
- printf("Apparat b worked for %lf (t.u.)\n", time_b);
- if (fabs(time_b_waiting) > EPS)
- printf("Apparat b waited for %lf (t.u.)\n", time_b_waiting);
- else
- printf("Apparat b worked all time\n");
- printf("Apparat a worked %li times\n", (long int) cnt_a_work);
- printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
- wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict /= (1 - p);
- double model_time_predict;
- if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
- model_time_predict = wait_b_queue_predict;
- else
- model_time_predict = (max_process_b + min_process_b) / 2;
- double wait_a_queue_predict = (max_process_a + min_process_a) / 2;
- wait_a_queue_predict /= p;
- wait_a_queue_predict -= (max_process_a + min_process_a) / 2;
- double tmp_wait_a = (max_process_a + min_process_a) / 2;
- tmp_wait_a /= (1 - p);
- tmp_wait_a += (max_process_b + min_process_b) / 2;
- //printf("\n\nWAITING ROOM %lf %lf\n\n", wait_a_queue_predict, tmp_wait_a);
- wait_a_queue_predict = wait_a_queue_predict < tmp_wait_a ? wait_a_queue_predict : tmp_wait_a;
- //wait_a_queue_predict *= queue_size;
- //wait_a_queue_predict -= queue_size * (max_process_a + min_process_a) / 2;
- double work_a_queue_predict = (max_process_a + min_process_a) / 2;
- //printf("\n\nPREDICT WAIT %lf\n\n", wait_a_queue_predict);
- //printf("\n\nPREDICT WORK %lf\n\n", work_a_queue_predict);
- double simulate_a_predict = MAX(wait_a_queue_predict, work_a_queue_predict);
- printf("\n\nPREDICT SIMULATE A %lf\n\n", simulate_a_predict);
- double elems_check = (time_a + time_a_waiting) / model_time_predict;
- printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
- printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
- printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
- / MIN(elems_check, append_in_b) * 100));
- printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
- double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
- printf("Work time must be %lf\n", time_b_pred);
- printf("But simulation time work is %lf\n", time_b + time_b_waiting);
- printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
- - MIN(time_b_pred, time_b + time_b_waiting))
- / MIN(time_b_pred, time_b + time_b_waiting) * 100));
- free(waiting_time);
- waiting_time = NULL;
- system("pause");
- }
- if (strcmp(s, "2\n") == 0)
- {
- double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict *= 1000;
- wait_b_queue_predict /= ((1 - p) * 1000);
- wait_b_queue_predict *= elements_exit;
- //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
- list_descriptor_t *queue_a = NULL;
- queue_a = full_list(queue_a, queue_size);
- //print_list(queue_a);
- list_descriptor_t *queue_b = NULL;
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- double time_a_waiting = 0;
- size_t cnt_a_work = 0;
- size_t append_in_b = 0;
- double sum_time_wait = 0;
- list_imitate_queue(queue_a, queue_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting,
- &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, queue_size,
- &sum_time_wait, 1);
- printf("Apparat a worked for %lf (t.u.)\n", time_a);
- if (fabs(time_a_waiting) > EPS)
- printf("Apparat a waited for %lf (t.u.)\n", time_a_waiting);
- else
- printf("Apparat a worked all time\n");
- printf("Apparat b worked for %lf (t.u.)\n", time_b);
- if (fabs(time_b_waiting) > EPS)
- printf("Apparat b waited for %lf (t.u.)\n", time_b_waiting);
- else
- printf("Apparat b worked all time\n");
- printf("Apparat a worked %li times\n", (long int) cnt_a_work);
- printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
- wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict /= (1 - p);
- double model_time_predict;
- if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
- model_time_predict = wait_b_queue_predict;
- else
- model_time_predict = (max_process_b + min_process_b) / 2;
- double elems_check = (time_a + time_a_waiting) / model_time_predict;
- printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
- printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
- printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
- / MIN(elems_check, append_in_b) * 100));
- printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
- double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
- printf("Work time must be %lf\n", time_b_pred);
- printf("But simulation time work is %lf\n", time_b + time_b_waiting);
- printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
- - MIN(time_b_pred, time_b + time_b_waiting))
- / MIN(time_b_pred, time_b + time_b_waiting) * 100));
- for (list_descriptor_t *ptr = queue_a; ptr != NULL;)
- {
- list_descriptor_t *next = ptr -> next;
- free(ptr);
- ptr = NULL;
- ptr = next;
- }
- system("pause");
- }
- if (strcmp(s, "3\n") == 0)
- {
- double tmp;
- printf("Please, input minimum process time for first machine: ");
- if (scanf("%lf", &tmp) != 1 || tmp < 0)
- {
- printf("Wrong time!\n");
- system("pause");
- break;
- }
- min_process_a = tmp;
- printf("Please, input maximum process time for first machine: ");
- if (scanf("%lf", &tmp) != 1 || tmp < 0 || tmp < min_process_a)
- {
- printf("Wrong time!\n");
- system("pause");
- continue;
- }
- max_process_a = tmp;
- }
- if (strcmp(s, "4\n") == 0)
- {
- double tmp;
- printf("Please, input minimum process time for first machine: ");
- if (scanf("%lf", &tmp) != 1 || tmp < 0)
- {
- printf("Wrong time!\n");
- system("pause");
- continue;
- }
- min_process_b = tmp;
- printf("Please, input maximum process time for first machine: ");
- if (scanf("%lf", &tmp) != 1 || tmp < 0 || tmp < min_process_b)
- {
- printf("Wrong time!\n");
- system("pause");
- continue;
- }
- max_process_b = tmp;
- }
- if (strcmp(s, "5\n") == 0)
- {
- long int tmp;
- printf("Please, input amount of elements in first queue: ");
- if (scanf("%li", &tmp) != 1 || tmp <= 0)
- {
- printf("Wrong amount!\n");
- system("pause");
- continue;
- }
- queue_size = tmp;
- }
- if (strcmp(s, "6\n") == 0)
- {
- long int tmp;
- printf("Please, input amount of elements that the second machine needs to complete the process: ");
- if (scanf("%li", &tmp) != 1 || tmp <= 0)
- {
- printf("Wrong amount!\n");
- system("pause");
- continue;
- }
- elements_exit = tmp;
- }
- if (strcmp(s, "7\n") == 0)
- {
- double tmp;
- printf("Please, input new P: ");
- if (scanf("%lf", &tmp) != 1 || tmp <= 0 || tmp >= 1)
- {
- printf("Wrong P!\n");
- system("pause");
- continue;
- }
- p = tmp;
- }
- if (strcmp(s, "8\n") == 0)
- {
- clock_t time_list = clock();
- for(int i = 0; i < 100; i++)
- {
- double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict *= 1000;
- wait_b_queue_predict /= ((1 - p) * 1000);
- wait_b_queue_predict *= elements_exit;
- //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
- list_descriptor_t *queue_a = NULL;
- queue_a = full_list(queue_a, queue_size);
- //print_list(queue_a);
- list_descriptor_t *queue_b = NULL;
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- double time_a_waiting = 0;
- size_t cnt_a_work = 0;
- size_t append_in_b = 0;
- double sum_time_wait = 0;
- list_imitate_queue(queue_a, queue_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting,
- &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, queue_size,
- &sum_time_wait, 0);
- /*
- // printf("Apparat a worked %li times\n", (long int) cnt_a_work);
- // printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
- wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict /= (1 - p);
- // double model_time_predict;
- //if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
- model_time_predict = wait_b_queue_predict;
- else
- model_time_predict = (max_process_b + min_process_b) / 2;
- //double elems_check = (time_a + time_a_waiting) / model_time_predict;
- // printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
- //printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
- //printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
- // / MIN(elems_check, append_in_b) * 100));
- // printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
- double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
- // printf("Work time must be %lf\n", time_b_pred);
- // printf("But simulation time work is %lf\n", time_b + time_b_waiting);
- // printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
- - MIN(time_b_pred, time_b + time_b_waiting))
- / MIN(time_b_pred, time_b + time_b_waiting) * 100));
- */
- for (list_descriptor_t *ptr = queue_a; ptr != NULL;)
- {
- list_descriptor_t *next = ptr -> next;
- free(ptr);
- ptr = NULL;
- ptr = next;
- }
- }
- time_list = clock() - time_list;
- clock_t time_vector = clock();
- for(int i = 0; i < 100; i++)
- {
- double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict *= 1000;
- wait_b_queue_predict /= ((1 - p) * 1000);
- wait_b_queue_predict *= elements_exit;
- //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
- double *waiting_time;
- waiting_time = calloc(queue_size, sizeof(double));
- vector_descriptor_t queue_a;
- queue_a.p_in = waiting_time;
- queue_a.p_out = waiting_time;
- queue_a.low_border = waiting_time;
- queue_a.high_border = &(waiting_time[queue_size - 1]);
- queue_a.is_full = 1;
- queue_a.is_working = 0;
- vector_descriptor_t queue_b;
- queue_b.p_in = waiting_time;
- queue_b.p_out = waiting_time;
- queue_b.low_border = waiting_time;
- queue_b.high_border = &(waiting_time[queue_size - 1]);
- queue_b.is_full = 0;
- queue_b.is_working = 0;
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- double time_a_waiting = 0;
- size_t cnt_a_work = 0;
- size_t append_in_b = 0;
- vector_imitate_queue(queue_a, queue_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting,
- &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, 0);
- /*double sum_time_wait = 0;
- for (int i = 0; i < queue_size; i++)
- sum_time_wait += waiting_time[i];
- // printf("Apparat a worked for %lf (t.u.)\n", time_a);
- if (fabs(time_a_waiting) > EPS)
- //printf("Apparat a waited for %lf (t.u.)\n", time_a_waiting);
- else
- //printf("Apparat a worked all time\n");
- //printf("Apparat b worked for %lf (t.u.)\n", time_b);
- if (fabs(time_b_waiting) > EPS)
- // printf("Apparat b waited for %lf (t.u.)\n", time_b_waiting);
- else
- //printf("Apparat b worked all time\n");
- // printf("Apparat a worked %li times\n", (long int) cnt_a_work);
- // printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
- wait_b_queue_predict = (max_process_a + min_process_a) / 2;
- wait_b_queue_predict /= (1 - p);
- double model_time_predict;
- if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
- model_time_predict = wait_b_queue_predict;
- else
- model_time_predict = (max_process_b + min_process_b) / 2;
- double elems_check = (time_a + time_a_waiting) / model_time_predict;
- printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
- printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
- printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
- / MIN(elems_check, append_in_b) * 100));
- printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
- double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
- printf("Work time must be %lf\n", time_b_pred);
- printf("But simulation time work is %lf\n", time_b + time_b_waiting);
- printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
- - MIN(time_b_pred, time_b + time_b_waiting))
- / MIN(time_b_pred, time_b + time_b_waiting) * 100));
- */
- free(waiting_time);
- waiting_time = NULL;
- }
- time_vector = clock() - time_vector;
- printf("\n\nTime for vector %li\n\n", (long int) time_vector);
- printf("\n\nTime for list %li\n\n", (long int) time_list);
- printf("Precentage winning: %lf %% \n\n", ((double)time_list - time_vector) / time_vector * 100);
- }
- }
- return 0;
- }
- /*
- void vector_push(vector_descriptor_t *vector_descriptor)
- {
- if (vector_descriptor -> p_in == vector_descriptor -> high_border)
- vector_descriptor -> p_in = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_in)++;
- }
- int vector_pop(vector_descriptor_t *vector_descriptor)
- {
- if (vector_descriptor -> p_in == vector_descriptor -> p_out)
- return FULL_QUEUE;
- if (vector_descriptor -> p_out == vector_descriptor -> high_border)
- vector_descriptor -> p_out = vector_descriptor -> low_border;
- else
- (vector_descriptor -> p_out)++;
- return SUCCESS;
- }
- void vector_imitate_queue(vector_descriptor_t vector_descriptor_a,
- vector_descriptor_t vector_descriptor_b,
- float min_process_a, float max_process_a,
- float min_process_b, float max_process_b,
- double *time_a, double *time_b, double *time_b_waiting)
- {
- min_process_a =+ 0.001; // if min_proces == 0 we will have troubles
- int cnt_b_out = 0;
- double time_a_work = 0;
- double time_b_work = 0;
- time_a_work += (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- while (cnt_b_out != 100)
- {
- //printf("%lf ", (double) ((rand() % 100) / 100.0));
- if (fabs(time_a_work) > EPS)
- {
- if (fabs(time_b_work) > EPS)
- {
- double time_min = MIN(time_a_work, time_b_work);
- (*time_a) += time_min;
- (*time_b) += time_min;
- time_a_work -= time_min;
- time_b_work -= time_min;
- if (fabs(time_a_work) < EPS)
- {
- if(((rand() % 100)) > 70)
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_b);
- }
- else
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_a);
- }
- // if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
- // time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- }
- if (fabs(time_b_work) < EPS)
- {
- cnt_b_out++;
- if (!vector_pop(&vector_descriptor_b))
- vector_push(&vector_descriptor_a);
- }
- if (fabs(time_a_work) < EPS)
- if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- if (fabs(time_b_work) < EPS)
- if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- else
- {
- *time_b_waiting += time_a_work;
- *time_a += time_a_work;
- time_a_work = 0;
- if(((rand() % 100)) > 70)
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_b);
- }
- else
- {
- vector_pop(&vector_descriptor_a);
- vector_push(&vector_descriptor_a);
- }
- if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
- time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
- if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else
- {
- if (fabs(time_b_work) > EPS)
- {
- if (!vector_pop(&vector_descriptor_b))
- {
- cnt_b_out++;
- *time_b += time_b_work;
- time_b_work = 0;
- vector_push(&vector_descriptor_a);
- time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
- }
- }
- else
- {
- ;
- }
- }
- }
- return;
- }
- int main(void)
- {
- srand(time(NULL));
- //int choice;
- float min_process_a = 0;
- float max_process_a = 6;
- float min_process_b = 1;
- float max_process_b = 8;
- char s[16];
- s[0] = '\0';
- while (TRUE)
- {
- printf("\n");
- printf(" _ __ ___ ___ _ __ _ _ \n");
- printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
- printf("| | | | | | __/ | | | |_| |\n");
- printf("|_| |_| |_|\\___|_| |_|\\____|\n");
- printf("\n1: Imitate queue on vecotor");
- printf("\n2: Imitate queue on list");
- printf("\n0: EXIT\n");
- printf("Choice: ");
- fgets(s, 15, stdin);
- if (strcmp(s, "0\n") == 0)
- break;
- if (strcmp(s, "1\n") == 0)
- {
- double time_sum_a = 0;
- double time_sum_b = 0;
- double time_sum_diff = 0;
- for(int i = 0; i < 100; i++)
- {
- int queue_a [VECTOR_LEN];
- //random_vector(queue_a, VECTOR_LEN);
- // tbh this is not necessary, but why not?
- vector_descriptor_t vector_descriptor_a;
- vector_descriptor_a.p_out = queue_a;
- vector_descriptor_a.p_in = &(queue_a[99]);
- vector_descriptor_a.low_border = queue_a;
- vector_descriptor_a.high_border = &(queue_a[99]);
- int queue_b [VECTOR_LEN];
- vector_descriptor_t vector_descriptor_b;
- vector_descriptor_b.p_out = queue_b;
- vector_descriptor_b.p_in = queue_b;
- vector_descriptor_b.low_border = queue_b;
- vector_descriptor_b.high_border = &(queue_b[99]);
- double time_a = 0;
- double time_b = 0;
- double time_b_waiting = 0;
- vector_imitate_queue(vector_descriptor_a, vector_descriptor_b,
- min_process_a, max_process_a,
- min_process_b, max_process_b,
- &time_a, &time_b, &time_b_waiting);
- printf("time a = %lf\n", time_a);
- printf("time b = %lf\n", time_b);
- printf("\apparat b waiting time = %lf\n", time_b_waiting);
- time_sum_a += time_a;
- time_sum_b += time_b;
- time_sum_diff += time_b_waiting;
- }
- printf("time a average = %lf\n", time_sum_a / 100.0);
- printf("time b average = %lf\n", time_sum_b / 100.0);
- printf("\apparat b waiting time average = %lf\n", time_sum_diff / 100.0);
- }
- else if (strcmp(s, "2\n") == 0)
- {
- printf("LIST QUEUE");
- }
- else
- printf("\nWrong choice!");
- }
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement