-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunOneSimulation.c
139 lines (122 loc) · 4.93 KB
/
runOneSimulation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "runOneSimulation.h"
/* Runs the actual simulation of the program*/
extern float* runOneSimulation(float leftTraffic ,float rightTraffic ,int rightTrafficLight,int leftTrafficLight)
{
const gsl_rng_type *T;
gsl_rng *r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc(T);
gsl_rng_set(r,time(0));
int time_iteration = 1; /*inital start time*/
node_t *right = NULL; /*initial node*/
node_t *left = NULL; /*intial left node*/
int left_time = gsl_rng_uniform_int(r,1); /*randomly checks if 0 or 1 gets chosen to determine if left starts first*/
int right_time = left_time == 0 ? 1 : 0;/* checks if left is True ,if not*/
int left_time_count = 0;
int right_time_count = 0;
int rightMax = 0;
int leftMax = 0;
int total_left_waiting = 0;
int total_right_waiting = 0;
int left_vehicle_count = 0;
int right_vehicle_count = 0;
int total_left_clearance = 0;
int total_right_clearance = 0;
float* traffic_data;
/* check if there is adequate memory*/
if ( !(traffic_data = malloc(sizeof(int)*9)))
{
fprintf(stderr,"Out of memory\n");
exit(1);
}else{
traffic_data = malloc(sizeof(float)*9);
}
while (time_iteration != 0) {
if ((leftTraffic >= gsl_ran_flat(r,0,1)) && (time_iteration < 501)){
enqueue(&left, time_iteration);
}
if((rightTraffic >= gsl_ran_flat(r,0,1)) && (time_iteration < 501)){
enqueue(&right, time_iteration);
}
if(left_time == 1){
if (left_time_count!=leftTrafficLight) /* check time left for traffic counter*/
{ left_time_count += 1; /*increments time counter*/
if(isEmpty(&left)==0) /* checks if queue is empty*/
{
int queue_time;
int val = dequeue(&left);
/*vehicle count*/
left_vehicle_count += 1;
queue_time = time_iteration - val;
/*for calculating average total*/
total_left_waiting += queue_time;
/*Max waiting time*/
if(queue_time>leftMax){
leftMax = queue_time;
}
/* total clearance time*/
if(time_iteration>500){
total_left_clearance = time_iteration-500;
} else {
total_left_clearance = 0;
}
}
}
else
{ /*swaps the traffic light signs */
right_time = 1;
left_time = 0;
left_time_count = 0;
}
}
if (right_time == 1) {
if(right_time_count != rightTrafficLight) {
right_time_count = right_time_count + 1;
if(isEmpty(&right)==0) /* checks if queue is empty*/
{
int queue_time;
int val = dequeue(&right);
/*vehicle count*/
right_vehicle_count = right_vehicle_count + 1;
queue_time = time_iteration - val;
/*for calculating average total*/
total_right_waiting = total_right_waiting + queue_time;
/*Max waiting time*/
if(queue_time>rightMax){
rightMax = queue_time;
}
/* total clearance time*/
if(time_iteration>500){
total_right_clearance = time_iteration - 500;
} else{
total_right_clearance = 0;
}
}
}
else
{ /*swaps the traffic light signs */
left_time = 1;
right_time = 0;
right_time_count=0;
}
}
/*checks if the queues are empty and the vehicles have stopped coming and then gathers all the data*/
if(isEmpty(&right) == 1 && isEmpty(&left) == 1 && time_iteration>500){
float average_left_lane = (float)total_left_waiting/(float)left_vehicle_count;
float average_right_lane = (float)total_right_waiting/(float)right_vehicle_count;
traffic_data[0] = (float)left_vehicle_count;
traffic_data[1] = average_left_lane;
traffic_data[2] = (float)leftMax;
traffic_data[3] = (float)total_left_clearance;
traffic_data[4] = (float)right_vehicle_count;
traffic_data[5] = average_right_lane;
traffic_data[6] = (float)rightMax;
traffic_data[7] = (float)total_right_clearance;
gsl_rng_free(r);
break;
}
time_iteration +=1;
}
return traffic_data ;
};