1 /*------------------------------------------------------------------------
4 * Genetic Algorithm (GA) pool stuff
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/backend/optimizer/geqo/geqo_pool.c
11 *-------------------------------------------------------------------------
15 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
16 * Martin Utesch * Institute of Automatic Control *
17 = = University of Mining and Technology =
18 * utesch@aut.tu-freiberg.de * Freiberg, Germany *
19 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
22 /* -- parts of this are adapted from D. Whitley's Genitor algorithm -- */
30 #include "optimizer/geqo_copy.h"
31 #include "optimizer/geqo_pool.h"
32 #include "optimizer/geqo_recombination.h"
35 static int compare(const void *arg1, const void *arg2);
39 * allocates memory for GA pool
42 alloc_pool(PlannerInfo *root, int pool_size, int string_length)
49 new_pool = (Pool *) palloc(sizeof(Pool));
50 new_pool->size = (int) pool_size;
51 new_pool->string_length = (int) string_length;
54 new_pool->data = (Chromosome *) palloc(pool_size * sizeof(Chromosome));
57 chromo = (Chromosome *) new_pool->data; /* vector of all chromos */
58 for (i = 0; i < pool_size; i++)
59 chromo[i].string = palloc((string_length + 1) * sizeof(Gene));
66 * deallocates memory for GA pool
69 free_pool(PlannerInfo *root, Pool *pool)
75 chromo = (Chromosome *) pool->data; /* vector of all chromos */
76 for (i = 0; i < pool->size; i++)
77 pfree(chromo[i].string);
88 * initialize genetic pool
91 random_init_pool(PlannerInfo *root, Pool *pool)
93 Chromosome *chromo = (Chromosome *) pool->data;
96 for (i = 0; i < pool->size; i++)
98 init_tour(root, chromo[i].string, pool->string_length);
99 pool->data[i].worth = geqo_eval(root, chromo[i].string,
100 pool->string_length);
106 * sorts input pool according to worth, from smallest to largest
108 * maybe you have to change compare() for different ordering ...
111 sort_pool(PlannerInfo *root, Pool *pool)
113 qsort(pool->data, pool->size, sizeof(Chromosome), compare);
118 * qsort comparison function for sort_pool
121 compare(const void *arg1, const void *arg2)
123 const Chromosome *chromo1 = (const Chromosome *) arg1;
124 const Chromosome *chromo2 = (const Chromosome *) arg2;
126 if (chromo1->worth == chromo2->worth)
128 else if (chromo1->worth > chromo2->worth)
135 * allocates a chromosome and string space
138 alloc_chromo(PlannerInfo *root, int string_length)
142 chromo = (Chromosome *) palloc(sizeof(Chromosome));
143 chromo->string = (Gene *) palloc((string_length + 1) * sizeof(Gene));
149 * deallocates a chromosome and string space
152 free_chromo(PlannerInfo *root, Chromosome *chromo)
154 pfree(chromo->string);
159 * inserts a new chromosome into the pool, displacing worst gene in pool
160 * assumes best->worst = smallest->largest
163 spread_chromo(PlannerInfo *root, Chromosome *chromo, Pool *pool)
170 Chromosome swap_chromo,
173 /* new chromo is so bad we can't use it */
174 if (chromo->worth > pool->data[pool->size - 1].worth)
177 /* do a binary search to find the index of the new chromo */
180 mid = pool->size / 2;
181 bot = pool->size - 1;
186 /* these 4 cases find a new location */
188 if (chromo->worth <= pool->data[top].worth)
190 else if (chromo->worth == pool->data[mid].worth)
192 else if (chromo->worth == pool->data[bot].worth)
194 else if (bot - top <= 1)
199 * these 2 cases move the search indices since a new location has not
203 else if (chromo->worth < pool->data[mid].worth)
206 mid = top + ((bot - top) / 2);
209 { /* (chromo->worth > pool->data[mid].worth) */
211 mid = top + ((bot - top) / 2);
215 /* now we have index for chromo */
218 * move every gene from index on down one position to make room for chromo
222 * copy new gene into pool storage; always replace worst gene in pool
225 geqo_copy(root, &pool->data[pool->size - 1], chromo, pool->string_length);
227 swap_chromo.string = pool->data[pool->size - 1].string;
228 swap_chromo.worth = pool->data[pool->size - 1].worth;
230 for (i = index; i < pool->size; i++)
232 tmp_chromo.string = pool->data[i].string;
233 tmp_chromo.worth = pool->data[i].worth;
235 pool->data[i].string = swap_chromo.string;
236 pool->data[i].worth = swap_chromo.worth;
238 swap_chromo.string = tmp_chromo.string;
239 swap_chromo.worth = tmp_chromo.worth;