1 /*------------------------------------------------------------------------
4 * solution of the query optimization problem
5 * by means of a Genetic Algorithm (GA)
7 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: geqo_main.c,v 1.21 2000/05/31 00:28:19 petere 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 -- */
30 #include "optimizer/geqo.h"
31 #include "optimizer/geqo_misc.h"
32 #include "optimizer/geqo_pool.h"
33 #include "optimizer/geqo_selection.h"
37 * Configuration options
42 double Geqo_selection_bias;
46 static int gimme_pool_size(int nr_rel);
47 static int gimme_number_generations(int pool_size, int effort);
50 /* define edge recombination crossover [ERX] per default */
51 #if !defined(ERX) && \
63 * solution of the query optimization problem
64 * similar to a constrained Traveling Salesman Problem (TSP)
83 Edge *edge_table; /* list of edges */
84 int edge_failures = 0;
88 #if defined(CX) || defined(PX) || defined(OX1) || defined(OX2)
89 City *city_table; /* list of cities */
99 number_of_rels = length(root->base_rel_list);
101 /* set GA parameters */
102 pool_size = gimme_pool_size(number_of_rels);
103 number_generations = gimme_number_generations(pool_size, Geqo_effort);
104 status_interval = 10;
106 /* seed random number generator */
107 /* XXX why is this done every time around? */
108 if (Geqo_random_seed >= 0)
109 srandom(Geqo_random_seed);
113 /* initialize plan evaluator */
116 /* allocate genetic pool memory */
117 pool = alloc_pool(pool_size, number_of_rels);
119 /* random initialization of the pool */
120 random_init_pool(root, pool, 0, pool->size);
122 /* sort the pool according to cheapest path as fitness */
123 sort_pool(pool); /* we have to do it only one time, since
124 * all kids replace the worst individuals
125 * in future (-> geqo_pool.c:spread_chromo
128 /* allocate chromosome momma and daddy memory */
129 momma = alloc_chromo(pool->string_length);
130 daddy = alloc_chromo(pool->string_length);
133 elog(DEBUG, "geqo_main: using edge recombination crossover [ERX]");
134 /* allocate edge table memory */
135 edge_table = alloc_edge_table(pool->string_length);
137 elog(DEBUG, "geqo_main: using partially matched crossover [PMX]");
138 /* allocate chromosome kid memory */
139 kid = alloc_chromo(pool->string_length);
141 elog(DEBUG, "geqo_main: using cycle crossover [CX]");
142 /* allocate city table memory */
143 kid = alloc_chromo(pool->string_length);
144 city_table = alloc_city_table(pool->string_length);
146 elog(DEBUG, "geqo_main: 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);
151 elog(DEBUG, "geqo_main: 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);
156 elog(DEBUG, "geqo_main: using order crossover [OX2]");
157 /* allocate city table memory */
158 kid = alloc_chromo(pool->string_length);
159 city_table = alloc_city_table(pool->string_length);
163 /* my pain main part: */
164 /* iterative optimization */
166 for (generation = 0; generation < number_generations; generation++)
170 geqo_selection(momma, daddy, pool, Geqo_selection_bias);/* using linear bias
176 /* EDGE RECOMBINATION CROSSOVER */
177 difference = gimme_edge_table(momma->string, daddy->string, pool->string_length, edge_table);
179 /* let the kid grow in momma's womb (storage) for nine months ;-) */
180 /* sleep(23328000) -- har har har */
183 /* are there any edge failures ? */
184 edge_failures += gimme_tour(edge_table, kid->string, pool->string_length);
186 /* PARTIALLY MATCHED CROSSOVER */
187 pmx(momma->string, daddy->string, kid->string, pool->string_length);
189 /* CYCLE CROSSOVER */
190 cycle_diffs = cx(momma->string, daddy->string, kid->string, pool->string_length, city_table);
191 /* mutate the child */
192 if (cycle_diffs == 0)
195 geqo_mutation(kid->string, pool->string_length);
198 /* POSITION CROSSOVER */
199 px(momma->string, daddy->string, kid->string, pool->string_length, city_table);
201 /* ORDER CROSSOVER */
202 ox1(momma->string, daddy->string, kid->string, pool->string_length, city_table);
204 /* ORDER CROSSOVER */
205 ox2(momma->string, daddy->string, kid->string, pool->string_length, city_table);
209 /* EVALUATE FITNESS */
210 kid->worth = geqo_eval(root, kid->string, pool->string_length);
212 /* push the kid into the wilderness of life according to its worth */
213 spread_chromo(kid, pool);
217 if (status_interval && !(generation % status_interval))
218 print_gen(stdout, pool, generation);
221 } /* end of iterative optimization */
224 #if defined(ERX) && defined(GEQO_DEBUG)
225 if (edge_failures != 0)
226 fprintf(stdout, "\nFailures: %d Avg: %d\n", edge_failures, (int) generation / edge_failures);
229 fprintf(stdout, "No edge failures detected.\n");
233 #if defined(CX) && defined(GEQO_DEBUG)
235 fprintf(stdout, "\nMutations: %d Generations: %d\n", mutations, generation);
238 fprintf(stdout, "No mutations processed.\n");
243 fprintf(stdout, "\n");
244 print_pool(stdout, pool, 0, pool_size - 1);
248 /* got the cheapest query tree processed by geqo;
249 first element of the population indicates the best query tree */
251 best_tour = (Gene *) pool->data[0].string;
253 /* root->join_relation_list_ will be modified during this ! */
254 best_rel = (RelOptInfo *) gimme_tree(root, best_tour, 0,
255 pool->string_length, NULL);
257 /* DBG: show the query plan
258 print_plan(best_plan, root);
261 /* ... free memory stuff */
266 free_edge_table(edge_table);
271 free_city_table(city_table);
274 free_city_table(city_table);
277 free_city_table(city_table);
280 free_city_table(city_table);
291 * Return either configured pool size or
292 * a good default based on query size (no. of relations)
294 * also constrain between 128 and 1024
297 gimme_pool_size(int nr_rel)
301 if (Geqo_pool_size != 0)
303 if (Geqo_pool_size < MIN_GEQO_POOL_SIZE)
304 return MIN_GEQO_POOL_SIZE;
305 else if (Geqo_pool_size > MAX_GEQO_POOL_SIZE)
306 return MAX_GEQO_POOL_SIZE;
308 return Geqo_pool_size;
311 size = pow(2.0, nr_rel + 1.0);
313 if (size < MIN_GEQO_POOL_SIZE)
314 return MIN_GEQO_POOL_SIZE;
315 else if (size > MAX_GEQO_POOL_SIZE)
316 return MAX_GEQO_POOL_SIZE;
318 return (int) ceil(size);
324 * Return either configured number of generations or
325 * some reasonable default calculated on the fly.
326 * = Effort * Log2(PoolSize)
329 gimme_number_generations(int pool_size, int effort)
331 if (Geqo_generations <= 0)
332 return effort * (int) ceil(log((double) pool_size) / log(2.0));
334 return Geqo_generations;