1 /*------------------------------------------------------------------------
4 * solution to the query optimization problem
5 * by means of a Genetic Algorithm (GA)
7 * Portions Copyright (c) 1996-2006, 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.52 2006/03/05 15:58:28 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.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 all
110 * kids replace the worst individuals in
111 * future (-> geqo_pool.c:spread_chromo ) */
114 elog(DEBUG1, "GEQO selected %d pool entries, best %.2f, worst %.2f",
117 pool->data[pool_size - 1].worth);
120 /* allocate chromosome momma and daddy memory */
121 momma = alloc_chromo(pool->string_length);
122 daddy = alloc_chromo(pool->string_length);
126 elog(DEBUG2, "using edge recombination crossover [ERX]");
128 /* allocate edge table memory */
129 edge_table = alloc_edge_table(pool->string_length);
132 elog(DEBUG2, "using partially matched crossover [PMX]");
134 /* allocate chromosome kid memory */
135 kid = alloc_chromo(pool->string_length);
138 elog(DEBUG2, "using cycle crossover [CX]");
140 /* allocate city table memory */
141 kid = alloc_chromo(pool->string_length);
142 city_table = alloc_city_table(pool->string_length);
145 elog(DEBUG2, "using position crossover [PX]");
147 /* allocate city table memory */
148 kid = alloc_chromo(pool->string_length);
149 city_table = alloc_city_table(pool->string_length);
152 elog(DEBUG2, "using order crossover [OX1]");
154 /* allocate city table memory */
155 kid = alloc_chromo(pool->string_length);
156 city_table = alloc_city_table(pool->string_length);
159 elog(DEBUG2, "using order crossover [OX2]");
161 /* allocate city table memory */
162 kid = alloc_chromo(pool->string_length);
163 city_table = alloc_city_table(pool->string_length);
167 /* my pain main part: */
168 /* iterative optimization */
170 for (generation = 0; generation < number_generations; generation++)
172 /* SELECTION: using linear bias function */
173 geqo_selection(momma, daddy, pool, Geqo_selection_bias);
176 /* EDGE RECOMBINATION CROSSOVER */
177 difference = gimme_edge_table(momma->string, daddy->string, pool->string_length, edge_table);
181 /* are there any edge failures ? */
182 edge_failures += gimme_tour(edge_table, kid->string, pool->string_length);
184 /* PARTIALLY MATCHED CROSSOVER */
185 pmx(momma->string, daddy->string, kid->string, pool->string_length);
187 /* CYCLE CROSSOVER */
188 cycle_diffs = cx(momma->string, daddy->string, kid->string, pool->string_length, city_table);
189 /* mutate the child */
190 if (cycle_diffs == 0)
193 geqo_mutation(kid->string, pool->string_length);
196 /* POSITION CROSSOVER */
197 px(momma->string, daddy->string, kid->string, pool->string_length, city_table);
199 /* ORDER CROSSOVER */
200 ox1(momma->string, daddy->string, kid->string, pool->string_length, city_table);
202 /* ORDER CROSSOVER */
203 ox2(momma->string, daddy->string, kid->string, pool->string_length, city_table);
207 /* EVALUATE FITNESS */
208 kid->worth = geqo_eval(kid->string, pool->string_length, &evaldata);
210 /* push the kid into the wilderness of life according to its worth */
211 spread_chromo(kid, pool);
215 if (status_interval && !(generation % status_interval))
216 print_gen(stdout, pool, generation);
222 #if defined(ERX) && defined(GEQO_DEBUG)
223 if (edge_failures != 0)
224 elog(LOG, "[GEQO] failures: %d, average: %d",
225 edge_failures, (int) number_generations / edge_failures);
227 elog(LOG, "[GEQO] no edge failures detected");
230 #if defined(CX) && defined(GEQO_DEBUG)
232 elog(LOG, "[GEQO] mutations: %d, generations: %d",
233 mutations, number_generations);
235 elog(LOG, "[GEQO] no mutations processed");
239 print_pool(stdout, pool, 0, pool_size - 1);
243 elog(DEBUG1, "GEQO best is %.2f after %d generations",
244 pool->data[0].worth, number_generations);
249 * got the cheapest query tree processed by geqo; first element of the
250 * population indicates the best query tree
252 best_tour = (Gene *) pool->data[0].string;
254 best_rel = gimme_tree(best_tour, pool->string_length, &evaldata);
256 if (best_rel == NULL)
257 elog(ERROR, "failed to make a valid plan");
259 /* DBG: show the query plan */
261 print_plan(best_plan, root);
264 /* ... free memory stuff */
269 free_edge_table(edge_table);
274 free_city_table(city_table);
277 free_city_table(city_table);
280 free_city_table(city_table);
283 free_city_table(city_table);
293 * Return either configured pool size or a good default
295 * The default is based on query size (no. of relations) = 2^(QS+1),
296 * but constrained to a range based on the effort value.
299 gimme_pool_size(int nr_rel)
305 /* Legal pool size *must* be at least 2, so ignore attempt to select 1 */
306 if (Geqo_pool_size >= 2)
307 return Geqo_pool_size;
309 size = pow(2.0, nr_rel + 1.0);
311 maxsize = 50 * Geqo_effort; /* 50 to 500 individuals */
315 minsize = 10 * Geqo_effort; /* 10 to 100 individuals */
319 return (int) ceil(size);
324 * Return either configured number of generations or a good default
326 * The default is the same as the pool size, which allows us to be
327 * sure that less-fit individuals get pushed out of the breeding
328 * population before the run finishes.
331 gimme_number_generations(int pool_size)
333 if (Geqo_generations > 0)
334 return Geqo_generations;