1/*------------------------------------------------------------------------
2 *
3 * geqo_misc.c
4 * misc. printout and debug stuff
5 *
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/backend/optimizer/geqo/geqo_misc.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14/* contributed by:
15 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
16 * Martin Utesch * Institute of Automatic Control *
17 = = University of Mining and Technology =
18 * utesch@aut.tu-freiberg.de * Freiberg, Germany *
19 =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
20 */
21
22#include "postgres.h"
23
24#include "optimizer/geqo_misc.h"
25
26
27#ifdef GEQO_DEBUG
28
29
30/*
31 * avg_pool
32 */
33static double
34avg_pool(Pool *pool)
35{
36 int i;
37 double cumulative = 0.0;
38
39 if (pool->size <= 0)
40 elog(ERROR, "pool_size is zero");
41
42 /*
43 * Since the pool may contain multiple occurrences of DBL_MAX, divide by
44 * pool->size before summing, not after, to avoid overflow. This loses a
45 * little in speed and accuracy, but this routine is only used for debug
46 * printouts, so we don't care that much.
47 */
48 for (i = 0; i < pool->size; i++)
49 cumulative += pool->data[i].worth / pool->size;
50
51 return cumulative;
52}
53
54/* print_pool
55 */
56void
57print_pool(FILE *fp, Pool *pool, int start, int stop)
58{
59 int i,
60 j;
61
62 /* be extra careful that start and stop are valid inputs */
63
64 if (start < 0)
65 start = 0;
66 if (stop > pool->size)
67 stop = pool->size;
68
69 if (start + stop > pool->size)
70 {
71 start = 0;
72 stop = pool->size;
73 }
74
75 for (i = start; i < stop; i++)
76 {
77 fprintf(fp, "%d)\t", i);
78 for (j = 0; j < pool->string_length; j++)
79 fprintf(fp, "%d ", pool->data[i].string[j]);
80 fprintf(fp, "%g\n", pool->data[i].worth);
81 }
82
83 fflush(fp);
84}
85
86/* print_gen
87 *
88 * printout for chromosome: best, worst, mean, average
89 */
90void
91print_gen(FILE *fp, Pool *pool, int generation)
92{
93 int lowest;
94
95 /* Get index to lowest ranking gene in population. */
96 /* Use 2nd to last since last is buffer. */
97 lowest = pool->size > 1 ? pool->size - 2 : 0;
98
99 fprintf(fp,
100 "%5d | Best: %g Worst: %g Mean: %g Avg: %g\n",
101 generation,
102 pool->data[0].worth,
103 pool->data[lowest].worth,
104 pool->data[pool->size / 2].worth,
105 avg_pool(pool));
106
107 fflush(fp);
108}
109
110
111void
112print_edge_table(FILE *fp, Edge *edge_table, int num_gene)
113{
114 int i,
115 j;
116
117 fprintf(fp, "\nEDGE TABLE\n");
118
119 for (i = 1; i <= num_gene; i++)
120 {
121 fprintf(fp, "%d :", i);
122 for (j = 0; j < edge_table[i].unused_edges; j++)
123 fprintf(fp, " %d", edge_table[i].edge_list[j]);
124 fprintf(fp, "\n");
125 }
126
127 fprintf(fp, "\n");
128
129 fflush(fp);
130}
131
132#endif /* GEQO_DEBUG */
133