This repository was archived by the owner on Mar 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpopulation.c
More file actions
136 lines (104 loc) · 2.88 KB
/
population.c
File metadata and controls
136 lines (104 loc) · 2.88 KB
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
/*
* population.c -- Implementation of population management code.
* $Id: population.c,v 1.5 2002/06/04 16:33:57 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>
# include <string.h>
#endif /* STDC_HEADERS */
#include <assert.h>
#include <sys/types.h>
#include "chromosome.h"
#include "individual.h"
#include "individual-priv.h"
#include "population.h"
#include "population-priv.h"
struct population *
new_population(size_t len)
{
struct population *p;
assert(len > 0);
p = calloc(1, sizeof(struct population));
if (!p)
return NULL;
p->len = len;
p->pop = calloc(p->len, sizeof(struct individual *));
if (!p->pop) {
free(p);
return NULL;
}
return p;
}
void
delete_population(struct population *self)
{
size_t i;
assert(self);
if (self->pop)
for (i = 0; i < self->len; i++)
if (self->pop[i]) delete_individual(self->pop[i]);
free(self->pop);
free(self->select_data);
free(self);
}
struct fitness_stats *
population_get_fitness_stats(struct population *self)
{
assert(self);
return &self->stats;
}
struct individual *
population_get_fittest(struct population *self)
{
assert(self);
qsort(self->pop, self->len, sizeof(struct individual *),
(int (*)(const void *, const void *)) individual_compare);
return self->pop[0];
}
void
population_compute_fitness_stats(struct population *self)
{
int i;
struct individual **pop;
struct fitness_stats *stats;
assert(self);
stats = &self->stats;
memset(stats, 0, sizeof(struct fitness_stats));
pop = self->pop;
/*
* This function assumes the population array has been sorted in
* advance (either by population_get_fittest or other means).
*/
stats->minimum = individual_get_fitness(pop[0]);
stats->maximum = individual_get_fitness(pop[self->len - 1]);
for (i = 0; i < self->len; i++)
stats->total += individual_get_fitness(pop[i]);
stats->average = stats->total / (double) self->len;
}
void
population_print(struct population *self, FILE *fp)
{
int i;
individual_print(population_get_fittest(self), stdout);
/* for (i = 0; i < self->len; i++) */
/* individual_print(self->pop[i], fp); */
}