-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproyectoIII.c
454 lines (390 loc) · 11 KB
/
proyectoIII.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
453
454
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
// ============ VARIABLES Y FUNCIONES GLOBALES ==============
int senal = 0; // Cambia de valor cuando ha pasado un segundo.
int finEjecucion = 0; // Si es 1 , significa que ya el programa debe finalizar por que ha llegado al tiempo limite
void* runTime(void* arg); // Este hilo toma el tiempo de ejecucion.
void * cambioSegundo(void* arg); // Este hilo cambia la variable global senal cada segundo.
// ============ CONFIGURACION DE LA MATRIZ (MAPA) ==============
int*** mat;
int filas;
int columnas;
int tamArray = 4; // Es fijo porque son 4 tipos de agente.
// Posiciones de agentes: 0 = Rectos | 1 = Ciclicos | 2 = Aleatorios | 3 = Estaticos
void configuracionMapa();
// ===================== CANTIDADES =====================
int cantEnfermos = 0;
int cantSanos = 0;
int cantVacunados = 0;
// ===================== CODE-VID =====================
// Del archivo de config
float probMuerte = 0; // Probabilidad que muera.
int tiempoMuerte = 0; // Tiempo que tarda en morir
int tiempoCurar = 0; // Tiempo que se tarda en sanar
int reinfeccion = 0; // 0 = No hay reinfeccion | 1 = Si hay reinfeccion.
// ===================== AGENTES =====================
// variables o funciones globales...
char* estado;
int* velocMax;
int* velocMin;
void configuracionAgente();
// ===================== VACUNAS =====================
// variables o funciones globales...
// ============ TIPO DE AGENTE: CICLICO ==============
//
pthread_mutex_t mensaje;
pthread_mutex_t modificar;
void calcForward(int* direccion, int* x, int* y);
int calcBackward(int direccion);
void* ciclico(void* arg){
int tipo = 1; // Ciclico
int miGrupo = *(int*) arg;
char miEstado = estado[miGrupo];
//Generar una posicion aleatoria
int x = 0;
int y = 0;
int antX = 0;
int antY = 0;
int bandera = 0;
while(!bandera){
x = (rand() % ((filas - 1) - 0 + 1));
y = (rand() % ((columnas - 1) - 0 + 1));
if(mat[x][y][0] != -1){ // SI no es una pared...
bandera = 1;
if(miEstado == 'e'){
antX = x;
antY = y;
pthread_mutex_lock(&modificar);
mat[x][y][tipo]++;
pthread_mutex_unlock(&modificar);
}
}
}
//Definiendo ruta...
int pasos = 0;
int forward[7];
int backwards[7];
int direccion;
bandera = 0;
while(pasos < 7){
antX = x;
antY = y;
pthread_mutex_lock(&mensaje); // BORRAR MUTEX !!!
direccion = (rand() % ((8 - 1) - 1 + 1)) + 1;
calcForward(&direccion, &x, &y);
if(x<0 || y<0 || x>=filas || y>=columnas){
printf("hay una pared (%i,%i)\n", x, y);
x = antX;
y = antY;
bandera = 1; // Hay una pared en medio de su camino.
break;
}
printf("x: %i, y: %i\n",x, y); // BORRAR MUTEX !!!
pthread_mutex_unlock(&mensaje);
if(mat[x][y][0] == -1){
x = antX;
y = antY;
bandera = 1; // Hay una pared en medio de su camino.
break;
}
if(miEstado == 'e'){
pthread_mutex_lock(&modificar);
mat[antX][antY][tipo]--;
mat[x][y][tipo]++;
pthread_mutex_unlock(&modificar);
forward[pasos] = direccion;
}
if(miEstado == 's'){
}
pasos++;
}
if(bandera != 1){ pasos--; }
int pasosFinal = pasos;
// Determinando la ruta hacia atras...
int backwardCount = 0;
while(pasos >= 0){
antX = x;
antY = y;
direccion = calcBackward(forward[pasos]);
calcForward(&direccion, &x, &y);
//printf("x: %i, y: %i\n",x, y);
if(miEstado == 'e'){
pthread_mutex_lock(&modificar);
mat[antX][antY][tipo]--;
mat[x][y][tipo]++;
pthread_mutex_unlock(&modificar);
backwards[backwardCount] = direccion;
}
if(miEstado == 's'){
}
backwardCount++;
pasos--;
}
backwardCount--;
int backwardFinal = backwardCount;
int steps = 0;
int secondCount = 0;
int morire = 0; // :(
pasos = 0;
backwardCount = 0;
// while(!finEjecucion){
// // REVISAR CENTRO DE VACUNACION
// if(morire == 1){
// }
// // Si se sale de este ciclo, significa que ya ha pasado un segundo.
// while(steps<=velocMax[miGrupo] || steps>=velocMin[miGrupo]){
// pasos = 0;
// backwardCount = 0;
// // Walk forward
// while(pasos<=pasosFinal){
// antX = x;
// antY = y;
// direccion = forward[pasos];
// calcForward(&direccion, &x, &y);
// if(miEstado == 'e'){
// pthread_mutex_lock(&modificar);
// mat[antX][antY][tipo]--;
// mat[x][y][tipo]++;
// pthread_mutex_unlock(&modificar);
// backwards[backwardCount] = direccion;
// }
// if(miEstado == 's'){
// for(int i = 0; i<tamArray; i++){
// if(mat[x][y][i] > 0){ // hay personas enfermas...
// // calculando si me contagio con este tipo de agente de acuerdo al archivo de config...
// // if(si me enfermo, detengo este loop porque es solamente para validar si me enfermo o no) {
// // miEstado = 'e';
// // morire = 1; // :(
// // break;
// // }
// // si no, sigo buscando en las demas casillas...
// }
// }
// }
// pasos++;
// steps++;
// }
// }
// steps = 0;
// }
}
void calcForward(int* direccion, int* x, int* y){
switch(*direccion){
case 1: *x = *x - 1;
break;
case 2: *x = *x - 1; *y = *y + 1;
break;
case 3: *y = *y + 1;
break;
case 4: *x = *x + 1; *y = *y + 1;
break;
case 5: *x = *x + 1;
break;
case 6: *x = *x + 1; *y = *y - 1;
break;
case 7: *y = *y - 1;
break;
case 8: *x = *x - 1; *y = *y - 1;
break;
}
}
int calcBackward(int direccion){
switch(direccion){
case 1: return 5;
case 2: return 6;
case 3: return 7;
case 4: return 8;
case 5: return 1;
case 6: return 2;
case 7: return 3;
case 8: return 4;
}
}
// ============ TIPO DE AGENTE: ALEATORIO ==============
void* aleatorio(void* arg){
int tipo = 2; // aleatorio
int miGrupo = *(int*) arg;
//Generar una posicion aleatoria
int x = 0;
int y = 0;
char miEstado = estado[miGrupo];
int bandera = 0;
while(!bandera){
x = (rand() % ((filas - 1) - 0 + 1));
y = (rand() % ((columnas - 1) - 0 + 1));
if(mat[x][y][0] != -1){ // SI no es una pared...
bandera = 1;
if(miEstado == 'e'){
mat[x][y][tipo]++;
}
else{
mat[x][y][4]++;
}
}
}
}
void * cambioSegundo(void* arg){
while(!finEjecucion){
sleep(1);
if(!senal){ senal = 1; }
else{senal = 0;}
}
}
// Este hilo toma el tiempo de ejecucion.
void* runTime(void* arg){
int timeLimit = *(int*) arg;
sleep(timeLimit);
finEjecucion = 1;
}
// =========================================================================================================================
int main (int argc, char **argv)
{
int *tiempo = malloc(sizeof(*tiempo));
*tiempo = atoi(argv[1]);
pthread_mutex_init(&mensaje, NULL);
pthread_mutex_init(&modificar, NULL);
srand(time(0));
configuracionMapa();
configuracionAgente();
// Se crea un hilo que vaya tomando el tiempo de ejecucion.
pthread_t timeID;
pthread_create(&timeID, NULL, runTime, tiempo);
// Hilo que modifica la variable global "senal" cada segundo.
pthread_t segundosID;
pthread_create(&segundosID, NULL, cambioSegundo, NULL);
pthread_join(timeID, NULL);
pthread_join(segundosID, NULL);
// for(int i = 0; i<filas; i++){
// for(int j = 0; j<columnas; j++)
// printf("%i ",mat[i][j][1]);
// printf("\n");
// }
pthread_mutex_destroy(&mensaje);
pthread_mutex_destroy(&modificar);
// Importante mantener esto al final del main porque
// borra la memoria de la matriz dinamica.
// Se libera la memoria de la matriz
for (int i = 0; i < filas; i++)
{
for (int j = 0; j < columnas; j++) {
free(mat[i][j]);
}
free(mat[i]);
}
free(mat);
free(estado);
free(velocMin);
free(velocMax);
return 0;
}
// =========================================================================================================================
// Funcion que lee el archivo de configuracion del mapa ("configMapa.txt")
// y crea la matriz en base a los valores leidos.
void configuracionMapa(){
char singleLine[150];
FILE * fPointer = fopen("configMapa.txt", "r");
fgets(singleLine, 150, fPointer);
// Se lee la cantidad de filas y columnas
sscanf(singleLine, "%i %i\n", &columnas, &filas);
// Creacion de la matriz dinamica
mat = (int***)malloc(filas * sizeof(int**));
if(mat == NULL){
printf("Out of memory!\n");
exit(0);
}
for(int i = 0; i < filas; i++){
mat[i] = (int**) malloc(columnas * sizeof(int*));
if(mat[i] == NULL){
printf("Out of memory!\n");
exit(0);
}
for(int j = 0; j<columnas; j++){
mat[i][j] = (int*)malloc(tamArray*sizeof(int));
if(mat[i][j] == NULL){
printf("Out of memory!\n");
exit(0);
}
}
}
// FALTA POR IMPLEMENTAR ...
// Se podria poner la regla de que si uno de los campos del
// array es -1, significa que es una pared
// Se lee la cantidad de paredes
// fgets(singleLine, 150, fPointer);
// int cantPared = 0;
// int x1, y1, x2, y2;
// scanf(singleLine, "%i", &cantPared);
// for(int i = 0; i < cantPared; i++){
// scanf(singleLine, "%i %i %i %i", &x1, &y1, &x2, &y2);
// }
fclose(fPointer);
}
void configuracionAgente(){
char singleLine[150];
FILE * fPointer = fopen("configAgente.txt", "r");
fgets(singleLine, 150, fPointer);
int cantidadGrupos = 0;
//Se lee la cantidad de grupos
sscanf(singleLine, "%i", &cantidadGrupos);
estado = (char*) malloc(cantidadGrupos * (sizeof(char)));
velocMin = (int*) malloc(cantidadGrupos * (sizeof(int)));
velocMax = (int*) malloc(cantidadGrupos * (sizeof(int)));
if(estado == NULL || velocMin == NULL || velocMax == NULL){
printf("Out of memory!\n");
exit(0);
}
int cantAgentes = 0;
int tipoAgente = 0;
int vMin = 0;
int vMax = 0;
char estadoConf = 'l';
for(int i = 0; i < cantidadGrupos; i++){
fgets(singleLine, 150, fPointer);
sscanf(singleLine, "%i", &cantAgentes);
fgets(singleLine, 150, fPointer);
sscanf(singleLine, "%i", &tipoAgente);
fgets(singleLine, 150, fPointer);
sscanf(singleLine, "%i %i", &vMin, &vMax);
fgets(singleLine, 150, fPointer);
sscanf(singleLine, "%c", &estadoConf);
estado[i] = estadoConf;
velocMin[i] = vMin;
velocMax[i] = vMax;
int * arg = malloc(sizeof(*arg));
*arg = i;
switch(tipoAgente){
case 1: // Rectos
break;
case 2: // Ciclicos
// generar un hilo que genere otros hilos.
for(int j = 0; j < cantAgentes; j++){
pthread_t myId;
pthread_create(&myId, NULL, ciclico, arg);
}
break;
case 3: // Aleatorios
break;
case 4: // Estaticos
break;
}
}
fclose(fPointer);
}
// ejemplo para imprimir la matriz entera
// printf("tamArray es: %i\n", tamArray);
// for(int a = 0; a<filas; a++){
// for(int b = 0; b<columnas; b++){
// printf("%i, %i es: \n", a, b);
// for(int c = 0; c<tamArray; c++){
// printf("%i (%i)\n", mat[a][b][c], c);
// }
// }
// }
// pthread_mutex_lock(&mensaje);
// printf("Soy un hilo del grupo %i y mis datos son los siguientes: \n", miGrupo);
// printf("Velocidad maxima: %i\n", velocMax[miGrupo]);
// printf("Velocidad minima: %i\n", velocMin[miGrupo]);
// printf("Estado: %c\n", estado[miGrupo]);
// pthread_mutex_unlock(&mensaje);