/population.c

http://github.com/jmbr/libeve · 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. #include "config.h"
  23. #ifdef STDC_HEADERS
  24. # include <stdio.h>
  25. # include <stdlib.h>
  26. # include <string.h>
  27. #endif /* STDC_HEADERS */
  28. #include <assert.h>
  29. #include <sys/types.h>
  30. #include "chromosome.h"
  31. #include "individual.h"
  32. #include "individual-priv.h"
  33. #include "population.h"
  34. #include "population-priv.h"
  35. struct population *
  36. new_population(size_t len)
  37. {
  38. struct population *p;
  39. assert(len > 0);
  40. p = calloc(1, sizeof(struct population));
  41. if (!p)
  42. return NULL;
  43. p->len = len;
  44. p->pop = calloc(p->len, sizeof(struct individual *));
  45. if (!p->pop) {
  46. free(p);
  47. return NULL;
  48. }
  49. return p;
  50. }
  51. void
  52. delete_population(struct population *self)
  53. {
  54. size_t i;
  55. assert(self);
  56. if (self->pop)
  57. for (i = 0; i < self->len; i++)
  58. if (self->pop[i]) delete_individual(self->pop[i]);
  59. free(self->pop);
  60. free(self->select_data);
  61. free(self);
  62. }
  63. struct fitness_stats *
  64. population_get_fitness_stats(struct population *self)
  65. {
  66. assert(self);
  67. return &self->stats;
  68. }
  69. struct individual *
  70. population_get_fittest(struct population *self)
  71. {
  72. assert(self);
  73. qsort(self->pop, self->len, sizeof(struct individual *),
  74. (int (*)(const void *, const void *)) individual_compare);
  75. return self->pop[0];
  76. }
  77. void
  78. population_compute_fitness_stats(struct population *self)
  79. {
  80. int i;
  81. struct individual **pop;
  82. struct fitness_stats *stats;
  83. assert(self);
  84. stats = &self->stats;
  85. memset(stats, 0, sizeof(struct fitness_stats));
  86. pop = self->pop;
  87. /*
  88. * This function assumes the population array has been sorted in
  89. * advance (either by population_get_fittest or other means).
  90. */
  91. stats->minimum = individual_get_fitness(pop[0]);
  92. stats->maximum = individual_get_fitness(pop[self->len - 1]);
  93. for (i = 0; i < self->len; i++)
  94. stats->total += individual_get_fitness(pop[i]);
  95. stats->average = stats->total / (double) self->len;
  96. }
  97. void
  98. population_print(struct population *self, FILE *fp)
  99. {
  100. int i;
  101. individual_print(population_get_fittest(self), stdout);
  102. /* for (i = 0; i < self->len; i++) */
  103. /* individual_print(self->pop[i], fp); */
  104. }