-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbblib.c
336 lines (296 loc) · 7.16 KB
/
bblib.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bblib.h"
#include <stdbool.h>
valor melhor;
int l, m, n, quantNos;
bool otimilidade, viabilidade, bound;
// L = Quantidade de Grupos Sociais
// M = Atores
// N = Personagens
void printaMelhor(ator *atores)
{
// if (quantEscolhidos(melhor.atores) != n || quantGrupos(melhor.atores) != l)
// {
// printf("Inviavel\n");
// exit(0);
// }
for (int i = 0; i < m; i++)
{
if (melhor.atores[i].escolhido)
{
printf("%d ", i + 1);
}
}
printf("\n");
printf("%.2f\n", melhor.custo);
// printaAtores(atores);
// printf("--------------\n");
// printaAtores(melhor.atores);
}
// retorna o custo dos atores escolhidos
float calculaCusto(ator *atores)
{
int custo = 0;
for (int i = 0; i < m; i++)
{
if (atores[i].escolhido == 1)
{
custo += atores[i].custo;
}
}
return custo;
}
int compare(const void *num1, const void *num2)
{
if (*(int *)num1 > *(int *)num2)
return 1;
else
return -1;
}
// Bound proprio
float Bound(int i, ator *atores)
{
float custo = 0;
if (m > i)
{
float *atoresRestantes = malloc(sizeof(float) * (m - i));
int k = 0;
for (int j = i; j < m; j++)
{
atoresRestantes[k] = atores[j].custo;
k++;
}
qsort(atoresRestantes, m - i, sizeof(float), compare);
int quantJaEscolhidos = quantEscolhidos(atores) - (m - i);
for (int j = 0; j < n - quantJaEscolhidos; j++)
{
custo += atoresRestantes[j];
}
free(atoresRestantes);
}
for (int j = 0; j < i; j++)
{
if (atores[j].escolhido)
custo += atores[j].custo;
}
return custo;
}
// Bound professor
float Bound2(int i, ator *atores)
{
float custo = 0;
if (m > i)
{
float *atoresRestantes = malloc(sizeof(float) * (m - i));
int k = 0;
for (int j = i; j < m; j++)
{
atoresRestantes[k] = atores[j].custo;
k++;
}
qsort(atoresRestantes, m - i, sizeof(float), compare);
int quantJaEscolhidos = quantEscolhidos(atores) - (m - i);
for (int j = 0; j < n - quantJaEscolhidos; j++)
{
custo += atoresRestantes[0];
}
free(atoresRestantes);
}
for (int j = 0; j < i; j++)
{
if (atores[j].escolhido)
custo += atores[j].custo;
}
return custo;
}
// retorna a quantidade de grupos distintos entre os atores escolhidos
int quantGrupos(ator *atores)
{
int *grupos = calloc(l, sizeof(int));
int quantGrupos = 0;
for (int i = 0; i < m; i++)
{
if (atores[i].escolhido == 1)
{
for (int j = 0; j < atores[i].grupos; j++)
{
if (grupos[atores[i].idGrupos[j]] == 0)
{
quantGrupos++;
grupos[atores[i].idGrupos[j]] = 1;
}
}
}
}
free(grupos);
return quantGrupos;
}
// retorna a quantidade de atores escolhidos
int quantEscolhidos(ator *atores)
{
int escolhidos = 0;
for (int i = 0; i < m; i++)
{
if (atores[i].escolhido == 1)
{
escolhidos++;
}
}
return escolhidos;
}
void trocaMelhor(int custo, ator *atores)
{
melhor.custo = custo;
for (int i = 0; i < m; i++)
{
melhor.atores[i] = atores[i];
}
}
// funcao para inicializar a variavel melhor
void inicializaMelhor()
{
melhor.custo = INFINITY;
melhor.atores = malloc(sizeof(ator) * m);
for (int i = 0; i < m; i++)
{
melhor.atores[i].escolhido = 1;
}
}
ator *leAtores()
{
ator *atores = malloc(sizeof(ator) * m);
for (int i = 0; i < m; i++)
{
scanf("%f %d", &atores[i].custo, &atores[i].grupos);
atores[i].idGrupos = malloc(sizeof(int) * atores[i].grupos);
for (int j = 0; j < atores[i].grupos; j++)
{
scanf("%d", &atores[i].idGrupos[j]);
}
atores[i].escolhido = 1;
}
return atores;
}
void printaAtores(ator *atores)
{
for (int i = 0; i < m; i++)
{
printf("ator %d\n", i);
printf("custo: %f grupos: %d\n", atores[i].custo, atores[i].grupos);
for (int j = 0; j < atores[i].grupos; j++)
{
printf("grupo: %d\n", atores[i].idGrupos[j]);
}
printf("escolhido: %d\n", atores[i].escolhido);
printf("\n");
}
}
bool factivel(int i, ator *atores)
{
if (m < n)
{
return false;
}
if (quantGrupos(atores) < l)
return false;
if (quantEscolhidos(atores) < n)
return false;
return true;
}
static float menor(float a, float b)
{
if (a < b)
return a;
else
return b;
}
void Soluciona(int i, ator *atores)
{
if (otimilidade)
{
if (viabilidade)
{
if (!factivel(i, atores))
{
return;
}
}
if (i > m)
{
return;
}
quantNos++;
if (quantEscolhidos(atores) == n && quantGrupos(atores) == l)
{
int custo_atual = calculaCusto(atores);
if (melhor.custo > custo_atual)
{
trocaMelhor(custo_atual, atores);
}
}
// bound pick
float escolhe_limitante;
if (!bound)
escolhe_limitante = Bound(i + 1, atores);
else
escolhe_limitante = Bound2(i + 1, atores);
// bound skip
atores[i].escolhido = 0;
float pula_limitante;
if (!bound)
pula_limitante = Bound(i + 1, atores);
else
pula_limitante = Bound2(i + 1, atores);
atores[i].escolhido = 1;
if (menor(escolhe_limitante, pula_limitante) >= melhor.custo)
return;
if (escolhe_limitante < pula_limitante)
{
Soluciona(i + 1, atores);
if (pula_limitante < melhor.custo)
{
// ramifica para esquerda
atores[i].escolhido = 0;
Soluciona(i + 1, atores);
// ramifica para direita
atores[i].escolhido = 1;
}
}
else
{
// ramifica para esquerda
atores[i].escolhido = 0;
Soluciona(i + 1, atores);
// ramifica para direita
atores[i].escolhido = 1;
if (pula_limitante < melhor.custo)
{
Soluciona(i + 1, atores);
}
}
}
else
{
if (i > m)
{
return;
}
quantNos++;
if (quantEscolhidos(atores) == n && quantGrupos(atores) == l)
{
float custo_atual = calculaCusto(atores);
if (melhor.custo > custo_atual)
{
trocaMelhor(custo_atual, atores);
}
}
// ramifica para esquerda
atores[i].escolhido = 0;
Soluciona(i + 1, atores);
// ramifica para direita
atores[i].escolhido = 1;
Soluciona(i + 1, atores);
}
}