-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcgp.c
144 lines (117 loc) · 2.81 KB
/
cgp.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
/**
* cgp.c
* Copyright (C) 2012 Jan Viktorin
*/
#include "cgp.h"
#include "cgp_config.h"
#include "log.h"
#include <assert.h>
#include <string.h>
int cgp_init(struct cgp_t *cgp)
{
cgp->gener = 0;
cgp->found_best = 0;
cgp->c = chromo_alloc(CGP_POPUL);
if(cgp->c == NULL)
return 1;
cgp->f = malloc(CGP_POPUL * sizeof(fitness_t));
if(cgp->f == NULL) {
chromo_free(cgp->c);
cgp->c = NULL;
return 2;
}
memset(cgp->f, 0, sizeof(fitness_t) * CGP_POPUL);
return 0;
}
void cgp_fini(struct cgp_t *cgp)
{
if(cgp->c != NULL) {
chromo_free(cgp->c);
cgp->c = NULL;
}
if(cgp->f != NULL) {
free(cgp->f);
cgp->f = NULL;
}
}
int cgp_done(const struct cgp_t *cgp)
{
return cgp->gener >= CGP_GENER;
}
static
int priv_eval_popul(struct chromo_t *c, size_t len, fitness_t *f, int *found_best)
{
*found_best = 0;
int err = 0;
int best_chromo_found = 0;
#pragma omp parallel for reduction(+:err,best_chromo_found)
for(size_t i = 0; i < len; ++i) {
if(fitness_compute(chromo_at(c, i), f + i))
err = 1;
if(fitness_isacceptable(f[i]))
best_chromo_found = 1;
}
*found_best = best_chromo_found;
return err;
}
int cgp_gen_popul(struct cgp_t *cgp)
{
for(size_t i = 0; i < CGP_POPUL; ++i) {
chromo_gen(chromo_at(cgp->c, i));
#if log_enabled(LOG_CGP)
log_call(LOG_CGP);
chromo_print(stderr, chromo_at(cgp->c, i));
fprintf(stderr, "\n");
#endif
}
return priv_eval_popul(cgp->c, CGP_POPUL, cgp->f, &cgp->found_best);
}
int cgp_gen_popul_from(struct cgp_t *cgp, struct chromo_t *c, size_t count)
{
if(count == 0)
return cgp_gen_popul(cgp);
for(size_t i = 0; i < count && i < CGP_POPUL; ++i)
chromo_copy(chromo_at(cgp->c, i), chromo_at(c, i));
for(size_t i = count; i < CGP_POPUL; ++i)
chromo_gen(chromo_at(cgp->c, i));
return priv_eval_popul(cgp->c, CGP_POPUL, cgp->f, &cgp->found_best);
}
int cgp_eval_popul(struct cgp_t *cgp)
{
assert(cgp->gener > 0);
return priv_eval_popul(
chromo_at(cgp->c, 1), CGP_POPUL - 1, cgp->f + 1, &cgp->found_best);
}
static
size_t choose_winner(struct cgp_t *cgp)
{
size_t winner_i = 0;
for(size_t i = 0; i < CGP_POPUL; ++i) {
if(fitness_cmp(cgp->f[i], cgp->f[winner_i]) >= 0)
winner_i = i;
}
return winner_i;
}
int cgp_next_popul(struct cgp_t *cgp)
{
const size_t winner_i = choose_winner(cgp);
const struct chromo_t *winner = chromo_at(cgp->c, winner_i);
chromo_copy(cgp->c, winner);
cgp->f[0] = cgp->f[winner_i];
for(size_t i = 1; i < CGP_POPUL - 1; ++i) {
chromo_copy(chromo_at(cgp->c, i), cgp->c);
chromo_mut(chromo_at(cgp->c, i));
#if log_enabled(LOG_CGP)
log_call(LOG_CGP);
chromo_print(stderr, chromo_at(cgp->c, i));
fprintf(stderr, "\n");
#endif
}
cgp->gener += 1;
return 0;
}
void cgp_walk_popul(struct cgp_t *cgp, cgp_walk_f walkf, void *ctx)
{
for(size_t i = 0; i < CGP_POPUL; ++i)
walkf(i, chromo_at(cgp->c, i), cgp->f[i], ctx);
}