#include "postgres.h"
#include <float.h>
#include <limits.h>
#include <math.h>
#include "optimizer/geqo_copy.h"
#include "optimizer/geqo_pool.h"
#include "optimizer/geqo_recombination.h"
Go to the source code of this file.
Functions | |
static int | compare (const void *arg1, const void *arg2) |
Pool * | alloc_pool (PlannerInfo *root, int pool_size, int string_length) |
void | free_pool (PlannerInfo *root, Pool *pool) |
void | random_init_pool (PlannerInfo *root, Pool *pool) |
void | sort_pool (PlannerInfo *root, Pool *pool) |
Chromosome * | alloc_chromo (PlannerInfo *root, int string_length) |
void | free_chromo (PlannerInfo *root, Chromosome *chromo) |
void | spread_chromo (PlannerInfo *root, Chromosome *chromo, Pool *pool) |
Chromosome* alloc_chromo | ( | PlannerInfo * | root, | |
int | string_length | |||
) |
Definition at line 138 of file geqo_pool.c.
References palloc(), and Chromosome::string.
Referenced by geqo().
{ Chromosome *chromo; chromo = (Chromosome *) palloc(sizeof(Chromosome)); chromo->string = (Gene *) palloc((string_length + 1) * sizeof(Gene)); return chromo; }
Pool* alloc_pool | ( | PlannerInfo * | root, | |
int | pool_size, | |||
int | string_length | |||
) |
Definition at line 42 of file geqo_pool.c.
References Pool::data, i, palloc(), Pool::size, and Pool::string_length.
Referenced by geqo().
{ Pool *new_pool; Chromosome *chromo; int i; /* pool */ new_pool = (Pool *) palloc(sizeof(Pool)); new_pool->size = (int) pool_size; new_pool->string_length = (int) string_length; /* all chromosome */ new_pool->data = (Chromosome *) palloc(pool_size * sizeof(Chromosome)); /* all gene */ chromo = (Chromosome *) new_pool->data; /* vector of all chromos */ for (i = 0; i < pool_size; i++) chromo[i].string = palloc((string_length + 1) * sizeof(Gene)); return new_pool; }
static int compare | ( | const void * | arg1, | |
const void * | arg2 | |||
) | [static] |
Definition at line 121 of file geqo_pool.c.
References Chromosome::worth.
Referenced by sort_pool().
{ const Chromosome *chromo1 = (const Chromosome *) arg1; const Chromosome *chromo2 = (const Chromosome *) arg2; if (chromo1->worth == chromo2->worth) return 0; else if (chromo1->worth > chromo2->worth) return 1; else return -1; }
void free_chromo | ( | PlannerInfo * | root, | |
Chromosome * | chromo | |||
) |
Definition at line 152 of file geqo_pool.c.
References pfree(), and Chromosome::string.
Referenced by geqo().
void free_pool | ( | PlannerInfo * | root, | |
Pool * | pool | |||
) |
Definition at line 69 of file geqo_pool.c.
References Pool::data, i, pfree(), and Pool::size.
Referenced by geqo().
{ Chromosome *chromo; int i; /* all gene */ chromo = (Chromosome *) pool->data; /* vector of all chromos */ for (i = 0; i < pool->size; i++) pfree(chromo[i].string); /* all chromosome */ pfree(pool->data); /* pool */ pfree(pool); }
void random_init_pool | ( | PlannerInfo * | root, | |
Pool * | pool | |||
) |
Definition at line 91 of file geqo_pool.c.
References Pool::data, geqo_eval(), i, init_tour(), Pool::size, Pool::string_length, and Chromosome::worth.
Referenced by geqo().
{ Chromosome *chromo = (Chromosome *) pool->data; int i; for (i = 0; i < pool->size; i++) { init_tour(root, chromo[i].string, pool->string_length); pool->data[i].worth = geqo_eval(root, chromo[i].string, pool->string_length); } }
void sort_pool | ( | PlannerInfo * | root, | |
Pool * | pool | |||
) |
Definition at line 111 of file geqo_pool.c.
References compare(), Pool::data, qsort, and Pool::size.
Referenced by geqo().
{ qsort(pool->data, pool->size, sizeof(Chromosome), compare); }
void spread_chromo | ( | PlannerInfo * | root, | |
Chromosome * | chromo, | |||
Pool * | pool | |||
) |
Definition at line 163 of file geqo_pool.c.
References Pool::data, geqo_copy(), i, Pool::size, Chromosome::string, Pool::string_length, and Chromosome::worth.
Referenced by geqo().
{ int top, mid, bot; int i, index; Chromosome swap_chromo, tmp_chromo; /* new chromo is so bad we can't use it */ if (chromo->worth > pool->data[pool->size - 1].worth) return; /* do a binary search to find the index of the new chromo */ top = 0; mid = pool->size / 2; bot = pool->size - 1; index = -1; while (index == -1) { /* these 4 cases find a new location */ if (chromo->worth <= pool->data[top].worth) index = top; else if (chromo->worth == pool->data[mid].worth) index = mid; else if (chromo->worth == pool->data[bot].worth) index = bot; else if (bot - top <= 1) index = bot; /* * these 2 cases move the search indices since a new location has not * yet been found. */ else if (chromo->worth < pool->data[mid].worth) { bot = mid; mid = top + ((bot - top) / 2); } else { /* (chromo->worth > pool->data[mid].worth) */ top = mid; mid = top + ((bot - top) / 2); } } /* ... while */ /* now we have index for chromo */ /* * move every gene from index on down one position to make room for chromo */ /* * copy new gene into pool storage; always replace worst gene in pool */ geqo_copy(root, &pool->data[pool->size - 1], chromo, pool->string_length); swap_chromo.string = pool->data[pool->size - 1].string; swap_chromo.worth = pool->data[pool->size - 1].worth; for (i = index; i < pool->size; i++) { tmp_chromo.string = pool->data[i].string; tmp_chromo.worth = pool->data[i].worth; pool->data[i].string = swap_chromo.string; pool->data[i].worth = swap_chromo.worth; swap_chromo.string = tmp_chromo.string; swap_chromo.worth = tmp_chromo.worth; } }