source: branches/1.4/packages/optimizer/src/pgapack/gekco/pgapack/source/create.c @ 5674

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

merge r5673 from trunk (eol-style)

  • Property svn:eol-style set to native
File size: 35.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: create.c: This file contains functions to create and initialize
66*                     data structures and populations.
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  PGACreate - creates an uninitialized context variable.  The Fortran version
76  of this function call contains only the last three arguments
77
78  Category: Generation
79
80  Inputs:
81     argc     - address of the count of the number of command line arguments.
82     argv     - array of command line arguments.
83     datatype - the data type used for the strings.  Must be one of
84                PGA_DATATYPE_BINARY, PGA_DATATYPE_CHARACTER,
85                PGA_DATATYPE_INTEGER, PGA_DATATYPE_REAL, or PGA_DATATYPE_USER
86                to specify binary-valued, character-valued, integer-valued,
87                real-valued, or a user-defined datatype, respectively.
88     len      - the string length (number of genes).
89     maxormin - the direction of optimization. Must be one of PGA_MAXIMIZE or
90                PGA_MINIMIZE for maximization or minimization, respectively.
91
92  Outputs:
93     A pointer to the context variable.
94
95  Example:
96
97     In C:
98     void main(int argc, char **argv) {
99         PGAContext *ctx;
100         :
101         ctx = PGACreate(&argc, argv, PGA_DATATYPE_BINARY, 100, PGA_MAXIMIZE);
102         :
103         //  Set options here
104         :
105         PGASetUp(ctx);
106         :
107         //  Run the GA here
108         :
109         PGADestroy(ctx);
110     }
111
112     In FORTRAN:
113             integer ctx
114             :
115             ctx = PGACreate(PGA_DATATYPE_BINARY, 100, PGA_MAXIMIZE)
116             :
117     c       Set options here
118             :
119             call PGASetUp(ctx)
120             :
121     c       Run the GA here
122             :
123             call PGADestroy(ctx)
124             stop
125             end
126
127****************************************************************************U*/
128PGAContext *PGACreate ( int *argc, char **argv,
129                        int datatype, int len, int maxormin )
130{
131    int i;
132    PGAContext *ctx;
133
134    /* gekco purify ctx = (PGAContext *) malloc ( sizeof(PGAContext) ); */
135    ctx = (PGAContext *) calloc ( 1, sizeof(PGAContext) );
136
137    /*  We cannot make PGA calls until we sort the FuncNameIndex below,
138     *  so we just manually print the (rather severe) error message.
139     */
140    if( ctx == NULL ) {
141        fprintf(stderr, "PGACreate: No room to allocate ctx\n");
142        exit(-1);
143    }
144
145   
146    /*  We use this (indirectly) in PGAReadCmdLine -- in processing
147     *  -pgahelp and -pgahelp debug.
148     */
149    MPI_Initialized (&ctx->par.MPIAlreadyInit);
150
151    /* Initialize MPI, only if it isn't already running (fortran)  */
152    if (!ctx->par.MPIAlreadyInit)
153         MPI_Init (argc, &argv);
154
155
156#if OPTIMIZE==0
157    /*  Sort the FuncNameIndex.  This allows us to use a binary search
158     *  for finding the function names.
159     */
160    PGASortFuncNameIndex(ctx);
161#endif
162
163    /* Initialize debug flags, then parse command line arguments.  */
164    for (i=0; i<PGA_DEBUG_MAXFLAGS; i++)
165        ctx->debug.PGADebugFlags[i] = PGA_FALSE;
166    PGAReadCmdLine( ctx, argc, argv );
167
168
169    /*  The context variable is now initialized enough to allow this
170     *  call to complete successfully.
171     */
172    PGADebugEntered("PGACreate");
173
174    /* required parameter 1: abstract data type */
175    switch (datatype)
176    {
177    case PGA_DATATYPE_BINARY:
178    case PGA_DATATYPE_INTEGER:
179    case PGA_DATATYPE_REAL:
180    case PGA_DATATYPE_CHARACTER:
181    case PGA_DATATYPE_USER:
182         ctx->ga.datatype  = datatype;
183         break;
184    default:
185         PGAError( ctx, "PGACreate: Invalid value of datatype:",
186                  PGA_FATAL, PGA_INT, (void *) &datatype );
187    };
188
189    /* required parameter 2: string string length */
190    if (len <= 1)
191        PGAError( ctx,  "PGACreate: Invalid value of len:",
192                  PGA_FATAL, PGA_INT, (void *) &len );
193    else
194        ctx->ga.StringLen = len;
195
196
197    /* required parameter 3: optimization direction */
198    switch (maxormin) {
199        case PGA_MAXIMIZE:
200        case PGA_MINIMIZE:
201          ctx->ga.optdir = maxormin;
202          break;
203        default:
204          PGAError( ctx, "PGACreate: Invalid value of optdir:",
205                    PGA_FATAL, PGA_INT, (void *) &maxormin );
206    };
207
208
209    /*  For datatype == PGA_DATATYPE_BINARY, set how many full words
210     *  are used in the packed representation, and how many extra bits
211     *  this leaves us with.  Finally, set how many total words are used;
212     *  if there are no extra bits, this is just the number of full words,
213     *  else, there is one more word used than the number of full words.
214     */
215    switch (datatype) {
216    case PGA_DATATYPE_BINARY:
217        ctx->ga.fw = ctx->ga.StringLen/WL;
218        ctx->ga.eb = ctx->ga.StringLen%WL;
219        if ( ctx->ga.eb == 0 )
220            ctx->ga.tw = ctx->ga.fw;
221        else
222            ctx->ga.tw = ctx->ga.fw+1;
223        break;
224    default:
225        ctx->ga.fw = PGA_UNINITIALIZED_INT;
226        ctx->ga.eb = PGA_UNINITIALIZED_INT;
227        ctx->ga.tw = PGA_UNINITIALIZED_INT;
228        break;
229    }
230
231    /*  Clear all the setting.  Later on, PGASetUp() will be called, and then
232     *  it will notice which setting are uninitialized, and set them to the
233     *  default value.
234     */
235    ctx->ga.PopSize            = PGA_UNINITIALIZED_INT;
236    ctx->ga.StoppingRule       = PGA_STOP_MAXITER;
237    ctx->ga.MaxIter            = PGA_UNINITIALIZED_INT;
238    ctx->ga.MaxNoChange        = PGA_UNINITIALIZED_INT;
239    ctx->ga.MaxSimilarity      = PGA_UNINITIALIZED_INT;
240    ctx->ga.NumReplace         = PGA_UNINITIALIZED_INT;
241    ctx->ga.CrossoverType      = PGA_UNINITIALIZED_INT;
242    ctx->ga.SelectType         = PGA_UNINITIALIZED_INT;
243    ctx->ga.FitnessType        = PGA_UNINITIALIZED_INT;
244    ctx->ga.FitnessMinType     = PGA_UNINITIALIZED_INT;
245    ctx->ga.MutationType       = PGA_UNINITIALIZED_INT;
246    ctx->ga.MutateOnlyNoCross  = PGA_UNINITIALIZED_INT;
247    ctx->ga.MutateRealValue    = PGA_UNINITIALIZED_DOUBLE;
248    ctx->ga.MutateIntegerValue = PGA_UNINITIALIZED_INT;
249    ctx->ga.MutateBoundedFlag  = PGA_UNINITIALIZED_INT;
250    ctx->ga.NoDuplicates       = PGA_UNINITIALIZED_INT;
251    ctx->ga.MutationProb       = PGA_UNINITIALIZED_DOUBLE;
252    ctx->ga.CrossoverProb      = PGA_UNINITIALIZED_DOUBLE;
253    ctx->ga.UniformCrossProb   = PGA_UNINITIALIZED_DOUBLE;
254    ctx->ga.PTournamentProb    = PGA_UNINITIALIZED_DOUBLE;
255    ctx->ga.FitnessRankMax     = PGA_UNINITIALIZED_DOUBLE;
256    ctx->ga.FitnessCmaxValue   = PGA_UNINITIALIZED_DOUBLE;
257    ctx->ga.PopReplace         = PGA_UNINITIALIZED_INT;
258    ctx->ga.iter               = 0;
259    ctx->ga.ItersOfSame        = 0;
260    ctx->ga.PercentSame        = 0;
261    ctx->ga.selected           = NULL;
262    ctx->ga.SelectIndex        = 0;
263    ctx->ga.restart            = PGA_UNINITIALIZED_INT;
264    ctx->ga.restartFreq        = PGA_UNINITIALIZED_INT;
265    ctx->ga.restartAlleleProb  = PGA_UNINITIALIZED_DOUBLE;
266
267
268    /*****************************
269     *  EHW Addition             *
270     *****************************/
271    ctx->inputex                = NULL;
272
273
274
275    /* Operations */
276    ctx->cops.CreateString      = NULL;
277    ctx->cops.Mutation          = NULL;
278    ctx->cops.Crossover         = NULL;
279    ctx->cops.PrintString       = NULL;
280    ctx->cops.CopyString        = NULL;
281    ctx->cops.Duplicate         = NULL;
282    ctx->cops.InitString        = NULL;
283    ctx->cops.BuildDatatype     = NULL;
284    ctx->cops.StopCond           = NULL;
285    ctx->cops.EndOfGen          = NULL;
286
287    /*****************************
288     *  EHW Addition             *
289     *****************************/
290    ctx->cops.SetTemp           = NULL;
291    ctx->cops.EndOfRepFcn       = NULL;
292    ctx->cops.DestroyUD         = NULL;
293    ctx->cops.InitUserCtx       = NULL;
294    ctx->cops.KillUserCtx       = NULL;
295
296
297
298    ctx->fops.Mutation          = NULL;
299    ctx->fops.Crossover         = NULL;
300    ctx->fops.PrintString       = NULL;
301    ctx->fops.CopyString        = NULL;
302    ctx->fops.Duplicate         = NULL;
303    ctx->fops.InitString        = NULL;
304    ctx->fops.StopCond          = NULL;
305    ctx->fops.EndOfGen          = NULL;
306
307    /*****************************
308     *  EHW Addition             *
309     *****************************/
310    ctx->fops.SetTemp           = NULL;
311    ctx->fops.EndOfRepFcn       = NULL;
312    ctx->fops.DestroyUD         = NULL;
313    ctx->fops.InitUserCtx       = NULL;
314    ctx->fops.KillUserCtx       = NULL;
315
316
317    /* Parallel */
318    ctx->par.NumIslands        = PGA_UNINITIALIZED_INT;
319    ctx->par.NumDemes          = PGA_UNINITIALIZED_INT;
320    ctx->par.DefaultComm       = (MPI_Comm) NULL;
321#ifdef FAKE_MPI
322    ctx->par.MPIStubLibrary    = PGA_TRUE;
323#else
324    ctx->par.MPIStubLibrary    = PGA_FALSE;
325#endif
326
327    /* Reporting */
328    ctx->rep.PrintFreq         = PGA_UNINITIALIZED_INT;
329    ctx->rep.PrintOptions      = 0;
330    ctx->rep.Online            = 0;
331    ctx->rep.Offline           = 0;
332    ctx->rep.Best              = PGA_UNINITIALIZED_DOUBLE;
333    ctx->rep.starttime         = PGA_UNINITIALIZED_INT;
334
335    /* System
336     *
337     *  If ctx->sys.UserFortran is not set to PGA_TRUE in pgacreate_ (the
338     *  fortran stub to PGACreate), the user program is in C.
339     */
340    if (ctx->sys.UserFortran != PGA_TRUE)
341         ctx->sys.UserFortran  = PGA_FALSE;
342    ctx->sys.SetUpCalled       = PGA_FALSE;
343    ctx->sys.PGAMaxInt         = INT_MAX;
344    ctx->sys.PGAMinInt         = INT_MIN;
345    ctx->sys.PGAMaxDouble      = DBL_MAX;
346    ctx->sys.PGAMinDouble      = DBL_MIN;
347
348    /* Debug */
349    /* Set above before parsing command line arguments */
350
351    /* Initialization */
352    ctx->init.RandomInit        = PGA_UNINITIALIZED_INT;
353    ctx->init.BinaryProbability = PGA_UNINITIALIZED_DOUBLE;
354    ctx->init.RealType          = PGA_UNINITIALIZED_INT;
355    ctx->init.IntegerType       = PGA_UNINITIALIZED_INT;
356    ctx->init.CharacterType     = PGA_UNINITIALIZED_INT;
357    ctx->init.RandomSeed        = PGA_UNINITIALIZED_INT;
358
359    /*  Allocate and clear arrays to define the minimum and maximum values
360     *  allowed by integer and real datatypes.
361     */
362    switch (datatype)
363    {
364    case PGA_DATATYPE_INTEGER:
365         ctx->init.IntegerMax = (int *) malloc(len * sizeof(PGAInteger));
366         if (!ctx->init.IntegerMax)
367              PGAError(ctx, "PGACreate: No room to allocate:", PGA_FATAL,
368                       PGA_CHAR, (void *) "ctx->init.IntegerMax");
369         ctx->init.IntegerMin = (int *) malloc(len * sizeof(PGAInteger));
370         if (!ctx->init.IntegerMin)
371              PGAError(ctx, "PGACreate: No room to allocate:", PGA_FATAL,
372                       PGA_CHAR, (void *) "ctx->init.IntegerMin");
373         ctx->init.RealMax = NULL;
374         ctx->init.RealMin = NULL;
375         for (i = 0; i < len; i++)
376         {
377              ctx->init.IntegerMin[i] = PGA_UNINITIALIZED_INT;
378              ctx->init.IntegerMax[i] = PGA_UNINITIALIZED_INT;
379         }
380         break;
381    case PGA_DATATYPE_REAL:
382         ctx->init.RealMax = (PGAReal *) malloc(len * sizeof(PGAReal));
383         if (!ctx->init.RealMax)
384              PGAError(ctx, "PGACreate: No room to allocate:", PGA_FATAL,
385                       PGA_CHAR, (void *) "ctx->init.RealMax");
386         ctx->init.RealMin = (PGAReal *) malloc(len * sizeof(PGAReal));
387         if (!ctx->init.RealMin)
388              PGAError(ctx, "PGACreate: No room to allocate:", PGA_FATAL,
389                       PGA_CHAR, (void *) "ctx->init.RealMin");
390         ctx->init.IntegerMax = NULL;
391         ctx->init.IntegerMin = NULL;
392         for (i = 0; i < len; i++)
393         {
394              ctx->init.RealMin[i] = PGA_UNINITIALIZED_DOUBLE;
395              ctx->init.RealMax[i] = PGA_UNINITIALIZED_DOUBLE;
396         }
397         break;
398    default:
399         ctx->init.RealMax = NULL;
400         ctx->init.RealMin = NULL;
401         ctx->init.IntegerMax = NULL;
402         ctx->init.IntegerMin = NULL;
403         break;
404    }
405
406    PGADebugExited("PGACreate");
407
408    return(ctx);
409}
410
411
412/*U****************************************************************************
413  PGASetUp - set all uninitialized variables to default values and initialize
414  some internal arrays.  Must be called after PGACreate() and before the GA
415  is started.
416
417  Category: Generation
418
419  Inputs:
420     ctx - context variable
421
422  Outputs:
423     Uninitialized values in the context variable are set to defaults, and
424     set values are checked for legality.
425
426  Example:
427     PGAContext *ctx;
428     :
429     PGACreate(ctx, ...);
430     :
431     //  Set options here
432     :
433     PGASetUp(ctx);
434
435****************************************************************************U*/
436void PGASetUp ( PGAContext *ctx )
437{
438    /*  These are for temporary storage of datatype specific functions.
439     *  They allow some (understatement of the yesr!!) cleaning of the
440     *  code below.
441     */
442    void         (*CreateString)(PGAContext *, int, int, int);
443    int          (*Mutation)(PGAContext *, int, int, double);
444    void         (*Crossover)(PGAContext *, int, int, int, int, int, int);
445    void         (*PrintString)(PGAContext *, FILE *, int, int);
446    void         (*CopyString)(PGAContext *, int, int, int, int);
447    int          (*Duplicate)(PGAContext *, int, int, int, int);
448    void         (*InitString)(PGAContext *, int, int);
449    MPI_Datatype (*BuildDatatype)(PGAContext *, int, int);
450    int err=0, i;
451
452    PGADebugEntered("PGASetUp");
453    PGAFailIfSetUp("PGASetUp");
454
455    ctx->sys.SetUpCalled = PGA_TRUE;
456
457    if ( ctx->ga.datatype           == PGA_DATATYPE_BINARY   &&
458         ctx->ga.tw                 == PGA_UNINITIALIZED_INT )
459      PGAError( ctx,
460               "PGASetUp: Binary: Total Words (ctx->ga.tw) == UNINITIALIZED?",
461               PGA_FATAL, PGA_INT, (void *) &ctx->ga.tw );
462
463    if ( ctx->ga.datatype           == PGA_DATATYPE_BINARY  &&
464         ctx->ga.fw                 == PGA_UNINITIALIZED_INT )
465      PGAError( ctx,
466               "PGASetUp: Binary: Full Words (ctx->ga.fw) == UNINITIALIZED?",
467               PGA_FATAL, PGA_INT,  (void *) &ctx->ga.fw );
468
469    if ( ctx->ga.datatype           == PGA_DATATYPE_BINARY  &&
470         ctx->ga.eb                 == PGA_UNINITIALIZED_INT )
471      PGAError( ctx,
472               "PGASetUp: Binary: Empty Bits (ctx->ga.eb) == UNINITIALIZED?",
473               PGA_FATAL, PGA_INT, (void *) &ctx->ga.eb );
474
475    if ( ctx->ga.PopSize            == PGA_UNINITIALIZED_INT)
476      ctx->ga.PopSize                = 100;
477
478    if ( ctx->ga.MaxIter            == PGA_UNINITIALIZED_INT)
479         ctx->ga.MaxIter             = 1000;
480
481    if ( ctx->ga.MaxNoChange        == PGA_UNINITIALIZED_INT)
482         ctx->ga.MaxNoChange         = 100;
483
484    if ( ctx->ga.MaxSimilarity      == PGA_UNINITIALIZED_INT)
485         ctx->ga.MaxSimilarity       = 95;
486
487    if ( ctx->ga.NumReplace         == PGA_UNINITIALIZED_INT)
488         ctx->ga.NumReplace          = (int) ceil(ctx->ga.PopSize * 0.1);
489
490    if ( ctx->ga.NumReplace          > ctx->ga.PopSize)
491         PGAError(ctx, "PGASetUp: NumReplace > PopSize",
492                  PGA_FATAL, PGA_VOID, NULL);
493
494    if ( ctx->ga.CrossoverType      == PGA_UNINITIALIZED_INT)
495         ctx->ga.CrossoverType       = PGA_CROSSOVER_TWOPT;
496
497    if (ctx->ga.CrossoverType       == PGA_CROSSOVER_TWOPT &&
498        ctx->ga.StringLen == 2)
499         PGAError(ctx, "PGASetUp: Invalid Crossover type for string of length "
500                  "2", PGA_FATAL, PGA_INT, (void *) &ctx->ga.CrossoverType);
501
502    if ( ctx->ga.SelectType        == PGA_UNINITIALIZED_INT)
503         ctx->ga.SelectType         = PGA_SELECT_TOURNAMENT;
504
505    if ( ctx->ga.FitnessType       == PGA_UNINITIALIZED_INT)
506         ctx->ga.FitnessType        = PGA_FITNESS_RAW;
507
508    if ( ctx->ga.FitnessMinType    == PGA_UNINITIALIZED_INT)
509         ctx->ga.FitnessMinType     = PGA_FITNESSMIN_CMAX;
510
511    if ( ctx->ga.MutateOnlyNoCross == PGA_UNINITIALIZED_INT)
512         ctx->ga.MutateOnlyNoCross  = PGA_TRUE;
513
514    if ( ctx->ga.MutationProb      == PGA_UNINITIALIZED_DOUBLE)
515         ctx->ga.MutationProb       = 1. / ctx->ga.StringLen;
516
517    if ( ctx->ga.MutationType      == PGA_UNINITIALIZED_INT) {
518        switch (ctx->ga.datatype) {
519        case PGA_DATATYPE_BINARY:
520        case PGA_DATATYPE_CHARACTER:
521        case PGA_DATATYPE_USER:
522             /* leave PGA_UNINITIALIZED_INT for these data types */
523             break;
524        case PGA_DATATYPE_REAL:
525             ctx->ga.MutationType   = PGA_MUTATION_GAUSSIAN;
526             break;
527        case PGA_DATATYPE_INTEGER:
528             switch (ctx->init.IntegerType) {
529                 case PGA_UNINITIALIZED_INT:
530                 case PGA_IINIT_PERMUTE:
531                     ctx->ga.MutationType   = PGA_MUTATION_PERMUTE;
532                     break;
533                 case PGA_IINIT_RANGE:
534                     ctx->ga.MutationType   = PGA_MUTATION_RANGE;
535                     break;
536             }
537             break;
538        default:
539             PGAError( ctx, "PGASetup: Invalid value of ctx->ga.datatype:",
540                       PGA_FATAL, PGA_INT, (void *) &(ctx->ga.datatype) );
541         }
542    }
543
544    if (ctx->ga.MutateRealValue   == PGA_UNINITIALIZED_DOUBLE) {
545        switch (ctx->ga.MutationType) {
546        case PGA_MUTATION_GAUSSIAN:
547            ctx->ga.MutateRealValue   = 0.1;
548            break;
549        case PGA_MUTATION_UNIFORM:
550            ctx->ga.MutateRealValue   = 0.1;
551            break;
552        case PGA_MUTATION_CONSTANT:
553            ctx->ga.MutateRealValue   = 0.01;
554            break;
555        case PGA_MUTATION_RANGE:
556        default:
557            ctx->ga.MutateRealValue   = 0.0;
558        }
559    }
560
561    if ( ctx->ga.MutateIntegerValue == PGA_UNINITIALIZED_INT)
562         ctx->ga.MutateIntegerValue  = 1;
563
564    if ( ctx->ga.MutateBoundedFlag == PGA_UNINITIALIZED_INT)
565         ctx->ga.MutateBoundedFlag  = PGA_FALSE;
566
567    if ( ctx->ga.NoDuplicates      == PGA_UNINITIALIZED_INT)
568         ctx->ga.NoDuplicates       = PGA_FALSE;
569
570    if ( ctx->ga.NoDuplicates && ((ctx->ga.StoppingRule & PGA_STOP_TOOSIMILAR)
571                                   == PGA_STOP_TOOSIMILAR))
572         PGAError(ctx, "PGASetUp: No Duplicates inconsistent with Stopping "
573                  "Rule:", PGA_FATAL, PGA_INT, (void *) &ctx->ga.StoppingRule);
574
575    if ( ctx->ga.CrossoverProb     == PGA_UNINITIALIZED_DOUBLE)
576         ctx->ga.CrossoverProb      = 0.85;
577
578    if ( ctx->ga.UniformCrossProb  == PGA_UNINITIALIZED_DOUBLE)
579         ctx->ga.UniformCrossProb   = 0.6;
580
581    if ( ctx->ga.PTournamentProb   == PGA_UNINITIALIZED_DOUBLE)
582         ctx->ga.PTournamentProb    = 0.6;
583
584    if ( ctx->ga.FitnessRankMax    == PGA_UNINITIALIZED_DOUBLE)
585         ctx->ga.FitnessRankMax     = 1.2;
586
587    if ( ctx->ga.FitnessCmaxValue  == PGA_UNINITIALIZED_DOUBLE)
588         ctx->ga.FitnessCmaxValue   = 1.01;
589
590    if ( ctx->ga.PopReplace        == PGA_UNINITIALIZED_INT)
591         ctx->ga.PopReplace         = PGA_POPREPL_BEST;
592
593    if ( ctx->ga.restart           == PGA_UNINITIALIZED_INT)
594         ctx->ga.restart            = PGA_FALSE;
595
596    if ( ctx->ga.restartFreq       == PGA_UNINITIALIZED_INT)
597         ctx->ga.restartFreq        = 50;
598
599    if ( ctx->ga.restartAlleleProb == PGA_UNINITIALIZED_DOUBLE)
600         ctx->ga.restartAlleleProb = 0.5;
601
602
603/* ops */
604    /*  If no user supplied "done" function, use the built in one.
605     *  No need to check EndOfGen; they only get called if they
606     *  are defined.
607     */
608    if (((void *)ctx->cops.StopCond == (void *)PGADone) ||
609        ((void *)ctx->fops.StopCond == (void *)PGADone))
610        PGAError( ctx,
611                 "PGASetUp: Using PGADone as the user stopping condition will"
612                 " result in an infinite loop!", PGA_FATAL, PGA_VOID, NULL);
613
614    switch (ctx->ga.datatype) {
615    case PGA_DATATYPE_BINARY:
616        CreateString  = PGABinaryCreateString;
617        BuildDatatype = PGABinaryBuildDatatype;
618        Mutation      = PGABinaryMutation;
619
620        switch (ctx->ga.CrossoverType) {
621          case PGA_CROSSOVER_ONEPT:
622            Crossover  = PGABinaryOneptCrossover;
623            break;
624          case PGA_CROSSOVER_TWOPT:
625            Crossover  = PGABinaryTwoptCrossover;
626            break;
627          case PGA_CROSSOVER_UNIFORM:
628            Crossover  = PGABinaryUniformCrossover;
629            break;
630        }
631        PrintString    = PGABinaryPrintString;
632        CopyString     = PGABinaryCopyString;
633        Duplicate      = PGABinaryDuplicate;
634        InitString     = PGABinaryInitString;
635        break;
636      case PGA_DATATYPE_INTEGER:
637        CreateString   = PGAIntegerCreateString;
638        BuildDatatype  = PGAIntegerBuildDatatype;
639        Mutation       = PGAIntegerMutation;
640        switch (ctx->ga.CrossoverType) {
641          case PGA_CROSSOVER_ONEPT:
642            Crossover  = PGAIntegerOneptCrossover;
643            break;
644          case PGA_CROSSOVER_TWOPT:
645            Crossover  = PGAIntegerTwoptCrossover;
646            break;
647          case PGA_CROSSOVER_UNIFORM:
648            Crossover  = PGAIntegerUniformCrossover;
649            break;
650        }
651        PrintString    = PGAIntegerPrintString;
652        CopyString     = PGAIntegerCopyString;
653        Duplicate      = PGAIntegerDuplicate;
654        InitString     = PGAIntegerInitString;
655        break;
656      case PGA_DATATYPE_REAL:
657        CreateString   = PGARealCreateString;
658        BuildDatatype  = PGARealBuildDatatype;
659        Mutation       = PGARealMutation;
660        switch (ctx->ga.CrossoverType) {
661          case PGA_CROSSOVER_ONEPT:
662            Crossover  = PGARealOneptCrossover;
663            break;
664          case PGA_CROSSOVER_TWOPT:
665            Crossover  = PGARealTwoptCrossover;
666            break;
667          case PGA_CROSSOVER_UNIFORM:
668            Crossover  = PGARealUniformCrossover;
669            break;
670        }
671        PrintString    = PGARealPrintString;
672        CopyString     = PGARealCopyString;
673        Duplicate      = PGARealDuplicate;
674        InitString     = PGARealInitString;
675        break;
676      case PGA_DATATYPE_CHARACTER:
677        CreateString   = PGACharacterCreateString;
678        BuildDatatype  = PGACharacterBuildDatatype;
679        Mutation       = PGACharacterMutation;
680        switch (ctx->ga.CrossoverType) {
681          case PGA_CROSSOVER_ONEPT:
682            Crossover  = PGACharacterOneptCrossover;
683            break;
684          case PGA_CROSSOVER_TWOPT:
685            Crossover  = PGACharacterTwoptCrossover;
686            break;
687          case PGA_CROSSOVER_UNIFORM:
688            Crossover  = PGACharacterUniformCrossover;
689            break;
690        }
691        PrintString    = PGACharacterPrintString;
692        CopyString     = PGACharacterCopyString;
693        Duplicate      = PGACharacterDuplicate;
694        InitString     = PGACharacterInitString;
695        break;
696      case PGA_DATATYPE_USER:
697        if (ctx->cops.CreateString == NULL)
698            PGAError( ctx,
699                     "PGASetUp: User datatype needs CreateString function:",
700                     PGA_WARNING, PGA_INT, (void *) &err );
701        if (ctx->cops.Mutation     == NULL)
702            PGAError( ctx,
703                     "PGASetUp: User datatype needs Mutation function:",
704                     PGA_WARNING, PGA_INT, (void *) &err );
705        if (ctx->cops.Crossover    == NULL)
706            PGAError( ctx,
707                     "PGASetUp: User datatype needs Crossover function:",
708                     PGA_WARNING, PGA_INT, (void *) &err );
709        if (ctx->cops.PrintString  == NULL)
710            PGAError( ctx,
711                     "PGASetUp: User datatype needs PrintString function:",
712                     PGA_WARNING, PGA_INT, (void *) &err );
713        if (ctx->cops.Duplicate    == NULL)
714            PGAError( ctx,
715                     "PGASetUp: User datatype needs Duplicate function:",
716                     PGA_WARNING, PGA_INT, (void *) &err );
717        if (ctx->cops.CopyString    == NULL)
718            PGAError( ctx,
719                     "PGASetUp: User datatype needs CopyString function:",
720                     PGA_WARNING, PGA_INT, (void *) &err );
721        if (ctx->cops.BuildDatatype == NULL)
722             PGAError(ctx,
723                      "PGASetUp: User datatype needs BuildDatatype "
724                      "function:", PGA_FATAL, PGA_INT, (void *) &err );
725        break;
726    }
727    if ((ctx->cops.Mutation     == NULL) && (ctx->fops.Mutation    == NULL))
728        ctx->cops.Mutation      = Mutation;
729    if ((ctx->cops.Crossover    == NULL) && (ctx->fops.Crossover   == NULL))
730        ctx->cops.Crossover     = Crossover;
731    if ((ctx->cops.PrintString  == NULL) && (ctx->fops.PrintString == NULL))
732        ctx->cops.PrintString   = PrintString;
733    if ((ctx->cops.Duplicate    == NULL) && (ctx->fops.Duplicate   == NULL))
734        ctx->cops.Duplicate     = Duplicate;
735    if ((ctx->cops.InitString   == NULL) && (ctx->fops.InitString  == NULL))
736        ctx->cops.InitString    = InitString;
737    if (ctx->cops.CreateString  == NULL)
738        ctx->cops.CreateString  = CreateString;
739    if (ctx->cops.CopyString    == NULL)
740        ctx->cops.CopyString    = CopyString;
741    if (ctx->cops.BuildDatatype == NULL)
742        ctx->cops.BuildDatatype = BuildDatatype;
743   
744/* par */
745    if ( ctx->par.NumIslands       == PGA_UNINITIALIZED_INT)
746         ctx->par.NumIslands        = 1;
747    if ( ctx->par.NumDemes         == PGA_UNINITIALIZED_INT)
748         ctx->par.NumDemes          = 1;
749    if ( ctx->par.DefaultComm      == (MPI_Comm) NULL )
750         ctx->par.DefaultComm       = MPI_COMM_WORLD;
751
752   
753
754/* rep */
755    if ( ctx->rep.PrintFreq == PGA_UNINITIALIZED_INT)
756         ctx->rep.PrintFreq  = 10;
757
758/* sys */
759    /* no more sets necessary here. */
760
761/* debug */
762
763/* init */
764    if ( ctx->init.RandomInit == PGA_UNINITIALIZED_INT)
765         ctx->init.RandomInit  = PGA_TRUE;
766
767    if ( ctx->init.BinaryProbability == PGA_UNINITIALIZED_DOUBLE)
768         ctx->init.BinaryProbability  = 0.5;
769
770    if ( ctx->init.RealType == PGA_UNINITIALIZED_INT)
771         ctx->init.RealType  = PGA_RINIT_RANGE;
772    if ( ctx->init.IntegerType == PGA_UNINITIALIZED_INT)
773         ctx->init.IntegerType  = PGA_IINIT_PERMUTE;
774    if ( ctx->init.CharacterType == PGA_UNINITIALIZED_INT)
775         ctx->init.CharacterType = PGA_CINIT_LOWER;
776
777    switch (ctx->ga.datatype)
778    {
779    case PGA_DATATYPE_INTEGER:
780         for (i = 0; i < ctx->ga.StringLen; i++)
781         {
782              if (ctx->init.IntegerMin[i] == PGA_UNINITIALIZED_INT)
783                   ctx->init.IntegerMin[i] = 0;
784              if (ctx->init.IntegerMax[i] == PGA_UNINITIALIZED_INT)
785                   ctx->init.IntegerMax[i] = ctx->ga.StringLen - 1;
786         }
787         break;
788    case PGA_DATATYPE_REAL:
789         for (i = 0; i < ctx->ga.StringLen; i++)
790         {
791              if (ctx->init.RealMin[i] == PGA_UNINITIALIZED_DOUBLE)
792                   ctx->init.RealMin[i] = 0.;
793              if (ctx->init.RealMax[i] == PGA_UNINITIALIZED_DOUBLE)
794                   ctx->init.RealMax[i] = 1.;
795         }
796         break;
797    }
798
799    /* If a seed was not specified, get one from a time of day call */
800    if ( ctx->init.RandomSeed == PGA_UNINITIALIZED_INT)
801         ctx->init.RandomSeed = (int)time(NULL);
802
803    /* seed random number generator with this process' unique seed */
804    ctx->init.RandomSeed += PGAGetRank(ctx, MPI_COMM_WORLD);
805    PGARandom01( ctx, ctx->init.RandomSeed );
806
807    ctx->ga.selected        = (int *)malloc( sizeof(int) * ctx->ga.PopSize );
808    if (ctx->ga.selected == NULL)
809         PGAError(ctx, "PGASetUp: No room to allocate ctx->ga.selected",
810                  PGA_FATAL, PGA_VOID, NULL);
811
812    ctx->ga.sorted          = (int *)malloc( sizeof(int) * ctx->ga.PopSize );
813    if (ctx->ga.sorted == NULL)
814         PGAError(ctx, "PGASetUp: No room to allocate ctx->ga.sorted",
815                  PGA_FATAL, PGA_VOID, NULL);
816
817    ctx->scratch.intscratch = (int *)malloc( sizeof(int) * ctx->ga.PopSize );
818    if (ctx->scratch.intscratch == NULL)
819         PGAError(ctx, "PGASetUp: No room to allocate ctx->scratch.intscratch",
820                  PGA_FATAL, PGA_VOID, NULL);
821
822    ctx->scratch.dblscratch = (double *)malloc(sizeof(double) * ctx->ga.PopSize);
823    if (ctx->scratch.dblscratch == NULL)
824         PGAError(ctx, "PGASetUp: No room to allocate ctx->scratch.dblscratch",
825                  PGA_FATAL, PGA_VOID, NULL);
826
827    PGACreatePop ( ctx , PGA_OLDPOP );
828    PGACreatePop ( ctx , PGA_NEWPOP );
829
830    ctx->rep.starttime = time(NULL);
831
832    PGADebugExited("PGASetUp");
833}
834
835/*U****************************************************************************
836   PGASetRandomInitFlag - A boolean flag to indicate whether to randomly
837   initialize alleles.  Legal values are PGA_TRUE and PGA_FALSE.  Default
838   is PGA_TRUE -- randomly initialize alleles.
839
840   Category: Initialization
841
842   Inputs:
843      ctx  - context variable
844      flag - either PGA_TRUE or PGA_FALSE
845
846   Outputs:
847      None
848
849   Example:
850      Set the initialization routine to initialize all alleles to zero
851
852      PGAContext *ctx;
853      :
854      PGASetRandomInitFlag(ctx,PGA_FALSE);
855
856****************************************************************************U*/
857void PGASetRandomInitFlag(PGAContext *ctx, int RandomBoolean)
858{
859    PGADebugEntered("PGASetRandomInitFlag");
860    PGAFailIfSetUp("PGASetRandomInitFlag");
861
862  switch (RandomBoolean) {
863    case PGA_TRUE:
864    case PGA_FALSE:
865      ctx->init.RandomInit = RandomBoolean;
866      break;
867    default:
868      PGAError(ctx, "PGASetRandomInitFlag: Invalid value of RandomBoolean:",
869               PGA_FATAL, PGA_INT, (void *) &RandomBoolean);
870      break;
871    }
872    PGADebugExited("PGASetRandomInitFlag");
873}
874
875/*U***************************************************************************
876   PGAGetRandomInitFlag - returns true/false to indicate whether or not
877   alleles are randomly initialized.
878
879   Category: Initialization
880
881   Inputs:
882      ctx - context variable
883
884   Outputs:
885      Returns PGA_TRUE if alleles are randomly initialized.
886      Otherwise, returns PGA_FALSE
887
888   Example:
889      PGAContext *ctx;
890      int raninit;
891      :
892      raninit = PGAGetRandomInitFlag(ctx);
893      switch (raninit) {
894      case PGA_TRUE:
895          printf("Population is randomly initialized\n");
896          break;
897      case PGA_FALSE:
898          printf("Population initialized to zero\n");
899          break;
900      }
901
902***************************************************************************U*/
903int PGAGetRandomInitFlag (PGAContext *ctx)
904{
905    PGADebugEntered("PGAGetRandomInitFlag");
906
907    PGAFailIfNotSetUp("PGAGetRandomInitFlag");
908
909    PGADebugExited("PGAGetRandomInitFlag");
910
911    return(ctx->init.RandomInit);
912}
913
914
915/*I****************************************************************************
916  PGACreatePop - allocates a population of individuals and calls
917  PGACreateIndividual to set up each one
918
919  Inputs:
920     ctx - context variable
921     pop - symbolic constant of the population to create
922
923  Outputs:
924     None
925
926  Example:
927     PGAContext *ctx;
928     :
929     PGACreatePop(ctx, PGA_NEWPOP);
930
931****************************************************************************I*/
932void PGACreatePop (PGAContext *ctx, int pop)
933{
934     int p, flag=0;
935
936    PGADebugEntered("PGACreatePop");
937
938     switch (pop)
939     {
940     case PGA_OLDPOP:
941          ctx->ga.oldpop = (PGAIndividual *)malloc(sizeof(PGAIndividual) *
942                                                   (ctx->ga.PopSize + 3));
943          if (ctx->ga.oldpop == NULL)
944               PGAError(ctx, "PGACreatePop: No room to allocate "
945                        "ctx->ga.oldpop", PGA_FATAL, PGA_VOID, NULL);
946          flag = ctx->init.RandomInit;
947          break;
948     case PGA_NEWPOP:
949          ctx->ga.newpop = (PGAIndividual *)malloc(sizeof(PGAIndividual) *
950                                                   (ctx->ga.PopSize + 3));
951          if (ctx->ga.newpop == NULL)
952               PGAError(ctx, "PGACreatePop: No room to allocate "
953                        "ctx->ga.newpop", PGA_FATAL, PGA_VOID, NULL);
954          flag = PGA_FALSE;
955          break;
956     default:
957          PGAError(ctx, "PGACreatePop: Invalid value of pop:", PGA_FATAL,
958                   PGA_INT, (void *) &pop );
959          break;
960     };
961     for (p = 0; p < ctx->ga.PopSize; p++)
962          PGACreateIndividual (ctx, p, pop, flag);
963     PGACreateIndividual (ctx, PGA_TEMP1, pop, PGA_FALSE);
964     PGACreateIndividual (ctx, PGA_TEMP2, pop, PGA_FALSE);
965     PGACreateIndividual (ctx, PGA_TEMP3, pop, PGA_FALSE);
966
967
968    PGADebugExited("PGACreatePop");
969}
970
971
972/*I****************************************************************************
973  PGACreateIndividual - initialize to zero various data structures of an
974  individual and call the appropriate function to create and initialize the
975  string for the specific data type
976
977  Inputs:
978     ctx      - context variable
979     p        - string index
980     pop      - symbolic constant of the population string p is in
981     initflag - if the value is PGA_TRUE, the string is randomly initialized.
982                Otherwise it is set to zero.
983
984  Outputs:
985     None
986
987  Example:
988     PGAContext *ctx;
989     int p;
990     :
991     PGACreateIndividual(ctx, p, PGA_NEWPOP, PGA_TRUE);
992
993****************************************************************************I*/
994void PGACreateIndividual (PGAContext *ctx, int p, int pop, int initflag)
995{
996    PGAIndividual *ind = PGAGetIndividual(ctx, p, pop);
997
998    PGADebugEntered("PGACreateIndividual");
999
1000    ind->evalfunc     = 0.0;
1001    ind->fitness      = 0.0;
1002    ind->evaluptodate = PGA_FALSE;
1003    ind->UserData     = NULL;
1004
1005    (*ctx->cops.CreateString)(ctx, p, pop, initflag);
1006   
1007    PGADebugExited("PGACreateIndividual");
1008}
1009
Note: See TracBrowser for help on using the repository browser.