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
45
46/*
47 * cx
48 *
49 * cycle crossover
50 */
51int
54{
55 int i,
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 */
71
72 /* child inherits first city */
74
75 /* begin cycle with tour1 */
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;
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) */
int Gene
Definition geqo_gene.h:33
int geqo_randint(PlannerInfo *root, int upper, int lower)
Definition geqo_random.c:35
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
int i
Definition isn.c:77
static int fb(int x)
tree ctl root
Definition radixtree.h:1857