PostgreSQL Source Code git master
Loading...
Searching...
No Matches
geqo_pmx.c
Go to the documentation of this file.
1/*------------------------------------------------------------------------
2*
3* geqo_pmx.c
4*
5* partially matched crossover [PMX] routines;
6* PMX operator according to Goldberg & Lingle
7* (Proc Int'l Conf on GA's)
8*
9* src/backend/optimizer/geqo/geqo_pmx.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 pmx 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(PMX)
41
44
45/*
46 * pmx
47 *
48 * partially matched crossover
49 */
50void
52{
53 int *failed = palloc_array(int, num_gene + 1);
54 int *from = palloc_array(int, num_gene + 1);
55 int *indx = palloc_array(int, num_gene + 1);
56 int *check_list = palloc_array(int, num_gene + 1);
57
58 int left,
59 right,
60 temp,
61 i,
62 j,
63 k;
64 int mx_fail,
65 found,
66 mx_hold;
67
68
69/* no mutation so start up the pmx replacement algorithm */
70/* initialize failed[], from[], check_list[] */
71 for (k = 0; k < num_gene; k++)
72 {
73 failed[k] = -1;
74 from[k] = -1;
75 check_list[k + 1] = 0;
76 }
77
78/* locate crossover points */
79 left = geqo_randint(root, num_gene - 1, 0);
80 right = geqo_randint(root, num_gene - 1, 0);
81
82 if (left > right)
83 {
84 temp = left;
85 left = right;
86 right = temp;
87 }
88
89
90/* copy tour2 into offspring */
91 for (k = 0; k < num_gene; k++)
92 {
93 offspring[k] = tour2[k];
94 from[k] = DAD;
95 check_list[tour2[k]]++;
96 }
97
98/* copy tour1 into offspring */
99 for (k = left; k <= right; k++)
100 {
101 check_list[offspring[k]]--;
102 offspring[k] = tour1[k];
103 from[k] = MOM;
104 check_list[tour1[k]]++;
105 }
106
107
108/* pmx main part */
109
110 mx_fail = 0;
111
112/* STEP 1 */
113
114 for (k = left; k <= right; k++)
115 { /* for all elements in the tour1-2 */
116
117 if (tour1[k] == tour2[k])
118 found = 1; /* find match in tour2 */
119
120 else
121 {
122 found = 0; /* substitute elements */
123
124 j = 0;
125 while (!(found) && (j < num_gene))
126 {
127 if ((offspring[j] == tour1[k]) && (from[j] == DAD))
128 {
129
131 offspring[j] = tour2[k];
132 found = 1;
133 check_list[tour2[k]]++;
134 }
135
136 j++;
137 }
138 }
139
140 if (!(found))
141 { /* failed to replace gene */
142 failed[mx_fail] = (int) tour1[k];
143 indx[mx_fail] = k;
144 mx_fail++;
145 }
146 } /* ... for */
147
148
149/* STEP 2 */
150
151 /* see if any genes could not be replaced */
152 if (mx_fail > 0)
153 {
155
156 for (k = 0; k < mx_hold; k++)
157 {
158 found = 0;
159
160 j = 0;
161 while (!(found) && (j < num_gene))
162 {
163
164 if ((failed[k] == (int) offspring[j]) && (from[j] == DAD))
165 {
167 offspring[j] = tour2[indx[k]];
168 check_list[tour2[indx[k]]]++;
169
170 found = 1;
171 failed[k] = -1;
172 mx_fail--;
173 }
174
175 j++;
176 }
177 } /* ... for */
178 } /* ... if */
179
180
181/* STEP 3 */
182
183 for (k = 1; k <= num_gene; k++)
184 {
185
186 if (check_list[k] > 1)
187 {
188 i = 0;
189
190 while (i < num_gene)
191 {
192 if ((offspring[i] == (Gene) k) && (from[i] == DAD))
193 {
194 j = 1;
195
196 while (j <= num_gene)
197 {
198 if (check_list[j] == 0)
199 {
200 offspring[i] = (Gene) j;
201 check_list[k]--;
202 check_list[j]++;
203 i = num_gene + 1;
204 j = i;
205 }
206
207 j++;
208 }
209 } /* ... if */
210
211 i++;
212 } /* end while */
213 }
214 } /* ... for */
215
216 pfree(failed);
217 pfree(from);
218 pfree(indx);
220}
221
222#endif /* defined(PMX) */
#define palloc_array(type, count)
Definition fe_memutils.h:91
int Gene
Definition geqo_gene.h:33
int geqo_randint(PlannerInfo *root, int upper, int lower)
Definition geqo_random.c:35
void pmx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene)
#define DAD
#define MOM
int j
Definition isn.c:78
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1619
static int fb(int x)
tree ctl root
Definition radixtree.h:1857