1997-02-19 13:59:07 +01:00
|
|
|
/*------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* geqo_recombination.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* misc recombination procedures
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/optimizer/geqo/geqo_recombination.c
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* contributed by:
|
|
|
|
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
|
1997-09-07 07:04:48 +02:00
|
|
|
* Martin Utesch * Institute of Automatic Control *
|
|
|
|
= = University of Mining and Technology =
|
|
|
|
* utesch@aut.tu-freiberg.de * Freiberg, Germany *
|
1997-02-19 13:59:07 +01:00
|
|
|
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* -- parts of this are adapted from D. Whitley's Genitor algorithm -- */
|
|
|
|
|
|
|
|
#include "postgres.h"
|
2004-01-24 00:54:21 +01:00
|
|
|
|
1997-02-19 13:59:07 +01:00
|
|
|
#include "optimizer/geqo_random.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/geqo_recombination.h"
|
1997-02-19 13:59:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* init_tour
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Randomly generates a legal "traveling salesman" tour
|
|
|
|
* (i.e. where each point is visited only once.)
|
1997-02-19 13:59:07 +01:00
|
|
|
*/
|
|
|
|
void
|
2009-07-16 22:55:44 +02:00
|
|
|
init_tour(PlannerInfo *root, Gene *tour, int num_gene)
|
1997-02-19 13:59:07 +01:00
|
|
|
{
|
2015-11-05 16:46:14 +01:00
|
|
|
int i,
|
|
|
|
j;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
2015-11-05 16:46:14 +01:00
|
|
|
/*
|
|
|
|
* We must fill the tour[] array with a random permutation of the numbers
|
|
|
|
* 1 .. num_gene. We can do that in one pass using the "inside-out"
|
|
|
|
* variant of the Fisher-Yates shuffle algorithm. Notionally, we append
|
|
|
|
* each new value to the array and then swap it with a randomly-chosen
|
|
|
|
* array element (possibly including itself, else we fail to generate
|
|
|
|
* permutations with the last city last). The swap step can be optimized
|
|
|
|
* by combining it with the insertion.
|
|
|
|
*/
|
|
|
|
if (num_gene > 0)
|
|
|
|
tour[0] = (Gene) 1;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
2015-11-05 16:46:14 +01:00
|
|
|
for (i = 1; i < num_gene; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2015-11-05 16:46:14 +01:00
|
|
|
j = geqo_randint(root, i, 0);
|
|
|
|
/* i != j check avoids fetching uninitialized array element */
|
|
|
|
if (i != j)
|
|
|
|
tour[i] = tour[j];
|
|
|
|
tour[j] = (Gene) (i + 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1999-02-14 00:22:53 +01:00
|
|
|
/* alloc_city_table
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* allocate memory for city table
|
1997-02-19 13:59:07 +01:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
City *
|
2009-07-16 22:55:44 +02:00
|
|
|
alloc_city_table(PlannerInfo *root, int num_gene)
|
1997-02-19 13:59:07 +01:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
City *city_table;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* palloc one extra location so that nodes numbered 1..n can be indexed
|
|
|
|
* directly; 0 will not be used
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
city_table = (City *) palloc((num_gene + 1) * sizeof(City));
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return city_table;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1999-02-14 00:22:53 +01:00
|
|
|
/* free_city_table
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* deallocate memory of city table
|
1997-02-19 13:59:07 +01:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
void
|
2009-07-16 22:55:44 +02:00
|
|
|
free_city_table(PlannerInfo *root, City *city_table)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
pfree(city_table);
|
|
|
|
}
|