This repository was archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheuristica.c
384 lines (305 loc) · 13.3 KB
/
heuristica.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
/*
============================================================================
TRABALHO PRÁTICO 6 - Paralelismo
Algoritmos e Estruturas de Dados III
Bruno Maciel Peres
HEURISITCA.C - Define as operações que realizarão o método heurístico e avaliarão a qualidade dos resultados através do índice de satisfabilidade
============================================================================
*/
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "heuristica.h"
#include "lista.h"
#include "io.h"
#include "timerlinux.h"
#define NOMEARQLATEX "Analise-latex.txt"
#define NOMEARQGNUPLOT "Analise-gnuplot.txt"
GlobalVars global;
pthread_mutex_t mutex_global;
pthread_mutex_t* mutex_mulheres;
pthread_mutex_t* mutex_homens;
void *casa_homem_SEQ(void *t);
void men_porpose_algorithm_SEQ(int n);
LISTA * AlocaVetLista(int n) {
LISTA * mulher;
int i;
mulher = (LISTA *)malloc((n+1) * sizeof (LISTA)); //Aloca um vetor de listas para guardar as ids de preferências de n mulheres
if (mulher == NULL) exit (1); //Finaliza o programa com status de erro
for (i = 0; i <= n; i++) {
lista_criar(&mulher[i]);
}
return mulher;
}
void DesalocaVetLista(LISTA *l, int n) {
int i;
for (i=0; i<=n; i++)
lista_apagar(&l[i]);
lista_apagar(l);
}
void CalculaMetricas(float *satisf_masc, float *satisf_fem, float *satisf_geral, int n) {
*satisf_masc = calcula_satisfabilidade(global.homem, n);
*satisf_fem = calcula_satisfabilidade(global.mulher, n);
*satisf_geral = calcula_satisfabilidade_geral(*satisf_masc, *satisf_fem);
}
//Mostra os casais formados até agora
//Omite aqueles que não estão casados
void mostracasais(LISTA* mulher, LISTA* homem, int n) {
int i;
for (i=1; i<=n; i++)
printf ("mulher %d casa com homem %d\n", i, mulher[i].casado_com_id);
printf ("\n");
}
void SMP(FILE *ArqEntrada, int numThreads, FILE *ArqSaida, bool Analise) {
int j,n;
//Le a quantidade de instâncias do arquivo de entrada
int instancias = LeInt(ArqEntrada);
float satisf_masc;
float satisf_fem;
float satisf_geral;
FILE *ArqAnaliseLatex = fopen(NOMEARQLATEX,"a");
FILE *ArqAnaliseGnuPlot = fopen(NOMEARQGNUPLOT,"a");
float tempoexec;
stopWatch cronometro;
//Executa 'instancias' vezes
for (j=0; j<instancias; j++) {
n = LeInt(ArqEntrada); //Lê o número n de indíviduos no conjunto
//Criar vetor de listas alocadas dinamicamente
global.mulher = AlocaVetLista(n);
global.homem = AlocaVetLista(n);
//Povoar as listas de homens e de mulheres
LeituraInput(ArqEntrada, n, global.mulher, global.homem);
if (Analise) startTimer(&cronometro);
//Heurística
men_porpose_algorithm(n, numThreads);
//Calcular Satisfabilidades
CalculaMetricas(&satisf_masc, &satisf_fem, &satisf_geral, n);
if (Analise) stopTimer(&cronometro);
if (Analise) tempoexec = getElapsedTime(&cronometro);
if (Analise) EscreveAnaliseLatex(ArqAnaliseLatex, numThreads, n, tempoexec);
if (Analise) EscreveAnaliseGnuPlot(ArqAnaliseGnuPlot, numThreads, n, tempoexec);
//Escreve os resultados no arquivo
EscreveResultados(ArqSaida, global.mulher, global.homem, n, satisf_fem, satisf_masc, satisf_geral);
//Liberação da memória alocada para as listas de homem e de mulher
DesalocaVetLista(global.homem, n);
DesalocaVetLista(global.mulher, n);
free(global.homem);
free(global.mulher);
}
fclose (ArqAnaliseLatex);
fclose (ArqAnaliseGnuPlot);
}
//Calcula a satisfabilidade de uma lista após o algoritmo de casamento
float calcula_satisfabilidade(LISTA lista[], int n) {
int i;
NO* cursor;
float satisf=0;
for (i=1; i<=n; i++) {
cursor = lista[i].inicio;
while (cursor->item.valor != lista[i].casado_com_id) { //Continua incrementando somente se não estiver na primeira posição
cursor = cursor->proximo;
}
satisf+=cursor->item.indice;
}
return satisf /= n;
}
//Calcula a satisfabilidade após o algoritmo de casamento
float calcula_satisfabilidade_geral(float satisf_fem, float satisf_masc) {
return (satisf_fem+satisf_masc)/2;
}
//Perorre a lista de preferência da mulher à procura das ids dos pretendentes conflitantes, retorna o que for mais interessante para a mulher
//O(n) pois percorre as prioridades da mulher
NO* desempata(LISTA* mulher, int id_homem1, int id_homem2) {
NO* cursor;
cursor = mulher->inicio; //Recebe a ordem de pretendentes que o interessado quer casar
while (cursor != NULL) { //Percorre todos os pretendentes do interessado na lista de preferência
if (cursor->item.valor == id_homem1) {
return cursor;
} else if(cursor->item.valor == id_homem2) {
return cursor;
}
cursor = cursor->proximo;
}
return cursor;
}
//Retorna 1 se a pessoa estiver casada e 0 se não
int casado(LISTA* pretendente) {
return pretendente->casado;
}
//Altera o estado de casado dentre dois casados para CASADOS
void casa(LISTA* pessoa1, int id_pessoa1, LISTA* pessoa2, int id_pessoa2) {
pessoa1->casado_com_id = id_pessoa2;
pessoa2->casado_com_id = id_pessoa1;
pessoa1->casado = true;
pessoa2->casado = true;
}
//Altera o estado de casado dentre dois casados para NÃO casados
void descasa(LISTA* pessoa1, LISTA* pessoa2) {
pessoa1->casado_com_id = 0;
pessoa2->casado_com_id = 0;
pessoa1->casado = false;
pessoa2->casado = false;
}
//Recebe a lista de preferencias de um homem e casa esse homem, respeitando critério de desempate da heurística
void *casa_homem(void *t) {
//Pegar a id da thread
int i = (int) t;
int id_homem = i; //Função tenta casar o homem atual
pthread_mutex_lock (&mutex_homens[id_homem]);
NO* cursor = global.homem[i].inicio; //Percorrerá a lista de preferência do homem
int id_mulher = cursor->item.valor;
int id_homem_ja_casado; //Id do homem que disputará com o homem id_homem caso a mulher já esteja casada
//O(n²)
while (global.homem[i].casado == false && cursor != NULL) {
//Verifica se a mulher está casada
pthread_mutex_lock (&mutex_mulheres[id_mulher]);
if (casado(&global.mulher[id_mulher])) {
//Se ela está casada, ela escolhe o de sua maior preferência, nomeado como 'melhor_opcao'
NO* melhor_opcao;
id_homem_ja_casado = global.mulher[id_mulher].casado_com_id;
pthread_mutex_lock (&mutex_homens[id_homem_ja_casado]);
melhor_opcao = desempata(&global.mulher[id_mulher], id_homem_ja_casado, id_homem);
if (melhor_opcao->item.valor != global.mulher[id_mulher].casado_com_id) {
//Se a melhor opção NÃO for com quem ela está casada atualmente, casa com a melhor opção
//Descasa a mulher 'id_mulher' com o homem 'id_homem'
descasa(&global.mulher[id_mulher], &global.homem[global.mulher[id_mulher].casado_com_id]); //Descasa o homem com quem a mulher está atualmente casada
pthread_mutex_unlock (&mutex_homens[id_homem_ja_casado]);
//Casa homem 'melhor_opcao->item.valor' com mulher 'id_mulher'
casa(&global.mulher[id_mulher], id_mulher, &global.homem[melhor_opcao->item.valor], melhor_opcao->item.valor); //Casa a melhor opção
} else {
//Caso a melhor opção seja com quem ela está casada atualmente, passa para a próxima opção na lista de preferências do homem
pthread_mutex_unlock (&mutex_homens[id_homem_ja_casado]);
pthread_mutex_unlock (&mutex_mulheres[id_mulher]);
cursor = cursor->proximo;
id_mulher = cursor->item.valor;
lista_remover_no(&global.homem[id_homem],cursor->anterior);
}
} else {
//Caso a mulher não esteja casada, casa ela com o homem 'i', tal que i = id_homem
casa(&global.mulher[id_mulher], id_mulher, &global.homem[id_homem], id_homem);
pthread_mutex_lock (&mutex_global);
global.qnt_solteiros--;
pthread_mutex_unlock (&mutex_global);
}
pthread_mutex_unlock (&mutex_mulheres[id_mulher]);
}
pthread_mutex_unlock (&mutex_homens[id_homem]);
pthread_exit((void *)NULL);
}
//Casa dois grupos através do algoritmo de Gale-Stanley, onde os homens propõem e o critério de desempate de conflito é a preferência da mulher
void men_porpose_algorithm(int n, int numThreads) {
int i;
pthread_t threads[numThreads];
int rc;
int t; //indice da thread
pthread_mutex_init(&mutex_global, NULL);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
mutex_mulheres = (pthread_mutex_t*)malloc((n+1) * sizeof (pthread_mutex_t));
mutex_homens = (pthread_mutex_t*)malloc((n+1) * sizeof (pthread_mutex_t));
if (mutex_mulheres == NULL || mutex_homens == NULL) exit(1);
for (i=0; i<=n; i++) {
pthread_mutex_init(&mutex_mulheres[i], NULL);
pthread_mutex_init(&mutex_homens[i], NULL);
}
global.numThreads = numThreads;
global.qnt_solteiros = n;
t=0;
i=1;
//Enquanto houverem solteiros, percorrer todo o vetor de homens
pthread_mutex_lock (&mutex_global);
while (global.qnt_solteiros > 0) {
pthread_mutex_unlock (&mutex_global);
if (i > n) i = 1;
pthread_mutex_lock (&mutex_homens[i]);
if (!casado(&global.homem[i])) {
pthread_mutex_unlock (&mutex_homens[i]);
rc = pthread_create(&threads[t], &attr, casa_homem, (void *) i);
t++;
if (rc) {
printf("[ERROR] return code from pthread_create() is %d: %s\n\n", rc, strerror(rc));
exit(-1);
}
} else pthread_mutex_unlock (&mutex_homens[i]);
if (t >= global.numThreads-1) {
for ( t--; t>=0; t--) {
rc = pthread_join(threads[t], NULL);
if (rc) {
printf("[ERROR] return code from pthread_join() is %d: %s\n\n", rc, strerror(rc));
exit(-1);
}
}
t=0;
}
i++;
pthread_mutex_lock (&mutex_global);
}
//Join final, para caso não tenha entrado na condição do join acima (caso: nº homens % threads != 0)
pthread_mutex_unlock (&mutex_global);
for ( t--; t>=0; t--) {
rc = pthread_join(threads[t], NULL);
if (rc) {
printf("[ERROR] return code from pthread_join() is %d: %s\n\n", rc, strerror(rc));
exit(-1);
}
}
//Liberação da memória alocada
pthread_mutex_destroy(&mutex_global);
pthread_attr_destroy(&attr);
for (i=0; i<=n; i++) {
pthread_mutex_destroy(&mutex_mulheres[i]);
pthread_mutex_destroy(&mutex_homens[i]);
}
free(mutex_mulheres);
free(mutex_homens);
return;
}
void men_porpose_algorithm_SEQ(int n) {
int i;
global.qnt_solteiros = n;
//Enquanto houverem solteiros, percorrer todo o vetor de homens
i=1;
while (global.qnt_solteiros > 0) {
if (i > n) i = 1;
if (!casado(&global.homem[i]))
casa_homem_SEQ((void *)i);
i++;
}
return;
}
void *casa_homem_SEQ(void *t) {
int i = (int) t;
int id_homem = i; //Função tenta casar o homem atual
NO* cursor = global.homem[i].inicio; //Percorrerá a lista de preferência do homem
int id_mulher = cursor->item.valor;
int id_homem_ja_casado; //Id do homem que disputará com o homem id_homem caso a mulher já esteja casada
//O(n²)
while (global.homem[i].casado == false && cursor != NULL) {
//Verifica se a mulher está casada
if (casado(&global.mulher[id_mulher])) {
//Se ela está casada, ela escolhe o de sua maior preferência, nomeado como 'melhor_opcao'
NO* melhor_opcao;
id_homem_ja_casado = global.mulher[id_mulher].casado_com_id;
melhor_opcao = desempata(&global.mulher[id_mulher], id_homem_ja_casado, id_homem);
if (melhor_opcao->item.valor != global.mulher[id_mulher].casado_com_id) {
//Se a melhor opção NÃO for com quem ela está casada atualmente, casa com a melhor opção
descasa(&global.mulher[id_mulher], &global.homem[global.mulher[id_mulher].casado_com_id]); //Descasa o homem com quem a mulher está atualmente casada
//Casa homem 'melhor_opcao->item.valor' com mulher 'id_mulher'
casa(&global.mulher[id_mulher], id_mulher, &global.homem[melhor_opcao->item.valor], melhor_opcao->item.valor); //Casa a melhor opção
} else {
//Caso a melhor opção seja com quem ela está casada atualmente, passa para a próxima opção na lista de preferências do homem
cursor = cursor->proximo;
id_mulher = cursor->item.valor;
lista_remover_no(&global.homem[id_homem],cursor->anterior);
}
} else {
//Caso a mulher não esteja casada, casa ela com o homem 'i', tal que i = id_homem
casa(&global.mulher[id_mulher], id_mulher, &global.homem[id_homem], id_homem);
global.qnt_solteiros--;
}
}
return NULL;
}