source: branches/uq/packages/optimizer/src/pgapack/gekco/pgapack/source/mutation.c @ 5679

Last change on this file since 5679 was 5679, checked in by ldelgass, 9 years ago

Full merge 1.3 branch to uq branch to sync. Fixed partial subdirectory merge
by removing mergeinfo from lang/python/Rappture directory.

  • Property svn:eol-style set to native
File size: 14.7 KB
Line 
1/*
2 * 
3 *  *********************************************************************
4 *  (C) COPYRIGHT 1995 UNIVERSITY OF CHICAGO
5 *  *********************************************************************
6 * 
7 *  This software was authored by
8 * 
9 *  D. Levine
10 *  Mathematics and Computer Science Division Argonne National Laboratory
11 *  Argonne IL 60439
12 *  levine@mcs.anl.gov
13 *  (708) 252-6735
14 *  (708) 252-5986 (FAX)
15 * 
16 *  with programming assistance of participants in Argonne National
17 *  Laboratory's SERS program.
18 * 
19 *  This program contains material protectable under copyright laws of the
20 *  United States.  Permission is hereby granted to use it, reproduce it,
21 *  to translate it into another language, and to redistribute it to
22 *  others at no charge except a fee for transferring a copy, provided
23 *  that you conspicuously and appropriately publish on each copy the
24 *  University of Chicago's copyright notice, and the disclaimer of
25 *  warranty and Government license included below.  Further, permission
26 *  is hereby granted, subject to the same provisions, to modify a copy or
27 *  copies or any portion of it, and to distribute to others at no charge
28 *  materials containing or derived from the material.
29 * 
30 *  The developers of the software ask that you acknowledge its use in any
31 *  document referencing work based on the  program, such as published
32 *  research.  Also, they ask that you supply to Argonne National
33 *  Laboratory a copy of any published research referencing work based on
34 *  the software.
35 * 
36 *  Any entity desiring permission for further use must contact:
37 * 
38 *  J. Gleeson
39 *  Industrial Technology Development Center Argonne National Laboratory
40 *  Argonne IL 60439
41 *  gleesonj@smtplink.eid.anl.gov
42 *  (708) 252-6055
43 * 
44 *  ********************************************************************
45 *  DISCLAIMER
46 * 
47 *  THIS PROGRAM WAS PREPARED AS AN ACCOUNT OF WORK SPONSORED BY AN AGENCY
48 *  OF THE UNITED STATES GOVERNMENT.  NEITHER THE UNIVERSITY OF CHICAGO,
49 *  THE UNITED STATES GOVERNMENT NOR ANY OF THEIR EMPLOYEES MAKE ANY
50 *  WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR
51 *  RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY
52 *  INFORMATION OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT
53 *  INFRINGE PRIVATELY OWNED RIGHTS.
54 * 
55 *  **********************************************************************
56 *  GOVERNMENT LICENSE
57 * 
58 *  The Government is granted for itself and others acting on its behalf a
59 *  paid-up, non-exclusive, irrevocable worldwide license in this computer
60 *  software to reproduce, prepare derivative works, and perform publicly
61 *  and display publicly.
62 */
63
64/*****************************************************************************
65*     File: mutation.c: This file contains the data structure neutral mutation
66*                       routines
67*
68*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
69*              Brian P. Walenz
70*****************************************************************************/
71
72#include "pgapack.h"
73
74/*U****************************************************************************
75  PGAMutate - This routine performs mutation on a string.  The type of mutation
76  depends on the data type.  Refer to the user guide for data-specific
77  examples.
78
79  Category: Operators
80
81  Inputs:
82      ctx  - context variable
83      p   - index of string to mutate
84      pop - symbolic constant of the population containing p
85
86  Output:
87      The number of mutations performed.  Member p in population pop is
88      mutated by side-effect.
89
90  Example:
91      Mutate the best string in the population, until 10 or more mutations
92      have occured.
93
94      PGAContext *ctx;
95      int p, count = 0;
96      :
97      p = PGAGetBestIndex(ctx, PGA_NEWPOP);
98      while (count < 10) {
99          count += PGAMutate(ctx, p, PGA_NEWPOP);
100      }
101
102****************************************************************************U*/
103int PGAMutate(PGAContext *ctx, int p, int pop)
104{
105    double mr;
106    int count;
107    int fp;
108    PGADebugEntered("PGAMutate");
109   
110    mr    = ctx->ga.MutationProb;
111    if (ctx->fops.Mutation) {
112        fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1;
113        count = (*ctx->fops.Mutation)(&ctx, &fp, &pop, &mr);
114    } else {
115        count = (*ctx->cops.Mutation)( ctx, p, pop, mr );
116    }
117   
118    if ( count > 0 )
119        PGASetEvaluationUpToDateFlag(ctx, p, pop, PGA_FALSE);
120   
121    PGADebugExited("PGAMutate");
122   
123    return(count);
124}
125
126/*U****************************************************************************
127   PGASetMutationType - set type of mutation to use. Only effects integer-
128   and real-valued strings.  Binary-valued strings are always complemented.
129   In character-valued strings, one alphabetic character is replaced with
130   another chosen uniformly randomly.  The alphabetic characters will be lower,
131   upper, or mixed case depending on how the strings were initialized.
132
133   Valid choices are PGA_MUTATION_CONSTANT (Real/Integer), PGA_MUTATION_RANGE
134   (Real/Integer), PGA_MUTATION_UNIFORM (Real), PGA_MUTATION_GAUSSIAN (Real),
135   and PGA_MUTATION_PERMUTE (Integer).  The default for integer-valued strings
136   conforms to how the strings were initialized.  The default for real-valued
137   strings is PGA_MUTATION_GAUSSIAN.  See the user guide for more details.
138
139   Category: Operators
140
141   Inputs:
142      ctx           - context variable
143      mutation_type - symbolic constant to specify the mutation type
144
145   Outputs:
146      None
147
148   Example:
149      PGAContext *ctx;
150      :
151      PGASetMutationType(ctx, PGA_MUTATION_UNIFORM);
152
153****************************************************************************U*/
154void PGASetMutationType( PGAContext *ctx, int mutation_type)
155{
156    PGADebugEntered("PGASetMutationType");
157
158     switch (mutation_type)
159     {
160     case PGA_MUTATION_CONSTANT:
161     case PGA_MUTATION_RANGE:
162     case PGA_MUTATION_UNIFORM:
163     case PGA_MUTATION_GAUSSIAN:
164     case PGA_MUTATION_PERMUTE:
165          ctx->ga.MutationType = mutation_type;
166          break;
167     default:
168          PGAError ( ctx,
169                    "PGASetMutationType: Invalid value of mutation_type:",
170                    PGA_FATAL, PGA_INT, (void *) &mutation_type);
171          break;
172     }
173
174    PGADebugExited("PGASetMutationType");
175}
176
177/*U***************************************************************************
178   PGAGetMutationType - Returns the type of mutation used
179
180   Category: Operators
181
182   Inputs:
183      ctx - context variable
184
185   Outputs:
186      Returns the integer corresponding to the symbolic constant
187      used to specify the type of mutation specified
188
189   Example:
190      PGAContext *ctx;
191      int mutatetype;
192      :
193      mutatetype = PGAGetMutationType(ctx);
194      switch (mutatetype) {
195      case PGA_MUTATION_CONSTANT:
196          printf("Mutation Type = PGA_MUTATION_CONSTANT\n");
197          break;
198      case PGA_MUTATION_RANGE:
199          printf("Mutation Type = PGA_MUTATION_RANGE\n");
200          break;
201      case PGA_MUTATION_UNIFORM:
202          printf("Mutation Type = PGA_MUTATION_UNIFORM\n");
203          break;
204      case PGA_MUTATION_GAUSSIAN:
205          printf("Mutation Type = PGA_MUTATION_GAUSSIAN\n");
206          break;
207      case PGA_MUTATION_PERMUTE:
208          printf("Mutation Type = PGA_MUTATION_PERMUTE\n");
209          break;
210      }
211
212***************************************************************************U*/
213int PGAGetMutationType (PGAContext *ctx)
214{
215    PGADebugEntered("PGAGetMutationType");
216    PGAFailIfNotSetUp("PGAGetMutationType");
217    PGADebugExited("PGAGetMutationType");
218    return(ctx->ga.MutationType);
219}
220
221/*U****************************************************************************
222   PGASetMutationRealValue - Set multiplier to mutate PGA_DATATYPE_REAL
223   strings with.  The use of this value depends on the type of mutation
224   being used.  The default value is 0.1.  See the user guide for more details.
225
226   Category: Operators
227
228   Inputs:
229      ctx - context variable
230      val - the mutation value to use for Real mutation
231
232   Outputs:
233      None
234
235   Example:
236      PGAContext *ctx;
237      :
238      PGASetMutationRealValue(ctx,50.0);
239
240****************************************************************************U*/
241void PGASetMutationRealValue( PGAContext *ctx, double val)
242{
243    PGADebugEntered("PGASetMutationRealValue");
244
245    if (val < 0.0)
246        PGAError ( ctx,
247                  "PGASetMutationRealValue: Invalid value of val:",
248                   PGA_FATAL, PGA_DOUBLE, (void *) &val);
249    else
250        ctx->ga.MutateRealValue = val;
251
252    PGADebugExited("PGASetMutationRealValue");
253}
254
255/*U***************************************************************************
256   PGAGetMutationRealValue - Returns the value of the multiplier used to
257   mutate PGA_DATATYPE_REAL strings with.
258
259   Category: Operators
260
261   Inputs:
262      ctx - context variable
263
264   Outputs:
265      The value of the multiplier used to mutate PGA_DATATYPE_REAL strings with
266
267   Example:
268      PGAContext *ctx;
269      double val;
270      :
271      val = PGAGetMutationRealValue(ctx);
272
273***************************************************************************U*/
274double PGAGetMutationRealValue (PGAContext *ctx)
275{
276    PGADebugEntered("PGAGetMutationRealValue");
277    PGAFailIfNotSetUp("PGAGetMutationRealValue");
278
279    PGADebugExited("PGAGetMutationRealValue");
280
281    return(ctx->ga.MutateRealValue);
282}
283
284/*U****************************************************************************
285   PGASetMutationIntegerValue - Set multiplier to mutate PGA_DATATYPE_INTEGER
286   strings with.  The use of this value depends on the type of mutation
287   being used.  The default value is 1.  See the user guide for more details.
288
289   Category: Operators
290
291   Inputs:
292      ctx - context variable
293      val - the mutation value to use for Integer mutation
294
295   Outputs:
296      None
297
298   Example:
299      PGAContext *ctx;
300      :
301      PGASetMutationIntegerValue(ctx, 5);
302
303****************************************************************************U*/
304void PGASetMutationIntegerValue( PGAContext *ctx, int val)
305{
306    PGADebugEntered("PGASetMutationIntegerValue");
307
308    if (val < 0.0)
309        PGAError ( ctx,
310                  "PGASetMutationIntegerValue: Invalid value of val:",
311                   PGA_FATAL, PGA_DOUBLE, (void *) &val);
312    else
313        ctx->ga.MutateIntegerValue = val;
314
315    PGADebugExited("PGASetMutationIntegerValue");
316}
317
318
319/*U***************************************************************************
320  PGAGetMutationIntegerValue - Returns the value of the multiplier
321  used to mutate PGA_DATATYPE_INTEGER strings with.
322
323   Category: Operators
324
325   Inputs:
326      ctx - context variable
327
328   Outputs:
329      The value of the multiplier used to mutate PGA_DATATYPE_INTEGER
330      strings with
331
332   Example:
333      PGAContext *ctx;
334      int ival;
335      :
336      ival = PGAGetMutationIntegerValue(ctx);
337
338***************************************************************************U*/
339int PGAGetMutationIntegerValue (PGAContext *ctx)
340{
341    PGADebugEntered("PGAGetMutationIntegerValue");
342    PGAFailIfNotSetUp("PGAGetMutationIntegerValue");
343
344    PGADebugExited("PGAGetMutationIntegerValue");
345
346    return(ctx->ga.MutateIntegerValue);
347}
348
349/*U****************************************************************************
350   PGASetMutationBoundedFlag - If this flag is set to PGA_TRUE, then for
351   Integer and Real strings whenever a gene is mutated, if it underflows
352   (overflows) the lower (upper)bound it is reset to the lower (upper) bound.
353   In this way all allele values remain within the range the integer strings
354   were initialized on.  If this flag is PGA_FALSE (the default), the alleles
355   may take any values.
356
357   Category: Operators
358
359   Inputs:
360      ctx  - context variable
361      flag - either PGA_TRUE or PGA_FALSE
362
363   Outputs:
364      None
365
366   Example:
367      PGAContext *ctx;
368      :
369      PGASetMutationBoundedFlag(ctx, PGA_TRUE);
370
371****************************************************************************U*/
372void PGASetMutationBoundedFlag(PGAContext *ctx, int val)
373{
374    PGADebugEntered("PGASetMutationBoundedFlag");
375
376    switch (val)
377    {
378    case PGA_TRUE:
379    case PGA_FALSE:
380         ctx->ga.MutateBoundedFlag = val;
381         break;
382    default:
383         PGAError(ctx, "PGASetMutationBoundedFlag: Invalid value:",
384                  PGA_FATAL, PGA_INT, (void *) &val);
385         break;
386    }
387
388    PGADebugExited("PGASetMutationBoundedFlag");
389}
390
391
392/*U****************************************************************************
393   PGAGetMutationBoundedFlag - returns PGA_TRUE or PGA_FALSE to indicate
394   whether mutated integer strings remain in the range specified when
395   initialized with PGASetIntegerInitRange.
396
397   Category: Operators
398
399   Inputs:
400      ctx - context variable
401
402   Outputs:
403      PGA_TRUE if restricted to the given range, otherwise PGA_FALSE.
404
405   Example:
406      PGAContext *ctx;
407      int val;
408      :
409      val = PGAGetMutationBoundedFlag(ctx);
410
411****************************************************************************U*/
412int PGAGetMutationBoundedFlag(PGAContext *ctx)
413{
414    PGADebugEntered  ("PGAGetMutationBoundedFlag");
415    PGAFailIfNotSetUp("PGAGetMutationBoundedFlag");
416    PGADebugExited   ("PGAGetMutationBoundedFlag");
417    return (ctx->ga.MutateBoundedFlag);
418}
419
420
421
422/*U****************************************************************************
423   PGASetMutationProb - Specifies the probability that a given allele will
424   be mutated.  If this is called without calling PGASetMutationType(), the
425   default mutation type is PGA_MUTATION_FIXED.  The default probability is
426   the reciprocal of the string length.
427
428   Category: Operators
429
430   Inputs:
431      ctx - context variable
432      p   - the mutation probability
433
434   Outputs:
435      None
436
437   Example:
438      PGAContext *ctx;
439      :
440      PGASetMutationProb(ctx,0.001);
441
442****************************************************************************U*/
443void PGASetMutationProb(PGAContext *ctx, double mutation_prob)
444{
445    PGADebugEntered("PGASetMutationProb");
446
447    if ((mutation_prob < 0.0) || (mutation_prob > 1.0))
448        PGAError ( ctx,
449                  "PGASetMutationProb: Invalid value of mutation_prob:",
450                   PGA_FATAL, PGA_DOUBLE, (void *) &mutation_prob);
451    else
452        ctx->ga.MutationProb = mutation_prob;
453
454    PGADebugExited("PGASetMutationProb");
455}
456
457/*U***************************************************************************
458   PGAGetMutationProb - Returns the probability of mutation.
459
460   Category: Operators
461
462   Inputs:
463      ctx - context variable
464
465   Outputs:
466      The mutation probability
467
468   Example:
469      PGAContext *ctx;
470      double pm;
471      :
472      pm = PGAGetMutateProb(ctx);
473
474***************************************************************************U*/
475double PGAGetMutationProb (PGAContext *ctx)
476{
477    PGADebugEntered("PGAGetMutationProb");
478    PGAFailIfNotSetUp("PGAGetMutationProb");
479    PGADebugExited("PGAGetMutationProb");
480    return(ctx->ga.MutationProb);
481}
Note: See TracBrowser for help on using the repository browser.