1 /*-------------------------------------------------------------------------
4 * linear selection scheme for the genetic query optimizer
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * $Id: geqo_selection.c,v 1.11 2000/01/26 05:56:33 momjian Exp $
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 /* this is adopted from D. Whitley's Genitor algorithm */
24 /*************************************************************/
26 /* Copyright (c) 1990 */
27 /* Darrell L. Whitley */
28 /* Computer Science Department */
29 /* Colorado State University */
31 /* Permission is hereby granted to copy all or any part of */
32 /* this program for free distribution. The author's name */
33 /* and this copyright notice must be included in any copy. */
35 /*************************************************************/
40 #include "optimizer/geqo_copy.h"
41 #include "optimizer/geqo_random.h"
42 #include "optimizer/geqo_selection.h"
44 static int linear(int max, double bias);
48 * according to bias described by input parameters,
49 * second genes are selected from the pool
52 geqo_selection(Chromosome *momma, Chromosome *daddy, Pool *pool, double bias)
57 first = (int) linear(pool->size, bias);
58 second = (int) linear(pool->size, bias);
62 while (first == second)
63 second = (int) linear(pool->size, bias);
66 geqo_copy(momma, &pool->data[first], pool->string_length);
67 geqo_copy(daddy, &pool->data[second], pool->string_length);
71 * generates random integer between 0 and input max number
72 * using input linear bias
74 * probability distribution function is: f(x) = bias - 2(bias - 1)x
75 * bias = (prob of first rule) / (prob of middle rule)
80 linear(int pool_size, double bias) /* bias is y-intercept of linear
83 double index; /* index between 0 and pop_size */
84 double max = (double) pool_size;
86 index = max * (bias - sqrt((bias * bias) - 4.0 * (bias - 1.0) * geqo_rand()))