aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/geqo_recombination.c
blob: df175dcb866abc03688faa7e9502cd9fc43cd6d9 (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
/*------------------------------------------------------------------------
*
* geqo_recombination.c--
*    misc recombination procedures
*
* $Id: geqo_recombination.c,v 1.1 1997/02/19 12:57:42 scrappy Exp $
*
*-------------------------------------------------------------------------
*/

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

/* -- 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--
 *
 *   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.
 *
 */
void
init_tour(Gene *tour, int num_gene)
{
Gene *tmp;
int  remainder;
int  next, i;

tmp = (Gene *) palloc (num_gene*sizeof(Gene));
   
for(i = 0; i < num_gene; i++) {
   tmp[i] = (Gene) i+1; /* builds tours "1 - 2 - 3" etc. */
   }

remainder = num_gene - 1;

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);
} 

/* alloc_city_table--
 *
 *   allocate memory for city table
 *
 */
City *
alloc_city_table(int num_gene)
{
 City *city_table;

 /* palloc one extra location so that nodes numbered
    1..n can be indexed directly; 0 will not be used */

  city_table = (City *) palloc ((num_gene+1)*sizeof(City));

  return (city_table);
 }

/* free_city_table--
 *
 *    deallocate memory of city table
 *
 */
 void
 free_city_table(City *city_table)
 {
  pfree(city_table);
 }