/population.c
C | 136 lines | 77 code | 32 blank | 27 comment | 6 complexity | 96faa25c06229c7eaa234b4117927c3b MD5 | raw file
1/* 2 * population.c -- Implementation of population management code. 3 * $Id: population.c,v 1.5 2002/06/04 16:33:57 rwx Exp $ 4 */ 5/* 6 * Copyright (C) 2002 Juan M. Bello Rivas <rwx@synnergy.net> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 24#include "config.h" 25 26#ifdef STDC_HEADERS 27# include <stdio.h> 28# include <stdlib.h> 29# include <string.h> 30#endif /* STDC_HEADERS */ 31 32#include <assert.h> 33#include <sys/types.h> 34 35#include "chromosome.h" 36#include "individual.h" 37#include "individual-priv.h" 38#include "population.h" 39#include "population-priv.h" 40 41 42struct population * 43new_population(size_t len) 44{ 45 struct population *p; 46 47 assert(len > 0); 48 49 p = calloc(1, sizeof(struct population)); 50 if (!p) 51 return NULL; 52 53 p->len = len; 54 p->pop = calloc(p->len, sizeof(struct individual *)); 55 if (!p->pop) { 56 free(p); 57 return NULL; 58 } 59 60 return p; 61} 62 63void 64delete_population(struct population *self) 65{ 66 size_t i; 67 68 assert(self); 69 70 if (self->pop) 71 for (i = 0; i < self->len; i++) 72 if (self->pop[i]) delete_individual(self->pop[i]); 73 free(self->pop); 74 free(self->select_data); 75 free(self); 76} 77 78 79struct fitness_stats * 80population_get_fitness_stats(struct population *self) 81{ 82 assert(self); 83 84 return &self->stats; 85} 86 87 88struct individual * 89population_get_fittest(struct population *self) 90{ 91 assert(self); 92 93 qsort(self->pop, self->len, sizeof(struct individual *), 94 (int (*)(const void *, const void *)) individual_compare); 95 96 return self->pop[0]; 97} 98 99 100void 101population_compute_fitness_stats(struct population *self) 102{ 103 int i; 104 struct individual **pop; 105 struct fitness_stats *stats; 106 107 assert(self); 108 109 stats = &self->stats; 110 memset(stats, 0, sizeof(struct fitness_stats)); 111 112 pop = self->pop; 113 114 /* 115 * This function assumes the population array has been sorted in 116 * advance (either by population_get_fittest or other means). 117 */ 118 stats->minimum = individual_get_fitness(pop[0]); 119 stats->maximum = individual_get_fitness(pop[self->len - 1]); 120 121 for (i = 0; i < self->len; i++) 122 stats->total += individual_get_fitness(pop[i]); 123 124 stats->average = stats->total / (double) self->len; 125} 126 127 128void 129population_print(struct population *self, FILE *fp) 130{ 131 int i; 132 133 individual_print(population_get_fittest(self), stdout); 134 /* for (i = 0; i < self->len; i++) */ 135 /* individual_print(self->pop[i], fp); */ 136}