PostgreSQL Source Code  git master
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

◆ geqo_selection()

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

Definition at line 54 of file geqo_selection.c.

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  if (pool->size > 1)
68  {
69  while (first == second)
70  second = linear_rand(root, pool->size, bias);
71  }
72 
73  geqo_copy(root, momma, &pool->data[first], pool->string_length);
74  geqo_copy(root, daddy, &pool->data[second], pool->string_length);
75 }
void geqo_copy(PlannerInfo *root, Chromosome *chromo1, Chromosome *chromo2, int string_length)
Definition: geqo_copy.c:45
static int linear_rand(PlannerInfo *root, int max, double bias)
int string_length
Definition: geqo_gene.h:42
int size
Definition: geqo_gene.h:41
Chromosome * data
Definition: geqo_gene.h:40

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

Referenced by geqo().

◆ linear_rand()

static int linear_rand ( PlannerInfo root,
int  max,
double  bias 
)
static

Definition at line 88 of file geqo_selection.c.

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

References geqo_rand().

Referenced by geqo_selection().