1
/*------------------------------------------------------------------------
5
* cycle crossover [CX] routines;
6
* CX operator according to Oliver et al
7
* (Proc 2nd Int'l Conf on GA's)
9
* src/backend/optimizer/geqo/geqo_cx.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 cx 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
/*************************************************************/
38
#include "optimizer/geqo_recombination.h"
39
#include "optimizer/geqo_random.h"
47
cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring,
48
int num_gene, City *city_table)
57
/* initialize city table */
58
for (i = 1; i <= num_gene; i++)
60
city_table[i].used = 0;
61
city_table[tour2[i - 1]].tour2_position = i - 1;
62
city_table[tour1[i - 1]].tour1_position = i - 1;
65
/* choose random cycle starting position */
66
start_pos = geqo_randint(root, num_gene - 1, 0);
68
/* child inherits first city */
69
offspring[start_pos] = tour1[start_pos];
71
/* begin cycle with tour1 */
73
city_table[(int) tour1[start_pos]].used = 1;
82
while (tour2[curr_pos] != tour1[start_pos])
84
city_table[(int) tour2[curr_pos]].used = 1;
85
curr_pos = city_table[(int) tour2[curr_pos]].tour1_position;
86
offspring[curr_pos] = tour1[curr_pos];
93
/* failed to create a complete tour */
96
for (i = 1; i <= num_gene; i++)
98
if (!city_table[i].used)
100
offspring[city_table[i].tour2_position] =
101
tour2[(int) city_table[i].tour2_position];
110
/* still failed to create a complete tour */
111
if (count < num_gene)
114
/* count the number of differences between mom and offspring */
115
for (i = 0; i < num_gene; i++)
116
if (tour1[i] != offspring[i])