| 1 | /*------------------------------------------------------------------------ |
| 2 | * |
| 3 | * geqo_ox1.c |
| 4 | * |
| 5 | * order crossover [OX] routines; |
| 6 | * OX1 operator according to Davis |
| 7 | * (Proc Int'l Joint Conf on AI) |
| 8 | * |
| 9 | * src/backend/optimizer/geqo/geqo_ox1.c |
| 10 | * |
| 11 | *------------------------------------------------------------------------- |
| 12 | */ |
| 13 | |
| 14 | /* contributed by: |
| 15 | =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= |
| 16 | * Martin Utesch * Institute of Automatic Control * |
| 17 | = = University of Mining and Technology = |
| 18 | * utesch@aut.tu-freiberg.de * Freiberg, Germany * |
| 19 | =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*= |
| 20 | */ |
| 21 | |
| 22 | /* the ox algorithm is adopted from Genitor : */ |
| 23 | /*************************************************************/ |
| 24 | /* */ |
| 25 | /* Copyright (c) 1990 */ |
| 26 | /* Darrell L. Whitley */ |
| 27 | /* Computer Science Department */ |
| 28 | /* Colorado State University */ |
| 29 | /* */ |
| 30 | /* Permission is hereby granted to copy all or any part of */ |
| 31 | /* this program for free distribution. The author's name */ |
| 32 | /* and this copyright notice must be included in any copy. */ |
| 33 | /* */ |
| 34 | /*************************************************************/ |
| 35 | |
| 36 | #include "postgres.h" |
| 37 | #include "optimizer/geqo_random.h" |
| 38 | #include "optimizer/geqo_recombination.h" |
| 39 | |
| 40 | #if defined(OX1) |
| 41 | |
| 42 | /* ox1 |
| 43 | * |
| 44 | * position crossover |
| 45 | */ |
| 46 | void |
| 47 | ox1(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, |
| 48 | City * city_table) |
| 49 | { |
| 50 | int left, |
| 51 | right, |
| 52 | k, |
| 53 | p, |
| 54 | temp; |
| 55 | |
| 56 | /* initialize city table */ |
| 57 | for (k = 1; k <= num_gene; k++) |
| 58 | city_table[k].used = 0; |
| 59 | |
| 60 | /* select portion to copy from tour1 */ |
| 61 | left = geqo_randint(root, num_gene - 1, 0); |
| 62 | right = geqo_randint(root, num_gene - 1, 0); |
| 63 | |
| 64 | if (left > right) |
| 65 | { |
| 66 | temp = left; |
| 67 | left = right; |
| 68 | right = temp; |
| 69 | } |
| 70 | |
| 71 | /* copy portion from tour1 to offspring */ |
| 72 | for (k = left; k <= right; k++) |
| 73 | { |
| 74 | offspring[k] = tour1[k]; |
| 75 | city_table[(int) tour1[k]].used = 1; |
| 76 | } |
| 77 | |
| 78 | k = (right + 1) % num_gene; /* index into offspring */ |
| 79 | p = k; /* index into tour2 */ |
| 80 | |
| 81 | /* copy stuff from tour2 to offspring */ |
| 82 | while (k != left) |
| 83 | { |
| 84 | if (!city_table[(int) tour2[p]].used) |
| 85 | { |
| 86 | offspring[k] = tour2[p]; |
| 87 | k = (k + 1) % num_gene; |
| 88 | city_table[(int) tour2[p]].used = 1; |
| 89 | } |
| 90 | p = (p + 1) % num_gene; /* increment tour2-index */ |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | #endif /* defined(OX1) */ |
| 96 | |