1/*------------------------------------------------------------------------
4* edge recombination crossover [ER]
6* src/backend/optimizer/geqo/geqo_erx.c
8*-------------------------------------------------------------------------
12 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
13 * Martin Utesch * Institute of Automatic Control *
14 = = University of Mining and Technology =
15 * utesch@aut.tu-freiberg.de * Freiberg, Germany *
16 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
19/* the edge recombination algorithm is adopted from Genitor : */
20/*************************************************************/
22/* Copyright (c) 1990 */
23/* Darrell L. Whitley */
24/* Computer Science Department */
25/* Colorado State University */
27/* Permission is hereby granted to copy all or any part of */
28/* this program for free distribution. The author's name */
29/* and this copyright notice must be included in any copy. */
31/*************************************************************/
51 * allocate memory for edge table
61 * palloc one extra location so that nodes numbered 1..n can be indexed
62 * directly; 0 will not be used
72 * deallocate memory of edge table
83 * fills a data structure which represents the set of explicit
84 * edges between points in the (2) input genes
86 * assumes circular tours and bidirectional edges
88 * gimme_edge() will set "shared" edges to negative values
90 * returns average number edges/city in range 2.0 - 4.0
91 * where 2.0=homogeneous; 4.0=diverse
96 int num_gene,
Edge *edge_table)
101 int edge_total;
/* total number of unique edges in two genes */
103 /* at first clear the edge table's old data */
104 for (
i = 1;
i <= num_gene;
i++)
110 /* fill edge table with new data */
114 for (index1 = 0; index1 < num_gene; index1++)
117 * presume the tour is circular, i.e. 1->2, 2->3, 3->1 this operation
121 index2 = (index1 + 1) % num_gene;
124 * edges are bidirectional, i.e. 1->2 is same as 2->1 call gimme_edge
128 edge_total +=
gimme_edge(
root, tour1[index1], tour1[index2], edge_table);
131 edge_total +=
gimme_edge(
root, tour2[index1], tour2[index2], edge_table);
135 /* return average number of edges per index */
136 return ((
float) (edge_total * 2) / (
float) num_gene);
141 * registers edge from city1 to city2 in input edge table
143 * no assumptions about directionality are made;
144 * therefore it is up to the calling routine to
145 * call gimme_edge twice to make a bi-directional edge
146 * between city1 and city2;
147 * uni-directional edges are possible as well (just call gimme_edge
148 * once with the direction from city1 to city2)
150 * returns 1 if edge was not already registered and was just added;
151 * 0 if edge was already registered and edge_table is unchanged
158 int city1 = (int) gene1;
159 int city2 = (int) gene2;
162 /* check whether edge city1->city2 already exists */
165 for (
i = 0;
i < edges;
i++)
167 if ((
Gene) abs(edge_table[city1].edge_list[
i]) == city2)
170 /* mark shared edges as negative */
177 /* add city1->city2; */
178 edge_table[city1].
edge_list[edges] = city2;
180 /* increment the number of edges from city1 */
189 * creates a new tour using edges from the edge table.
190 * priority is given to "shared" edges (i.e. edges which
191 * all parent genes possess and are marked as negative
192 * in the edge table.)
199 int edge_failures = 0;
201 /* choose int between 1 and num_gene */
204 for (
i = 1;
i < num_gene;
i++)
207 * as each point is entered into the tour, remove it from the edge
211 remove_gene(
root, new_gene[
i - 1], edge_table[(
int) new_gene[
i - 1]], edge_table);
213 /* find destination for the newly entered point */
215 if (edge_table[new_gene[
i - 1]].unused_edges > 0)
216 new_gene[
i] =
gimme_gene(
root, edge_table[(
int) new_gene[
i - 1]], edge_table);
219 {
/* cope with fault */
225 /* mark this node as incorporated */
226 edge_table[(int) new_gene[
i - 1]].unused_edges = -1;
227 }
/* for (i=1; i<num_gene; i++) */
229 return edge_failures;
234 * removes input gene from edge_table.
236 * to identify deletion locations within edge table.
248 * do for every gene known to have an edge to input gene (i.e. in
249 * edge_list for input edge)
255 genes_remaining = edge_table[possess_edge].
unused_edges;
257 /* find the input gene in all edge_lists and delete it */
258 for (
j = 0;
j < genes_remaining;
j++)
261 if ((
Gene) abs(edge_table[possess_edge].edge_list[
j]) == gene)
267 edge_table[possess_edge].
edge_list[genes_remaining - 1];
277 * priority is given to "shared" edges
278 * (i.e. edges which both genes possess)
287 int minimum_count = -1;
291 * no point has edges to more than 4 other points thus, this contrived
292 * minimum will be replaced
297 /* consider candidate destination points in edge list */
304 * give priority to shared edges that are negative; so return 'em
308 * negative values are caught here so we need not worry about
309 * converting to absolute values
312 return (
Gene) abs(
friend);
316 * give priority to candidates with fewest remaining unused edges;
317 * find out what the minimum number of unused edges is
318 * (minimum_edges); if there is more than one candidate with the
319 * minimum number of unused edges keep count of this number
324 * The test for minimum_count can probably be removed at some point
325 * but comments should probably indicate exactly why it is guaranteed
326 * that the test will always succeed the first time around. If it can
327 * fail then the code is in error
331 if (edge_table[(
int)
friend].unused_edges < minimum_edges)
333 minimum_edges = edge_table[(int)
friend].unused_edges;
336 else if (minimum_count == -1)
338 else if (edge_table[(
int)
friend].unused_edges == minimum_edges)
340 }
/* for (i=0; i<edge.unused_edges; i++) */
343 /* random decision of the possible candidates to use */
351 /* return the chosen candidate point */
356 if (minimum_count == rand_decision)
361 /* ... should never be reached */
362 elog(
ERROR,
"neither shared nor minimum number nor random edge found");
363 return 0;
/* to keep the compiler quiet */
368 * routine for handling edge failure
376 int remaining_edges = 0;
382 * how many edges remain? how many gene with four total (initial) edges
386 for (
i = 1;
i <= num_gene;
i++)
388 if ((edge_table[
i].unused_edges != -1) && (
i != (
int) fail_gene))
392 if (edge_table[
i].total_edges == 4)
398 * random decision of the gene with remaining edges and whose total_edges
407 for (
i = 1;
i <= num_gene;
i++)
410 if ((
Gene)
i != fail_gene &&
411 edge_table[
i].unused_edges != -1 &&
412 edge_table[
i].total_edges == 4)
417 if (rand_decision == four_count)
422 elog(
LOG,
"no edge found via random decision and total_edges == 4");
424 else if (remaining_edges != 0)
426 /* random decision of the gene with remaining edges */
429 for (
i = 1;
i <= num_gene;
i++)
432 if ((
Gene)
i != fail_gene &&
433 edge_table[
i].unused_edges != -1)
438 if (rand_decision == remaining_edges)
443 elog(
LOG,
"no edge found via random decision with remaining edges");
447 * edge table seems to be empty; this happens sometimes on the last point
448 * due to the fact that the first point is removed from the table even
449 * though only one of its edges has been determined
453 {
/* occurs only at the last point in the tour;
454 * simply look for the point which is not yet
457 for (
i = 1;
i <= num_gene;
i++)
458 if (edge_table[
i].unused_edges >= 0)
461 elog(
LOG,
"no edge found via looking for the last unused point");
465 /* ... should never be reached */
467 return 0;
/* to keep the compiler quiet */
470#endif /* defined(ERX) */
static int gimme_edge(PlannerInfo *root, Gene gene1, Gene gene2, Edge *edge_table)
Edge * alloc_edge_table(PlannerInfo *root, int num_gene)
float gimme_edge_table(PlannerInfo *root, Gene *tour1, Gene *tour2, int num_gene, Edge *edge_table)
void free_edge_table(PlannerInfo *root, Edge *edge_table)
static Gene gimme_gene(PlannerInfo *root, Edge edge, Edge *edge_table)
int gimme_tour(PlannerInfo *root, Edge *edge_table, Gene *new_gene, int num_gene)
static Gene edge_failure(PlannerInfo *root, Gene *gene, int index, Edge *edge_table, int num_gene)
static void remove_gene(PlannerInfo *root, Gene gene, Edge edge, Edge *edge_table)
int geqo_randint(PlannerInfo *root, int upper, int lower)
if(TABLE==NULL||TABLE_index==NULL)
void pfree(void *pointer)