source: trunk/packages/optimizer/src/pgapack/pgapack/source/pga.c @ 1202

Last change on this file since 1202 was 1202, checked in by liveletlive, 14 years ago

Changes made to include the following items:
1) Hard restart
2) SBX
3) Random population replacement proportion
4) Definitions and macros for the above functions in pgapack.h

File size: 24.0 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: pga.c: This file contains all the routines that are data structure
66*                  neutral
67*
68*     Authors: David M. Levine, Philip L. Hallstrom, and David M. Noelle,
69*              Brian P. Walenz
70*****************************************************************************/
71
72#include "pgapack.h"
73#include <setjmp.h>
74
75jmp_buf pgapack_jmpbuf;
76
77/*U****************************************************************************
78  PGARun - Highest level routine to execute the genetic algorithm.  It
79  is called after PGACreate and PGASetup have been called.
80
81  Category: Generation
82
83  Inputs:
84    ctx      - context variable
85    evaluate - a pointer to the user's evaluation function, which must
86               have the calling sequence shown in the example.
87
88  Outputs:
89    none
90
91  Example:
92    PGAContext *ctx,
93    double f(PGAContext *ctx, int p, int pop);
94    :
95    ctx = PGACreate(&argc, argv, PGA_DATATYPE_BINARY, 100, PGA_MAXIMIZE);
96    PGASetUp(ctx);
97    PGARun(ctx, f);
98    PGADestroy(ctx);
99
100****************************************************************************U*/
101void PGARun(PGAContext *ctx, double (*evaluate)(PGAContext *c, int p, int pop))
102{
103     MPI_Comm comm;                  /* value of default communicator */
104     int nprocs;                     /* number of processes in above  */
105     int npops;                      /* number of populations         */
106     int ndemes;                     /* number of demes               */
107     
108
109     PGADebugEntered("PGARun");
110     PGAFailIfNotSetUp("PGARun");
111
112     comm   = PGAGetCommunicator(ctx);
113     nprocs = PGAGetNumProcs    (ctx, comm);
114     npops  = PGAGetNumIslands  (ctx);
115     ndemes = PGAGetNumDemes    (ctx);
116
117    if (setjmp(pgapack_jmpbuf)) {
118        return;                 /* PGA pack was aborted. */
119    }
120     /**********************************************************************/
121     /*              Global model, one island, one deme                    */
122     /**********************************************************************/
123     if ( (npops == 1) && (ndemes == 1) ) {
124         PGARunGM(ctx, evaluate, comm);
125     }
126     
127     /**********************************************************************/
128     /*              Island model, > one island, one deme                  */
129     /**********************************************************************/
130     else if( (npops > 1) && (ndemes == 1) ) {
131         if ( nprocs == 1 )
132             PGAError (ctx, "PGARun: island model with one process",
133                       PGA_FATAL, PGA_VOID, (void *) &nprocs);
134         if ( nprocs != npops) {
135             PGAError (ctx, "PGARun: island model no. processes != no. pops",
136                       PGA_FATAL, PGA_VOID, (void *) &nprocs);
137         }
138         PGARunIM(ctx,evaluate,comm);
139     }
140             
141     /**********************************************************************/
142     /*              Neighborhood model, one island, > one deme            */
143     /**********************************************************************/
144     else if( (npops == 1) && (ndemes > 1) ) {
145         if ( nprocs == 1 )
146             PGAError (ctx, "PGARun: neighborhood model with one process",
147                       PGA_FATAL, PGA_VOID, (void *) &nprocs);
148         if ( nprocs != ndemes)
149             PGAError (ctx, "PGARun: neighborhood model no. processes "
150                       "!= no. demes", PGA_FATAL, PGA_VOID, (void *) &nprocs);
151         PGARunNM(ctx,evaluate,comm);
152     }
153             
154     /**********************************************************************/
155     /*              Mixed model, > one island, > one deme                 */
156     /**********************************************************************/
157     else if( (npops > 1) && (ndemes > 1) ) {
158         PGAError (ctx, "PGARun: Cannot execute mixed models",
159                   PGA_FATAL, PGA_VOID, (void *) &nprocs);
160     }
161
162     /**********************************************************************/
163     /*                        E R R O R                                   */
164     /**********************************************************************/
165     else {
166         PGAError (ctx, "PGARun: Invalid combination of numislands,"
167                   "ndemes, and nprocs.",
168                   PGA_FATAL, PGA_VOID, (void *) &nprocs);
169     }
170
171     /**********************************************************************/
172     /*                         E X I T                                    */
173     /**********************************************************************/
174     PGADebugExited("PGARun");
175     return;
176 }
177
178
179/*U****************************************************************************
180  PGARunMutationAndCrossover - Performs crossover and mutation from one
181  population to create the next.  Assumes PGASelect has been called.
182
183  Category: Generation
184
185  Inputs:
186    ctx - context variable
187    oldpop - symbolic constant of old population
188    newpop - symbolic constant of new population
189
190  Outputs:
191    newpop is modified by side-effect.
192
193  Example:
194     PGAContext *ctx,
195    :
196    PGARunMutationAndCrossover(ctx, PGA_OLDPOP, PGA_NEWPOP);
197
198****************************************************************************U*/
199void PGARunMutationAndCrossover (PGAContext *ctx, int oldpop, int newpop)
200{
201    int i, j, n, m1, m2;
202    int popsize, numreplace, randnumreplace;
203    double pc, randreplprop;
204
205    PGADebugEntered("PGARunMutationAndCrossover");
206
207    popsize = PGAGetPopSize(ctx);
208    numreplace = PGAGetNumReplaceValue(ctx);
209    randreplprop = PGAGetRandReplProp(ctx);
210    randnumreplace = (int)randreplprop*popsize;
211    /*** first, copy n best strings (sorted by fitness) to new pop ***/
212    PGASortPop( ctx, oldpop );
213    n = popsize - numreplace - randnumreplace;
214    for ( i=0; i < n; i++ ) {
215        j = PGAGetSortedPopIndex( ctx, i );
216        PGACopyIndividual ( ctx, j, oldpop, i, newpop );
217    }
218    pc = PGAGetCrossoverProb(ctx);
219    /*** reproduce to create the rest of the new population ***/
220    while ( n < (popsize- randnumreplace)) {
221        m1 = PGASelectNextIndex( ctx );
222        m2 = PGASelectNextIndex( ctx );
223        if ( PGARandomFlip(ctx, pc) ) {
224             PGACrossover ( ctx, m1, m2, oldpop, PGA_TEMP1,
225                            PGA_TEMP2, newpop);
226
227              /*** mutate and copy first string to new population ***/
228              PGAMutate ( ctx, PGA_TEMP1, newpop);
229              while (PGADuplicate( ctx, PGA_TEMP1, newpop, newpop, n))
230                   PGAChange ( ctx, PGA_TEMP1, newpop );
231              PGACopyIndividual ( ctx, PGA_TEMP1, newpop, n, newpop);
232              n++;
233
234              if ( n < (popsize-randnumreplace) ) {
235              /*** mutate and copy second string to new population ***/
236              PGAMutate ( ctx, PGA_TEMP2, newpop);
237              while ( PGADuplicate( ctx, PGA_TEMP2, newpop, newpop, n))
238                   PGAChange ( ctx, PGA_TEMP2, newpop );
239              PGACopyIndividual ( ctx, PGA_TEMP2, newpop, n, newpop);
240              n++;
241              }
242         }
243         else {
244            PGACopyIndividual ( ctx, m1, oldpop, n, newpop );
245            n++;
246            if ( n < (popsize-randnumreplace) ) {
247                PGACopyIndividual ( ctx, m2, oldpop, n, newpop );
248                n++;
249            }
250       }
251    }
252   
253    if(randnumreplace > 0){
254            while(n < popsize){
255                PGACreateIndividual(ctx,n,newpop,PGA_TRUE);
256                n++;
257            }
258    }
259
260    PGADebugExited("PGARunMutationAndCrossover");
261}
262
263
264/*U****************************************************************************
265  PGARunMutationOrCrossover - Performs crossover or mutation (but not both)
266  from one populationto create the next.  Assumes PGASelect has been called.
267
268  Category: Generation
269
270  Inputs:
271    ctx - context variable
272    oldpop - symbolic constant of old population
273    newpop - symbolic constant of new population
274
275  Outputs:
276    newpop is modified by side-effect.
277
278  Example:
279    PGAContext *ctx,
280    :
281    PGARunMutationOrCrossover(ctx, PGA_OLDPOP, PGA_NEWPOP);
282
283****************************************************************************U*/
284void PGARunMutationOrCrossover ( PGAContext *ctx, int oldpop, int newpop )
285{
286    int i, j, n, m1, m2;
287    int popsize, numreplace, randnumreplace;
288    double pc, randreplprop;
289
290    PGADebugEntered("PGARunMutationOrCrossover");
291
292    popsize = PGAGetPopSize(ctx);
293    numreplace = PGAGetNumReplaceValue(ctx);
294    randreplprop = PGAGetRandReplProp(ctx);
295    randnumreplace = (int)randreplprop*popsize;
296    /*** first, copy n best strings (sorted by fitness) to new pop ***/
297    PGASortPop( ctx, oldpop );
298    n = popsize - numreplace - randnumreplace ;
299    for ( i=0; i < n; i++ ) {
300        j = PGAGetSortedPopIndex( ctx, i );
301        PGACopyIndividual ( ctx, j, oldpop, i, newpop );
302    }
303    pc = PGAGetCrossoverProb(ctx);
304    /*** reproduce to create the rest of the new population ***/
305    while ( n < (popsize-randnumreplace) ) {
306        m1 = PGASelectNextIndex( ctx );
307        m2 = PGASelectNextIndex( ctx );
308        if ( PGARandomFlip(ctx, pc) ) {
309            PGACrossover ( ctx, m1, m2, oldpop, PGA_TEMP1,
310                                        PGA_TEMP2, newpop);
311
312            /*** copy first string to new population ***/
313            while (PGADuplicate(ctx, PGA_TEMP1, newpop,  newpop, n))
314                PGAChange ( ctx, PGA_TEMP1, newpop );
315            PGACopyIndividual ( ctx, PGA_TEMP1, newpop, n, newpop);
316            n++;
317
318            if ( n < (popsize-randnumreplace) )
319            {
320                 /*** copy second string to new population ***/
321                 while (PGADuplicate(ctx, PGA_TEMP2, newpop,  newpop, n))
322                      PGAChange ( ctx, PGA_TEMP2, newpop );
323                 PGACopyIndividual ( ctx, PGA_TEMP2, newpop, n, newpop);
324                 n++;
325            }
326        }
327        else
328        {
329             PGACopyIndividual(ctx, m1, oldpop, PGA_TEMP1, newpop);
330             PGAMutate ( ctx, PGA_TEMP1, newpop );
331             while (PGADuplicate(ctx, PGA_TEMP1, newpop, newpop, n ))
332                  PGAChange ( ctx, PGA_TEMP1, newpop );
333             PGACopyIndividual ( ctx, PGA_TEMP1, newpop, n, newpop);
334             n++;
335
336             if ( n < (popsize-randnumreplace) ) {
337                  PGACopyIndividual(ctx, m2, oldpop, PGA_TEMP2, newpop);
338                  PGAMutate ( ctx, PGA_TEMP2, newpop );
339                  while (PGADuplicate(ctx, PGA_TEMP2, newpop, newpop, n ))
340                       PGAChange ( ctx, PGA_TEMP2, newpop );
341                  PGACopyIndividual ( ctx, PGA_TEMP2, newpop, n, newpop);
342                  n++;
343             }
344        }
345    }
346   
347    if(randnumreplace > 0){
348            while(n < popsize){
349                PGACreateIndividual(ctx,n,newpop,PGA_TRUE);
350                n++;
351            }
352    }
353
354    PGADebugExited("PGARunMutationOrCrossover");
355}
356
357
358/*U****************************************************************************
359  PGAUpdateGeneration - updates internal data structures for the next
360  genetic algorithm iteration, and checks if the termination conditions, both
361  user and PGAPack, have been met.  This routine must be called by both
362  master and slave processes at the end of each GA generation.
363
364  Category: Generation
365
366  Inputs:
367     ctx  - context variable
368     comm - an MPI communicator
369
370  Outputs:
371     PGA_TRUE if the genetic algorithm has terminated, otherwise PGA_FALSE.
372
373  Example:
374    PGAContext *ctx;
375    :
376    PGAUpdateGeneration(ctx, MPI_COMM_WORLD);
377
378****************************************************************************U*/
379void PGAUpdateGeneration(PGAContext *ctx, MPI_Comm comm)
380{
381    PGAIndividual *temp;
382    int i, rank;
383
384    PGADebugEntered("PGAUpdateGeneration");
385    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAUpdateGeneration",
386                  "ga.iter = ", PGA_INT, (void *) &(ctx->ga.iter) );
387
388    rank = PGAGetRank(ctx, comm);
389
390    ctx->ga.iter++;
391
392    if (rank == 0) {
393        if (ctx->rep.PrintOptions & PGA_REPORT_AVERAGE)
394            PGAUpdateAverage(ctx, PGA_NEWPOP);
395
396        if (ctx->rep.PrintOptions & PGA_REPORT_ONLINE)
397            PGAUpdateOnline(ctx, PGA_NEWPOP);
398
399        if (ctx->rep.PrintOptions & PGA_REPORT_OFFLINE)
400            PGAUpdateOffline(ctx, PGA_NEWPOP);
401
402        if ((ctx->ga.StoppingRule & PGA_STOP_NOCHANGE) || ctx->ga.restart) {
403            i = PGAGetBestIndex(ctx, PGA_NEWPOP);
404            if (ctx->rep.Best == PGAGetEvaluation(ctx, i, PGA_NEWPOP))
405                ctx->ga.ItersOfSame++;
406            else {
407                ctx->rep.Best = PGAGetEvaluation(ctx, i, PGA_NEWPOP);
408                ctx->ga.ItersOfSame = 1;
409            }
410        }
411
412        if (ctx->ga.StoppingRule & PGA_STOP_TOOSIMILAR)
413            ctx->ga.PercentSame = PGAComputeSimilarity(ctx, ctx->ga.newpop);
414
415        /*  Clear this twice in case the user EOG calls PGASelect.  */
416        ctx->ga.SelectIndex = 0;
417
418        if (ctx->fops.EndOfGen)
419            (*ctx->fops.EndOfGen)(&ctx);
420        if (ctx->cops.EndOfGen)
421            (*ctx->cops.EndOfGen)(ctx);
422
423        ctx->ga.SelectIndex = 0;
424        temp           = ctx->ga.oldpop;
425        ctx->ga.oldpop = ctx->ga.newpop;
426        ctx->ga.newpop = temp;
427    }
428
429    PGADebugExited("PGAUpdateGeneration");
430}
431
432
433/*U***************************************************************************
434   PGAGetDataType - Returns the data type used by the given context.
435
436   Category: Generation
437
438   Inputs:
439      ctx - context variable
440
441   Outputs:
442      Returns the integer corresponding to the symbolic constant
443      used to specify the data type.
444
445   Example:
446      PGAContext *ctx;
447      int datatype;
448      :
449      datatype = PGAGetDataType(ctx);
450      switch (datatype) {
451      case PGA_DATATYPE_BINARY:
452          printf("Data Type = PGA_DATATYPE_BINARY\n");
453          break;
454      case PGA_DATATYPE_CHARACTER:
455          printf("Data Type = PGA_DATATYPE_CHARACTER\n");
456          break;
457      case PGA_DATATYPE_INTEGER:
458          printf("Data Type = PGA_DATATYPE_INTEGER\n");
459          break;
460      case PGA_DATATYPE_REAL:
461          printf("Data Type = PGA_DATATYPE_REAL\n");
462          break;
463      case PGA_DATATYPE_USER:
464          printf("Data Type = PGA_DATATYPE_USER\n");
465          break;
466      }
467
468***************************************************************************U*/
469int PGAGetDataType (PGAContext *ctx)
470{
471    PGADebugEntered("PGAGetDataType");
472
473    PGADebugExited("PGAGetDataType");
474
475    return(ctx->ga.datatype);
476}
477
478/*U***************************************************************************
479   PGAGetOptDirFlag - Returns a symbolic constant that represents the
480   direction of optimization
481
482   Category: Generation
483
484   Inputs:
485      ctx - context variable
486
487   Outputs:
488      Returns the integer corresponding to the symbolic constant
489      used to specify the  direction of optimization
490
491   Example:
492      PGAContext *ctx;
493      int optdir;
494      :
495      optdir = PGAGetOptDirFlag(ctx);
496      switch (optdir) {
497      case PGA_MAXIMIZE:
498          printf("Optimization direction = PGA_MAXIMIZE\n");
499          break;
500      case PGA_MINIMIZE:
501          printf("Optimization direction = PGA_MINIMIZE\n");
502          break;
503      }
504
505***************************************************************************U*/
506int PGAGetOptDirFlag (PGAContext *ctx)
507{
508    PGADebugEntered("PGAGetOptDirFlag");
509
510    PGADebugExited("PGAGetOptDirFlag");
511
512    return(ctx->ga.optdir);
513}
514
515/*U***************************************************************************
516   PGAGetStringLength - Returns the string length
517
518   Category: Generation
519
520   Inputs:
521      ctx - context variable
522
523   Outputs:
524      The string length
525
526   Example:
527      PGAContext *ctx;
528      int stringlen;
529      :
530      stringlen = PGAGetStringLength(ctx);
531
532***************************************************************************U*/
533int PGAGetStringLength (PGAContext *ctx)
534{
535    PGADebugEntered("PGAGetStringLength");
536
537    PGADebugExited("PGAGetStringLength");
538
539    return(ctx->ga.StringLen);
540}
541
542/*I***************************************************************************
543   PGAGetVariableStringLength - Returns the length of a variable length
544   string.
545
546   Category: Generation
547
548   Inputs:
549      ctx - context variable
550      p   - index into the population
551      pop - symbolic constant for the population
552
553   Outputs:
554      The string length
555
556   Example:
557      PGAContext *ctx;
558      int stringlen;
559      :
560      stringlen = PGAGetVariableStringLength(ctx, 0, PGA_NEWPOP);
561
562***************************************************************************I*/
563int PGAGetVariableStringLength (PGAContext *ctx, int p, int pop)
564{
565    PGADebugEntered("PGAGetVariableStringLength");
566
567    PGADebugExited("PGAGetVariableStringLength");
568
569    PGAError(ctx, "PGAGetVariableStringLength:  Variable length strings not "
570             "currently supported.", PGA_FATAL, PGA_VOID, NULL);
571#if 0
572    ind = PGAGetIndividual(ctx, p, pop);
573    return(ind->StringLength);
574#endif
575    /*  Make the compilers be quiet.  */
576    return(0);
577}
578
579/*U***************************************************************************
580  PGAGetGAIterValue - returns the number of the current genetic
581  algorithm generation
582
583   Category: Generation
584
585   Inputs:
586      ctx - context variable
587
588   Outputs:
589      The genetic algorithm generation number
590
591   Example:
592      PGAContext *ctx;
593      int g;
594      :
595      g = PGAGetGAIterValue(ctx);
596
597***************************************************************************U*/
598int PGAGetGAIterValue (PGAContext *ctx)
599{
600    PGADebugEntered("PGAGetGAIterValue");
601    PGAFailIfNotSetUp("PGAGetGAIterValue");
602
603    PGADebugExited("PGAGetGAIterValue");
604
605    return(ctx->ga.iter);
606}
607
608/*U****************************************************************************
609  PGASetMutationOrCrossoverFlag - A boolean flag to indicate if recombination
610  uses exactly one of crossover or mutation on selected strings.
611
612   Category: Generation
613
614   Inputs:
615      ctx  - context variable
616      flag - PGA_TRUE (default) or PGA_FALSE
617
618   Outputs:
619      None
620
621   Example:
622      Set the genetic algorithm to use mutation only when crossover is
623      not used.
624
625      PGAContext *ctx;
626      :
627      PGASetMutationOrCrossoverFlag(ctx,PGA_FALSE);
628
629****************************************************************************U*/
630void PGASetMutationOrCrossoverFlag( PGAContext *ctx, int flag)
631{
632    PGADebugEntered("PGASetMutationOrCrossoverFlag");
633
634     switch (flag)
635     {
636     case PGA_TRUE:
637     case PGA_FALSE:
638          ctx->ga.MutateOnlyNoCross = flag;
639          break;
640     default:
641          PGAError (ctx, "PGASetMutationOrCrossoverFlag: Invalid value of "
642                    "flag:", PGA_FATAL, PGA_INT, (void *) &flag);
643          break;
644     }
645
646    PGADebugExited("PGASetMutationOrCrossoverFlag");
647}
648
649/*U****************************************************************************
650  PGASetMutationAndCrossoverFlag - A boolean flag to indicate if
651  recombination uses both crossover and mutation on selected strings
652
653   Category: Generation
654
655   Inputs:
656      ctx  - context variable
657      flag - PGA_TRUE (default) or PGA_FALSE
658
659   Outputs:
660      None
661
662   Example:
663      Set the genetic algorithm to use both crossover and mutation when
664      reproducing strings.
665
666      PGAContext *ctx;
667      :
668      PGASetMutationAndCrossoverFlag(ctx,PGA_FALSE);
669
670****************************************************************************U*/
671void PGASetMutationAndCrossoverFlag( PGAContext *ctx, int flag)
672{
673    PGADebugEntered("PGASetMutationAndCrossoverFlag");
674
675     switch (flag)
676     {
677     case PGA_TRUE:
678     case PGA_FALSE:
679          ctx->ga.MutateOnlyNoCross = !flag;
680          break;
681     default:
682          PGAError (ctx, "PGASetMutationAndCrossoverFlag: Invalid value of "
683                    "flag:", PGA_FATAL, PGA_INT, (void *) &flag);
684          break;
685     }
686
687    PGADebugExited("PGASetMutationAndCrossoverFlag");
688}
689/*U***************************************************************************
690   PGAGetMutationOrCrossoverFlag - Returns true if mutation only occurs when
691   crossover does not.
692
693   Category: Generation
694
695   Inputs:
696      ctx - context variable
697
698   Outputs:
699      Returns PGA_TRUE if mutation only occurs when crossover does not,
700      otherwise, returns PGA_FALSE.
701
702   Example:
703      PGAContext *ctx;
704      int mutatetype;
705      :
706      mutatetype = PGAGetMutationOrCrossoverFlag(ctx);
707      switch (mutatetype) {
708      case PGA_TRUE:
709          printf("Only mutating strings not undergoing crossover\n");
710          break;
711      case PGA_FALSE:
712          printf("Mutating strings only after crossover\n");
713          break;
714      }
715
716***************************************************************************U*/
717int PGAGetMutationOrCrossoverFlag (PGAContext *ctx)
718{
719    PGADebugEntered("PGAGetMutationOrCrossoverFlag");
720    PGAFailIfNotSetUp("PGAGetMutationOrCrossoverFlag");
721
722    PGADebugExited("PGAGetMutationOrCrossoverFlag");
723
724    return(ctx->ga.MutateOnlyNoCross);
725}
726
727/*U***************************************************************************
728   PGAGetMutationAndCrossoverFlag - Returns true if mutation occurs only
729   when crossover does.
730
731   Category: Generation
732
733   Inputs:
734      ctx - context variable
735
736   Outputs:
737      Returns PGA_TRUE if mutation is applied to crossed-over strings.
738      Otherwise, returns PGA_FALSE
739
740   Example:
741      PGAContext *ctx;
742      int mutatetype;
743      :
744      mutatetype = PGAGetMutationAndCrossoverFlag(ctx);
745      switch (mutatetype) {
746      case PGA_TRUE:
747          printf("Mutating strings only after crossover\n");
748          break;
749      case PGA_FALSE:
750          printf("Only mutating strings not undergoing crossover\n");
751          break;
752      }
753
754***************************************************************************U*/
755int PGAGetMutationAndCrossoverFlag (PGAContext *ctx)
756{
757    PGADebugEntered("PGAGetMutationAndCrossoverFlag");
758    PGAFailIfNotSetUp("PGAGetMutationAndCrossoverFlag");
759
760    PGADebugExited("PGAGetMutationAndCrossoverFlag");
761
762    return(!ctx->ga.MutateOnlyNoCross);
763}
764
Note: See TracBrowser for help on using the repository browser.