PostgreSQL Source Code  git master
geqo_recombination.h File Reference
#include "optimizer/geqo.h"
Include dependency graph for geqo_recombination.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Edge
 
struct  City
 

Macros

#define DAD   1 /* indicator for gene from dad */
 
#define MOM   0 /* indicator for gene from mom */
 

Typedefs

typedef struct Edge Edge
 
typedef struct City City
 

Functions

void init_tour (PlannerInfo *root, Gene *tour, int num_gene)
 
Edgealloc_edge_table (PlannerInfo *root, int num_gene)
 
void free_edge_table (PlannerInfo *root, Edge *edge_table)
 
float gimme_edge_table (PlannerInfo *root, Gene *tour1, Gene *tour2, int num_gene, Edge *edge_table)
 
int gimme_tour (PlannerInfo *root, Edge *edge_table, Gene *new_gene, int num_gene)
 
void pmx (PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene)
 
Cityalloc_city_table (PlannerInfo *root, int num_gene)
 
void free_city_table (PlannerInfo *root, City *city_table)
 
int cx (PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
 
void px (PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
 
void ox1 (PlannerInfo *root, Gene *mom, Gene *dad, Gene *offspring, int num_gene, City *city_table)
 
void ox2 (PlannerInfo *root, Gene *mom, Gene *dad, Gene *offspring, int num_gene, City *city_table)
 

Macro Definition Documentation

◆ DAD

#define DAD   1 /* indicator for gene from dad */

Definition at line 54 of file geqo_recombination.h.

◆ MOM

#define MOM   0 /* indicator for gene from mom */

Definition at line 55 of file geqo_recombination.h.

Typedef Documentation

◆ City

typedef struct City City

◆ Edge

typedef struct Edge Edge

Function Documentation

◆ alloc_city_table()

City* alloc_city_table ( PlannerInfo root,
int  num_gene 
)

Referenced by geqo().

◆ alloc_edge_table()

Edge* alloc_edge_table ( PlannerInfo root,
int  num_gene 
)

Definition at line 54 of file geqo_erx.c.

55 {
56  Edge *edge_table;
57 
58  /*
59  * palloc one extra location so that nodes numbered 1..n can be indexed
60  * directly; 0 will not be used
61  */
62 
63  edge_table = (Edge *) palloc((num_gene + 1) * sizeof(Edge));
64 
65  return edge_table;
66 }
struct Edge Edge
void * palloc(Size size)
Definition: mcxt.c:1226

References palloc().

Referenced by geqo().

◆ cx()

int cx ( PlannerInfo root,
Gene tour1,
Gene tour2,
Gene offspring,
int  num_gene,
City city_table 
)

◆ free_city_table()

void free_city_table ( PlannerInfo root,
City city_table 
)

Referenced by geqo().

◆ free_edge_table()

void free_edge_table ( PlannerInfo root,
Edge edge_table 
)

Definition at line 74 of file geqo_erx.c.

75 {
76  pfree(edge_table);
77 }
void pfree(void *pointer)
Definition: mcxt.c:1456

References pfree().

Referenced by geqo().

◆ gimme_edge_table()

float gimme_edge_table ( PlannerInfo root,
Gene tour1,
Gene tour2,
int  num_gene,
Edge edge_table 
)

Definition at line 93 of file geqo_erx.c.

95 {
96  int i,
97  index1,
98  index2;
99  int edge_total; /* total number of unique edges in two genes */
100 
101  /* at first clear the edge table's old data */
102  for (i = 1; i <= num_gene; i++)
103  {
104  edge_table[i].total_edges = 0;
105  edge_table[i].unused_edges = 0;
106  }
107 
108  /* fill edge table with new data */
109 
110  edge_total = 0;
111 
112  for (index1 = 0; index1 < num_gene; index1++)
113  {
114  /*
115  * presume the tour is circular, i.e. 1->2, 2->3, 3->1 this operation
116  * maps n back to 1
117  */
118 
119  index2 = (index1 + 1) % num_gene;
120 
121  /*
122  * edges are bidirectional, i.e. 1->2 is same as 2->1 call gimme_edge
123  * twice per edge
124  */
125 
126  edge_total += gimme_edge(root, tour1[index1], tour1[index2], edge_table);
127  gimme_edge(root, tour1[index2], tour1[index1], edge_table);
128 
129  edge_total += gimme_edge(root, tour2[index1], tour2[index2], edge_table);
130  gimme_edge(root, tour2[index2], tour2[index1], edge_table);
131  }
132 
133  /* return average number of edges per index */
134  return ((float) (edge_total * 2) / (float) num_gene);
135 }
static int gimme_edge(PlannerInfo *root, Gene gene1, Gene gene2, Edge *edge_table)
Definition: geqo_erx.c:152
int i
Definition: isn.c:73
int unused_edges
int total_edges

References gimme_edge(), i, Edge::total_edges, and Edge::unused_edges.

Referenced by geqo().

◆ gimme_tour()

int gimme_tour ( PlannerInfo root,
Edge edge_table,
Gene new_gene,
int  num_gene 
)

Definition at line 194 of file geqo_erx.c.

195 {
196  int i;
197  int edge_failures = 0;
198 
199  /* choose int between 1 and num_gene */
200  new_gene[0] = (Gene) geqo_randint(root, num_gene, 1);
201 
202  for (i = 1; i < num_gene; i++)
203  {
204  /*
205  * as each point is entered into the tour, remove it from the edge
206  * table
207  */
208 
209  remove_gene(root, new_gene[i - 1], edge_table[(int) new_gene[i - 1]], edge_table);
210 
211  /* find destination for the newly entered point */
212 
213  if (edge_table[new_gene[i - 1]].unused_edges > 0)
214  new_gene[i] = gimme_gene(root, edge_table[(int) new_gene[i - 1]], edge_table);
215 
216  else
217  { /* cope with fault */
218  edge_failures++;
219 
220  new_gene[i] = edge_failure(root, new_gene, i - 1, edge_table, num_gene);
221  }
222 
223  /* mark this node as incorporated */
224  edge_table[(int) new_gene[i - 1]].unused_edges = -1;
225  } /* for (i=1; i<num_gene; i++) */
226 
227  return edge_failures;
228 }
static Gene gimme_gene(PlannerInfo *root, Edge edge, Edge *edge_table)
Definition: geqo_erx.c:280
static Gene edge_failure(PlannerInfo *root, Gene *gene, int index, Edge *edge_table, int num_gene)
Definition: geqo_erx.c:370
static void remove_gene(PlannerInfo *root, Gene gene, Edge edge, Edge *edge_table)
Definition: geqo_erx.c:238
int Gene
Definition: geqo_gene.h:30
int geqo_randint(PlannerInfo *root, int upper, int lower)
Definition: geqo_random.c:36

References edge_failure(), geqo_randint(), gimme_gene(), i, and remove_gene().

Referenced by geqo().

◆ 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 j
Definition: isn.c:74

References geqo_randint(), i, and j.

Referenced by random_init_pool().

◆ ox1()

void ox1 ( PlannerInfo root,
Gene mom,
Gene dad,
Gene offspring,
int  num_gene,
City city_table 
)

Referenced by geqo().

◆ ox2()

void ox2 ( PlannerInfo root,
Gene mom,
Gene dad,
Gene offspring,
int  num_gene,
City city_table 
)

Referenced by geqo().

◆ pmx()

void pmx ( PlannerInfo root,
Gene tour1,
Gene tour2,
Gene offspring,
int  num_gene 
)

Referenced by geqo().

◆ px()

void px ( PlannerInfo root,
Gene tour1,
Gene tour2,
Gene offspring,
int  num_gene,
City city_table 
)