PostgreSQL Source Code
git master
Loading...
Searching...
No Matches
geqo_cx.c
Go to the documentation of this file.
1
/*------------------------------------------------------------------------
2
*
3
* geqo_cx.c
4
*
5
* cycle crossover [CX] routines;
6
* CX operator according to Oliver et al
7
* (Proc 2nd Int'l Conf on GA's)
8
*
9
* src/backend/optimizer/geqo/geqo_cx.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 cx 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
38
#include "
postgres.h
"
39
#include "
optimizer/geqo.h
"
40
41
#if defined(CX)
42
43
#include "
optimizer/geqo_random.h
"
44
#include "
optimizer/geqo_recombination.h
"
45
46
/*
47
* cx
48
*
49
* cycle crossover
50
*/
51
int
52
cx
(
PlannerInfo
*
root
,
Gene
*
tour1
,
Gene
*
tour2
,
Gene
*
offspring
,
53
int
num_gene
,
City
*
city_table
)
54
{
55
int
i
,
56
start_pos
,
57
curr_pos
;
58
int
count = 0;
59
int
num_diffs
= 0;
60
61
/* initialize city table */
62
for
(
i
= 1;
i
<=
num_gene
;
i
++)
63
{
64
city_table
[
i
].used = 0;
65
city_table
[
tour2
[
i
- 1]].tour2_position =
i
- 1;
66
city_table
[
tour1
[
i
- 1]].tour1_position =
i
- 1;
67
}
68
69
/* choose random cycle starting position */
70
start_pos
=
geqo_randint
(
root
,
num_gene
- 1, 0);
71
72
/* child inherits first city */
73
offspring
[
start_pos
] =
tour1
[
start_pos
];
74
75
/* begin cycle with tour1 */
76
curr_pos
=
start_pos
;
77
city_table
[(
int
)
tour1
[
start_pos
]].used = 1;
78
79
count++;
80
81
/* cx main part */
82
83
84
/* STEP 1 */
85
86
while
(
tour2
[
curr_pos
] !=
tour1
[
start_pos
])
87
{
88
city_table
[(
int
)
tour2
[
curr_pos
]].used = 1;
89
curr_pos
=
city_table
[(
int
)
tour2
[
curr_pos
]].tour1_position;
90
offspring
[
curr_pos
] =
tour1
[
curr_pos
];
91
count++;
92
}
93
94
95
/* STEP 2 */
96
97
/* failed to create a complete tour */
98
if
(count <
num_gene
)
99
{
100
for
(
i
= 1;
i
<=
num_gene
;
i
++)
101
{
102
if
(!
city_table
[
i
].used)
103
{
104
offspring
[
city_table
[
i
].tour2_position] =
105
tour2
[(
int
)
city_table
[
i
].tour2_position];
106
count++;
107
}
108
}
109
}
110
111
112
/* STEP 3 */
113
114
/* still failed to create a complete tour */
115
if
(count <
num_gene
)
116
{
117
118
/* count the number of differences between mom and offspring */
119
for
(
i
= 0;
i
<
num_gene
;
i
++)
120
if
(
tour1
[
i
] !=
offspring
[
i
])
121
num_diffs
++;
122
}
123
124
return
num_diffs
;
125
}
126
127
#endif
/* defined(CX) */
geqo.h
Gene
int Gene
Definition
geqo_gene.h:33
geqo_randint
int geqo_randint(PlannerInfo *root, int upper, int lower)
Definition
geqo_random.c:35
geqo_random.h
geqo_recombination.h
cx
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
i
int i
Definition
isn.c:77
postgres.h
fb
static int fb(int x)
Definition
preproc-init.c:92
root
tree ctl root
Definition
radixtree.h:1857
City
Definition
geqo_recombination.h:64
PlannerInfo
Definition
pathnodes.h:303
src
backend
optimizer
geqo
geqo_cx.c
Generated on Sat May 30 2026 01:13:11 for PostgreSQL Source Code by
1.9.8