1 /*------------------------------------------------------------------------
4 * solution to the query optimization problem
5 * by means of a Genetic Algorithm (GA)
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/optimizer/geqo/geqo_main.c,v 1.50 2005/06/08 23:02:04 tgl Exp $
12 *-------------------------------------------------------------------------
16 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
17 * Martin Utesch * Institute of Automatic Control *
18 = = University of Mining and Technology =
19 * utesch@aut.tu-freiberg.de * Freiberg, Germany *
20 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
23 /* -- parts of this are adapted from D. Whitley's Genitor algorithm -- */
29 #include "optimizer/geqo.h"
30 #include "optimizer/geqo_misc.h"
31 #include "optimizer/geqo_mutation.h"
32 #include "optimizer/geqo_pool.h"
33 #include "optimizer/geqo_selection.h"
37 * Configuration options
42 double Geqo_selection_bias;
45 static int gimme_pool_size(int nr_rel);
46 static int gimme_number_generations(int pool_size);
48 /* define edge recombination crossover [ERX] per default */
49 #if !defined(ERX) && \
61 * solution of the query optimization problem
62 * similar to a constrained Traveling Salesman Problem (TSP)
66 geqo(PlannerInfo *root, int number_of_rels, List *initial_rels)
68 GeqoEvalData evaldata;
81 Edge *edge_table; /* list of edges */
82 int edge_failures = 0;
85 #if defined(CX) || defined(PX) || defined(OX1) || defined(OX2)
86 City *city_table; /* list of cities */
95 evaldata.initial_rels = initial_rels;
97 /* set GA parameters */
98 pool_size = gimme_pool_size(number_of_rels);
99 number_generations = gimme_number_generations(pool_size);
100 status_interval = 10;
102 /* allocate genetic pool memory */
103 pool = alloc_pool(pool_size, number_of_rels);
105 /* random initialization of the pool */
106 random_init_pool(pool, &evaldata);
108 /* sort the pool according to cheapest path as fitness */
109 sort_pool(pool); /* we have to do it only one time, since
110 * all kids replace the worst individuals
111 * in future (-> geqo_pool.c:spread_chromo
115 elog(DEBUG1, "GEQO selected %d pool entries, best %.2f, worst %.2f",
118 pool->data[pool_size - 1].worth);
121 /* allocate chromosome momma and daddy memory */
122 momma = alloc_chromo(pool->string_length);
123 daddy = alloc_chromo(pool->string_length);
127 elog(DEBUG2, "using edge recombination crossover [ERX]");
129 /* allocate edge table memory */
130 edge_table = alloc_edge_table(pool->string_length);
133 elog(DEBUG2, "using partially matched crossover [PMX]");
135 /* allocate chromosome kid memory */
136 kid = alloc_chromo(pool->string_length);
139 elog(DEBUG2, "using cycle crossover [CX]");
141 /* allocate city table memory */
142 kid = alloc_chromo(pool->string_length);
143 city_table = alloc_city_table(pool->string_length);
146 elog(DEBUG2, "using position crossover [PX]");
148 /* allocate city table memory */
149 kid = alloc_chromo(pool->string_length);
150 city_table = alloc_city_table(pool->string_length);
153 elog(DEBUG2, "using order crossover [OX1]");
155 /* allocate city table memory */
156 kid = alloc_chromo(pool->string_length);
157 city_table = alloc_city_table(pool->string_length);
160 elog(DEBUG2, "using order crossover [OX2]");
162 /* allocate city table memory */
163 kid = alloc_chromo(pool->string_length);
164 city_table = alloc_city_table(pool->string_length);
168 /* my pain main part: */
169 /* iterative optimization */
171 for (generation = 0; generation < number_generations; generation++)
173 /* SELECTION: using linear bias function */
174 geqo_selection(momma, daddy, pool, Geqo_selection_bias);
177 /* EDGE RECOMBINATION CROSSOVER */
178 difference = gimme_edge_table(momma->string, daddy->string, pool->string_length, edge_table);
182 /* are there any edge failures ? */
183 edge_failures += gimme_tour(edge_table, kid->string, pool->string_length);
185 /* PARTIALLY MATCHED CROSSOVER */
186 pmx(momma->string, daddy->string, kid->string, pool->string_length);
188 /* CYCLE CROSSOVER */
189 cycle_diffs = cx(momma->string, daddy->string, kid->string, pool->string_length, city_table);
190 /* mutate the child */
191 if (cycle_diffs == 0)
194 geqo_mutation(kid->string, pool->string_length);
197 /* POSITION CROSSOVER */
198 px(momma->string, daddy->string, kid->string, pool->string_length, city_table);
200 /* ORDER CROSSOVER */
201 ox1(momma->string, daddy->string, kid->string, pool->string_length, city_table);
203 /* ORDER CROSSOVER */
204 ox2(momma->string, daddy->string, kid->string, pool->string_length, city_table);
208 /* EVALUATE FITNESS */
209 kid->worth = geqo_eval(kid->string, pool->string_length, &evaldata);
211 /* push the kid into the wilderness of life according to its worth */
212 spread_chromo(kid, pool);
216 if (status_interval && !(generation % status_interval))
217 print_gen(stdout, pool, generation);
223 #if defined(ERX) && defined(GEQO_DEBUG)
224 if (edge_failures != 0)
225 elog(LOG, "[GEQO] failures: %d, average: %d",
226 edge_failures, (int) number_generations / edge_failures);
228 elog(LOG, "[GEQO] no edge failures detected");
231 #if defined(CX) && defined(GEQO_DEBUG)
233 elog(LOG, "[GEQO] mutations: %d, generations: %d",
234 mutations, number_generations);
236 elog(LOG, "[GEQO] no mutations processed");
240 print_pool(stdout, pool, 0, pool_size - 1);
244 elog(DEBUG1, "GEQO best is %.2f after %d generations",
245 pool->data[0].worth, number_generations);
250 * got the cheapest query tree processed by geqo; first element of the
251 * population indicates the best query tree
253 best_tour = (Gene *) pool->data[0].string;
255 best_rel = gimme_tree(best_tour, pool->string_length, &evaldata);
257 if (best_rel == NULL)
258 elog(ERROR, "failed to make a valid plan");
260 /* DBG: show the query plan */
262 print_plan(best_plan, root);
265 /* ... free memory stuff */
270 free_edge_table(edge_table);
275 free_city_table(city_table);
278 free_city_table(city_table);
281 free_city_table(city_table);
284 free_city_table(city_table);
294 * Return either configured pool size or a good default
296 * The default is based on query size (no. of relations) = 2^(QS+1),
297 * but constrained to a range based on the effort value.
300 gimme_pool_size(int nr_rel)
306 /* Legal pool size *must* be at least 2, so ignore attempt to select 1 */
307 if (Geqo_pool_size >= 2)
308 return Geqo_pool_size;
310 size = pow(2.0, nr_rel + 1.0);
312 maxsize = 50 * Geqo_effort; /* 50 to 500 individuals */
316 minsize = 10 * Geqo_effort; /* 10 to 100 individuals */
320 return (int) ceil(size);
325 * Return either configured number of generations or a good default
327 * The default is the same as the pool size, which allows us to be
328 * sure that less-fit individuals get pushed out of the breeding
329 * population before the run finishes.
332 gimme_number_generations(int pool_size)
334 if (Geqo_generations > 0)
335 return Geqo_generations;