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: char.c: This file contains the routines specific to the |
---|
66 | * character datatype. |
---|
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 | PGASetCharacterAllele - sets the value of an allele in a |
---|
76 | PGA_DATATYPE_CHARACTER string. |
---|
77 | |
---|
78 | Category: Fitness & Evaluation |
---|
79 | |
---|
80 | Inputs: |
---|
81 | ctx - context variable |
---|
82 | p - string index |
---|
83 | pop - symbolic constant of the population the string is in |
---|
84 | i - allele index |
---|
85 | val - character value to set the allele to |
---|
86 | |
---|
87 | Outputs: |
---|
88 | The allele is changed by side-effect. |
---|
89 | |
---|
90 | Example: |
---|
91 | Copies the alleles from member p in PGA_OLDPOP to member q in PGA_NEWPOP. |
---|
92 | Assumes the strings are of the same length. |
---|
93 | |
---|
94 | PGAContext *ctx; |
---|
95 | int p, q, i; |
---|
96 | : |
---|
97 | for (i=PGAGetStringLength(ctx)-1; i>=0; i--) |
---|
98 | PGASetCharacterAllele(ctx, q, PGA_NEWPOP, i, |
---|
99 | PGAGetCharacterAllele(ctx, p, PGA_OLDPOP, i)) |
---|
100 | |
---|
101 | ****************************************************************************U*/ |
---|
102 | void PGASetCharacterAllele (PGAContext *ctx, int p, int pop, int i, char value) |
---|
103 | { |
---|
104 | PGAIndividual *ind; |
---|
105 | |
---|
106 | PGADebugEntered("PGASetCharacterAllele"); |
---|
107 | PGACheckDataType("PGASetCharacterAllele", PGA_DATATYPE_CHARACTER); |
---|
108 | |
---|
109 | ind = PGAGetIndividual ( ctx, p, pop ); |
---|
110 | ((PGACharacter *)ind->chrom)[i] = value; |
---|
111 | |
---|
112 | PGADebugExited("PGASetCharacterAllele"); |
---|
113 | } |
---|
114 | |
---|
115 | /*U**************************************************************************** |
---|
116 | PGAGetCharacterAllele: returns the value of character allele in a |
---|
117 | PGA_DATATYPE_CHARACTER string |
---|
118 | |
---|
119 | Category: Fitness & Evaluation |
---|
120 | |
---|
121 | Inputs: |
---|
122 | ctx - context variable |
---|
123 | p - string index |
---|
124 | pop - symbolic constant of the population the string is in |
---|
125 | i - allele index |
---|
126 | |
---|
127 | Outputs: |
---|
128 | The value of allele i in string p. |
---|
129 | |
---|
130 | Example: |
---|
131 | Copies the alleles from member p in PGA_OLDPOP to member q in PGA_NEWPOP. |
---|
132 | Assumes the strings are of the same length. |
---|
133 | |
---|
134 | PGAContext *ctx; |
---|
135 | int p, q, i; |
---|
136 | : |
---|
137 | for (i=PGAGetStringLength(ctx, p, PGA_NEWPOP)-1; i>=0; i--) |
---|
138 | PGASetCharacterAllele(ctx, q, PGA_NEWPOP, i, |
---|
139 | PGAGetCharacterAllele(ctx, p, PGA_OLDPOP, i)) |
---|
140 | |
---|
141 | ****************************************************************************U*/ |
---|
142 | char PGAGetCharacterAllele (PGAContext *ctx, int p, int pop, int i) |
---|
143 | { |
---|
144 | PGAIndividual *ind; |
---|
145 | |
---|
146 | PGADebugEntered("PGAGetCharacterAllele"); |
---|
147 | PGACheckDataType("PGAGetCharacterAllele", PGA_DATATYPE_CHARACTER); |
---|
148 | |
---|
149 | ind = PGAGetIndividual ( ctx, p, pop ); |
---|
150 | |
---|
151 | PGADebugExited("PGAGetCharacterAllele"); |
---|
152 | |
---|
153 | return (((PGACharacter *)ind->chrom)[i]); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /*U**************************************************************************** |
---|
158 | PGASetCharacterInitType - sets a flag to specify whether the character |
---|
159 | strings will be exclusively lowercase, exclusively uppercase, or a mixure |
---|
160 | of both cases. Legal flags are PGA_CINIT_UPPER, PGA_CINIT_LOWER, and |
---|
161 | PGA_CINIT_MIXED. Default is PGA_CINIT_LOWER. |
---|
162 | |
---|
163 | Category: Initialization |
---|
164 | |
---|
165 | Inputs: |
---|
166 | ctx - context variable |
---|
167 | value - symbolic constant specifying which case |
---|
168 | |
---|
169 | Outputs: |
---|
170 | |
---|
171 | Example: |
---|
172 | Set program to generate exclusively uppercase letters |
---|
173 | |
---|
174 | PGAContext *ctx; |
---|
175 | : |
---|
176 | PGASetCharacterInitType(ctx, PGA_CINIT_UPPER); |
---|
177 | |
---|
178 | ****************************************************************************U*/ |
---|
179 | void PGASetCharacterInitType(PGAContext *ctx, int value) |
---|
180 | { |
---|
181 | PGADebugEntered("PGASetCharacterInitType"); |
---|
182 | PGACheckDataType("PGASetCharacterInitType", PGA_DATATYPE_CHARACTER); |
---|
183 | |
---|
184 | switch (value) |
---|
185 | { |
---|
186 | case PGA_CINIT_UPPER: |
---|
187 | case PGA_CINIT_LOWER: |
---|
188 | case PGA_CINIT_MIXED: |
---|
189 | ctx->init.CharacterType = value; |
---|
190 | break; |
---|
191 | default: |
---|
192 | PGAError(ctx, "PGASetCharacterInitType: Invalid case type:", |
---|
193 | PGA_FATAL, PGA_INT, (void *)&value); |
---|
194 | break; |
---|
195 | } |
---|
196 | |
---|
197 | PGADebugExited("PGASetCharacterInitType"); |
---|
198 | } |
---|
199 | |
---|
200 | /*I**************************************************************************** |
---|
201 | PGACharacterCreateString - Allocate memory for a string of type PGACharacter |
---|
202 | |
---|
203 | Inputs: |
---|
204 | ctx - context variable |
---|
205 | p - string index |
---|
206 | pop - symbolic constant of the population string p is in |
---|
207 | initflag - A true/false flag used in conjunction with ctx->ga.RandomInit |
---|
208 | to initialize the string either randomly or set to zero |
---|
209 | |
---|
210 | Outputs: |
---|
211 | Member p in population pop is allocated and initialized. |
---|
212 | |
---|
213 | Example: |
---|
214 | Allocates memory and assigns the address of the allocated memory to |
---|
215 | the string field (ind->chrom) of the individual. Additionally, the |
---|
216 | string is initialized to zero. |
---|
217 | |
---|
218 | PGAContext *ctx; |
---|
219 | int p; |
---|
220 | : |
---|
221 | PGACharacterCreateString( ctx, p, PGA_NEWPOP, PGA_FALSE ); |
---|
222 | |
---|
223 | ****************************************************************************I*/ |
---|
224 | void PGACharacterCreateString (PGAContext *ctx, int p, int pop, int InitFlag) |
---|
225 | { |
---|
226 | int i, fp; |
---|
227 | PGACharacter *c; |
---|
228 | PGAIndividual *new = PGAGetIndividual(ctx, p, pop); |
---|
229 | |
---|
230 | PGADebugEntered("PGACharacterCreateString"); |
---|
231 | |
---|
232 | new->chrom = (void *)malloc(ctx->ga.StringLen * sizeof(PGACharacter)); |
---|
233 | if (new->chrom == NULL) |
---|
234 | PGAError(ctx, "PGACharacterCreateString: No room to allocate " |
---|
235 | "new->chrom", PGA_FATAL, PGA_VOID, NULL); |
---|
236 | c = (PGACharacter *)new->chrom; |
---|
237 | if (InitFlag) |
---|
238 | if (ctx->fops.InitString) { |
---|
239 | fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1; |
---|
240 | (*ctx->fops.InitString)(&ctx, &fp, &pop); |
---|
241 | } else { |
---|
242 | (*ctx->cops.InitString)(ctx, p, pop); |
---|
243 | } |
---|
244 | else |
---|
245 | for (i=0; i<ctx->ga.StringLen; i++) |
---|
246 | c[i] = 0; |
---|
247 | |
---|
248 | PGADebugExited("PGACharacterCreateString"); |
---|
249 | } |
---|
250 | |
---|
251 | /*I**************************************************************************** |
---|
252 | PGACharacterMutation - randomly mutates a character-valued gene with a |
---|
253 | specified probability. This routine is called from PGAMutation. |
---|
254 | |
---|
255 | Inputs: |
---|
256 | ctx - context variable |
---|
257 | p - string index |
---|
258 | pop - symbolic constant of the population string p is in |
---|
259 | mr - probability of mutating an character-valued gene |
---|
260 | |
---|
261 | Outputs: |
---|
262 | Returns the number of mutations |
---|
263 | |
---|
264 | Example: |
---|
265 | PGAContext *ctx; |
---|
266 | int p; |
---|
267 | int NumMutations; |
---|
268 | : |
---|
269 | NumMutations = PGACharacterMutation(ctx, p, PGA_NEWPOP, 0.01); |
---|
270 | ****************************************************************************I*/ |
---|
271 | int PGACharacterMutation( PGAContext *ctx, int p, int pop, double mr ) |
---|
272 | { |
---|
273 | PGACharacter *c; |
---|
274 | int i, j; |
---|
275 | int count = 0; |
---|
276 | |
---|
277 | PGADebugEntered("PGACharacterMutation"); |
---|
278 | |
---|
279 | c = (PGACharacter *)PGAGetIndividual(ctx, p, pop)->chrom; |
---|
280 | for(i=0; i<ctx->ga.StringLen; i++) |
---|
281 | if ( PGARandomFlip(ctx, mr) ) /* randomly choose an allele */ |
---|
282 | { |
---|
283 | switch (ctx->init.CharacterType) |
---|
284 | { |
---|
285 | case PGA_CINIT_LOWER: |
---|
286 | c[i] = PGARandomInterval(ctx, 'a', 'z'); |
---|
287 | break; |
---|
288 | case PGA_CINIT_UPPER: |
---|
289 | c[i] = PGARandomInterval(ctx, 'A', 'Z'); |
---|
290 | break; |
---|
291 | case PGA_CINIT_MIXED: |
---|
292 | j = PGARandomInterval(ctx, 0, 51); |
---|
293 | if (j < 26) |
---|
294 | c[i] = 'A' + j; |
---|
295 | else |
---|
296 | c[i] = 'a' + j - 26; |
---|
297 | break; |
---|
298 | } |
---|
299 | count++; |
---|
300 | } |
---|
301 | |
---|
302 | PGADebugExited("PGACharacterMutation"); |
---|
303 | |
---|
304 | return (count); |
---|
305 | } |
---|
306 | |
---|
307 | /*I**************************************************************************** |
---|
308 | PGACharacterOneptCrossover - performs one-point crossover on two parent |
---|
309 | strings producing two children via side-effect |
---|
310 | |
---|
311 | Inputs: |
---|
312 | ctx - context variable |
---|
313 | p1 - the first parent string |
---|
314 | p2 - the second parent string |
---|
315 | pop1 - symbolic constant of the population containing string p1 and p2 |
---|
316 | c1 - the first child string |
---|
317 | c2 - the second child string |
---|
318 | pop2 - symbolic constant of the population to contain string c1 and c2 |
---|
319 | |
---|
320 | Outputs: |
---|
321 | |
---|
322 | Example: |
---|
323 | Performs crossover on the two parent strings m and d, producing |
---|
324 | children s and b. |
---|
325 | |
---|
326 | PGAContext *ctx; |
---|
327 | int m, d, s, b; |
---|
328 | : |
---|
329 | PGACharacterOneptCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP ); |
---|
330 | |
---|
331 | ****************************************************************************I*/ |
---|
332 | void PGACharacterOneptCrossover(PGAContext *ctx, int p1, int p2, int pop1, |
---|
333 | int c1, int c2, int pop2) |
---|
334 | { |
---|
335 | PGACharacter *parent1, *parent2, *child1, *child2; |
---|
336 | int i, xsite; |
---|
337 | |
---|
338 | PGADebugEntered("PGACharacterOneptCrossover"); |
---|
339 | |
---|
340 | parent1 = (PGACharacter *)PGAGetIndividual(ctx, p1, pop1)->chrom; |
---|
341 | parent2 = (PGACharacter *)PGAGetIndividual(ctx, p2, pop1)->chrom; |
---|
342 | child1 = (PGACharacter *)PGAGetIndividual(ctx, c1, pop2)->chrom; |
---|
343 | child2 = (PGACharacter *)PGAGetIndividual(ctx, c2, pop2)->chrom; |
---|
344 | xsite = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1); |
---|
345 | |
---|
346 | for(i=0;i<xsite;i++) |
---|
347 | { |
---|
348 | child1[i] = parent1[i]; |
---|
349 | child2[i] = parent2[i]; |
---|
350 | } |
---|
351 | |
---|
352 | for(i=xsite;i<ctx->ga.StringLen;i++) |
---|
353 | { |
---|
354 | child1[i] = parent2[i]; |
---|
355 | child2[i] = parent1[i]; |
---|
356 | } |
---|
357 | |
---|
358 | PGADebugExited("PGACharacterOneptCrossover"); |
---|
359 | } |
---|
360 | |
---|
361 | /*I**************************************************************************** |
---|
362 | PGACharacterTwoptCrossover - performs two-point crossover on two parent |
---|
363 | strings producing two children via side-effect |
---|
364 | |
---|
365 | Inputs: |
---|
366 | ctx - context variable |
---|
367 | p1 - the first parent string |
---|
368 | p2 - the second parent string |
---|
369 | pop1 - symbolic constant of the population containing string p1 and p2 |
---|
370 | c1 - the first child string |
---|
371 | c2 - the second child string |
---|
372 | pop2 - symbolic constant of the population to contain string c1 and c2 |
---|
373 | |
---|
374 | Outputs: |
---|
375 | |
---|
376 | Example: |
---|
377 | Performs crossover on the two parent strings m and d, producing |
---|
378 | children s and b. |
---|
379 | |
---|
380 | PGAContext *ctx; |
---|
381 | int m, d, s, b; |
---|
382 | : |
---|
383 | PGACharacterTwoptCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP ); |
---|
384 | |
---|
385 | ****************************************************************************I*/ |
---|
386 | void PGACharacterTwoptCrossover( PGAContext *ctx, int p1, int p2, int pop1, |
---|
387 | int c1, int c2, int pop2) |
---|
388 | { |
---|
389 | PGACharacter *parent1, *parent2, *child1, *child2; |
---|
390 | int i, temp, xsite1, xsite2; |
---|
391 | |
---|
392 | PGADebugEntered("PGACharacterTwoptCrossover"); |
---|
393 | |
---|
394 | parent1 = (PGACharacter *)PGAGetIndividual(ctx, p1, pop1)->chrom; |
---|
395 | parent2 = (PGACharacter *)PGAGetIndividual(ctx, p2, pop1)->chrom; |
---|
396 | child1 = (PGACharacter *)PGAGetIndividual(ctx, c1, pop2)->chrom; |
---|
397 | child2 = (PGACharacter *)PGAGetIndividual(ctx, c2, pop2)->chrom; |
---|
398 | /* pick two cross sites such that xsite2 > xsite1 */ |
---|
399 | xsite1 = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1); |
---|
400 | xsite2 = xsite1; |
---|
401 | while ( xsite2 == xsite1 ) |
---|
402 | xsite2 = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1); |
---|
403 | if ( xsite1 > xsite2 ) |
---|
404 | { |
---|
405 | temp = xsite1; |
---|
406 | xsite1 = xsite2; |
---|
407 | xsite2 = temp; |
---|
408 | } |
---|
409 | |
---|
410 | for(i=0;i<xsite1;i++) |
---|
411 | { |
---|
412 | child1[i] = parent1[i]; |
---|
413 | child2[i] = parent2[i]; |
---|
414 | } |
---|
415 | |
---|
416 | for(i=xsite1;i<xsite2;i++) |
---|
417 | { |
---|
418 | child1[i] = parent2[i]; |
---|
419 | child2[i] = parent1[i]; |
---|
420 | } |
---|
421 | |
---|
422 | for(i=xsite2;i<ctx->ga.StringLen;i++) |
---|
423 | { |
---|
424 | child1[i] = parent1[i]; |
---|
425 | child2[i] = parent2[i]; |
---|
426 | } |
---|
427 | |
---|
428 | PGADebugExited("PGACharacterTwoptCrossover"); |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | /*I**************************************************************************** |
---|
433 | PGACharacterUniformCrossover - performs uniform crossover on two parent |
---|
434 | strings producing two children via side-effect |
---|
435 | |
---|
436 | Inputs: |
---|
437 | ctx - context variable |
---|
438 | p1 - the first parent string |
---|
439 | p2 - the second parent string |
---|
440 | pop1 - symbolic constant of the population containing string p1 and p2 |
---|
441 | c1 - the first child string |
---|
442 | c2 - the second child string |
---|
443 | pop2 - symbolic constant of the population to contain string c1 and c2 |
---|
444 | |
---|
445 | Outputs: |
---|
446 | |
---|
447 | Example: |
---|
448 | Performs crossover on the two parent strings m and d, producing |
---|
449 | children s and b. |
---|
450 | |
---|
451 | PGAContext *ctx; |
---|
452 | int m, d, s, b; |
---|
453 | : |
---|
454 | PGACharacterUniformCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP ); |
---|
455 | |
---|
456 | ****************************************************************************I*/ |
---|
457 | void PGACharacterUniformCrossover(PGAContext *ctx, int p1, int p2, int pop1, |
---|
458 | int c1, int c2, int pop2) |
---|
459 | { |
---|
460 | PGACharacter *parent1, *parent2, *child1, *child2; |
---|
461 | int i; |
---|
462 | |
---|
463 | PGADebugEntered("PGACharacterUniformCrossover"); |
---|
464 | |
---|
465 | parent1 = (PGACharacter *)PGAGetIndividual(ctx, p1, pop1)->chrom; |
---|
466 | parent2 = (PGACharacter *)PGAGetIndividual(ctx, p2, pop1)->chrom; |
---|
467 | child1 = (PGACharacter *)PGAGetIndividual(ctx, c1, pop2)->chrom; |
---|
468 | child2 = (PGACharacter *)PGAGetIndividual(ctx, c2, pop2)->chrom; |
---|
469 | |
---|
470 | for(i=0;i<ctx->ga.StringLen;i++) |
---|
471 | if ( parent1[i] == parent2[i] ) |
---|
472 | { |
---|
473 | child1[i] = parent1[i]; |
---|
474 | child2[i] = parent2[i]; |
---|
475 | } |
---|
476 | else if (PGARandomFlip(ctx, ctx->ga.UniformCrossProb)) |
---|
477 | { |
---|
478 | child1[i] = parent1[i]; |
---|
479 | child2[i] = parent2[i]; |
---|
480 | } |
---|
481 | else |
---|
482 | { |
---|
483 | child1[i] = parent2[i]; |
---|
484 | child2[i] = parent1[i]; |
---|
485 | } |
---|
486 | |
---|
487 | PGADebugExited("PGACharacterUniformCrossover"); |
---|
488 | } |
---|
489 | |
---|
490 | /*I**************************************************************************** |
---|
491 | PGACharacterPrintString - writes a character-valued string to a file. |
---|
492 | |
---|
493 | Inputs: |
---|
494 | ctx - context variable |
---|
495 | fp - file pointer to file to write the string to |
---|
496 | p - index of the string to write out |
---|
497 | pop - symbolic constant of the population string p is in |
---|
498 | |
---|
499 | Outputs: |
---|
500 | |
---|
501 | Example: |
---|
502 | Write string s to stdout. |
---|
503 | |
---|
504 | PGAContext *ctx; |
---|
505 | int p; |
---|
506 | : |
---|
507 | PGACharacterPrintString( ctx, stdout, p, PGA_NEWPOP ); |
---|
508 | |
---|
509 | ****************************************************************************I*/ |
---|
510 | void PGACharacterPrintString ( PGAContext *ctx, FILE *fp, int p, int pop) |
---|
511 | { |
---|
512 | PGACharacter *c; |
---|
513 | int i, pos, len; |
---|
514 | |
---|
515 | PGADebugEntered("PGACharacterPrintString"); |
---|
516 | |
---|
517 | c = (PGACharacter *)PGAGetIndividual(ctx, p, pop)->chrom; |
---|
518 | len = PGAGetStringLength(ctx); |
---|
519 | |
---|
520 | pos = 0; |
---|
521 | while (len > 0) { |
---|
522 | fprintf(fp, "#%5d: [", pos); |
---|
523 | for (i=0; i<50 && len>0; i++,len--,c++) |
---|
524 | fputc(*c, fp); |
---|
525 | pos+=50; |
---|
526 | fprintf(fp, "]\n"); |
---|
527 | } |
---|
528 | fprintf(fp, "\n"); |
---|
529 | |
---|
530 | PGADebugExited("PGACharacterPrintString"); |
---|
531 | } |
---|
532 | |
---|
533 | /*I**************************************************************************** |
---|
534 | PGACharacterCopyString - Copy one character-valued string to another |
---|
535 | Assumes the strings are of the same length. |
---|
536 | |
---|
537 | Inputs: |
---|
538 | ctx - context variable |
---|
539 | p1 - string to copy |
---|
540 | pop1 - symbolic constant of population containing string p1 |
---|
541 | p2 - string to copy p1 to |
---|
542 | pop2 - symbolic constant of population containing string p2 |
---|
543 | |
---|
544 | Outputs: |
---|
545 | |
---|
546 | Example: |
---|
547 | Copy character string x to y (both are implicitly assumed to be the same |
---|
548 | length) |
---|
549 | |
---|
550 | PGAContext *ctx; |
---|
551 | int x, y; |
---|
552 | : |
---|
553 | PGACharacterCopyString ( ctx, x, PGA_OLDPOP, y, PGA_NEWPOP ); |
---|
554 | |
---|
555 | ****************************************************************************I*/ |
---|
556 | void PGACharacterCopyString (PGAContext *ctx, int p1, int pop1, int p2, |
---|
557 | int pop2) |
---|
558 | { |
---|
559 | void *source, *dest; |
---|
560 | int len; |
---|
561 | |
---|
562 | PGADebugEntered("PGACharacterCopyString"); |
---|
563 | |
---|
564 | source = PGAGetIndividual(ctx, p1, pop1)->chrom; |
---|
565 | dest = PGAGetIndividual(ctx, p2, pop2)->chrom; |
---|
566 | len = PGAGetStringLength(ctx); |
---|
567 | memcpy(dest, source, len * sizeof(PGACharacter)); |
---|
568 | |
---|
569 | PGADebugExited("PGACharacterCopyString"); |
---|
570 | } |
---|
571 | |
---|
572 | /*I**************************************************************************** |
---|
573 | PGACharacterDuplicate - Returns true if string p1 in pop1 is a dublicate |
---|
574 | of string p2 in pop2, else returns false. |
---|
575 | Assumes the strings are the same length. |
---|
576 | |
---|
577 | Inputs: |
---|
578 | ctx - context variable |
---|
579 | p1 - string index of the first string to compare |
---|
580 | pop1 - symbolic constant of the population string p1 is in |
---|
581 | p2 - string index of the second string to compare |
---|
582 | pop2 - symbolic constant of the population string p2 is in |
---|
583 | |
---|
584 | Outputs: |
---|
585 | Returns true if strings are duplicates. |
---|
586 | |
---|
587 | Example: |
---|
588 | Compare string x with y to see if they are duplicates |
---|
589 | |
---|
590 | PGAContext *ctx; |
---|
591 | int x, y; |
---|
592 | : |
---|
593 | if ( PGACharacterDuplicate( ctx, x, PGA_NEWPOP, y, PGA_NEWPOP ) ) |
---|
594 | printf("strings are duplicates\n"); |
---|
595 | |
---|
596 | ****************************************************************************I*/ |
---|
597 | int PGACharacterDuplicate( PGAContext *ctx, int p1, int pop1, int p2, int pop2) |
---|
598 | { |
---|
599 | void *a, *b; |
---|
600 | int len; |
---|
601 | |
---|
602 | PGADebugEntered("PGACharacterDuplicate"); |
---|
603 | |
---|
604 | a = PGAGetIndividual(ctx, p1, pop1)->chrom; |
---|
605 | b = PGAGetIndividual(ctx, p2, pop2)->chrom; |
---|
606 | len = PGAGetStringLength(ctx); |
---|
607 | |
---|
608 | PGADebugExited("PGACharacterDuplicate"); |
---|
609 | |
---|
610 | return (!memcmp(a, b, len * sizeof(PGACharacter))); |
---|
611 | } |
---|
612 | |
---|
613 | /*I**************************************************************************** |
---|
614 | PGACharacterInitString - randomly initialize a string of type PGACharacter |
---|
615 | |
---|
616 | Inputs: |
---|
617 | ctx - context variable |
---|
618 | p - index of string to randomly initialize |
---|
619 | pop - symbolic constant of the population string p is in |
---|
620 | |
---|
621 | Outputs: |
---|
622 | |
---|
623 | Example: |
---|
624 | PGAContext *ctx; |
---|
625 | int p; |
---|
626 | : |
---|
627 | PGACharacterInitString ( ctx, p, PGA_NEWPOP ); |
---|
628 | |
---|
629 | ****************************************************************************I*/ |
---|
630 | void PGACharacterInitString(PGAContext *ctx, int p, int pop) |
---|
631 | { |
---|
632 | int len, i, j; |
---|
633 | PGACharacter *c; |
---|
634 | |
---|
635 | PGADebugEntered("PGACharacterInitString"); |
---|
636 | |
---|
637 | len = ctx->ga.StringLen; |
---|
638 | c = (PGACharacter *)PGAGetIndividual(ctx, p, pop)->chrom; |
---|
639 | switch (ctx->init.CharacterType) |
---|
640 | { |
---|
641 | case PGA_CINIT_LOWER: |
---|
642 | for (i = 0; i < len; i++) |
---|
643 | c[i] = PGARandomInterval(ctx, 'a', 'z'); |
---|
644 | break; |
---|
645 | case PGA_CINIT_UPPER: |
---|
646 | for (i = 0; i < len; i++) |
---|
647 | c[i] = PGARandomInterval(ctx, 'A', 'Z'); |
---|
648 | break; |
---|
649 | case PGA_CINIT_MIXED: |
---|
650 | for (i = 0; i < len; i++) |
---|
651 | { |
---|
652 | j = PGARandomInterval(ctx, 0, 51); |
---|
653 | if (j < 26) |
---|
654 | c[i] = 'A' + j; |
---|
655 | else |
---|
656 | c[i] = 'a' + j - 26; |
---|
657 | } |
---|
658 | break; |
---|
659 | } |
---|
660 | PGADebugExited("PGACharacterInitString"); |
---|
661 | } |
---|
662 | |
---|
663 | /*I**************************************************************************** |
---|
664 | PGACharacterBuildDatatype - Build an MPI_Datatype for a character string. |
---|
665 | |
---|
666 | Inputs: |
---|
667 | ctx - context variable |
---|
668 | p - index of the string to build a datatype from |
---|
669 | pop - symbolic constant of the population string p is in |
---|
670 | |
---|
671 | Outputs: |
---|
672 | MPI_Datatype |
---|
673 | |
---|
674 | Example: |
---|
675 | Called only by MPI routines. Not for user consumption. |
---|
676 | |
---|
677 | ****************************************************************************I*/ |
---|
678 | MPI_Datatype PGACharacterBuildDatatype(PGAContext *ctx, int p, int pop) |
---|
679 | { |
---|
680 | |
---|
681 | int counts[4]; /* Number of elements in each |
---|
682 | block (array of integer) */ |
---|
683 | MPI_Aint displs[4]; /* byte displacement of each |
---|
684 | block (array of integer) */ |
---|
685 | MPI_Datatype types[4]; /* type of elements in each block (array |
---|
686 | of handles to datatype objects) */ |
---|
687 | MPI_Datatype individualtype; /* new datatype (handle) */ |
---|
688 | PGAIndividual *traveller; /* address of individual in question */ |
---|
689 | |
---|
690 | PGADebugEntered("PGACharacterBuildDatatype"); |
---|
691 | |
---|
692 | traveller = PGAGetIndividual(ctx, p, pop); |
---|
693 | MPI_Address(&traveller->evalfunc, &displs[0]); |
---|
694 | counts[0] = 1; |
---|
695 | types[0] = MPI_DOUBLE; |
---|
696 | |
---|
697 | MPI_Address(&traveller->fitness, &displs[1]); |
---|
698 | counts[1] = 1; |
---|
699 | types[1] = MPI_DOUBLE; |
---|
700 | |
---|
701 | MPI_Address(&traveller->evaluptodate, &displs[2]); |
---|
702 | counts[2] = 1; |
---|
703 | types[2] = MPI_INT; |
---|
704 | |
---|
705 | MPI_Address(traveller->chrom, &displs[3]); |
---|
706 | counts[3] = ctx->ga.StringLen; |
---|
707 | types[3] = MPI_CHAR; |
---|
708 | |
---|
709 | MPI_Type_struct(4, counts, displs, types, &individualtype); |
---|
710 | MPI_Type_commit(&individualtype); |
---|
711 | |
---|
712 | PGADebugExited("PGACharacterBuildDatatype"); |
---|
713 | |
---|
714 | return (individualtype); |
---|
715 | } |
---|