Header And Logo

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

geqo_cx.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002 *
00003 * geqo_cx.c
00004 *
00005 *    cycle crossover [CX] routines;
00006 *    CX operator according to Oliver et al
00007 *    (Proc 2nd Int'l Conf on GA's)
00008 *
00009 * src/backend/optimizer/geqo/geqo_cx.c
00010 *
00011 *-------------------------------------------------------------------------
00012 */
00013 
00014 /* contributed by:
00015    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
00016    *  Martin Utesch              * Institute of Automatic Control      *
00017    =                             = University of Mining and Technology =
00018    *  [email protected]  * Freiberg, Germany                   *
00019    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
00020  */
00021 
00022 /* the cx algorithm is adopted from Genitor : */
00023 /*************************************************************/
00024 /*                                                           */
00025 /*  Copyright (c) 1990                                       */
00026 /*  Darrell L. Whitley                                       */
00027 /*  Computer Science Department                              */
00028 /*  Colorado State University                                */
00029 /*                                                           */
00030 /*  Permission is hereby granted to copy all or any part of  */
00031 /*  this program for free distribution.   The author's name  */
00032 /*  and this copyright notice must be included in any copy.  */
00033 /*                                                           */
00034 /*************************************************************/
00035 
00036 
00037 #include "postgres.h"
00038 #include "optimizer/geqo_recombination.h"
00039 #include "optimizer/geqo_random.h"
00040 
00041 
00042 /* cx
00043  *
00044  *   cycle crossover
00045  */
00046 int
00047 cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring,
00048    int num_gene, City *city_table)
00049 {
00050 
00051     int         i,
00052                 start_pos,
00053                 curr_pos;
00054     int         count = 0;
00055     int         num_diffs = 0;
00056 
00057     /* initialize city table */
00058     for (i = 1; i <= num_gene; i++)
00059     {
00060         city_table[i].used = 0;
00061         city_table[tour2[i - 1]].tour2_position = i - 1;
00062         city_table[tour1[i - 1]].tour1_position = i - 1;
00063     }
00064 
00065     /* choose random cycle starting position */
00066     start_pos = geqo_randint(root, num_gene - 1, 0);
00067 
00068     /* child inherits first city  */
00069     offspring[start_pos] = tour1[start_pos];
00070 
00071     /* begin cycle with tour1 */
00072     curr_pos = start_pos;
00073     city_table[(int) tour1[start_pos]].used = 1;
00074 
00075     count++;
00076 
00077     /* cx main part */
00078 
00079 
00080 /* STEP 1 */
00081 
00082     while (tour2[curr_pos] != tour1[start_pos])
00083     {
00084         city_table[(int) tour2[curr_pos]].used = 1;
00085         curr_pos = city_table[(int) tour2[curr_pos]].tour1_position;
00086         offspring[curr_pos] = tour1[curr_pos];
00087         count++;
00088     }
00089 
00090 
00091 /* STEP 2 */
00092 
00093     /* failed to create a complete tour */
00094     if (count < num_gene)
00095     {
00096         for (i = 1; i <= num_gene; i++)
00097         {
00098             if (!city_table[i].used)
00099             {
00100                 offspring[city_table[i].tour2_position] =
00101                     tour2[(int) city_table[i].tour2_position];
00102                 count++;
00103             }
00104         }
00105     }
00106 
00107 
00108 /* STEP 3 */
00109 
00110     /* still failed to create a complete tour */
00111     if (count < num_gene)
00112     {
00113 
00114         /* count the number of differences between mom and offspring */
00115         for (i = 0; i < num_gene; i++)
00116             if (tour1[i] != offspring[i])
00117                 num_diffs++;
00118 
00119     }
00120 
00121     return num_diffs;
00122 }