-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulate.c
452 lines (423 loc) · 9.86 KB
/
Simulate.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "Simulate.h"
//Function to find the floors the lift will be at for all times.
//VARIABLE EXPLANATION:-
// arr: floors array
// size: size of the array.
void findfloors(float * arr, int size)
{
int i;
int prev,aft;
int end;
for(i=0;i<=size;++i)
if(arr[i]!=-1)
arr[i]*=FLOOR_TIME;
for(i=0;i<size;++i)
{
if(arr[i]!=-1)
{
prev=arr[i];
end=arr[i];
}
else
{
int j=i;
while(arr[j]==-1 && j<size)j++;
aft=arr[j];
int temp=j;
j=i;
i=temp-1;
int m=1;
while(arr[j]==-1 && j<size)
{
if(prev>aft)
arr[j]=prev-m;
else if(prev==aft)
arr[j]=prev;
else
arr[j]=prev+m;
j++;
m++;
}
}
}
for(;i<10000;++i)
arr[i]=end;
return ;
}
//Function to create a new State (Lift's state).
//VARIABLE EXPLANATION:-
// curr_floor: current floor
// num_floors: total number of floors
// time: current time which you want to store in state.
// dir: direction of lift.
// lift_num: number of the lift.
State * State_new(int curr_floor, int num_floors, int time, int dir, int lift_num)
{
State * st = (State *)malloc(sizeof(State));
st->floor = curr_floor;
st->time = time;
st->dst = (int *)malloc((num_floors+1)*(sizeof(int)));
int i;
for(i=0;i<=num_floors;++i)
{
st->dst[i]=0;
}
for(i=0;i<10000;++i)
{
st->place_time[i]=-1;
}
st->place_time[0] = curr_floor;
st->lift_no = lift_num;
st->direction = dir;
return st;
}
void State_delete(State * st)
{
free(st->dst);
free(st);
}
//Function to make the lift to go to a patrticular floor
//VARIABLE EXPLANATION:-
// begin: starting floor of the lift
// end: destination of the lift
// st: state of the lift which needs to go to the floor
// qf: array of queues (one for each floor)
// floor: number of floors
// reserved: amount of lift space to be reserved when reaching end
void GoTo(int begin, int end, State * st, pqueue ** qf, int floor, int reserved)
{
if(begin==end)
return;
int direction = end>begin?1:-1;
person * p;
int i;
int flag=0;
if(direction==1)
for(i=0;i<=floor;++i)
{
flag=0;
if(direction*(i-begin)>=0 && direction*(end-i)>0)
{
st->floor = i;
st->num_people-=st->dst[i];
if(i-begin!=0)
{
st->time+=FLOOR_TIME;
st->place_time[st->time]=st->floor;
}
for(p=qf[i]->head;p!=NULL;p=p->next)
{
int ch1 = direction*(p->floor_dest-end);
int ch2 = direction*(p->floor_dest-i);
if(st->num_people<MAX_CAPACITY && ch2>0
&& p->lift==-1 && p->time==-1 && p->time_arrival <= st->time)
{
if(ch1<=0 || (st->num_people < MAX_CAPACITY - reserved && reserved>0))
{
st->num_people++;
p->lift = st->lift_no;
p->time = st->time;
st->dst[p->floor_dest]++;
flag=1;
}
}
}
if(flag==1 || st->dst[i]>0)
{
st->time+=WAIT_TIME;
st->place_time[st->time]=st->floor;
st->dst[i]=0;
}
}
if(i==end)
{
st->floor = i;
st->time+=FLOOR_TIME;
st->place_time[st->time]=st->floor;
return ;
}
}
else
for(i=floor;i>=0;--i)
{
flag=0;
if(direction*(i-begin)>=0 && direction*(end-i)>0)
{
st->floor = i;
st->num_people-=st->dst[i];
if(i-begin!=0)
{
st->time+=FLOOR_TIME;
st->place_time[st->time]=st->floor;
}
for(p=qf[i]->head;p!=NULL;p=p->next)
{
int ch1 = direction*(p->floor_dest-end);
int ch2 = direction*(p->floor_dest-i);
if(st->num_people<MAX_CAPACITY && ch2>0
&& p->lift==-1 && p->time==-1 && p->time_arrival <= st->time)
{
if(ch1<=0 || (st->num_people < MAX_CAPACITY - reserved && reserved>0))
{
st->num_people++;
p->lift = st->lift_no;
p->time = st->time;
st->dst[p->floor_dest]++;
flag=1;
}
}
}
if(flag==1 || st->dst[i]>0)
{
st->time+=WAIT_TIME;
st->place_time[st->time]=st->floor;
st->dst[i]=0;
}
}
if(i==end)
{
st->floor = i;
st->time+=FLOOR_TIME;
st->place_time[st->time]=st->floor;
return ;
}
}
}
//function for picking up people at their floors.
//VARIABLE EXPLANATION:-
// floor: floor at which the lift picks people up
// st: lift state
// qf: array of floor queues.
void Pickup(int floor, State * st, pqueue ** qf)
{
person * p;
pqueue * fl = qf[floor];
st->num_people-=st->dst[floor];
st->dst[floor]=0;
int dir=0;
for(p=fl->head;p!=NULL;p=p->next)
{
//Checking if there is space and if the person is not already pickedup
if(st->num_people<MAX_CAPACITY && p->lift==-1 && p->time==-1)
{
if(dir==0)
{
if(st->time<p->time_arrival)
{
st->time = p->time_arrival;
st->place_time[st->time] = st->floor;
}
if(p->floor_dest>floor)
dir=1;
else
dir=-1;
}
else
{
if(dir*(p->floor_dest-floor)<=0)
{
continue;
}
}
st->num_people++;
p->lift = st->lift_no;
p->time = st->time;
st->dst[p->floor_dest]++;
}
}
st->time+=WAIT_TIME;
st->place_time[st->time]=st->floor;
}
//function for dropping people at their floors.
//VARIABLE EXPLANATION:-
// begin: floor at which lift starts.
// end: floor at which lift ends.
// st: state of the system.
// qf: queue to be passed.
// floor: floor at which the lift drops.
void Drop(int begin, int end, State * st, pqueue ** qf, int floor)
{
int direction = end>begin?1:-1;
person * p;
int i;
int flag=0;
if(direction==1)
for(i=0;i<=floor;++i)
{
flag=0;
if(direction*(i-begin)>0)
{
st->floor = i;
st->num_people-=st->dst[i];
st->time+=FLOOR_TIME;
st->place_time[st->time]=st->floor;
if(st->num_people==0)
{
st->num_people+=st->dst[i];
return ;
}
for(p=qf[i]->head;p!=NULL;p=p->next)
{
int ch1 = direction*(p->floor_dest-end);
int ch2 = direction*(p->floor_dest-i);
if(st->num_people<MAX_CAPACITY && ch2>0
&& p->lift==-1 && p->time==-1 && p->time_arrival <= st->time)
{
st->num_people++;
p->lift = st->lift_no;
p->time = st->time;
flag=1;
st->dst[p->floor_dest]++;
}
}
if(flag==1 || st->dst[i]>0)
{
st->time+=WAIT_TIME;
st->place_time[st->time]=st->floor;
st->dst[i]=0;
}
}
}
else
for(i=floor;i>=0;--i)
{
flag=0;
if(direction*(i-begin)>0)
{
st->floor = i;
st->num_people-=st->dst[i];
st->time+=FLOOR_TIME;
st->place_time[st->time]=st->floor;
if(st->num_people==0)
{
st->num_people+=st->dst[i];
return ;
}
for(p=qf[i]->head;p!=NULL;p=p->next)
{
int ch1 = direction*(p->floor_dest-end);
int ch2 = direction*(p->floor_dest-i);
if(st->num_people<MAX_CAPACITY && ch2>0
&& p->lift==-1 && p->time==-1 && p->time_arrival <= st->time)
{
st->num_people++;
p->lift = st->lift_no;
p->time = st->time;
flag=1;
st->dst[p->floor_dest]++;
}
}
if(flag==1 || st->dst[i]>0)
{
st->time+=WAIT_TIME;
st->place_time[st->time]=st->floor;
st->dst[i]=0;
}
}
}
st->dst[end]=0;
st->num_people=0;
}
//function to calculate num of persons in the queue.
//VARIABLE EXPLANATION:-
// pq: queue to be passed for each floor.
int CalculateNumPersons(pqueue * pq)
{
person * p;
int flag=0;
int count=0;
int prevtime=0;
for(p=pq->head;p!=NULL;p=p->next)
{
if(flag==0)
{
if(p->time==-1)
{
flag=1;
count++;
prevtime=p->time_arrival;
}
}
else
{
if(p->time_arrival==prevtime && p->time==-1)
count++;
}
}
return count;
}
//Function to simulate the twin lifts.
//VARIABLE EXPLANATION:-
// qf: array of array of queues
// mq: main queue which contains people int their order of ariival.
// num_floors: total number of floors.
void Simulate(pqueue ** qf, pqueue * mq, int num_floors)
{
State * lift1 = State_new(0, num_floors, 0, 1, 1);
State * lift2 = State_new(num_floors, num_floors, 0, -1, 2);
person * p;
for(p=mq->head;p!=NULL;p=p->mainnext)
{
if(p->time!=-1)
continue;
int reserved = CalculateNumPersons(qf[p->floor_arrival]);
int a1 = lift1->floor-p->floor_arrival;
int a2 = lift2->floor-p->floor_arrival;
if(a1<0)a1*=-1;
if(a2<0)a2*=-1;
a1*=FLOOR_TIME;
a2*=FLOOR_TIME;
if(a1+lift1->time<=a2+lift2->time)
{
GoTo(lift1->floor, p->floor_arrival, lift1, qf, num_floors, reserved);
Pickup(p->floor_arrival, lift1, qf);
Drop(lift1->floor, p->floor_dest, lift1, qf, num_floors);
}
else
{
GoTo(lift2->floor, p->floor_arrival, lift2, qf, num_floors, reserved);
Pickup(p->floor_arrival, lift2, qf);
Drop(lift2->floor, p->floor_dest, lift2, qf, num_floors);
}
printf("Lift 1: %d %d\n",lift1->time,lift1->floor);
printf("Lift 2: %d %d\n",lift2->time,lift2->floor);
}
lift1->num_people-=lift1->dst[lift1->floor];
lift2->num_people-=lift2->dst[lift2->floor];
if(lift1->num_people==0 && lift2->num_people==0)
printf("\nSuccessfuly simulated!!\n\n");
int j;
lift1->place_time[lift1->time+1] = lift1->floor;
lift2->place_time[lift2->time+1] = lift2->floor;
findfloors(lift1->place_time,lift1->time+1);
findfloors(lift2->place_time,lift2->time+1);
/*printf("\nLift1 : \n");
for(j=0;j<=lift1->time;++j)
{
printf("%d %.1f\n",j,lift1->place_time[j]);
}
printf("\nLift2 : \n");
for(j=0;j<=lift2->time;++j)
{
printf("%d %.1f\n",j,lift2->place_time[j]);
}*/
state_print(qf, num_floors, lift1, lift2, lift1->time>lift2->time?lift1->time:lift2->time);
}
//function to calculate mean time.
//VARIABLE EXPLANATION:-
// mq: main queue to be passed.
float CalculateMeanTime(pqueue * mq)
{
long long int sum=0;
int num_of_people=0;
person * p;
for(p=mq->head;p!=NULL;p=p->mainnext)
{
sum+=(p->time - p->time_arrival);
num_of_people++;
}
if(num_of_people==0)
return 0;
else
return sum*1.0/num_of_people;
}