/*
 * geqo_selection
 *	 according to bias described by input parameters,
 *	 first and second genes are selected from the pool
 */
void
geqo_selection(PlannerInfo *root, Chromosome *momma, Chromosome *daddy,
               Pool *pool, double bias)
{
    int			first,
                second;

    first = linear_rand(root, pool->size, bias);
    second = linear_rand(root, pool->size, bias);

    /*
     * Ensure we have selected different genes, except if pool size is only
     * one, when we can't.
     *
     * This code has been observed to hang up in an infinite loop when the
     * platform's implementation of erand48() is broken.  We consider that a
     * feature: it lets you know you'd better fix the random-number generator.
     */
    if (pool->size > 1)
    {
        while (first == second)
            second = linear_rand(root, pool->size, bias);
    }

    geqo_copy(root, momma, &pool->data[first], pool->string_length);
    geqo_copy(root, daddy, &pool->data[second], pool->string_length);
}
Пример #2
0
/*
 * geqo_selection
 *	 according to bias described by input parameters,
 *	 first and second genes are selected from the pool
 */
void
geqo_selection(PlannerInfo *root, Chromosome *momma, Chromosome *daddy,
			   Pool *pool, double bias)
{
	int			first,
				second;

	first = linear_rand(root, pool->size, bias);
	second = linear_rand(root, pool->size, bias);

	/*
	 * Ensure we have selected different genes, except if pool size is only
	 * one, when we can't.
	 *
	 * This code was observed to hang up in an infinite loop when the
	 * platform's implementation of erand48() was broken.  We now always use
	 * our own version.
	 */
	if (pool->size > 1)
	{
		while (first == second)
			second = linear_rand(root, pool->size, bias);
	}

	geqo_copy(root, momma, &pool->data[first], pool->string_length);
	geqo_copy(root, daddy, &pool->data[second], pool->string_length);
}
Пример #3
0
/*
 * geqo_selection
 *	 according to bias described by input parameters,
 *	 first and second genes are selected from the pool
 */
void
geqo_selection(Chromosome *momma, Chromosome *daddy, Pool *pool, double bias)
{
	int			first,
				second;

	first = linear(pool->size, bias);
	second = linear(pool->size, bias);

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

	geqo_copy(momma, &pool->data[first], pool->string_length);
	geqo_copy(daddy, &pool->data[second], pool->string_length);
}
Пример #4
0
/* spread_chromo
 *	 inserts a new chromosome into the pool, displacing worst gene in pool
 *	 assumes best->worst = smallest->largest
 */
void
spread_chromo(planner_info_n *root, Chromosome *chromo, Pool *pool)
{
	int top;
	int mid;
	int bot;
	int i;
	int index;
	Chromosome swap_chromo;
	Chromosome 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;
	}
}