1 /*------------------------------------------------------------------------
4 * solution to the query optimization problem
5 * by means of a Genetic Algorithm (GA)
7 * Portions Copyright (c) 1996-2007, 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.54 2007/01/05 22:19:30 momjian 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_misc.h"
30 #include "optimizer/geqo_pool.h"
31 #include "optimizer/geqo_selection.h"
35 * Configuration options
40 double Geqo_selection_bias;
43 static int gimme_pool_size(int nr_rel);
44 static int gimme_number_generations(int pool_size);
46 /* define edge recombination crossover [ERX] per default */
47 #if !defined(ERX) && \
59 * solution of the query optimization problem
60 * similar to a constrained Traveling Salesman Problem (TSP)
64 geqo(PlannerInfo *root, int number_of_rels, List *initial_rels)
66 GeqoEvalData evaldata;
79 Edge *edge_table; /* list of edges */
80 int edge_failures = 0;
83 #if defined(CX) || defined(PX) || defined(OX1) || defined(OX2)
84 City *city_table; /* list of cities */
93 evaldata.initial_rels = initial_rels;
95 /* set GA parameters */
96 pool_size = gimme_pool_size(number_of_rels);
97 number_generations = gimme_number_generations(pool_size);
100 /* allocate genetic pool memory */
101 pool = alloc_pool(pool_size, number_of_rels);
103 /* random initialization of the pool */
104 random_init_pool(pool, &evaldata);
106 /* sort the pool according to cheapest path as fitness */
107 sort_pool(pool); /* we have to do it only one time, since all
108 * kids replace the worst individuals in
109 * future (-> geqo_pool.c:spread_chromo ) */
112 elog(DEBUG1, "GEQO selected %d pool entries, best %.2f, worst %.2f",
115 pool->data[pool_size - 1].worth);
118 /* allocate chromosome momma and daddy memory */
119 momma = alloc_chromo(pool->string_length);
120 daddy = alloc_chromo(pool->string_length);
124 elog(DEBUG2, "using edge recombination crossover [ERX]");
126 /* allocate edge table memory */
127 edge_table = alloc_edge_table(pool->string_length);
130 elog(DEBUG2, "using partially matched crossover [PMX]");
132 /* allocate chromosome kid memory */
133 kid = alloc_chromo(pool->string_length);
136 elog(DEBUG2, "using cycle crossover [CX]");
138 /* allocate city table memory */
139 kid = alloc_chromo(pool->string_length);
140 city_table = alloc_city_table(pool->string_length);
143 elog(DEBUG2, "using position crossover [PX]");
145 /* allocate city table memory */
146 kid = alloc_chromo(pool->string_length);
147 city_table = alloc_city_table(pool->string_length);
150 elog(DEBUG2, "using order crossover [OX1]");
152 /* allocate city table memory */
153 kid = alloc_chromo(pool->string_length);
154 city_table = alloc_city_table(pool->string_length);
157 elog(DEBUG2, "using order crossover [OX2]");
159 /* allocate city table memory */
160 kid = alloc_chromo(pool->string_length);
161 city_table = alloc_city_table(pool->string_length);
165 /* my pain main part: */
166 /* iterative optimization */
168 for (generation = 0; generation < number_generations; generation++)
170 /* SELECTION: using linear bias function */
171 geqo_selection(momma, daddy, pool, Geqo_selection_bias);
174 /* EDGE RECOMBINATION CROSSOVER */
175 difference = gimme_edge_table(momma->string, daddy->string, pool->string_length, edge_table);
179 /* are there any edge failures ? */
180 edge_failures += gimme_tour(edge_table, kid->string, pool->string_length);
182 /* PARTIALLY MATCHED CROSSOVER */
183 pmx(momma->string, daddy->string, kid->string, pool->string_length);
185 /* CYCLE CROSSOVER */
186 cycle_diffs = cx(momma->string, daddy->string, kid->string, pool->string_length, city_table);
187 /* mutate the child */
188 if (cycle_diffs == 0)
191 geqo_mutation(kid->string, pool->string_length);
194 /* POSITION CROSSOVER */
195 px(momma->string, daddy->string, kid->string, pool->string_length, city_table);
197 /* ORDER CROSSOVER */
198 ox1(momma->string, daddy->string, kid->string, pool->string_length, city_table);
200 /* ORDER CROSSOVER */
201 ox2(momma->string, daddy->string, kid->string, pool->string_length, city_table);
205 /* EVALUATE FITNESS */
206 kid->worth = geqo_eval(kid->string, pool->string_length, &evaldata);
208 /* push the kid into the wilderness of life according to its worth */
209 spread_chromo(kid, pool);
213 if (status_interval && !(generation % status_interval))
214 print_gen(stdout, pool, generation);
220 #if defined(ERX) && defined(GEQO_DEBUG)
221 if (edge_failures != 0)
222 elog(LOG, "[GEQO] failures: %d, average: %d",
223 edge_failures, (int) number_generations / edge_failures);
225 elog(LOG, "[GEQO] no edge failures detected");
228 #if defined(CX) && defined(GEQO_DEBUG)
230 elog(LOG, "[GEQO] mutations: %d, generations: %d",
231 mutations, number_generations);
233 elog(LOG, "[GEQO] no mutations processed");
237 print_pool(stdout, pool, 0, pool_size - 1);
241 elog(DEBUG1, "GEQO best is %.2f after %d generations",
242 pool->data[0].worth, number_generations);
247 * got the cheapest query tree processed by geqo; first element of the
248 * population indicates the best query tree
250 best_tour = (Gene *) pool->data[0].string;
252 best_rel = gimme_tree(best_tour, pool->string_length, &evaldata);
254 if (best_rel == NULL)
255 elog(ERROR, "failed to make a valid plan");
257 /* DBG: show the query plan */
259 print_plan(best_plan, root);
262 /* ... free memory stuff */
267 free_edge_table(edge_table);
272 free_city_table(city_table);
275 free_city_table(city_table);
278 free_city_table(city_table);
281 free_city_table(city_table);
291 * Return either configured pool size or a good default
293 * The default is based on query size (no. of relations) = 2^(QS+1),
294 * but constrained to a range based on the effort value.
297 gimme_pool_size(int nr_rel)
303 /* Legal pool size *must* be at least 2, so ignore attempt to select 1 */
304 if (Geqo_pool_size >= 2)
305 return Geqo_pool_size;
307 size = pow(2.0, nr_rel + 1.0);
309 maxsize = 50 * Geqo_effort; /* 50 to 500 individuals */
313 minsize = 10 * Geqo_effort; /* 10 to 100 individuals */
317 return (int) ceil(size);
322 * Return either configured number of generations or a good default
324 * The default is the same as the pool size, which allows us to be
325 * sure that less-fit individuals get pushed out of the breeding
326 * population before the run finishes.
329 gimme_number_generations(int pool_size)
331 if (Geqo_generations > 0)
332 return Geqo_generations;