summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/geqo_params.c
blob: 47c56ba333f2e9457871eb47314535cf4f88fe81 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*------------------------------------------------------------------------
*
* geqo_params.c--
*    routines for determining necessary genetic optimization parameters
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: geqo_params.c,v 1.1 1997/02/19 12:57:20 scrappy Exp $
*
*-------------------------------------------------------------------------
*/

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

#include <stdio.h>
#include <time.h>
#include <math.h>

#include "postgres.h"
#include "miscadmin.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"

#define POOL_TAG	"Pool_Size"
#define TRIAL_TAG	"Generations"
#define RAND_TAG	"Random_Seed"
#define BIAS_TAG	"Selection_Bias"

#define EFFORT_TAG	"Effort" /* optimization effort and */
#define LOW	         "low"    /* corresponding tags */
#define MEDIUM	      "medium"
#define HIGH	      "high"

#define MAX_TOKEN 80                    /* Maximum size of one token in the  *
                                         * configuration file                */

static int gimme_pool_size(int string_length);
static int gimme_number_generations(int pool_size, int effort);
static int next_token(FILE *, char *, int);

/*
 * geqo_param--
 *	 get ga parameters out of "$PGDATA/pg_geqo" file.
 */
void
geqo_params(int string_length)
{
	int i;

	char buf[MAX_TOKEN];
	FILE *file;

	char *conf_file;

/* these static variables are used to signal that a value has been set */
	int          pool_size = 0;
	int      number_trials = 0;
	int        random_seed = 0;
	int     selection_bias = 0;
	int             effort = 0;


	/* put together the full pathname to the config file */
	conf_file =
	(char *) palloc((strlen(DataDir)+strlen(GEQO_FILE)+2)*sizeof(char));

	sprintf(conf_file, "%s/%s", DataDir, GEQO_FILE);

	/* open the config file */
	file = fopen(conf_file, "r");
	if (file)
	{
		/*
		 * empty and comment line stuff
		 */
		while ((i = next_token(file, buf, sizeof(buf))) != EOF)
		{
		    /* If only token on the line, ignore */
		    if (i == '\n') continue;
	    
		    /* Comment -- read until end of line then next line */
		    if (buf[0] == '#')
		    {
		        while (next_token(file, buf, sizeof(buf)) == 0) ;
		        continue;
		    }

			/*
			 * get ga parameters by parsing
			 */
	
			/*------------------------------------------------- pool size */
			if ( strcmp(buf, POOL_TAG) == 0 )
			{
				i = next_token(file, buf, sizeof(buf)); /* get next token */
	
				if (i != EOF) /* only ignore if we got no text at all */
				{
					if (sscanf (buf, "%d", &PoolSize) == 1) pool_size = 1;
				}
	
			}
			    
			/*------------------------------------------------- number of trials */
			else if ( strcmp(buf, TRIAL_TAG) == 0 )
			{
				i = next_token(file, buf, sizeof(buf));
	
				if (i != EOF)
				{
					if (sscanf (buf, "%d", &Generations) == 1) number_trials = 1;
				}
	
			}
			    
			/*------------------------------------------------- optimization effort */
			else if ( strcmp(buf, EFFORT_TAG) == 0 )
			{
				i = next_token(file, buf, sizeof(buf));
	
				if (i != EOF)
				{
					if (strcmp (buf, LOW) == 0) effort = LOW_EFFORT;
					else if (strcmp (buf, MEDIUM) == 0) effort = MEDIUM_EFFORT;
					else if (strcmp (buf, HIGH) == 0) effort = HIGH_EFFORT;
				}
	
			}
			    
			/*------------------------------------------- random seed */
			else if ( strcmp(buf, RAND_TAG) == 0 )
		   {
				i = next_token(file, buf, sizeof(buf));
	
				if (i != EOF)
				{
					if (sscanf (buf, "%ld", &RandomSeed) == 1) random_seed = 1;
				}
	
			}
			    
			/*------------------------------------------- selection bias */
			else if ( strcmp(buf, BIAS_TAG) == 0 )
			{
				i = next_token(file, buf, sizeof(buf));
	
				if (i != EOF)
				{
					if (sscanf (buf, "%f", &SelectionBias) == 1) selection_bias = 1;
				}
	
			}
			    
			/* unrecognized tags */
			else
			{
				if (i != EOF)
				{
				}
	
				elog(DEBUG,"geqo_params: unknown parameter type \"%s\"\nin file \'%s\'", buf, conf_file);

				/* if not at end-of-line, keep reading til we are */
				while (i == 0) i = next_token(file, buf, sizeof(buf));
		   }
		}

		fclose(file);

		pfree(conf_file);
	}

	else 
	{
		elog(DEBUG,"geqo_params: ga parameter file\n\'%s\'\ndoes not exist or permissions are not setup correctly", conf_file);
	}

	/*
	 * parameter checkings follow
	 */

	/**************** PoolSize: essential ****************/
	if ( !(pool_size) )
	{
		PoolSize = gimme_pool_size(string_length);

		elog(DEBUG,"geqo_params: no pool size specified;\nusing computed value of %d", PoolSize);
	}
	
	
	/**************** Effort: essential ****************/
	if ( !(effort) )
	{
		if (PoolSize == MAX_POOL)
			effort = HIGH_EFFORT;
		else
			effort = MEDIUM_EFFORT;
	
		elog(DEBUG,"geqo_params: no optimization effort specified;\nusing value of %d", effort);

	}

	/**************** Generations: essential ****************/
	if ( !(number_trials) )
	{
		Generations = gimme_number_generations(PoolSize, effort);
	
		elog(DEBUG,"geqo_params: no number of trials specified;\nusing computed value of %d", Generations);

	}

	/* RandomSeed: */
	if ( !(random_seed) )
	{
		RandomSeed = (long) time(NULL);

		elog(DEBUG,"geqo_params: no random seed specified;\nusing computed value of %ld", RandomSeed);
	}

	/* SelectionBias: */
	if ( !(selection_bias) )
	{
		SelectionBias = SELECTION_BIAS;

		elog(DEBUG,"geqo_params: no selection bias specified;\nusing default value of %f", SelectionBias);
	}

}


/*
 * Grab one token out of fp.  Defined as the next string of non-whitespace
 * in the file.  After we get the token, continue reading until EOF, end of
 * line or the next token.  If it's the last token on the line, return '\n'
 * for the value.  If we get EOF before reading a token, return EOF.  In all
 * other cases return 0.
 */
static int 
next_token(FILE *fp, char *buf, int bufsz)
{
    int c;
    char *eb = buf+(bufsz-1);

    /* Discard inital whitespace */
    while (isspace(c = getc(fp))) ;

    /* EOF seen before any token so return EOF */
    if (c == EOF) return -1;

    /* Form a token in buf */
    do {
	if (buf < eb) *buf++ = c;
	c = getc(fp);
    } while (!isspace(c) && c != EOF);
    *buf = '\0';

    /* Discard trailing tabs and spaces */
    while (c == ' ' || c == '\t') c = getc(fp);

    /* Put back the char that was non-whitespace (putting back EOF is ok) */
    (void) ungetc(c, fp);

    /* If we ended with a newline, return that, otherwise return 0 */
    return (c == '\n' ? '\n' : 0);
}

/* gimme_pool_size--
 *   compute good estimation for pool size
 *   according to number of involved rels in a query
 */
static int 
gimme_pool_size(int string_length)
{
	double exponent;
	double size;

	exponent = (double) string_length + 1.0;

	size = pow (2.0, exponent);

	if (size < MIN_POOL) {
		return (MIN_POOL);
		}
	else if (size > MAX_POOL) {
		return (MAX_POOL);
		}
	else 
		return ( (int) ceil(size) );
}

/* gimme_number_generations--
 *   compute good estimation for number of generations size
 *   for convergence
 */
static int 
gimme_number_generations(int pool_size, int effort)
{
	int number_gens;

	number_gens = (int) ceil ( geqo_log((double) pool_size, 2.0) );

	return (effort * number_gens);
}