summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/geqo_px.c
blob: 0567cb11d2515694fbe0c657c38cb0a53993ee7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*------------------------------------------------------------------------
*
* geqo_px.c
*
*	 position crossover [PX] routines;
*	 PX operator according to Syswerda
*	 (The Genetic Algorithms Handbook, L Davis, ed)
*
* $Id: geqo_px.c,v 1.9 1999/07/16 04:59:12 momjian Exp $
*
*-------------------------------------------------------------------------
*/

/* contributed by:
   =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
   *  Martin Utesch				 * Institute of Automatic Control	   *
   =							 = University of Mining and Technology =
   *  utesch@aut.tu-freiberg.de  * Freiberg, Germany				   *
   =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
 */

/* the px algorithm is adopted from Genitor : */
/*************************************************************/
/*															 */
/*	Copyright (c) 1990										 */
/*	Darrell L. Whitley										 */
/*	Computer Science Department								 */
/*	Colorado State University								 */
/*															 */
/*	Permission is hereby granted to copy all or any part of  */
/*	this program for free distribution.   The author's name  */
/*	and this copyright notice must be included in any copy.  */
/*															 */
/*************************************************************/

#include "postgres.h"
#include "optimizer/geqo_random.h"
#include "optimizer/geqo_recombination.h"


/* px
 *
 *	 position crossover
 */
void
px(Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
{

	int			num_positions;
	int			i,
				pos,
				tour2_index,
				offspring_index;

	/* initialize city table */
	for (i = 1; i <= num_gene; i++)
		city_table[i].used = 0;

	/* choose random positions that will be inherited directly from parent */
	num_positions = geqo_randint(2 * num_gene / 3, num_gene / 3);

	/* choose random position */
	for (i = 0; i < num_positions; i++)
	{
		pos = geqo_randint(num_gene - 1, 0);

		offspring[pos] = tour1[pos];	/* transfer cities to child */
		city_table[(int) tour1[pos]].used = 1;	/* mark city used */
	}

	tour2_index = 0;
	offspring_index = 0;


	/* px main part */

	while (offspring_index < num_gene)
	{

		/* next position in offspring filled */
		if (!city_table[(int) tour1[offspring_index]].used)
		{

			/* next city in tour1 not used */
			if (!city_table[(int) tour2[tour2_index]].used)
			{

				/* inherit from tour1 */
				offspring[offspring_index] = tour2[tour2_index];

				tour2_index++;
				offspring_index++;
			}
			else
			{					/* next city in tour2 has been used */
				tour2_index++;
			}

		}
		else
		{						/* next position in offspring is filled */
			offspring_index++;
		}

	}

}