1 /*------------------------------------------------------------------------
5 * order crossover [OX] routines;
6 * OX2 operator according to Syswerda
7 * (The Genetic Algorithms Handbook, ed L Davis)
9 * src/backend/optimizer/geqo/geqo_ox2.c
11 *-------------------------------------------------------------------------
15 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
16 * Martin Utesch * Institute of Automatic Control *
17 = = University of Mining and Technology =
18 * utesch@aut.tu-freiberg.de * Freiberg, Germany *
19 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
22 /* the ox algorithm is adopted from Genitor : */
23 /*************************************************************/
25 /* Copyright (c) 1990 */
26 /* Darrell L. Whitley */
27 /* Computer Science Department */
28 /* Colorado State University */
30 /* Permission is hereby granted to copy all or any part of */
31 /* this program for free distribution. The author's name */
32 /* and this copyright notice must be included in any copy. */
34 /*************************************************************/
37 #include "optimizer/geqo_random.h"
38 #include "optimizer/geqo_recombination.h"
47 ox2(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
56 /* initialize city table */
57 for (k = 1; k <= num_gene; k++)
59 city_table[k].used = 0;
60 city_table[k - 1].select_list = -1;
63 /* determine the number of positions to be inherited from tour1 */
64 num_positions = geqo_randint(root, 2 * num_gene / 3, num_gene / 3);
66 /* make a list of selected cities */
67 for (k = 0; k < num_positions; k++)
69 pos = geqo_randint(root, num_gene - 1, 0);
70 city_table[pos].select_list = (int) tour1[pos];
71 city_table[(int) tour1[pos]].used = 1; /* mark used */
78 /* consolidate the select list to adjacent positions */
79 while (count < num_positions)
81 if (city_table[k].select_list == -1)
84 while ((city_table[j].select_list == -1) && (j < num_gene))
87 city_table[k].select_list = city_table[j].select_list;
88 city_table[j].select_list = -1;
98 for (k = 0; k < num_gene; k++)
100 if (city_table[(int) tour2[k]].used)
102 offspring[k] = (Gene) city_table[select].select_list;
103 select++; /* next city in the select list */
106 /* city isn't used yet, so inherit from tour2 */
107 offspring[k] = tour2[k];
112 #endif /* defined(OX2) */