PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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/*
15 * contributed by:
16 * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
17 * * Martin Utesch * Institute of Automatic Control *
18 * = = University of Mining and Technology =
19 * * utesch@aut.tu-freiberg.de * Freiberg, Germany *
20 * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
21 */
22
23/* the ox algorithm is adopted from Genitor : */
24/*************************************************************/
25/* */
26/* Copyright (c) 1990 */
27/* Darrell L. Whitley */
28/* Computer Science Department */
29/* Colorado State University */
30/* */
31/* Permission is hereby granted to copy all or any part of */
32/* this program for free distribution. The author's name */
33/* and this copyright notice must be included in any copy. */
34/* */
35/*************************************************************/
36
37#include "postgres.h"
38#include "optimizer/geqo.h"
39
40#if defined(OX2)
41
44
45/*
46 * ox2
47 *
48 * position crossover
49 */
50void
52{
53 int k,
54 j,
55 count,
56 pos,
57 select,
59
60 /* initialize city table */
61 for (k = 1; k <= num_gene; k++)
62 {
63 city_table[k].used = 0;
64 city_table[k - 1].select_list = -1;
65 }
66
67 /* determine the number of positions to be inherited from tour1 */
69
70 /* make a list of selected cities */
71 for (k = 0; k < num_positions; k++)
72 {
73 pos = geqo_randint(root, num_gene - 1, 0);
74 city_table[pos].select_list = (int) tour1[pos];
75 city_table[(int) tour1[pos]].used = 1; /* mark used */
76 }
77
78
79 count = 0;
80 k = 0;
81
82 /* consolidate the select list to adjacent positions */
83 while (count < num_positions)
84 {
85 if (city_table[k].select_list == -1)
86 {
87 j = k + 1;
88 while ((city_table[j].select_list == -1) && (j < num_gene))
89 j++;
90
91 city_table[k].select_list = city_table[j].select_list;
92 city_table[j].select_list = -1;
93 count++;
94 }
95 else
96 count++;
97 k++;
98 }
99
100 select = 0;
101
102 for (k = 0; k < num_gene; k++)
103 {
104 if (city_table[(int) tour2[k]].used)
105 {
106 offspring[k] = (Gene) city_table[select].select_list;
107 select++; /* next city in the select list */
108 }
109 else
110 /* city isn't used yet, so inherit from tour2 */
111 offspring[k] = tour2[k];
112 }
113}
114
115#endif /* defined(OX2) */
int Gene
Definition geqo_gene.h:33
int geqo_randint(PlannerInfo *root, int upper, int lower)
Definition geqo_random.c:35
void ox2(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
int j
Definition isn.c:78
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
#define select(n, r, w, e, timeout)
Definition win32_port.h:500