PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
geqo_ox1.c
Go to the documentation of this file.
1 /*------------------------------------------------------------------------
2 *
3 * geqo_ox1.c
4 *
5 * order crossover [OX] routines;
6 * OX1 operator according to Davis
7 * (Proc Int'l Joint Conf on AI)
8 *
9 * src/backend/optimizer/geqo/geqo_ox1.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 /* ox1
42  *
43  * position crossover
44  */
45 void
46 ox1(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene,
47  City *city_table)
48 {
49  int left,
50  right,
51  k,
52  p,
53  temp;
54 
55  /* initialize city table */
56  for (k = 1; k <= num_gene; k++)
57  city_table[k].used = 0;
58 
59  /* select portion to copy from tour1 */
60  left = geqo_randint(root, num_gene - 1, 0);
61  right = geqo_randint(root, num_gene - 1, 0);
62 
63  if (left > right)
64  {
65  temp = left;
66  left = right;
67  right = temp;
68  }
69 
70  /* copy portion from tour1 to offspring */
71  for (k = left; k <= right; k++)
72  {
73  offspring[k] = tour1[k];
74  city_table[(int) tour1[k]].used = 1;
75  }
76 
77  k = (right + 1) % num_gene; /* index into offspring */
78  p = k; /* index into tour2 */
79 
80  /* copy stuff from tour2 to offspring */
81  while (k != left)
82  {
83  if (!city_table[(int) tour2[p]].used)
84  {
85  offspring[k] = tour2[p];
86  k = (k + 1) % num_gene;
87  city_table[(int) tour2[p]].used = 1;
88  }
89  p = (p + 1) % num_gene; /* increment tour2-index */
90  }
91 
92 }
void ox1(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
Definition: geqo_ox1.c:46
#define geqo_randint(root, upper, lower)
Definition: geqo_random.h:38
int Gene
Definition: geqo_gene.h:30