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