aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/geqo_selection.c
blob: 0c6502003bd38fdb0f03e35aca8a1dfcc3cd15a5 (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
/*-------------------------------------------------------------------------
 *
 * geqo_selection.c--
 *    linear selection scheme for the genetic query optimizer
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 * $Id: geqo_selection.c,v 1.1 1997/02/19 12:57:46 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */

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

/* this is adopted from D. Whitley's Genitor algorithm */

/*************************************************************/
/*                                                           */
/*  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 <math.h>

#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_selection.h"
#include "optimizer/geqo_copy.h"
#include "optimizer/geqo_random.h"

static int linear(int max, double bias);

/* geqo_selection--
 *
 *   according to bias described by input parameters,
 *   second genes are selected from the pool
 */
void
geqo_selection (Chromosome *momma, Chromosome *daddy, Pool *pool, double bias)
{
 int first, second;
 
 first  = (int) linear(pool->size, bias);
 second = (int) linear(pool->size, bias);

 if (pool->size > 1) {
 	while(first==second)
 		second = (int) linear(pool->size, bias);
 	}

 geqo_copy (momma, &pool->data[first],  pool->string_length);
 geqo_copy (daddy, &pool->data[second], pool->string_length);
}

/* linear--
 *    generates random integer between 0 and input max number
 *    using input linear bias
 *
 *    probability distribution function is: f(x) = bias - 2(bias - 1)x
 *           bias = (prob of first rule) / (prob of middle rule)
 *
 */

static int
linear(int pool_size, double bias) /* bias is y-intercept of linear distribution */
{
 double index; /* index between 0 and pop_size */
 double max = (double) pool_size;

 index =
 max*(  bias - sqrt ( (bias*bias) - 4.0*(bias-1.0)*geqo_rand() )  )
 / 2.0 / (bias-1.0);

 return((int) index);
}