| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 
 | /*
COPYRIGHT
The following is a notice of limited availability of the code, and disclaimer
which must be included in the prologue of the code and in all source listings
of the code.
(C) COPYRIGHT 2008 University of Chicago
Permission is hereby granted to use, reproduce, prepare derivative works, and
to redistribute to others. This software was authored by:
D. Levine
Mathematics and Computer Science Division 
Argonne National Laboratory Group
with programming assistance of participants in Argonne National 
Laboratory's SERS program.
GOVERNMENT LICENSE
Portions of this material resulted from work developed under a
U.S. Government Contract and are subject to the following license: the
Government is granted for itself and others acting on its behalf a paid-up,
nonexclusive, irrevocable worldwide license in this computer software to
reproduce, prepare derivative works, and perform publicly and display
publicly.
DISCLAIMER
This computer code material was prepared, in part, as an account of work
sponsored by an agency of the United States Government. Neither the United
States, nor the University of Chicago, nor any of their employees, makes any
warranty express or implied, or assumes any legal liability or responsibility
for the accuracy, completeness, or usefulness of any information, apparatus,
product, or process disclosed, or represents that its use would not infringe
privately owned rights.
*/
/*****************************************************************************
*     FILE: duplicate.c: This file contains the routines that have to do with
*                        testing for duplicate strings
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
*              Brian P. Walenz
*****************************************************************************/
#include "pgapack.h"
/*U****************************************************************************
  PGADuplicate - determines if a specified string is a duplicate of one
  already in an existing population
  Category: Generation
  Inputs:
     ctx  - context variable
     p    - string index
     pop1 - symbolic constant of the population containing string p
     pop2 - symbolic constant of the (possibly partial) population containing
            strings to compare string p against
     n    - the number of strings in pop2 to compare string p against
            (indexed 0,...,n-1)
  Outputs:
     Returns PGA_TRUE if PGAGetNoDuplicates() returns PGA_TRUE and
     string p in population pop1 is a duplicate of at least one strings
     0,...,n-1 in population pop2.  Otherwise returns PGA_FALSE
  Example:
     Change any string in PGA_NEWPOP that is an exact copy of a string
     in PGA_OLDPOP.
     PGAContext *ctx;
     int b, n;
     :
     n  = PGAGetPopsize(ctx);
     for (b=0; b<n; b++)
         if (PGADuplicate(ctx, b, PGA_NEWPOP, PGA_OLDPOP, n))
             PGAChange(ctx, b, PGA_NEWPOP);
     Check if the best string in population PGA_OLDPOP is a duplicate of any
     of the strings in the first half of population PGA_NEWPOP.
     PGAContext *ctx;
     int b, n;
     :
     b  = PGAGetBestIndex(ctx, PGA_OLDPOP);
     n  = PGAGetPopsize(ctx) / 2;
     if (PGADuplicate(ctx, b, PGA_OLDPOP, PGA_NEWPOP, n))
         printf("A duplicate!\n");
****************************************************************************U*/
int PGADuplicate(PGAContext *ctx, int p, int pop1, int pop2, int n)
{
    int p2, fp;
    int RetVal = PGA_FALSE;
    
    PGADebugEntered("PGADuplicate");
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGADuplicate", "p = ",
		  PGA_INT, (void *) &p );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGADuplicate", "pop1 = ",
		  PGA_INT, (void *) &pop1 );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGADuplicate", "pop2 = ",
		  PGA_INT, (void *) &pop2 );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGADuplicate", "n  = ",
		  PGA_INT, (void *) &n );
    
    if (ctx->ga.NoDuplicates == PGA_TRUE) {
	if (ctx->fops.Duplicate) {
	    fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1;
	    for (p2 = 1; p2 <= n; p2++)
		if ((*ctx->fops.Duplicate)(&ctx, &fp, &pop1, &p2, &pop2)) {
		    RetVal = PGA_TRUE;
		    p2 = n+1;
		}
	} else {
	    for (p2 = 0; p2 < n; p2++)
		if ((*ctx->cops.Duplicate)(ctx, p, pop1, p2, pop2)) {
		    RetVal = PGA_TRUE;
		    p2 = n;
		}
	}
    }
    
    PGADebugExited("PGADuplicate");
    
    return(RetVal);
}
/*U****************************************************************************
  PGAChange - Repeatedly apply mutation to a string (with an increasing
  mutation rate) until one or more mutations have occurred.  This routine is
  usually used with PGADuplicate to modify a duplicate string.  It is not
  intended to replace PGAMutation
  Category: Generation
  Inputs:
     ctx  - context variable
     p    - string index
     pop  - symbolic constant of the population containing string p
  Outputs:
     Mutates string p in population pop via side effect.
  Example:
     Change any string in PGA_NEWPOP that is an exact copy of a string
     in PGA_OLDPOP.  To be complete, we should check the population again
     if any changes are made; for simplicity, we don't.
     PGAContext *ctx;
     int b, n;
     :
     n  = PGAGetPopsize(ctx);
     for (b=0; b<n; b++)
         if (PGADuplicate(ctx, b, PGA_NEWPOP, PGA_OLDPOP, n))
             PGAChange(ctx, b, PGA_NEWPOP);
****************************************************************************U*/
void PGAChange( PGAContext *ctx, int p, int pop )
{
    int    changed = PGA_FALSE;
    int    fp, nflips;
    double mr;
    PGADebugEntered("PGAChange");
    mr = ctx->ga.MutationProb;
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGAChange", " mr = ",
                   PGA_DOUBLE, (void *) &mr );
    while (( changed == PGA_FALSE ) && (mr <= 1.0)) {
	if (ctx->fops.Mutation) {
	    fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1;
            nflips = (*ctx->fops.Mutation)(&ctx, &fp, &pop, &mr);
	} else {
	    nflips = (*ctx->cops.Mutation)( ctx, p, pop, mr );
	}
        if ( nflips > 0 )
            changed = PGA_TRUE;
        else
            mr = 1.1*mr;
    }
    if (changed == PGA_FALSE) {
	PGAError(ctx, "Could not change string:", PGA_WARNING, PGA_VOID, NULL);
	PGAPrintString(ctx, stderr, p, pop);
    }
    PGADebugExited("PGAChange");
}
/*U****************************************************************************
   PGASetNoDuplicatesFlag - A boolean flag to indicate if duplicate strings are
   allowed in the population. Valid choices are PGA_TRUE and PGA_FALSE.  The
   default is PGA_FALSE -- allow duplicates.
   Category: Generation
   Inputs:
      ctx  - context variable
      flag - PGA_TRUE or PGA_FALSE
   Outputs:
      None
   Example:
      Set the NoDuplicates flag to require that all strings are unique.
      PGAContext *ctx;
      :
      PGASetNoDuplicatesFlag(ctx,PGA_TRUE);
****************************************************************************U*/
void PGASetNoDuplicatesFlag( PGAContext *ctx, int no_dup)
{
    PGADebugEntered("PGASetNoDuplicatesFlag");
    switch (no_dup) {
        case PGA_TRUE:
        case PGA_FALSE:
            ctx->ga.NoDuplicates = no_dup;
            break;
        default:
            PGAError ( ctx, "PGASetNoDuplicatesFlag: Invalid value of no_dup:",
                       PGA_FATAL, PGA_INT, (void *) &no_dup);
            break;
    }
    PGADebugExited("PGASetNoDuplicatesFlag");
}
/*U***************************************************************************
   PGAGetNoDuplicatesFlag - Returns PGA_TRUE if duplicates are not allowed,
   else returns PGA_FALSE.
   Category: Generation
   Inputs:
      ctx - context variable
   Outputs:
      The value of the NoDuplicates flag.
   Example:
      PGAContext *ctx;
      int nodups;
      :
      nodups = PGAGetNoDuplicatesFlag(ctx);
      switch (nodups) {
      case PGA_TRUE:
          printf("Duplicate strings not allowed in population\n");
          break;
      case PGA_FALSE:
          printf("Duplicate strings allowed in population\n");
          break;
      }
***************************************************************************U*/
int PGAGetNoDuplicatesFlag (PGAContext *ctx)
{
    PGADebugEntered("PGAGetNoDuplicatesFlag");
    PGAFailIfNotSetUp("PGAGetNoDuplicatesFlag");
    PGADebugExited("PGAGetNoDuplicatesFlag");
    return(ctx->ga.NoDuplicates);
}
 |