File: mutation.c

package info (click to toggle)
pgapack 1.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,344 kB
  • ctags: 1,786
  • sloc: ansic: 10,331; fortran: 2,985; sh: 486; makefile: 462; perl: 105
file content (457 lines) | stat: -rw-r--r-- 13,730 bytes parent folder | download | duplicates (3)
1
2
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
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: mutation.c: This file contains the data structure neutral mutation
*                       routines
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
*              Brian P. Walenz
*****************************************************************************/

#include "pgapack.h"

/*U****************************************************************************
  PGAMutate - This routine performs mutation on a string.  The type of mutation
  depends on the data type.  Refer to the user guide for data-specific
  examples.

  Category: Operators

  Inputs:
      ctx  - context variable
      p   - index of string to mutate
      pop - symbolic constant of the population containing p

  Output:
      The number of mutations performed.  Member p in population pop is
      mutated by side-effect.

  Example:
      Mutate the best string in the population, until 10 or more mutations
      have occured.

      PGAContext *ctx;
      int p, count = 0;
      :
      p = PGAGetBestIndex(ctx, PGA_NEWPOP);
      while (count < 10) {
          count += PGAMutate(ctx, p, PGA_NEWPOP);
      }

****************************************************************************U*/
int PGAMutate(PGAContext *ctx, int p, int pop)
{
    double mr;
    int count;
    int fp;
    PGADebugEntered("PGAMutate");
    
    mr    = ctx->ga.MutationProb;
    if (ctx->fops.Mutation) {
	fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1;
        count = (*ctx->fops.Mutation)(&ctx, &fp, &pop, &mr);
    } else {
	count = (*ctx->cops.Mutation)( ctx, p, pop, mr );
    }
    
    if ( count > 0 )
	PGASetEvaluationUpToDateFlag(ctx, p, pop, PGA_FALSE);
    
    PGADebugExited("PGAMutate");
    
    return(count);
}

/*U****************************************************************************
   PGASetMutationType - set type of mutation to use. Only effects integer-
   and real-valued strings.  Binary-valued strings are always complemented.
   In character-valued strings, one alphabetic character is replaced with
   another chosen uniformly randomly.  The alphabetic characters will be lower,
   upper, or mixed case depending on how the strings were initialized.

   Valid choices are PGA_MUTATION_CONSTANT (Real/Integer), PGA_MUTATION_RANGE
   (Real/Integer), PGA_MUTATION_UNIFORM (Real), PGA_MUTATION_GAUSSIAN (Real),
   and PGA_MUTATION_PERMUTE (Integer).  The default for integer-valued strings
   conforms to how the strings were initialized.  The default for real-valued
   strings is PGA_MUTATION_GAUSSIAN.  See the user guide for more details.

   Category: Operators

   Inputs:
      ctx           - context variable
      mutation_type - symbolic constant to specify the mutation type

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetMutationType(ctx, PGA_MUTATION_UNIFORM);

****************************************************************************U*/
void PGASetMutationType( PGAContext *ctx, int mutation_type)
{
    PGADebugEntered("PGASetMutationType");

     switch (mutation_type)
     {
     case PGA_MUTATION_CONSTANT:
     case PGA_MUTATION_RANGE:
     case PGA_MUTATION_UNIFORM:
     case PGA_MUTATION_GAUSSIAN:
     case PGA_MUTATION_PERMUTE:
          ctx->ga.MutationType = mutation_type;
          break;
     default:
          PGAError ( ctx,
                    "PGASetMutationType: Invalid value of mutation_type:",
                    PGA_FATAL, PGA_INT, (void *) &mutation_type);
          break;
     }

    PGADebugExited("PGASetMutationType");
}

/*U***************************************************************************
   PGAGetMutationType - Returns the type of mutation used

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      Returns the integer corresponding to the symbolic constant
      used to specify the type of mutation specified

   Example:
      PGAContext *ctx;
      int mutatetype;
      :
      mutatetype = PGAGetMutationType(ctx);
      switch (mutatetype) {
      case PGA_MUTATION_CONSTANT:
          printf("Mutation Type = PGA_MUTATION_CONSTANT\n");
          break;
      case PGA_MUTATION_RANGE:
          printf("Mutation Type = PGA_MUTATION_RANGE\n");
          break;
      case PGA_MUTATION_UNIFORM:
          printf("Mutation Type = PGA_MUTATION_UNIFORM\n");
          break;
      case PGA_MUTATION_GAUSSIAN:
          printf("Mutation Type = PGA_MUTATION_GAUSSIAN\n");
          break;
      case PGA_MUTATION_PERMUTE:
          printf("Mutation Type = PGA_MUTATION_PERMUTE\n");
          break;
      }

***************************************************************************U*/
int PGAGetMutationType (PGAContext *ctx)
{
    PGADebugEntered("PGAGetMutationType");
    PGAFailIfNotSetUp("PGAGetMutationType");
    PGADebugExited("PGAGetMutationType");
    return(ctx->ga.MutationType);
}

/*U****************************************************************************
   PGASetMutationRealValue - Set multiplier to mutate PGA_DATATYPE_REAL
   strings with.  The use of this value depends on the type of mutation
   being used.  The default value is 0.1.  See the user guide for more details.

   Category: Operators

   Inputs:
      ctx - context variable
      val - the mutation value to use for Real mutation

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetMutationRealValue(ctx,50.0);

****************************************************************************U*/
void PGASetMutationRealValue( PGAContext *ctx, double val)
{
    PGADebugEntered("PGASetMutationRealValue");

    if (val < 0.0)
        PGAError ( ctx,
                  "PGASetMutationRealValue: Invalid value of val:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &val);
    else
        ctx->ga.MutateRealValue = val;

    PGADebugExited("PGASetMutationRealValue");
}

/*U***************************************************************************
   PGAGetMutationRealValue - Returns the value of the multiplier used to
   mutate PGA_DATATYPE_REAL strings with.

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      The value of the multiplier used to mutate PGA_DATATYPE_REAL strings with

   Example:
      PGAContext *ctx;
      double val;
      :
      val = PGAGetMutationRealValue(ctx);

***************************************************************************U*/
double PGAGetMutationRealValue (PGAContext *ctx)
{
    PGADebugEntered("PGAGetMutationRealValue");
    PGAFailIfNotSetUp("PGAGetMutationRealValue");

    PGADebugExited("PGAGetMutationRealValue");

    return(ctx->ga.MutateRealValue);
}

/*U****************************************************************************
   PGASetMutationIntegerValue - Set multiplier to mutate PGA_DATATYPE_INTEGER
   strings with.  The use of this value depends on the type of mutation
   being used.  The default value is 1.  See the user guide for more details.

   Category: Operators

   Inputs:
      ctx - context variable
      val - the mutation value to use for Integer mutation

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetMutationIntegerValue(ctx, 5);

****************************************************************************U*/
void PGASetMutationIntegerValue( PGAContext *ctx, int val)
{
    PGADebugEntered("PGASetMutationIntegerValue");

    if (val < 0.0)
        PGAError ( ctx,
                  "PGASetMutationIntegerValue: Invalid value of val:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &val);
    else
        ctx->ga.MutateIntegerValue = val;

    PGADebugExited("PGASetMutationIntegerValue");
}


/*U***************************************************************************
  PGAGetMutationIntegerValue - Returns the value of the multiplier
  used to mutate PGA_DATATYPE_INTEGER strings with.

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      The value of the multiplier used to mutate PGA_DATATYPE_INTEGER
      strings with

   Example:
      PGAContext *ctx;
      int ival;
      :
      ival = PGAGetMutationIntegerValue(ctx);

***************************************************************************U*/
int PGAGetMutationIntegerValue (PGAContext *ctx)
{
    PGADebugEntered("PGAGetMutationIntegerValue");
    PGAFailIfNotSetUp("PGAGetMutationIntegerValue");

    PGADebugExited("PGAGetMutationIntegerValue");

    return(ctx->ga.MutateIntegerValue);
}

/*U****************************************************************************
   PGASetMutationBoundedFlag - If this flag is set to PGA_TRUE, then for
   Integer and Real strings whenever a gene is mutated, if it underflows
   (overflows) the lower (upper)bound it is reset to the lower (upper) bound.
   In this way all allele values remain within the range the integer strings
   were initialized on.  If this flag is PGA_FALSE (the default), the alleles
   may take any values.

   Category: Operators

   Inputs:
      ctx  - context variable
      flag - either PGA_TRUE or PGA_FALSE

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetMutationBoundedFlag(ctx, PGA_TRUE);

****************************************************************************U*/
void PGASetMutationBoundedFlag(PGAContext *ctx, int val)
{
    PGADebugEntered("PGASetMutationBoundedFlag");

    switch (val)
    {
    case PGA_TRUE:
    case PGA_FALSE:
         ctx->ga.MutateBoundedFlag = val;
         break;
    default:
         PGAError(ctx, "PGASetMutationBoundedFlag: Invalid value:",
                  PGA_FATAL, PGA_INT, (void *) &val);
         break;
    }

    PGADebugExited("PGASetMutationBoundedFlag");
}


/*U****************************************************************************
   PGAGetMutationBoundedFlag - returns PGA_TRUE or PGA_FALSE to indicate
   whether mutated integer strings remain in the range specified when
   initialized with PGASetIntegerInitRange.

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      PGA_TRUE if restricted to the given range, otherwise PGA_FALSE.

   Example:
      PGAContext *ctx;
      int val;
      :
      val = PGAGetMutationBoundedFlag(ctx);

****************************************************************************U*/
int PGAGetMutationBoundedFlag(PGAContext *ctx)
{
    PGADebugEntered  ("PGAGetMutationBoundedFlag");
    PGAFailIfNotSetUp("PGAGetMutationBoundedFlag");
    PGADebugExited   ("PGAGetMutationBoundedFlag");
    return (ctx->ga.MutateBoundedFlag);
}



/*U****************************************************************************
   PGASetMutationProb - Specifies the probability that a given allele will
   be mutated.  If this is called without calling PGASetMutationType(), the
   default mutation type is PGA_MUTATION_FIXED.  The default probability is
   the reciprocal of the string length.

   Category: Operators

   Inputs:
      ctx - context variable
      p   - the mutation probability

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetMutationProb(ctx,0.001);

****************************************************************************U*/
void PGASetMutationProb(PGAContext *ctx, double mutation_prob)
{
    PGADebugEntered("PGASetMutationProb");

    if ((mutation_prob < 0.0) || (mutation_prob > 1.0))
        PGAError ( ctx,
                  "PGASetMutationProb: Invalid value of mutation_prob:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &mutation_prob);
    else
        ctx->ga.MutationProb = mutation_prob;

    PGADebugExited("PGASetMutationProb");
}

/*U***************************************************************************
   PGAGetMutationProb - Returns the probability of mutation.

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      The mutation probability

   Example:
      PGAContext *ctx;
      double pm;
      :
      pm = PGAGetMutateProb(ctx);

***************************************************************************U*/
double PGAGetMutationProb (PGAContext *ctx)
{
    PGADebugEntered("PGAGetMutationProb");
    PGAFailIfNotSetUp("PGAGetMutationProb");
    PGADebugExited("PGAGetMutationProb");
    return(ctx->ga.MutationProb);
}