|
6 | 6 | * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
7 | 7 | * Portions Copyright (c) 1994, Regents of the University of California
|
8 | 8 | *
|
9 |
| - * $PostgreSQL: pgsql/src/backend/optimizer/geqo/geqo_selection.c,v 1.18 2004/12/31 21:59:58 pgsql Exp $ |
| 9 | + * $PostgreSQL: pgsql/src/backend/optimizer/geqo/geqo_selection.c,v 1.19 2005/06/14 14:21:16 tgl Exp $ |
10 | 10 | *
|
11 | 11 | *-------------------------------------------------------------------------
|
12 | 12 | */
|
|
44 | 44 |
|
45 | 45 | static int linear(int max, double bias);
|
46 | 46 |
|
47 |
| -/* geqo_selection |
48 |
| - * |
| 47 | + |
| 48 | +/* |
| 49 | + * geqo_selection |
49 | 50 | * according to bias described by input parameters,
|
50 |
| - * second genes are selected from the pool |
| 51 | + * first and second genes are selected from the pool |
51 | 52 | */
|
52 | 53 | void
|
53 | 54 | geqo_selection(Chromosome *momma, Chromosome *daddy, Pool *pool, double bias)
|
54 | 55 | {
|
55 | 56 | int first,
|
56 | 57 | second;
|
57 | 58 |
|
58 |
| - first = (int) linear(pool->size, bias); |
59 |
| - second = (int) linear(pool->size, bias); |
| 59 | + first = linear(pool->size, bias); |
| 60 | + second = linear(pool->size, bias); |
60 | 61 |
|
61 | 62 | if (pool->size > 1)
|
62 | 63 | {
|
63 | 64 | while (first == second)
|
64 |
| - second = (int) linear(pool->size, bias); |
| 65 | + second = linear(pool->size, bias); |
65 | 66 | }
|
66 | 67 |
|
67 | 68 | geqo_copy(momma, &pool->data[first], pool->string_length);
|
68 | 69 | geqo_copy(daddy, &pool->data[second], pool->string_length);
|
69 | 70 | }
|
70 | 71 |
|
71 |
| -/* linear |
| 72 | +/* |
| 73 | + * linear |
72 | 74 | * generates random integer between 0 and input max number
|
73 | 75 | * using input linear bias
|
74 | 76 | *
|
75 | 77 | * probability distribution function is: f(x) = bias - 2(bias - 1)x
|
76 | 78 | * bias = (prob of first rule) / (prob of middle rule)
|
77 |
| - * |
78 | 79 | */
|
79 |
| - |
80 | 80 | static int
|
81 | 81 | linear(int pool_size, double bias) /* bias is y-intercept of linear
|
82 | 82 | * distribution */
|
83 | 83 | {
|
84 | 84 | double index; /* index between 0 and pop_size */
|
85 | 85 | double max = (double) pool_size;
|
86 | 86 |
|
87 |
| - index = max * (bias - sqrt((bias * bias) - 4.0 * (bias - 1.0) * geqo_rand())) |
88 |
| - / 2.0 / (bias - 1.0); |
| 87 | + /* |
| 88 | + * If geqo_rand() returns exactly 1.0 then we will get exactly max from |
| 89 | + * this equation, whereas we need 0 <= index < max. Also it seems possible |
| 90 | + * that roundoff error might deliver values slightly outside the range; |
| 91 | + * in particular avoid passing a value slightly less than 0 to sqrt(). |
| 92 | + * If we get a bad value just try again. |
| 93 | + */ |
| 94 | + do { |
| 95 | + double sqrtval; |
| 96 | + |
| 97 | + sqrtval = (bias * bias) - 4.0 * (bias - 1.0) * geqo_rand(); |
| 98 | + if (sqrtval > 0.0) |
| 99 | + sqrtval = sqrt(sqrtval); |
| 100 | + index = max * (bias - sqrtval) / 2.0 / (bias - 1.0); |
| 101 | + } while (index < 0.0 || index >= max); |
89 | 102 |
|
90 | 103 | return (int) index;
|
91 | 104 | }
|
0 commit comments