-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.c
329 lines (260 loc) · 7.38 KB
/
ga.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "simulador.h"
#include "atomo_molecula.h"
#include "ga.h"
Molecule *otimizar_ga(Molecule *molecula, int geracoes, int tam_populacao, int prob_mutacao, int prob_crossover)
{
/* parametros do GA */
double tamanho_arestas = log(molecula->num_atoms); // tamanho máximo das arestas
int i, j;
Agregado *geracao_ascendente = NULL, *geracao_descendente = NULL;
/* If we have an odd populations, make it even. */
if (tam_populacao % 2 != 0) ++tam_populacao;
//printf(">>>>>> Cria populacao inicial\n");
geracao_ascendente = cria_populacao_inicial(molecula, tam_populacao,tamanho_arestas);
printf("Geracao - melhor, media, pior\n");
for (i = 0; i < geracoes ; ++i )
{
printf(">> %3d -> ", i+1);
geracao_descendente = cria_populacao_descendente(prob_mutacao,prob_crossover,tamanho_arestas, geracao_ascendente);
/* Free memory */
destroy_agregado(geracao_ascendente);
/* Estatisticas globais */
melhor_global[i] = geracao_descendente->agregado[0]->energy;
pior_global[i] = geracao_descendente->agregado[tam_populacao-1]->energy;
double tmp_media = 0;
for (j=0; j<tam_populacao; ++j)
{
tmp_media += geracao_descendente->agregado[j]->energy;
}
media_global[i] = tmp_media/tam_populacao;
/************************/
printf("%9.4f %10.4f %10.4f\n", melhor_global[i], media_global[i], pior_global[i]);
geracao_ascendente = geracao_descendente;
}
Molecule *retorno = geracao_descendente->agregado[0];
geracao_descendente->agregado[0] = NULL;
destroy_agregado(geracao_descendente);
return retorno;
}
Agregado* cria_populacao_inicial(Molecule *recebida, int tamanho_populacao, double tamanho_arestas)
{
Agregado *geracao_ascendente = create_empty_agregado(tamanho_populacao);
int i;
for (i = 0; i < tamanho_populacao; ++i)
{
Molecule *aleatoria = gera_molecula_aleatoria(recebida, tamanho_arestas);
calcula_energia_molecula(aleatoria);
geracao_ascendente->agregado[i] = aleatoria;
}
ordena(geracao_ascendente);
return geracao_ascendente;
}
Agregado *cria_populacao_descendente(int Pm, int Pc, double tamanho_arestas, Agregado *geracao_ascendente)
{
int i;
int tam_populacao = geracao_ascendente->num_molecules;
Agregado *geracao_descendente = create_empty_agregado(tam_populacao);
Molecule *ind1 = NULL;
Molecule *ind2 = NULL;
/* TODO: Verificar de onde esta pegando os pais */
// cria nova geracao de individuos
for(i=0; i<tam_populacao; i+=2)
{
//ordena(geracao_ascendente);
int result;
do
{
result = rank(geracao_ascendente, &ind1, &ind2); // seleciona pais
if (!result)
{
free(ind1);
ind1 = NULL;
free(ind2);
ind2 = NULL;
}
} while (!result);
// crossover
crossover(ind1, ind2, Pm);
// mutacao
mutacao(ind1, Pm, tamanho_arestas);
mutacao(ind2, Pm, tamanho_arestas);
/* /\* Muda completamente o agregado *\/ */
/* if ((rand() % 100) == 0) */
/* { */
/* Agregado *geracao_temp = cria_populacao_inicial(geracao_ascendente->agregado[0], geracao_ascendente->num_molecules, tamanho_arestas); */
/* destroy_agregado(geracao_descendente); */
/* geracao_descendente = geracao_temp; */
/* goto mudou; */
/* } */
otimizador(ind1, ITERACOES);
otimizador(ind2, ITERACOES);
calcula_energia_molecula(ind1);
/* ind1->energy = fit; */
calcula_energia_molecula(ind2);
/* ind2->energy = fit; */
geracao_descendente->agregado[i ] = ind1;
geracao_descendente->agregado[i+1] = ind2;
ind1 = NULL;
ind2 = NULL;
}
/* mudou: */
/* Coloca o melhor na proxima geracao - Elitismo */
destroy_molecule(geracao_descendente->agregado[0]);
geracao_descendente->agregado[0] = copy_molecule(geracao_ascendente->agregado[0]);
for (i=0; i<tam_populacao; ++i)
{
calcula_energia_molecula(geracao_descendente->agregado[i]);
}
ordena(geracao_descendente);
/* TODO: Destruir geracao ascendente aqui ou fora da funcao.*/
return geracao_descendente;
}
double gera_coordenada_aleatoria(double tamanho_aresta)
{
//double retorno = (double) tamanho_aresta*((double) rand()/ (double) RAND_MAX);
double precisao = 10000000;
double retorno = (rand()%(int)(tamanho_aresta*precisao))/precisao;
return retorno;
}
Molecule *gera_molecula_aleatoria(Molecule *recebida, double tamanho_arestas)
{
int i;
double x, y, z;
char *elem;
Molecule *criadora = create_empty_molecule(recebida->num_atoms);
Atom *cria_atomo;
for (i = 0; i < (int)recebida->num_atoms; ++i )
{
x = gera_coordenada_aleatoria(tamanho_arestas);
y = gera_coordenada_aleatoria(tamanho_arestas);
z = gera_coordenada_aleatoria(tamanho_arestas);
elem = recebida->molecule[i]->element;
cria_atomo = create_atom(elem, x, y, z);
criadora->molecule[i] = cria_atomo;
}
return criadora;
}
void crossover(Molecule *ind1, Molecule *ind2, int prob_crossover)
{
int posicao_crossover;
int i;
int tamanho_molecula;
if ((rand()%100) < prob_crossover)
{
tamanho_molecula = ind1->num_atoms;
posicao_crossover = rand()%tamanho_molecula;
Atom *tmp_atom;
/* Molecule *auxiliar_ind1 = copy_molecule(ind1); */
/* Molecule *auxiliar_ind2 = copy_molecule(ind2); */
for (i = posicao_crossover; i < tamanho_molecula; ++i)
{
tmp_atom = ind1->molecule[i];
ind1->molecule[i] = ind2->molecule[i];
ind2->molecule[i] = tmp_atom;
}
}
}
void mutacao(Molecule *molecula, int prob_mutacao, double tamanho_arestas)
{
int i;
for (i=0; i<molecula->num_atoms; ++i)
{
/* X */
if ((rand()%100)<prob_mutacao)
{
molecula->molecule[i]->x = gera_coordenada_aleatoria(tamanho_arestas);
}
/* Y */
if ((rand()%100)<prob_mutacao)
{
molecula->molecule[i]->y = gera_coordenada_aleatoria(tamanho_arestas);
}
/* Z */
if ((rand()%100)<prob_mutacao)
{
molecula->molecule[i]->z = gera_coordenada_aleatoria(tamanho_arestas);
}
}
}
double calcula_energia_molecula(Molecule *molecula)
{
double energia = 0;
energia = calcula_energia(molecula);
molecula->energy = energia;
return energia;
}
void ordena(Agregado *original)
{
int tamanho = original->num_molecules;
int i, j;
Molecule *tmp;
/* Ordena o agregado em ordem de menor energia (maior fitness) */
for (i=0; i<tamanho; ++i)
{
for (j=i+1; j<tamanho; ++j)
{
if (original->agregado[j]->energy < original->agregado[i]->energy)
{
tmp = original->agregado[i];
original->agregado[i] = original->agregado[j];
original->agregado[j] = tmp;
}
}
}
}
int rank(Agregado *original, Molecule **ind1t, Molecule **ind2t)
{
int tamanho = original->num_molecules;
int i;
int counter = 0;
for (i=0; i<tamanho; ++i)
{
double prob = (double) rand()/(double) RAND_MAX;
double chance = (double) (tamanho - i)/(tamanho*2);
if (chance < prob)
{
if (counter == 0)
{
*ind1t = copy_molecule(original->agregado[i]);
++counter;
}
else if (counter == 1)
{
*ind2t = copy_molecule(original->agregado[i]);
++counter;
}
else
{
return 1;
}
}
}
return 0;
}
Agregado *create_empty_agregado(int tamanho)
{
Agregado *tmp = calloc(1,sizeof(Agregado));
tmp->num_molecules = tamanho;
tmp->agregado = (Molecule **) calloc(tamanho, sizeof(Molecule **));
return tmp;
}
void destroy_agregado(Agregado *a)
{
int i,tamanho = a->num_molecules;
for (i=0; i<tamanho; ++i)
{
if (a->agregado[i])
{
destroy_molecule(a->agregado[i]);
a->agregado[i] = NULL;
}
}
free(a->agregado);
a->agregado = NULL;
free(a);
a = NULL;
}