PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
geqo_ox2.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------------
2 *
3 * geqo_ox2.c
4 *
5 * order crossover [OX] routines;
6 * OX2 operator according to Syswerda
7 * (The Genetic Algorithms Handbook, ed L Davis)
8 *
9 * src/backend/optimizer/geqo/geqo_ox2.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 ox 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 /* ox2
42  *
43  * position crossover
44  */
45 void
46 ox2(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
47 {
48  int k,
49  j,
50  count,
51  pos,
52  select,
53  num_positions;
54 
55  /* initialize city table */
56  for (k = 1; k <= num_gene; k++)
57  {
58  city_table[k].used = 0;
59  city_table[k - 1].select_list = -1;
60  }
61 
62  /* determine the number of positions to be inherited from tour1 */
63  num_positions = geqo_randint(root, 2 * num_gene / 3, num_gene / 3);
64 
65  /* make a list of selected cities */
66  for (k = 0; k < num_positions; k++)
67  {
68  pos = geqo_randint(root, num_gene - 1, 0);
69  city_table[pos].select_list = (int) tour1[pos];
70  city_table[(int) tour1[pos]].used = 1; /* mark used */
71  }
72 
73 
74  count = 0;
75  k = 0;
76 
77  /* consolidate the select list to adjacent positions */
78  while (count < num_positions)
79  {
80  if (city_table[k].select_list == -1)
81  {
82  j = k + 1;
83  while ((city_table[j].select_list == -1) && (j < num_gene))
84  j++;
85 
86  city_table[k].select_list = city_table[j].select_list;
87  city_table[j].select_list = -1;
88  count++;
89  }
90  else
91  count++;
92  k++;
93  }
94 
95  select = 0;
96 
97  for (k = 0; k < num_gene; k++)
98  {
99  if (city_table[(int) tour2[k]].used)
100  {
101  offspring[k] = (Gene) city_table[select].select_list;
102  select++; /* next city in the select list */
103  }
104  else
105  /* city isn't used yet, so inherit from tour2 */
106  offspring[k] = tour2[k];
107  }
108 
109 }
#define geqo_randint(root, upper, lower)
Definition: geqo_random.h:38
int Gene
Definition: geqo_gene.h:30
int select_list
#define select(n, r, w, e, timeout)
Definition: win32.h:384
void ox2(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
Definition: geqo_ox2.c:46