PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
geqo_selection.c File Reference
#include "postgres.h"
#include <math.h>
#include "optimizer/geqo_copy.h"
#include "optimizer/geqo_random.h"
#include "optimizer/geqo_selection.h"
Include dependency graph for geqo_selection.c:

Go to the source code of this file.

Functions

static int linear_rand (PlannerInfo *root, int max, double bias)
 
void geqo_selection (PlannerInfo *root, Chromosome *momma, Chromosome *daddy, Pool *pool, double bias)
 

Function Documentation

void geqo_selection ( PlannerInfo root,
Chromosome momma,
Chromosome daddy,
Pool pool,
double  bias 
)

Definition at line 54 of file geqo_selection.c.

References Pool::data, geqo_copy(), linear_rand(), Pool::size, and Pool::string_length.

Referenced by geqo().

56 {
57  int first,
58  second;
59 
60  first = linear_rand(root, pool->size, bias);
61  second = linear_rand(root, pool->size, bias);
62 
63  /*
64  * Ensure we have selected different genes, except if pool size is only
65  * one, when we can't.
66  *
67  * This code was observed to hang up in an infinite loop when the
68  * platform's implementation of erand48() was broken. We now always use
69  * our own version.
70  */
71  if (pool->size > 1)
72  {
73  while (first == second)
74  second = linear_rand(root, pool->size, bias);
75  }
76 
77  geqo_copy(root, momma, &pool->data[first], pool->string_length);
78  geqo_copy(root, daddy, &pool->data[second], pool->string_length);
79 }
int size
Definition: geqo_gene.h:41
static int linear_rand(PlannerInfo *root, int max, double bias)
int string_length
Definition: geqo_gene.h:42
void geqo_copy(PlannerInfo *root, Chromosome *chromo1, Chromosome *chromo2, int string_length)
Definition: geqo_copy.c:45
Chromosome * data
Definition: geqo_gene.h:40
static int linear_rand ( PlannerInfo root,
int  max,
double  bias 
)
static

Definition at line 92 of file geqo_selection.c.

References geqo_rand().

Referenced by geqo_selection().

93 {
94  double index; /* index between 0 and pop_size */
95  double max = (double) pool_size;
96 
97  /*
98  * If geqo_rand() returns exactly 1.0 then we will get exactly max from
99  * this equation, whereas we need 0 <= index < max. Also it seems
100  * possible that roundoff error might deliver values slightly outside the
101  * range; in particular avoid passing a value slightly less than 0 to
102  * sqrt(). If we get a bad value just try again.
103  */
104  do
105  {
106  double sqrtval;
107 
108  sqrtval = (bias * bias) - 4.0 * (bias - 1.0) * geqo_rand(root);
109  if (sqrtval > 0.0)
110  sqrtval = sqrt(sqrtval);
111  index = max * (bias - sqrtval) / 2.0 / (bias - 1.0);
112  } while (index < 0.0 || index >= max);
113 
114  return (int) index;
115 }
double geqo_rand(PlannerInfo *root)
Definition: geqo_random.c:35
Definition: type.h:89