Header And Logo

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

geqo_px.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002 *
00003 * geqo_px.c
00004 *
00005 *    position crossover [PX] routines;
00006 *    PX operator according to Syswerda
00007 *    (The Genetic Algorithms Handbook, L Davis, ed)
00008 *
00009 * src/backend/optimizer/geqo/geqo_px.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 px 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 #include "postgres.h"
00037 #include "optimizer/geqo_random.h"
00038 #include "optimizer/geqo_recombination.h"
00039 
00040 
00041 /* px
00042  *
00043  *   position crossover
00044  */
00045 void
00046 px(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene,
00047    City *city_table)
00048 {
00049 
00050     int         num_positions;
00051     int         i,
00052                 pos,
00053                 tour2_index,
00054                 offspring_index;
00055 
00056     /* initialize city table */
00057     for (i = 1; i <= num_gene; i++)
00058         city_table[i].used = 0;
00059 
00060     /* choose random positions that will be inherited directly from parent */
00061     num_positions = geqo_randint(root, 2 * num_gene / 3, num_gene / 3);
00062 
00063     /* choose random position */
00064     for (i = 0; i < num_positions; i++)
00065     {
00066         pos = geqo_randint(root, num_gene - 1, 0);
00067 
00068         offspring[pos] = tour1[pos];    /* transfer cities to child */
00069         city_table[(int) tour1[pos]].used = 1;  /* mark city used */
00070     }
00071 
00072     tour2_index = 0;
00073     offspring_index = 0;
00074 
00075 
00076     /* px main part */
00077 
00078     while (offspring_index < num_gene)
00079     {
00080 
00081         /* next position in offspring filled */
00082         if (!city_table[(int) tour1[offspring_index]].used)
00083         {
00084 
00085             /* next city in tour1 not used */
00086             if (!city_table[(int) tour2[tour2_index]].used)
00087             {
00088 
00089                 /* inherit from tour1 */
00090                 offspring[offspring_index] = tour2[tour2_index];
00091 
00092                 tour2_index++;
00093                 offspring_index++;
00094             }
00095             else
00096             {                   /* next city in tour2 has been used */
00097                 tour2_index++;
00098             }
00099 
00100         }
00101         else
00102         {                       /* next position in offspring is filled */
00103             offspring_index++;
00104         }
00105 
00106     }
00107 
00108 }