/*U*************************************************************************** PGAGetMaxRealInitValue - returns the maximum value used to randomly initialize allele i in a real string Category: Initialization Inputs: ctx - context variable i - an allele position Outputs: The maximum value used to randomly initialize allele i Example: PGAContext *ctx; int max; : max = PGAGetMaxRealInitValue(ctx, 0); ***************************************************************************U*/ double PGAGetMaxRealInitValue (PGAContext *ctx, int i) { PGADebugEntered("PGAGetMaxRealInitValue"); PGAFailIfNotSetUp("PGAGetMaxRealInitValue"); PGACheckDataType("PGAGetMaxRealInitValue", PGA_DATATYPE_REAL); if (i < 0 || i >= ctx->ga.StringLen) PGAError(ctx, "PGAGetMaxRealInitValue: Index out of range:", PGA_FATAL, PGA_INT, (int *) &i); PGADebugExited("PGAGetMaxRealInitValue"); return(ctx->init.RealMax[i]); }
/*U**************************************************************************** PGASetPrintFrequencyValue - Specifies the frequency with which genetic algorithm statistics are reported. The default is every 10 GA iterations. Used only if PGARun() is used to run the GA. Category: Reporting Inputs: ctx - context variable print_freq - the genetic algorithm population size to use Outputs: None Example: PGAContext *ctx; : PGASetPrintFrequencyValue(ctx,1); ****************************************************************************U*/ void PGASetPrintFrequencyValue( PGAContext *ctx, int print_freq) { PGADebugEntered("PGASetPrintFrequencyValue"); if (print_freq < 0) PGAError ( ctx, "PGASetPrintFrequencyValue: Invalid value of print_freq:", PGA_FATAL, PGA_INT, (void *) &print_freq); else ctx->rep.PrintFreq = print_freq; PGADebugExited("PGASetPrintFrequencyValue"); }
/*I*************************************************************************** PGAGetVariableStringLength - Returns the length of a variable length string. Category: Generation Inputs: ctx - context variable p - index into the population pop - symbolic constant for the population Outputs: The string length Example: PGAContext *ctx; int stringlen; : stringlen = PGAGetVariableStringLength(ctx, 0, PGA_NEWPOP); ***************************************************************************I*/ int PGAGetVariableStringLength (PGAContext *ctx, int p, int pop) { PGADebugEntered("PGAGetVariableStringLength"); PGADebugExited("PGAGetVariableStringLength"); PGAError(ctx, "PGAGetVariableStringLength: Variable length strings not " "currently supported.", PGA_FATAL, PGA_VOID, NULL); #if 0 ind = PGAGetIndividual(ctx, p, pop); return(ind->StringLength); #endif /* Make the compilers be quiet. */ return(0); }
/*U**************************************************************************** PGASetMutationAndCrossoverFlag - A boolean flag to indicate if recombination uses both crossover and mutation on selected strings Category: Generation Inputs: ctx - context variable flag - PGA_TRUE (default) or PGA_FALSE Outputs: None Example: Set the genetic algorithm to use both crossover and mutation when reproducing strings. PGAContext *ctx; : PGASetMutationAndCrossoverFlag(ctx,PGA_FALSE); ****************************************************************************U*/ void PGASetMutationAndCrossoverFlag( PGAContext *ctx, int flag) { PGADebugEntered("PGASetMutationAndCrossoverFlag"); switch (flag) { case PGA_TRUE: case PGA_FALSE: ctx->ga.MutateOnlyNoCross = !flag; break; default: PGAError (ctx, "PGASetMutationAndCrossoverFlag: Invalid value of " "flag:", PGA_FATAL, PGA_INT, (void *) &flag); break; } PGADebugExited("PGASetMutationAndCrossoverFlag"); }
/*U**************************************************************************** PGASetPrintOptions - set flags to indicate what GA statistics should be printed whenever output is printed. May be called more than once to specify different report options. Valid choices are PGA_REPORT_AVERAGE, PGA_REPORT_OFFLINE, PGA_REPORT_ONLINE, PGA_REPORT_WORST, PGA_REPORT_HAMMING, and PGA_REPORT_STRING to specify offline analysis, online analysis, the worst string in the population, the Hamming distance of the population, and the actual allele values of the best string. The best string is always printed. Category: Reporting Inputs: ctx - context variable option - symbolic constant to specify a print option Outputs: None Example: PGAContext *ctx; : PGASetPrintOptions(ctx, PGA_REPORT_WORST); ****************************************************************************U*/ void PGASetPrintOptions (PGAContext *ctx, int option) { PGADebugEntered("PGASetPrintOptions"); switch (option) { case PGA_REPORT_AVERAGE: case PGA_REPORT_OFFLINE: case PGA_REPORT_ONLINE: case PGA_REPORT_WORST: case PGA_REPORT_HAMMING: case PGA_REPORT_STRING: ctx->rep.PrintOptions |= option; break; default: PGAError (ctx, "PGASetPrintOption: Invalid value of option:", PGA_FATAL, PGA_INT, (void *) &option); break; } PGADebugExited("PGASetPrintOptions"); }
/*U**************************************************************************** PGASetRealInitRange - sets the upper and lower bounds for randomly initializing real-valued genes. For each gene these bounds define an interval from which the initial allele value is selected uniformly randomly. The user specifies two arrays containing lower and bound for each gene to define the interval. This is the default strategy for initializing real-valued strings. The default interval is $[0,1.0]$ for each gene. Category: Initialization Inputs: ctx - context variable min - array containing the lower bound of the interval for each gene mac - array containing the upper bound of the interval for each gene Outputs: Example: Set the initialization routines to select a value for each real-valued gene i uniformly randomly from the interval [-10.,i] Assumes all strings are of the same length. PGAContext *ctx; double *low, *high; int i, stringlen; : stringlen = PGAGetStringLength(ctx); low = (double *) malloc(stringlen*sizeof(double)); high = (double *) malloc(stringlen*sizeof(double)); for(i=0;i<stringlen;i++) { low[i] = -10.0; high[i] = i; } PGASetRealInitRange(ctx, low, high); ****************************************************************************U*/ void PGASetRealInitRange (PGAContext *ctx, double *min, double *max) { int i; PGADebugEntered("PGASetRealInitRange"); PGAFailIfSetUp("PGASetRealInitRange"); PGACheckDataType("PGASetRealInitRange", PGA_DATATYPE_REAL); for (i=ctx->ga.StringLen-1; i>=0; i--) { if (max[i] < min[i]) PGAError(ctx, "PGASetRealInitRange: Lower bound exceeds upper " "bound for allele #", PGA_FATAL, PGA_INT, (void *) &i); else { ctx->init.RealMin[i] = min[i]; ctx->init.RealMax[i] = max[i]; } } ctx->init.RealType = PGA_RINIT_RANGE; PGADebugExited("PGASetRealInitRange"); }
/*U**************************************************************************** PGARun - Highest level routine to execute the genetic algorithm. It is called after PGACreate and PGASetup have been called. Category: Generation Inputs: ctx - context variable evaluate - a pointer to the user's evaluation function, which must have the calling sequence shown in the example. Outputs: none Example: PGAContext *ctx, double f(PGAContext *ctx, int p, int pop); : ctx = PGACreate(&argc, argv, PGA_DATATYPE_BINARY, 100, PGA_MAXIMIZE); PGASetUp(ctx); PGARun(ctx, f); PGADestroy(ctx); ****************************************************************************U*/ void PGARun(PGAContext *ctx, double (*evaluate)(PGAContext *c, int p, int pop)) { MPI_Comm comm; /* value of default communicator */ int nprocs; /* number of processes in above */ int npops; /* number of populations */ int ndemes; /* number of demes */ PGADebugEntered("PGARun"); PGAFailIfNotSetUp("PGARun"); comm = PGAGetCommunicator(ctx); nprocs = PGAGetNumProcs (ctx, comm); npops = PGAGetNumIslands (ctx); ndemes = PGAGetNumDemes (ctx); /**********************************************************************/ /* Global model, one island, one deme */ /**********************************************************************/ if ( (npops == 1) && (ndemes == 1) ) { PGARunGM(ctx, evaluate, comm); } /**********************************************************************/ /* Island model, > one island, one deme */ /**********************************************************************/ else if( (npops > 1) && (ndemes == 1) ) { if ( nprocs == 1 ) PGAError (ctx, "PGARun: island model with one process", PGA_FATAL, PGA_VOID, (void *) &nprocs); if ( nprocs != npops) { PGAError (ctx, "PGARun: island model no. processes != no. pops", PGA_FATAL, PGA_VOID, (void *) &nprocs); } PGARunIM(ctx,evaluate,comm); } /**********************************************************************/ /* Neighborhood model, one island, > one deme */ /**********************************************************************/ else if( (npops == 1) && (ndemes > 1) ) { if ( nprocs == 1 ) PGAError (ctx, "PGARun: neighborhood model with one process", PGA_FATAL, PGA_VOID, (void *) &nprocs); if ( nprocs != ndemes) PGAError (ctx, "PGARun: neighborhood model no. processes " "!= no. demes", PGA_FATAL, PGA_VOID, (void *) &nprocs); PGARunNM(ctx,evaluate,comm); } /**********************************************************************/ /* Mixed model, > one island, > one deme */ /**********************************************************************/ else if( (npops > 1) && (ndemes > 1) ) { PGAError (ctx, "PGARun: Cannot execute mixed models", PGA_FATAL, PGA_VOID, (void *) &nprocs); } /**********************************************************************/ /* E R R O R */ /**********************************************************************/ else { PGAError (ctx, "PGARun: Invalid combination of numislands," "ndemes, and nprocs.", PGA_FATAL, PGA_VOID, (void *) &nprocs); } /**********************************************************************/ /* E X I T */ /**********************************************************************/ PGADebugExited("PGARun"); return; }