Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

geqo_cx.c File Reference

#include "postgres.h"
#include "optimizer/geqo_recombination.h"
#include "optimizer/geqo_random.h"
Include dependency graph for geqo_cx.c:

Go to the source code of this file.

Functions

int cx (PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)

Function Documentation

int cx ( PlannerInfo root,
Gene tour1,
Gene tour2,
Gene offspring,
int  num_gene,
City city_table 
)

Definition at line 47 of file geqo_cx.c.

References geqo_randint, i, City::tour1_position, City::tour2_position, and City::used.

Referenced by bf_decrypt(), bf_encrypt(), bf_init(), geqo(), intctx_free(), rj_decrypt(), rj_encrypt(), and rj_init().

{

    int         i,
                start_pos,
                curr_pos;
    int         count = 0;
    int         num_diffs = 0;

    /* initialize city table */
    for (i = 1; i <= num_gene; i++)
    {
        city_table[i].used = 0;
        city_table[tour2[i - 1]].tour2_position = i - 1;
        city_table[tour1[i - 1]].tour1_position = i - 1;
    }

    /* choose random cycle starting position */
    start_pos = geqo_randint(root, num_gene - 1, 0);

    /* child inherits first city  */
    offspring[start_pos] = tour1[start_pos];

    /* begin cycle with tour1 */
    curr_pos = start_pos;
    city_table[(int) tour1[start_pos]].used = 1;

    count++;

    /* cx main part */


/* STEP 1 */

    while (tour2[curr_pos] != tour1[start_pos])
    {
        city_table[(int) tour2[curr_pos]].used = 1;
        curr_pos = city_table[(int) tour2[curr_pos]].tour1_position;
        offspring[curr_pos] = tour1[curr_pos];
        count++;
    }


/* STEP 2 */

    /* failed to create a complete tour */
    if (count < num_gene)
    {
        for (i = 1; i <= num_gene; i++)
        {
            if (!city_table[i].used)
            {
                offspring[city_table[i].tour2_position] =
                    tour2[(int) city_table[i].tour2_position];
                count++;
            }
        }
    }


/* STEP 3 */

    /* still failed to create a complete tour */
    if (count < num_gene)
    {

        /* count the number of differences between mom and offspring */
        for (i = 0; i < num_gene; i++)
            if (tour1[i] != offspring[i])
                num_diffs++;

    }

    return num_diffs;
}