Está en la página 1de 16

Problema Conjunto

a)

b)

c)

d)

e)

1
2
3
4
5
6
7
8
9
10

Estado del Sistema


i. Estado servidores
ii.
Estado de colas
iii.
Tiempo de simulacin
Entidades y sus atributos
i. Servidores
i. Estado.
ii.
Colas
i. Nmero de piezas
ii. Tiempo de espera
iii.
Piezas
i. Localizacin
ii. Tiempo
Eventos, Actividades y Procesos
i. Eventos
i. Llegada servidor A
ii. Llegada servidor B
iii. Termina Servidor A
iv. Termina Servidor B
v. Termina Servidor C
vi. Falla en Servidor A
vii. Falla en Servidor B
viii. Fin de la simulacin
ii.
Actividades
i. Fabricacin de una pieza
ii. Cambio de servidor de una pieza
iii. Cambio de cola de una pieza
Contadores estadsticos
i. Tiempo de espera en colas.
ii.
Tiempo de utilizacin de servidores
iii.
Piezas terminadas
Medidas de desempeo
i. Mejoramiento de tiempo en colas
ii.
Piezas en cola

#include
#include
#include
#include
#define
#define
#define
#define

<iostream>
<stdio.h>
<cstdlib>
"lcgrand.h"
Q_LIMIT 100
BUSY
1
IDLE
0
DMG

/* Header file for random-number generator. */


/* Limit on queue length. */
/* Mnemonics for server's being busy */
/* idle. */
2 /* and damaged. */

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

using namespace std;


int
guardar;
int
next_event_type, num_pc_delayed, num_events, server_A_status,
server_B_status, server_C_status;
int
num_in_q_A, num_in_q_NB, num_in_q_PB, num_in_q_NC, num_in_q_PC;
float area_server_status, mean_interarrival_a, mean_service,
sim_time, time_last_event, time_next_event[9],
total_of_delays_A, total_of_delays_NB, total_of_delays_PB,
total_of_delays_NC, total_of_delays_PC,
area_num_in_q_A, area_num_in_q_NB, area_num_in_q_PB,
area_num_in_q_NC,
area_num_in_q_PC, area_server_A_status, area_server_B_status,
area_server_C_status;
float time_arrival_A[Q_LIMIT + 1], time_arrival_NB[Q_LIMIT + 1],
time_arrival_PB[Q_LIMIT + 1],
time_arrival_NC[Q_LIMIT + 1], time_arrival_PC[Q_LIMIT + 1];

void initialize(void);
void timing(void);
void arrive_A(void);
void arrive_B(void);
void depart_A(void);
void depart_B(void);
void depart_C(void);
void dmg_A(void);
void dmg_B(void);
void report();
void update_time_avg_stats();
float uniform(float a, float b);
int main(int argc, char *argv[]) {
num_events = 9;
initialize();

do{
timing();
update_time_avg_stats();
printf("event %d time %f ", next_event_type, sim_time);
system("Pause");
switch (next_event_type){
case 1:
arrive_A();
break;
case 2:
arrive_B();

62
break;
63
case 3:
64
depart_A();
65
break;
66
case 4:
67
depart_B();
68
break;
69
case 5:
70
depart_C();
71
break;
72
case 6:
73
dmg_A();
74
break;
75
case 7:
76
dmg_B();
77
break;
78
case 8:
79
report();
80
/*Inicializo el reloj de simulacin en cero
81
muchas veces, entonces jams llego a las
82
40 horas
83
/*guardar = sim_time;
84
initialize();
85
time_next_event[8] = 1.0e+29;
86
sim_time=guardar;*/
87
break;
88
case 9:
89
report();
90
break;
91
}
92
}while (next_event_type != 8);
93
94
return 0;
95 }
96
97 void timing(void) /* Timing function. */
98 {
99
int
i;
100
float min_time_next_event = 1.0e+29;
101
102
next_event_type = 0;
103
104
/* Determine the event type of the next event to occur. */
105
106
for (i = 1; i <= num_events; ++i)
107
if (time_next_event[i] < min_time_next_event) {
108
min_time_next_event = time_next_event[i];
109
next_event_type
= i;
110
}
111
112

113
/* Check to see whether the event list is empty. */
114
115
if (next_event_type == 0) {
116
117
/* The event list is empty, so stop the simulation */
118
119
printf("\nEvent list empty at time %f", sim_time);
120
exit(1);
121
}
122
123
/* The event list is not empty, so advance the simulation clock. */
124
125
sim_time = min_time_next_event;
126 }
127
128 void initialize(void) /* Initialization function. */
129 {
130
/* Initialize the simulation clock. */
131
132
sim_time = 0.0;
133
134
/* Initialize the state variables. */
135
136
server_A_status
= IDLE;
137
server_B_status
= IDLE;
138
server_C_status
= IDLE;
139
num_in_q_A
= 0;
140
num_in_q_NB
= 0;
141
num_in_q_PB
= 0;
142
num_in_q_NC
= 0;
143
num_in_q_PC
= 0;
144
time_last_event
= 0.0;
145
total_of_delays_A = 0.0;
146
total_of_delays_NB = 0.0;
147
total_of_delays_PB = 0.0;
148
total_of_delays_NC = 0.0;
149
total_of_delays_PC = 0.0;
150
time_last_event = 0.0;
151
152
/* Initialize the statistical counters. */
153
154
num_pc_delayed = 0;
155
/*area_num_in_q
= 0.0;
156
area_server_status = 0.0;*/
157
158
/* Initialize event list. Since no customers are present, the departure
159
(service completion) event is eliminated from consideration. */
160
161
time_next_event[1] = sim_time + uniform(90, 110);
162
time_next_event[2] = sim_time + uniform(4, 16);
163
time_next_event[3] = 1.0e+30;

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

time_next_event[4]
time_next_event[5]
time_next_event[6]
time_next_event[7]
time_next_event[8]
time_next_event[9]

=
=
=
=
=
=

1.0e+30;
1.0e+30;
sim_time + uniform(50, 750);
sim_time + uniform(50, 350);
480.0;
2400.0;

area_num_in_q_A
area_num_in_q_NB
area_num_in_q_PB
area_num_in_q_NC
area_num_in_q_PC

=
=
=
=
=

0.0;
0.0;
0.0;
0.0;
0.0;

area_server_A_status = 0.0;
area_server_B_status = 0.0;
area_server_C_status = 0.0;
}
void arrive_A(void)
{

/* Arrival event function. */

/* Schedule next arrival. */


time_next_event[1] = sim_time + uniform(90, 110);
/* Check to see whether server is busy. */
if (server_A_status == BUSY) {
/* Server is busy, so increment number of customers in queue. */
++num_in_q_A;
/* Check to see whether an overflow condition exists. */
if (num_in_q_A > Q_LIMIT) {
/* The queue has overflowed, so stop the simulation. */
printf("\nOverflow of the array time_arrival A at");
printf(" time %f", sim_time);
exit(2);
}
/* There is still room in the queue, so store the time of arrival of
the
arriving customer at the (new) end of time_arrival. */
time_arrival_A[num_in_q_A] = sim_time;
}

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

else if (server_A_status == DMG){


if (server_B_status == IDLE){
server_B_status = BUSY;
time_next_event[4] = sim_time + uniform(80, 120);
}else{
++num_in_q_PB;
time_arrival_PB[num_in_q_PB] = sim_time;
}
}else{
server_A_status = BUSY;
/* Schedule a departure (service completion). */
time_next_event[3] = sim_time + uniform(75, 105);
}
}
void arrive_B(void){
time_next_event[2] = sim_time + uniform(4, 16);
if (server_B_status == BUSY || server_B_status == DMG){
++num_in_q_NB;
if(num_in_q_NB > Q_LIMIT){
/* The queue has overflowed, so stop the simulation.
*/
printf("\nOverflow of the array time_arrival NB at");
printf(" time %f", sim_time);
exit(2);
}
time_arrival_NB[num_in_q_NB] = sim_time;
}else{
server_B_status = BUSY;
time_next_event[4] = sim_time + uniform(3, 13);
}
}
void

depart_A(void){
if (server_A_status != DMG){

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

if (server_C_status == BUSY){
++num_in_q_PC;
if(num_in_q_PC > Q_LIMIT){
/* The queue has overflowed, so stop the
simulation. */
printf("\nOverflow of the array time_arrival PC at");
printf(" time %f", sim_time);
exit(2);
}
time_arrival_PC[num_in_q_PC] = sim_time;
}else{
server_C_status = BUSY;
time_next_event[5] = sim_time + uniform(2, 14);
}
}
int i;
float delay;
if (server_A_status == DMG){
/*Esta es la terminacin del dao, se asume que la pieza que
estaba
trabajando ya est en cola en B, o ya est siendo procesada
por B.
Ahora se procede a seguir con el procedimiento normal*/
server_A_status = IDLE;
time_next_event[3] = 1.0e+30;
time_next_event[6] = sim_time + uniform(50, 750);
}if (num_in_q_A == 0){
server_A_status = IDLE;
time_next_event[3] = 1.0e+30;
}else{
--num_in_q_A;
delay
= sim_time - time_arrival_A[1];
total_of_delays_A += delay;
time_next_event[3] = sim_time + uniform(75, 105);
/* Move each customer in queue (if any) up one place. */
for (i = 1; i <= num_in_q_A; ++i)
time_arrival_A[i] = time_arrival_A[i + 1];
}
}

void

depart_B(void){
if (server_B_status != DMG){

317
if (server_C_status == BUSY){
318
319
++num_in_q_NC;
320
if(num_in_q_NC > Q_LIMIT){
321
/* The queue has overflowed, so stop the
322 simulation. */
323
324
printf("\nOverflow of the array time_arrival PC at");
325
printf(" time %f", sim_time);
326
exit(2);
327
}
328
329
time_arrival_NC[num_in_q_NC] = sim_time;
330
}else{
331
332
server_C_status = BUSY;
333
time_next_event[5] = sim_time + uniform(2, 14);
334
}
335
}
336
int i;
337
float delay;
338
if (server_B_status == DMG){
339
/*Esta es la terminacin del dao, se asume que la pieza que
340
estaba
341
trabajando ya est en cola en B, o ya est siendo procesada
342
343 por B.
Ahora se procede a seguir con el procedimiento normal*/
344
server_B_status = IDLE;
345
time_next_event[4] = 1.0e+30;
346
time_next_event[7] = sim_time + uniform(50, 350);
347
}if (num_in_q_PB > 0){
348
--num_in_q_PB;
349
delay = sim_time - time_arrival_PB[1];
350
total_of_delays_PB += delay;
351
time_next_event[4] = sim_time + uniform(80, 120);
352
353
354
for (i = 1; i <= num_in_q_PB; ++i)
355
time_arrival_PB[i] = time_arrival_PB[i + 1];
356
}else if (num_in_q_NB == 0){
357
server_B_status = IDLE;
358
time_next_event[4] = 1.0e+30;
359
}else{
360
--num_in_q_NB;
361
delay = sim_time - time_arrival_NB[1];
362
total_of_delays_NB += delay;
363
time_next_event[4] = sim_time + uniform(3, 13);
364
365
for (i = 1; i <= num_in_q_NB; ++i)
366
time_arrival_NB[i] = time_arrival_NB[i + 1];
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

}
}
void

depart_C(void){
int i;
float delay;
++num_pc_delayed;
if (num_in_q_PC > 0){
--num_in_q_PC;
delay = sim_time - time_arrival_PC[1];
total_of_delays_PC += delay;
time_next_event[5] = sim_time + uniform(2, 14);
for (i = 1; i <= num_in_q_PC; ++i)
time_arrival_PC[i] = time_arrival_PC[i + 1];
}else if (num_in_q_NC == 0){
server_C_status = IDLE;
time_next_event[5] = 1.0e+30;
}else{
--num_in_q_NC;
delay = sim_time - time_arrival_NC[1];
total_of_delays_NC += delay;
time_next_event[5] = sim_time + uniform(2, 14);
for (i = 1; i <= num_in_q_NC; ++i)
time_arrival_NC[i] = time_arrival_NC[i + 1];
}

}
void

dmg_A(void){
server_A_status = DMG;
time_next_event[6] = sim_time + uniform(50, 750);
if (server_B_status == BUSY || server_B_status == DMG){
++num_in_q_PB;
if(num_in_q_PB > Q_LIMIT){
/* The queue has overflowed, so stop the simulation.

*/
printf("\nOverflow of the array time_arrival PB at");
printf(" time %f", sim_time);
exit(2);
}
time_arrival_PB[num_in_q_PB] = sim_time;
}else{
server_B_status = BUSY;
time_next_event[4] = sim_time + uniform(80, 120);
}
/*En esta parte le avisa al sistema que la interrupcion ha

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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

terminado*/
time_next_event[3] = sim_time + uniform(1, 29);
}

void

dmg_B(void){
server_B_status = DMG;
time_next_event[7] = sim_time + uniform(50, 350);
/*En esta parte le avisa al sistema que la interrupcion ha
terminado*/
time_next_event[4] = sim_time + uniform(2, 18);
}
void update_time_avg_stats(void)
average

/* Update area accumulators for timestatistics. */

{
float time_since_last_event;
/* Compute time since last event, and update last-event-time marker. */
time_since_last_event = sim_time - time_last_event;
time_last_event
= sim_time;
/* Update area under number-in-queue function. */
area_num_in_q_A
area_num_in_q_NB
area_num_in_q_PB
area_num_in_q_NC
area_num_in_q_PC

+= num_in_q_A *
+= num_in_q_NB
+= num_in_q_PB
+= num_in_q_NC
+= num_in_q_PC

time_since_last_event;
* time_since_last_event;
* time_since_last_event;
* time_since_last_event;
* time_since_last_event;

/* Update area under


area_server_A_status
area_server_B_status
area_server_C_status

server-busy indicator function. */


+= server_A_status * time_since_last_event;
+= server_B_status * time_since_last_event;
+= server_C_status * time_since_last_event;

}
void report(void) /* Report generator function. */
{
/* Compute and write estimates of desired measures of performance. */
/*printf("\n\nAverage delay in queue%11.3f minutes\n\n",
total_of_delays / num_custs_delayed);*/
printf("Average number in queue A%10.3f\n\n",
area_num_in_q_A / sim_time);
printf("Average number in queue NB%10.3f\n\n",
area_num_in_q_NB / sim_time);
printf("Average number in queue PB%10.3f\n\n",
area_num_in_q_PB / sim_time);
printf("Average number in queue NC%10.3f\n\n",

area_num_in_q_NC / sim_time);
printf("Average number in queue PC%10.3f\n\n",
area_num_in_q_PC / sim_time);
printf("Server A utilization%15.3f\n\n",
area_server_A_status / sim_time);
printf("Server B utilization%15.3f\n\n",
area_server_B_status / sim_time);
printf("Server C utilization%15.3f\n\n",
area_server_C_status / sim_time);
printf("Time simulation ended%12.3f minutes", sim_time);

470
471
472
473
474
475 }
476
477 float uniform(float a, float b) /* Uniform variate generation function. */
{
/* Return a U(a,b) random variate. */
return a + lcgrand(1) * (b - a);
}

Hay que tener como una consideracin adicional, que el tiempo de simulacin es
de 8 horas, pues los dueos estn interesados en el comportamiento de los
servidores en este tiempo.
Adicionalmente, en momentos en que el servidor A se encuentra Daado, o tiene
una falla, las piezas que lleguen, sern enviadas a la cola de espera del servidor, a
menos que el servidor B est disponible, por lo cual prioriza la pieza, y la empieza
a trabajar.

Problema Individual numero 7

f)
g)
h)

Estado del Sistema


i. Tiempo
ii.
Estacin actual
Entidades y sus atributos
i. Estacin
i. Tipo, probabilidad, tiempo.
Eventos, Actividades y Procesos
i. Eventos
i. Llegada a una estacin
ii.
Actividades
i. Medir tiempo de caminos
ii. Analizar probabilidad de camino
iii. Aumentar nmero de recorridos.

i)
j)

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

Contadores estadsticos
i. Tiempo recorrido actual.
ii.
Tiempo promedio total recorridos
Medidas de desempeo
i. Tiempo esperado
ii.
Tiempo real utilizado

#include <iostream>
#include lcgrand.h

/* Header file for random-number generator. */

int recorridos;
char sim_time, tipo;
float tiempoPromedioViajes, tiempoTotalViajesP, time_next_event,
prob_distrib_camA[3]={0.0,0.5,1.0}/*1 = camino 1; 2 = camino 2*/,
prob_distrib_camB[3]={0.0,0.8,1}/*1 = camino 3; 2 = camino 4*/,
prob_distrib_camC[3]={0.0,0.6,1}/*1 = camino 5; 2 = camino 6*/;
/* run this program using the console pauser or add your own getch,
system(pause) or input loop */
using namespace std;
void
void
void
int
float

initialize(void);
llegadaestacion(char T);
report(void);
random_integer(float prob_distrib []);
uniform(float a, float b);

int main(int argc, char *argv[]) {


/*Lee Archivos*/

initialize();
printf(Inicio de la XamaXacin \n);
while (recorridos <= 500){
llegadaestacion(tipo);
}
printf(Tiempo promedio total viajes %f, tiempoTotalViajesP/500);
return 0;
}
void initialize(void)
{
//sim_time = 0.0;
recorridos = 0;

43
time_next_event = 0.0;
44
tipo = A;
45
tiempoPromedioViajes = 0.0;
46
tiempoTotalViajesP = 0.0;
47
48 }
49
50 void llegadaestacion(char T){
51
int cam=0;
52
switch (T) {
53
case A:
54
tiempoPromedioViajes = 0.0;
55
printf(Recorrido %d Camino A , recorridos);
56
cam = random_integer(prob_distrib_camA);
57
if (cam == 1){
58
tiempoPromedioViajes += uniform(18, 22);
59
tipo = B;
60
}else{
61
tiempoPromedioViajes += uniform(14, 16);
62
tipo = C;
63
}
64
printf(%c , tipo);
65
break;
66
case B:
67
cam = random_integer(prob_distrib_camB);
68
if (cam == 1){
69
tiempoPromedioViajes += uniform(5, 6);
70
tipo = D;
71
}else{
72
tiempoPromedioViajes += uniform(6, 8);
73
tipo = E;
74
}
75
printf(%c , tipo);
76
break;
77
case C:
78
cam = random_integer(prob_distrib_camB);
79
if (cam == 1){
80
tiempoPromedioViajes += uniform(5, 7);
81
tipo = E;
82
}else{
83
tiempoPromedioViajes += uniform(8, 12);
84
tipo = F;
85
}
86
printf(%c , tipo);
87
break;
88
case D:
89
tiempoPromedioViajes += uniform(4, 6);
90
tipo = G;
91
printf(%c , tipo);
92
break;
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

case E:
tiempoPromedioViajes += uniform(5, 6);
tipo = G;
printf(%c , tipo);
break;
case F:
tiempoPromedioViajes += uniform(6, 9);
tipo = G;
printf(%c , tipo);
break;
case G:
tipo = A;
recorridos ++;
tiempoTotalViajesP += tiempoPromedioViajes;
printf(Tiempo = %f \n, tiempoPromedioViajes);
break;
}
}
int random_integer(float prob_distrib[])

/* Random integer generation


function. */

{
int
i;
float u;
/* Generate a U(0,1) random variate. */
u = lcgrand(1);
/* Return a random integer in accordance with the (cumulative)
distribution
function prob_distrib. */
for (i = 1; u >= prob_distrib[i]; ++i)
;
return i;
}
float uniform(float a, float b) /* Uniform variate generation function. */
{
/* Return a U(a,b) random variate. */
return a + lcgrand(1) * (b a);
}

Podemos ver que el tiempo promedio de todos los recorridos es de 29.399445


minutos. Adicionalmente cabe resaltar que tras las mltiples pruebas realizadas, el
camino ACEG es el ms rpido, el ACFG es el ms lento.

También podría gustarte