-
Notifications
You must be signed in to change notification settings - Fork 0
/
ag.c
389 lines (304 loc) · 8.93 KB
/
ag.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
/*
* Genetic Algorithm.
* version: 0.0
* Author: Marco Arthur P. B. Silva
*
* TODO:
* - generalized mutation
* - decent UI ( nice command line selection for gp/ehc/ etc... )
* - save history of individuals (tag population, tag parent data)
* - a nice output with -verbose mode also
* - a generalized match pair formation ( sex with two individuals)
* - optmize code (there is huge spaces for optmization)
*
*/
#include "ag.h"
/* Implementation */
double drand (void) {
double d;
do {
d = (((rand () * RS_SCALE) + rand ()) * RS_SCALE + rand ()) * RS_SCALE;
} while (d >= 1); /* Round off */
return d;
}
struct individual_str * find_best(struct population_str *p) {
double max = p->list[0].fitness;
struct individual_str ind;
int i,best = 0, min = fit_table[global_fit].min;
for(i = 0; i < p->size; i++) {
ind = p->list[i];
if ( min ) {
if (ind.fitness < max) {
max = ind.fitness;
best = i;
}
}
else {
if (ind.fitness > max) {
max = ind.fitness;
best = i;
}
}
}
return &(p->list[best]);
}
double init_gene(const double inf, const double sup) {
assert(inf < sup);
double d = (double) rand() / RAND_MAX; // U[0,1]
return d*(sup - inf) + inf; // U[inf, sup]
}
void do_crossover(struct individual_str *p1, struct individual_str *p2) {
p1->generation++;
p2->generation++;
int cross = RAFFLE(PC);
if ( ! cross )
return;
//mean_cross(p1, p2);
geom_cross(p1, p2);
}
void mean_cross(struct individual_str *p1, struct individual_str *p2) {
int i;
for (i = 0; i < GENES; i++)
p1->gene[i] = (p1->gene[i] + p2->gene[i])/ 2;
}
void geom_cross(struct individual_str *p1, struct individual_str *p2) {
int i;
for (i = 0; i < GENES; i++)
p1->gene[i] = sqrt(p1->gene[i] * p2->gene[i]);
}
void blx_cross(struct individual_str *p1, struct individual_str *p2) {
int i;
double b = 2*drand()*ALPHA - ALPHA + 1;
double inf, sup, c;
for (i = 0; i < GENES; i++) {
c = p1->gene[i] - b*(p2->gene[i] - p1->gene[i]);
get_domain(i, &inf, &sup);
/* gene outside range: fails */
if (c < inf || c > sup)
continue;
p1->gene[i] = sqrt(p1->gene[i] * p2->gene[i]);
}
}
void ga_crossover(struct population_str *p, int unsigned generation) {
int i;
if (generation == 0)
return;
struct individual_str *f = NULL;
struct individual_str *m = NULL;
for (i = 0; i < p->size; i++) {
if (! p->list[i].selected)
continue;
if ( !m ) {
m = &p->list[i];
continue;
}
if ( !f ) {
f = &p->list[i];
do_crossover(f, m);
f = m = NULL;
}
}
}
void do_mutation(struct individual_str *ind) {
int mut = RAFFLE(PM);
int i;
if (!mut)
return;
double inf, sup;
int direction;
/* extreme mutation */
for (i = 0; i < fit_table[global_fit].gene_num; i++) {
get_domain(i, &inf, &sup);
/* inf and sup has equal chances */
direction = RAFFLE(0.5);
ind->gene[i] = direction ? inf : sup;
}
/*linear mutation*/
//TODO: assert(NOT_IMPLEMENTED);
}
void ga_mutation(struct population_str *p, int unsigned generation) {
if (generation == 0)
return;
int i;
int pm = RAFFLE(PM);
if ( !pm )
return;
for (i = 0; i < p->size; i++) {
if (! p->list[i].selected)
continue;
do_mutation(&p->list[i]);
}
}
struct population_str * ga_init(void) {
int i,j, n_dim = fit_table[global_fit].gene_num;
struct population_str *p = malloc(sizeof(struct population_str));
p->size = MAX_POP;
p->seed = (time(0));
srand(p->seed);
double inf, sup;
for ( i = 0; i < p->size; i++) {
p->list[i].generation = 0;
p->list[i].gene = calloc(n_dim, sizeof(double));
for (j = 0; j < n_dim; j++) {
get_domain(j, &inf, &sup);
p->list[i].gene[j] = init_gene(inf, sup);
}
p->list[i].selected = 1;
p->filter[i] = 0;
}
return p;
}
double gp_fit(const double x, const double y) {
/* f(0,1) = - 3 best global*/
double s1, s2;
s1 = (double)(1.0 + pow(x + y + 1.0, 2.0) * (19.0 - 14.0*x + 3.0*pow(x, 2.0) - 14.0*y + 6.0 * x * y + 3.0 * pow(y,2.0)));
s2 = (double)(30.0 + pow(2.0 * x - 3.0 * y,2.0) * (18.0 - 32.0*x + 12.0 * pow(x, 2.0) + 48.0 * y - 36.0 * x * y + 27.0 * pow(y,2.0)));
return s1 * s2;
}
double hsk_fit(const double x, const double y) {
return (1 - 8 * x + 7 * pow(x,2) - 7.0/3.0*pow(x,3) + 1.0/4.0*pow(x,4))*pow(y,2)*exp(-1*y);
}
double exp_fitness(struct individual_str *ind) {
int i;
double sum;
for(i = 0; i < GENES ; i++)
sum += ind->gene[i];
ind->fitness = exp(-0.5*sum);
return ind->fitness;
}
double ack_fitness(struct individual_str *ind) {
int i;
double sum_sr = 0, sum_cs = 0;
for(i = 0; i < GENES; i++) {
sum_sr += ind->gene[i]*ind->gene[i];
sum_cs += cos(2*PI*ind->gene[i]);
}
ind->fitness = -20*exp(-0.02*sqrt(sum_sr/10)) - exp(sum_cs/10) + 20 + LE;
return ind->fitness;
}
double hsk_fitness(struct individual_str *ind) {
ind->fitness = hsk_fit(ind->gene[0], ind->gene[1]);
return ind->fitness;
}
double gp_fitness(struct individual_str *ind) {
ind->fitness = gp_fit(ind->gene[0], ind->gene[1]);
return ind->fitness;
}
double cm_fitness(struct individual_str *ind) {
int i;
double sum_sr = 0, sum_cs = 0;
for(i = 0; i < GENES; i++) {
sum_sr += ind->gene[i]*ind->gene[i];
sum_cs += cos(5*PI*ind->gene[i]);
}
ind->fitness = 0.1*sum_cs - sum_sr;
return ind->fitness;
}
double ga_selection(struct population_str *p, int unsigned generation) {
int i, cc = 0;
struct individual_str *ind;
double sum = 0, sum_sq, mean;
fitnessFunction f = fit_table[global_fit].f;
/* compute fitness of generation*/
for (i = 0; i < p->size; i++) {
ind = &p->list[i];
/*a dead body*/
if ( ! ind->selected )
continue;
sum += f( ind );
cc++;
}
/* prepare the new generation */
struct individual_str *best = find_best(p);
mean = sum / cc;
cc = 0;
for (i = 0; i < p->size; i++) {
ind = &p->list[i];
ind->per = ind->fitness / sum;
/* guarantee the ones near the best get in pool*/
ind->selected = ind->fitness > best->fitness*0.7 ? 1 : 0;
/* give a change again but based on pure luck */
if ( ind->selected == 0 )
ind->selected = RAFFLE(0.1);
/* calculate standard deviation */
if ( ind->selected == 1 ) {
sum_sq = (ind->fitness - mean)*(ind->fitness - mean);
cc++;
}
}
/* guarantee the best */
best->selected = 1;
return sqrt(sum_sq/( cc - 1));
}
void summary_ind(const struct individual_str *p) {
int i;
printf("fitness:%3.4f\t", p->fitness);
for (i = 0; i < GENES; i++)
printf("x[%d]=%3.4f\t", i, p->gene[i]);
printf("generation:%d\n", p->generation);
}
void summary(const struct population_str *p) {
int i;
for(i = 0; i < p->size; i++)
summary_ind(&p->list[i]);
}
int filter_selection(struct population_str *p, filterFunction f) {
int i;
for (i = 0; i < p->size; i++)
p->filter[i] = f(&p->list[i]);
return 0;
}
int get_domain(const int gene, double *inf, double *sup) {
*inf = *sup = 0;
switch(global_fit) {
case GP:
if (gene == 0 || gene == 1) {
*inf = -2.0;
*sup = 2.0;
return 0;
}
return -1;
case HSK:
if (gene == 0 || gene == 1) {
*inf = 0;
*sup = 5.0;
return 0;
}
case EXP:
assert(NOT_IMPLEMENTED);
case ACK:
/* for all genes are the same */
*inf = -30.0;
*sup = 30.0;
return 0;
case CM:
/* all genes are the same */
*inf = -1.0;
*sup = 1.0;
return 0;
};
return -1;
}
void summary_alg(void) {
printf("Fitness: %s, known otimal solution: %3.4f\n",
fit_table[global_fit].description,
fit_table[global_fit].best_fit);
}
int main(int argc, const char *argv[]) {
int i = 0;
double std;
struct individual_str *best;
/* population */
struct population_str *pop = ga_init();
summary_alg();
/* It's evolution babe :-) */
do {
ga_crossover(pop, i);
ga_mutation(pop, i);
std = ga_selection(pop, i++);
best = find_best(pop);
summary_ind(best);
} while (i < MAX_INTERATION && std > CRITERIA);
free(pop);
return 0;
}