|
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/3121: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 | staticintlinear(intmax,doublebias);
|
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 andsecond genes are selected from the pool |
51 | 52 | */
|
52 | 53 | void
|
53 | 54 | geqo_selection(Chromosome*momma,Chromosome*daddy,Pool*pool,doublebias)
|
54 | 55 | {
|
55 | 56 | intfirst,
|
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 | staticint
|
81 | 81 | linear(intpool_size,doublebias)/* bias is y-intercept of linear
|
82 | 82 | * distribution */
|
83 | 83 | {
|
84 | 84 | doubleindex;/* index between 0 and pop_size */
|
85 | 85 | doublemax= (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 | +doublesqrtval; |
| 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 | }
|