This repository has been archived by the owner on Mar 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathga.c
353 lines (269 loc) · 7.14 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* ga.c -- Implementation of the genetic algorithm's front-end.
* $Id: ga.c,v 1.3 2002/06/02 22:10:38 rwx Exp $
*/
/*
* Copyright (C) 2002 Juan M. Bello Rivas <rwx@synnergy.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#ifdef STDC_HEADERS
# include <stdio.h>
# include <stdlib.h>
#endif /* STDC_HEADERS */
#include <assert.h>
#include <sys/types.h>
#include "random.h"
#include "chromosome.h"
#include "individual.h"
#include "individual-priv.h"
#include "population.h"
#include "population-priv.h"
#include "ga.h"
#include "ga-priv.h"
static void ga_set_best_ever(struct ga *self, unsigned int generation,
struct individual *candidate);
static void ga_set_selection_strategy(struct ga *self,
enum ga_selection_strategies selection_strategy);
static void ga_set_crossover_strategy(struct ga *self,
enum ga_crossover_strategies crossover_strategy);
static void ga_cross(struct ga *self, struct individual *dad,
struct individual *mom, struct individual **son,
struct individual **daughter);
static void ga_preselect(struct ga *self);
static void ga_mutate(struct ga *self, struct individual *indiv);
static void ga_report(struct ga *self, FILE *fp);
struct ga *
new_ga(unsigned int max_gen,
size_t chrom_len,
size_t initial, size_t normal,
float pcrossover, float pmutation,
enum ga_selection_strategies selection_strategy,
enum ga_crossover_strategies crossover_strategy,
objective_fn obj_fn)
{
struct ga *g;
assert(chrom_len > 0
&& initial > 0 && normal > 0
&& pcrossover > 0 && pmutation > 0
&& obj_fn);
if (initial < normal) {
fprintf(stderr, "%s: The initial population must be at least "
"as big as the normal population\n", __FUNCTION__);
return NULL;
}
if ((initial % 2 != 0) || (normal % 2 != 0)) {
fprintf(stderr, "%s: Populations must have an even number "
"of individuals\n", __FUNCTION__);
return NULL;
}
g = calloc(1, sizeof(struct ga));
if (!g)
return NULL;
g->current = 1;
g->max_gen = max_gen;
g->chrom_len = chrom_len;
g->initial = initial;
g->normal = normal;
g->pcrossover = pcrossover;
g->pmutation = pmutation;
g->crossovers = g->mutations = 0;
ga_set_selection_strategy(g, selection_strategy);
ga_set_crossover_strategy(g, crossover_strategy);
g->mutate = mutate;
g->old_pop = g->cur_pop = NULL;
g->best.i = NULL;
g->best.generation = 0;
g->obj_fn = obj_fn;
return g;
}
void
delete_ga(struct ga *self)
{
assert(self);
if (self->best.i)
delete_individual(self->best.i);
if (self->old_pop)
delete_population(self->old_pop);
if (self->cur_pop)
delete_population(self->cur_pop);
free(self);
}
void
ga_evolve(struct ga *self, unsigned int max_gen)
{
assert(self);
ga_first(self);
while (self->current <= self->max_gen)
ga_next(self);
}
void
ga_first(struct ga *self)
{
unsigned int i;
struct population *cur_pop;
struct individual *cur_indiv;
assert(self && !self->cur_pop);
cur_pop = self->cur_pop = new_population(self->initial);
/*
* Generate a random population.
*/
for (cur_indiv = NULL, i = 0; i < self->cur_pop->len; i++) {
cur_indiv = cur_pop->pop[i] = new_individual(self->chrom_len);
individual_random(cur_indiv);
self->obj_fn(cur_indiv);
}
ga_set_best_ever(self, self->current,
population_get_fittest(cur_pop));
population_compute_fitness_stats(cur_pop);
ga_report(self, stdout);
++self->current;
}
static void
swap_populations(struct population **old, struct population *new)
{
if (*old)
delete_population(*old);
*old = new;
}
void
ga_next(struct ga *self)
{
unsigned int i;
struct population *cur_pop;
struct individual *dad, *mom, *son, *daughter;
assert(self && self->cur_pop);
swap_populations(&self->old_pop, self->cur_pop);
cur_pop = self->cur_pop = new_population(self->normal);
ga_preselect(self);
for (i = 0; i < self->normal; ) {
self->select(self->old_pop, &dad, &mom);
ga_cross(self, dad, mom, &son, &daughter);
ga_mutate(self, son);
ga_mutate(self, daughter);
cur_pop->pop[i++] = son;
cur_pop->pop[i++] = daughter;
self->obj_fn(son);
self->obj_fn(daughter);
};
ga_set_best_ever(self, self->current,
population_get_fittest(cur_pop));
population_compute_fitness_stats(cur_pop);
ga_report(self, stdout);
++self->current;
}
void
ga_preselect(struct ga *self)
{
if (self->preselect)
self->preselect(self->old_pop);
}
void
ga_cross(struct ga *self, struct individual *dad, struct individual *mom,
struct individual **son, struct individual **daughter)
{
if (random_flip(self->pcrossover) == 1) {
self->cross(dad, mom, son, daughter);
++self->crossovers;
} else {
*son = individual_dup(dad);
*daughter = individual_dup(mom);
}
}
void
ga_mutate(struct ga *self, struct individual *indiv)
{
self->mutations += self->mutate(self->pmutation, indiv);
}
void
ga_set_best_ever(struct ga *self, unsigned int generation,
struct individual *candidate)
{
int status, should_change = 1;
struct individual *best;
assert(self && candidate);
best = self->best.i;
if (best) {
status = individual_compare(&best, &candidate);
should_change = (status == 1) ? 1 : 0;
}
/*
* In case the candidate is actually better than the best individual so
* far (or in case there's no best yet)...
*/
if (should_change) {
if (best)
delete_individual(best);
self->best.i = individual_dup(candidate);
self->best.generation = generation;
}
}
struct fittest *
ga_get_best_ever(struct ga *self)
{
assert(self);
return &self->best;
}
void
ga_set_selection_strategy(struct ga *self,
enum ga_selection_strategies selection_strategy)
{
preselection_fn preselect = NULL;
selection_fn select = NULL;
assert(self);
switch (selection_strategy) {
case GA_S_TOPBOTTOM_PAIRING:
preselect = preselect_topbottom_pairing;
select = select_topbottom_pairing;
break;
case GA_S_ROULETTE_WHEEL:
preselect = preselect_roulette_wheel;
select = select_roulette_wheel;
break;
case GA_S_TOURNAMENT:
preselect = preselect_tournament;
select = select_tournament;
break;
}
self->preselect = preselect;
self->select = select;
}
void
ga_set_crossover_strategy(struct ga *self,
enum ga_crossover_strategies crossover_strategy)
{
crossover_fn cross = NULL;
assert(self);
switch (crossover_strategy) {
case GA_X_SINGLE_POINT:
cross = crossover_single_point;
break;
case GA_X_N_POINT:
cross = crossover_n_point;
break;
case GA_X_UNIFORM:
cross = crossover_uniform;
break;
}
self->cross = cross;
}
void
ga_report(struct ga *self, FILE *fp)
{
assert(self && fp);
if (self->report)
self->report(self, fp);
}