1997-02-19 13:59:07 +01:00
|
|
|
/*------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* geqo_recombination.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* misc recombination procedures
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* $Id: geqo_recombination.c,v 1.2 1997/09/07 04:43:21 momjian Exp $
|
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"
|
|
|
|
|
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "nodes/relation.h"
|
|
|
|
#include "nodes/primnodes.h"
|
|
|
|
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
#include "utils/elog.h"
|
|
|
|
|
|
|
|
#include "optimizer/internal.h"
|
|
|
|
#include "optimizer/paths.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "optimizer/cost.h"
|
|
|
|
|
|
|
|
#include "optimizer/geqo_gene.h"
|
|
|
|
#include "optimizer/geqo.h"
|
|
|
|
#include "optimizer/geqo_recombination.h"
|
|
|
|
#include "optimizer/geqo_random.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init_tour--
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Randomly generates a legal "traveling salesman" tour
|
|
|
|
* (i.e. where each point is visited only once.)
|
|
|
|
* Essentially, this routine fills an array with all possible
|
|
|
|
* points on the tour and randomly chooses the 'next' city from
|
|
|
|
* this array. When a city is chosen, the array is shortened
|
|
|
|
* and the procedure repeated.
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
1997-09-07 07:04:48 +02:00
|
|
|
init_tour(Gene * tour, int num_gene)
|
1997-02-19 13:59:07 +01:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
Gene *tmp;
|
|
|
|
int remainder;
|
|
|
|
int next,
|
|
|
|
i;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
tmp = (Gene *) palloc(num_gene * sizeof(Gene));
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < num_gene; i++)
|
|
|
|
{
|
|
|
|
tmp[i] = (Gene) i + 1; /* builds tours "1 - 2 - 3" etc. */
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
remainder = num_gene - 1;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < num_gene; i++)
|
|
|
|
{
|
|
|
|
next = (int) geqo_randint(remainder, 0); /* choose city between 0
|
|
|
|
* and remainder */
|
|
|
|
tour[i] = tmp[next];
|
|
|
|
tmp[next] = tmp[remainder];
|
|
|
|
remainder--;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfree(tmp);
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
|
|
|
/* alloc_city_table--
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* allocate memory for city table
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
City *
|
1997-02-19 13:59:07 +01:00
|
|
|
alloc_city_table(int num_gene)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
City *city_table;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* palloc one extra location so that nodes numbered 1..n can be
|
|
|
|
* indexed directly; 0 will not be used
|
|
|
|
*/
|
1997-02-19 13:59:07 +01:00
|
|
|
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return (city_table);
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
|
|
|
/* free_city_table--
|
|
|
|
*
|
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
|
|
|
|
free_city_table(City * city_table)
|
|
|
|
{
|
|
|
|
pfree(city_table);
|
|
|
|
}
|