1997-02-19 13:59:07 +01:00
|
|
|
/*------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* geqo_cx.c--
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* cycle crossover [CX] routines;
|
|
|
|
* CX operator according to Oliver et al
|
|
|
|
* (Proc 2nd Int'l Conf on GA's)
|
1997-02-19 13:59:07 +01:00
|
|
|
*
|
1997-09-08 23:56:23 +02:00
|
|
|
* $Id: geqo_cx.c,v 1.4 1997/09/08 21:44:15 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
|
|
|
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* the cx algorithm is adopted from Genitor : */
|
|
|
|
/*************************************************************/
|
1997-09-07 07:04:48 +02:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/* */
|
1997-02-19 13:59:07 +01:00
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
|
|
|
/* cx--
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* cycle crossover
|
1997-02-19 13:59:07 +01:00
|
|
|
*/
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
cx(Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
|
1997-02-19 13:59:07 +01:00
|
|
|
{
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
start_pos,
|
|
|
|
curr_pos;
|
|
|
|
int count = 0;
|
|
|
|
int num_diffs = 0;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* initialize city table */
|
|
|
|
for (i = 1; i <= num_gene; i++)
|
|
|
|
{
|
|
|
|
city_table[i].used = 0;
|
|
|
|
city_table[tour2[i - 1]].tour2_position = i - 1;
|
|
|
|
city_table[tour1[i - 1]].tour1_position = i - 1;
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* choose random cycle starting position */
|
|
|
|
start_pos = geqo_randint(num_gene - 1, 0);
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* child inherits first city */
|
|
|
|
offspring[start_pos] = tour1[start_pos];
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* begin cycle with tour1 */
|
|
|
|
curr_pos = start_pos;
|
|
|
|
city_table[(int) tour1[start_pos]].used = 1;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
count++;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* cx main part */
|
1997-02-19 13:59:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* STEP 1 */
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
while (tour2[curr_pos] != tour1[start_pos])
|
|
|
|
{
|
|
|
|
city_table[(int) tour2[curr_pos]].used = 1;
|
|
|
|
curr_pos = city_table[(int) tour2[curr_pos]].tour1_position;
|
|
|
|
offspring[curr_pos] = tour1[curr_pos];
|
|
|
|
count++;
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* STEP 2 */
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* failed to create a complete tour */
|
|
|
|
if (count < num_gene)
|
|
|
|
{
|
|
|
|
for (i = 1; i <= num_gene; i++)
|
|
|
|
{
|
|
|
|
if (!city_table[i].used)
|
|
|
|
{
|
|
|
|
offspring[city_table[i].tour2_position] =
|
|
|
|
tour2[(int) city_table[i].tour2_position];
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* STEP 3 */
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* still failed to create a complete tour */
|
|
|
|
if (count < num_gene)
|
|
|
|
{
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* count the number of differences between mom and offspring */
|
|
|
|
for (i = 0; i < num_gene; i++)
|
|
|
|
if (tour1[i] != offspring[i])
|
|
|
|
num_diffs++;
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-02-19 13:59:07 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return (num_diffs);
|
|
|
|
}
|