PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
geqo_px.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------------
2 *
3 * geqo_px.c
4 *
5 * position crossover [PX] routines;
6 * PX operator according to Syswerda
7 * (The Genetic Algorithms Handbook, L Davis, ed)
8 *
9 * src/backend/optimizer/geqo/geqo_px.c
10 *
11 *-------------------------------------------------------------------------
12 */
13 
14 /* contributed by:
15  =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
16  * Martin Utesch * Institute of Automatic Control *
17  = = University of Mining and Technology =
18  * utesch@aut.tu-freiberg.de * Freiberg, Germany *
19  =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
20  */
21 
22 /* the px algorithm is adopted from Genitor : */
23 /*************************************************************/
24 /* */
25 /* Copyright (c) 1990 */
26 /* Darrell L. Whitley */
27 /* Computer Science Department */
28 /* Colorado State University */
29 /* */
30 /* Permission is hereby granted to copy all or any part of */
31 /* this program for free distribution. The author's name */
32 /* and this copyright notice must be included in any copy. */
33 /* */
34 /*************************************************************/
35 
36 #include "postgres.h"
37 #include "optimizer/geqo_random.h"
39 
40 
41 /* px
42  *
43  * position crossover
44  */
45 void
46 px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene,
47  City *city_table)
48 {
49  int num_positions;
50  int i,
51  pos,
52  tour2_index,
53  offspring_index;
54 
55  /* initialize city table */
56  for (i = 1; i <= num_gene; i++)
57  city_table[i].used = 0;
58 
59  /* choose random positions that will be inherited directly from parent */
60  num_positions = geqo_randint(root, 2 * num_gene / 3, num_gene / 3);
61 
62  /* choose random position */
63  for (i = 0; i < num_positions; i++)
64  {
65  pos = geqo_randint(root, num_gene - 1, 0);
66 
67  offspring[pos] = tour1[pos]; /* transfer cities to child */
68  city_table[(int) tour1[pos]].used = 1; /* mark city used */
69  }
70 
71  tour2_index = 0;
72  offspring_index = 0;
73 
74 
75  /* px main part */
76 
77  while (offspring_index < num_gene)
78  {
79 
80  /* next position in offspring filled */
81  if (!city_table[(int) tour1[offspring_index]].used)
82  {
83 
84  /* next city in tour1 not used */
85  if (!city_table[(int) tour2[tour2_index]].used)
86  {
87 
88  /* inherit from tour1 */
89  offspring[offspring_index] = tour2[tour2_index];
90 
91  tour2_index++;
92  offspring_index++;
93  }
94  else
95  { /* next city in tour2 has been used */
96  tour2_index++;
97  }
98 
99  }
100  else
101  { /* next position in offspring is filled */
102  offspring_index++;
103  }
104 
105  }
106 
107 }
#define geqo_randint(root, upper, lower)
Definition: geqo_random.h:38
int Gene
Definition: geqo_gene.h:30
void px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
Definition: geqo_px.c:46
int i