PostgreSQL Source Code  git master
geqo_recombination.c File Reference
Include dependency graph for geqo_recombination.c:

Go to the source code of this file.

Functions

void init_tour (PlannerInfo *root, Gene *tour, int num_gene)
 

Function Documentation

◆ init_tour()

void init_tour ( PlannerInfo root,
Gene tour,
int  num_gene 
)

Definition at line 34 of file geqo_recombination.c.

35 {
36  int i,
37  j;
38 
39  /*
40  * We must fill the tour[] array with a random permutation of the numbers
41  * 1 .. num_gene. We can do that in one pass using the "inside-out"
42  * variant of the Fisher-Yates shuffle algorithm. Notionally, we append
43  * each new value to the array and then swap it with a randomly-chosen
44  * array element (possibly including itself, else we fail to generate
45  * permutations with the last city last). The swap step can be optimized
46  * by combining it with the insertion.
47  */
48  if (num_gene > 0)
49  tour[0] = (Gene) 1;
50 
51  for (i = 1; i < num_gene; i++)
52  {
53  j = geqo_randint(root, i, 0);
54  /* i != j check avoids fetching uninitialized array element */
55  if (i != j)
56  tour[i] = tour[j];
57  tour[j] = (Gene) (i + 1);
58  }
59 }
int Gene
Definition: geqo_gene.h:30
int geqo_randint(PlannerInfo *root, int upper, int lower)
Definition: geqo_random.c:36
int j
Definition: isn.c:74
int i
Definition: isn.c:73
tree ctl root
Definition: radixtree.h:1886

References geqo_randint(), i, j, and root.

Referenced by random_init_pool().