source: branches/uq/packages/optimizer/src/pgapack/pgapack/source/real.c @ 5679

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

Full merge 1.3 branch to uq branch to sync. Fixed partial subdirectory merge
by removing mergeinfo from lang/python/Rappture directory.

  • Property svn:eol-style set to native
File size: 30.5 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: real.c: This file contains the routines specific to the floating
66*                   point data structure
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   PGASetRealAllele - sets the value of real-valued allele i in string p
76   in population pop
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 - real value to set the allele to
86
87   Outputs:
88      The specified allele in p is modified by side-effect.
89
90   Example:
91      Sets the value of the ith allele of string p in population PGA_NEWPOP
92      to 1.57
93
94      PGAContext *ctx;
95      int i, p;
96      :
97      PGASetRealAllele ( ctx, p, PGA_NEWPOP, i, 1.57)
98
99****************************************************************************U*/
100void PGASetRealAllele (PGAContext *ctx, int p, int pop, int i, double value)
101{
102    PGAIndividual *ind;
103    PGAReal      *chrom;
104
105    PGADebugEntered("PGASetRealAllele");
106    PGACheckDataType("PGASetRealAllele", PGA_DATATYPE_REAL);
107
108    ind = PGAGetIndividual ( ctx, p, pop );
109    chrom = (PGAReal *)ind->chrom;
110    chrom[i] = value;
111
112    PGADebugExited("PGASetRealAllele");
113}
114
115/*U****************************************************************************
116   PGAGetRealAllele - returns the value of real-valued allele i in string p
117   in population pop
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
129
130   Example:
131      Returns the value of the ith real-valued allele of string p
132      in population PGA_NEWPOP
133
134      PGAContext *ctx;
135      int p, i, r;
136      r =  PGAGetRealAllele (ctx, p, PGA_NEWPOP, i)
137
138****************************************************************************U*/
139double PGAGetRealAllele (PGAContext *ctx, int p, int pop, int i)
140{
141    PGAIndividual *ind;
142    PGAReal      *chrom;
143
144    PGADebugEntered("PGAGetRealAllele");
145    PGACheckDataType("PGAGetRealAllele", PGA_DATATYPE_REAL);
146
147    ind = PGAGetIndividual ( ctx, p, pop );
148    chrom = (PGAReal *)ind->chrom;
149
150    PGADebugExited("PGAGetRealAllele");
151
152    return( (double) chrom[i] );
153}
154
155/*U****************************************************************************
156  PGASetRealInitPercent - sets the upper and lower bounds for randomly
157  initializing real-valued genes.  For each gene these bounds define an
158  interval from which the initial allele value is selected uniformly randomly.
159  With this routine the user specifies a median value and a percent offset
160  for each allele.
161
162  Category: Initialization
163
164  Inputs:
165     ctx     - context variable
166     median  - an array containing the mean value of the interval
167     percent - an array containing the percent offset to add and subtract to
168               the median to define the interval
169
170  Outputs:
171
172  Example:
173     Set the initialization routines to select a value for each real-valued
174     gene i uniformly randomly from the interval [i-v,i+v], where $v = i/2$.
175     Assumes all strings are the same length.
176
177     PGAContext *ctx;
178     double *median, *percent;
179     int i, stringlen;
180     :
181     stringlen = PGAGetStringLength(ctx);
182     median  = (double *) malloc(stringlen*sizeof(double));
183     percent = (double *) malloc(stringlen*sizeof(double));
184     for(i=0;i<stringlen;i++) {
185        median[i]  = (double) i;
186        percent[i] = 0.5;
187     }
188     PGASetRealInitPercent(ctx, median, percent);
189
190****************************************************************************U*/
191void PGASetRealInitPercent ( PGAContext *ctx, double *median, double *percent)
192{
193     int i;
194     int stringlen;
195     double offset;
196
197    PGADebugEntered("PGASetRealInitPercent");
198    PGAFailIfSetUp("PGASetRealInitPercent");
199    PGACheckDataType("PGASetRealInitPercent", PGA_DATATYPE_REAL);
200
201    stringlen = PGAGetStringLength(ctx);
202    for (i=0; i<stringlen; i++) {
203    }
204    for (i=0; i<stringlen; i++) {
205         offset = fabs(median[i] * percent[i]);
206         ctx->init.RealMin[i] = median[i] - offset;
207         ctx->init.RealMax[i] = median[i] + offset;
208         
209    }
210    ctx->init.RealType = PGA_RINIT_PERCENT;
211
212    PGADebugExited("PGASetRealInitPercent");
213}
214
215/*U****************************************************************************
216  PGASetRealInitRange - sets the upper and lower bounds for randomly
217  initializing real-valued genes.  For each gene these bounds define an
218  interval from which the initial allele value is selected uniformly randomly.
219  The user specifies two arrays containing lower and bound for each gene to
220  define the interval.  This is the default strategy for initializing
221  real-valued strings.  The default interval is $[0,1.0]$ for each gene.
222
223  Category: Initialization
224
225  Inputs:
226     ctx - context variable
227     min - array containing the lower bound of the interval for each gene
228     mac - array containing the upper bound of the interval for each gene
229
230  Outputs:
231
232  Example:
233     Set the initialization routines to select a value for each real-valued
234     gene i uniformly randomly from the interval [-10.,i]
235     Assumes all strings are of the same length.
236
237     PGAContext *ctx;
238     double *low, *high;
239     int i, stringlen;
240     :
241     stringlen = PGAGetStringLength(ctx);
242     low  = (double *) malloc(stringlen*sizeof(double));
243     high = (double *) malloc(stringlen*sizeof(double));
244     for(i=0;i<stringlen;i++) {
245        low[i]  = -10.0;
246        high[i] = i;
247     }
248     PGASetRealInitRange(ctx, low, high);
249
250****************************************************************************U*/
251void PGASetRealInitRange (PGAContext *ctx, double *min, double *max)
252{
253     int i;
254    PGADebugEntered("PGASetRealInitRange");
255    PGAFailIfSetUp("PGASetRealInitRange");
256    PGACheckDataType("PGASetRealInitRange", PGA_DATATYPE_REAL);
257
258    for (i=ctx->ga.StringLen-1; i>=0; i--) {
259         if (max[i] < min[i])
260              PGAError(ctx, "PGASetRealInitRange: Lower bound exceeds upper "
261                       "bound for allele #", PGA_FATAL, PGA_INT, (void *) &i);
262         else
263         {
264              ctx->init.RealMin[i] = min[i];
265              ctx->init.RealMax[i] = max[i];
266         }
267    }
268    ctx->init.RealType = PGA_RINIT_RANGE;
269
270    PGADebugExited("PGASetRealInitRange");
271}
272
273
274/*U***************************************************************************
275  PGAGetMinRealInitValue - returns the minimum value used to randomly
276  initialize allele i in a real string
277
278   Category: Initialization
279
280   Inputs:
281      ctx - context variable
282      i   - an allele position
283
284   Outputs:
285      The minimum value used to randomly initialize allele i
286
287   Example:
288      PGAContext *ctx;
289      int min;
290      :
291      min = PGAGetMinRealInitValue(ctx, 0);
292
293***************************************************************************U*/
294double PGAGetMinRealInitValue (PGAContext *ctx, int i)
295{
296    PGADebugEntered("PGAGetMinRealInitValue");
297    PGAFailIfNotSetUp("PGAGetMinRealInitValue");
298    PGACheckDataType("PGAGetMinRealInitValue", PGA_DATATYPE_REAL);
299
300    if (i < 0 || i >= ctx->ga.StringLen)
301         PGAError(ctx, "PGAGetMinRealInitValue: Index out of range:",
302                  PGA_FATAL, PGA_INT, (int *) &i);
303
304    PGADebugExited("PGAGetMinRealInitValue");
305
306    return(ctx->init.RealMin[i]);
307}
308
309/*U***************************************************************************
310  PGAGetMaxRealInitValue - returns the maximum value used to randomly
311  initialize allele i in a real string
312
313   Category: Initialization
314
315   Inputs:
316      ctx - context variable
317      i   - an allele position
318
319   Outputs:
320      The maximum value used to randomly initialize allele i
321
322   Example:
323      PGAContext *ctx;
324      int max;
325      :
326      max = PGAGetMaxRealInitValue(ctx, 0);
327
328***************************************************************************U*/
329double PGAGetMaxRealInitValue (PGAContext *ctx, int i)
330{
331    PGADebugEntered("PGAGetMaxRealInitValue");
332    PGAFailIfNotSetUp("PGAGetMaxRealInitValue");
333    PGACheckDataType("PGAGetMaxRealInitValue", PGA_DATATYPE_REAL);
334
335    if (i < 0 || i >= ctx->ga.StringLen)
336         PGAError(ctx, "PGAGetMaxRealInitValue: Index out of range:",
337                  PGA_FATAL, PGA_INT, (int *) &i);
338
339    PGADebugExited("PGAGetMaxRealInitValue");
340
341    return(ctx->init.RealMax[i]);
342}
343
344
345/*U***************************************************************************
346  PGAGetRealInitType - returns the type of scheme used to randomly
347  initialize strings of data type PGA_DATATYPE_REAL.
348
349   Category: Initialization
350
351   Inputs:
352      ctx - context variable
353
354   Outputs:
355      Returns the integer corresponding to the symbolic constant
356      used to specify the scheme used to initialize real strings
357
358   Example:
359      PGAContext *ctx;
360      int inittype;
361      :
362      inittype = PGAGetRealInitType(ctx);
363      switch (inittype) {
364      case PGA_RINIT_PERCENT:
365          printf("Data Type = PGA_RINIT_PERCENT\n");
366          break;
367      case PGA_RINIT_RANGE:
368          printf("Data Type = PGA_RINIT_RANGE\n");
369          break;
370      }
371
372***************************************************************************U*/
373int PGAGetRealInitType (PGAContext *ctx)
374{
375    PGADebugEntered("PGAGetRealInitType");
376    PGAFailIfNotSetUp("PGAGetRealInitType");
377    PGACheckDataType("PGAGetRealInitType", PGA_DATATYPE_REAL);
378
379    PGADebugExited("PGAGetRealInitType");
380
381    return(ctx->init.RealType);
382}
383
384
385/*I****************************************************************************
386   PGARealCreateString - Allocate memory for a string of type PGAReal
387
388   Inputs:
389      ctx      - context variable
390      p        - string index
391      pop      - symbolic constant of the population string p is in
392      initflag - A true/false flag used in conjunction with ctx->ga.RandomInit
393                 to initialize the string either randomly or set to zero
394
395   Outputs:
396
397   Example:
398      Allocates memory and assigns the address of the allocated memory to
399      the real string field (ind->chrom) of the individual.  Also, clears
400      the string.
401
402      PGAContext *ctx;
403      int p;
404      :
405      PGARealCreateString( ctx, p, PGA_NEWPOP, PGA_FALSE );
406
407****************************************************************************I*/
408void PGARealCreateString (PGAContext *ctx, int p, int pop, int initflag)
409{
410    PGAIndividual *new = PGAGetIndividual(ctx, p, pop);
411    int i, fp;
412    PGAReal *c;
413   
414    PGADebugEntered("PGARealCreateString");
415   
416    new->chrom = (void *) malloc (ctx->ga.StringLen * sizeof(PGAReal));
417    if (new->chrom == NULL)
418        PGAError(ctx, "PGARealCreateString: No room to allocate new->chrom",
419                 PGA_FATAL, PGA_VOID, NULL);
420    c = (PGAReal *)new->chrom;
421    if (initflag)
422        if (ctx->fops.InitString) {
423            fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1;
424            (*ctx->fops.InitString)(&ctx, &fp, &pop);
425        } else {
426            (*ctx->cops.InitString)(ctx, p, pop);
427        }
428    else
429        for (i=ctx->ga.StringLen-1; i>=0; i--)
430            c[i] = 0.0;
431   
432    PGADebugExited("PGARealCreateString");
433}
434
435/*I****************************************************************************
436   PGARealMutation - randomly mutates a floating point string with probability
437   mr.  Three of the four mutation operators are of the form v = v +- p*v.
438   That is, the new value of v (allele i) is the old value + or - a percentage,
439   p, of the old value. There are three possibilities for choosing p: (1)
440   constant value (0.01 by default), (2) selected uniformly on (0,UB) (UB is
441   .1 by default), and (3) selected from a Gaussian distribution (with mean 0
442   and standard deviation .1 be default).  The change to an allele, p*v, is
443   added or subtracted to the old value with a probability of .5. The fourth
444   option is to replace v with a value selected uniformly random from the
445   initialization range of that gene. Alleles to mutate are randomly selected.
446   The value set by the routine PGASetMutationRealValue is used as p, UB, and
447   sigma in cases 1,2, and 3, respectively.
448
449   Inputs:
450      ctx - context variable
451      p        - string index
452      pop      - symbolic constant of the population string p is in
453      mr  - probability of mutating a real-valued gene
454
455   Outputs: The number of mutations performed.
456
457   Example:
458      Sets the value of the ith gene of string p
459      in population PGA_NEWPOP to one
460
461      PGAContext *ctx;
462      int NumMutations, p;
463      :
464      NumMutations = PGARealMutation( ctx, p, PGA_NEWPOP, .001 );
465
466****************************************************************************I*/
467int PGARealMutation( PGAContext *ctx, int p, int pop, double mr )
468{
469     PGAReal *c;
470     int i;
471     int count = 0;
472     double val;
473
474     PGADebugEntered("PGARealMutation");
475
476     c = (PGAReal *)PGAGetIndividual(ctx, p, pop)->chrom;
477     for(i=0; i<ctx->ga.StringLen; i++) {
478
479         /* randomly choose an allele   */
480         if ( PGARandomFlip(ctx, mr) ) {
481
482             /* generate on range, or calculate multplier */
483             switch (ctx->ga.MutationType) {
484             case PGA_MUTATION_RANGE:
485                 c[i] = PGARandomUniform(ctx, ctx->init.RealMin[i],
486                                              ctx->init.RealMax[i]);
487                 break;
488             case PGA_MUTATION_CONSTANT:
489                 val = ctx->ga.MutateRealValue;
490                 break;
491             case PGA_MUTATION_UNIFORM:
492                 val = PGARandomUniform (ctx, 0.0, ctx->ga.MutateRealValue);
493                 break;
494             case PGA_MUTATION_GAUSSIAN:
495                 val = PGARandomGaussian(ctx, 0.0, ctx->ga.MutateRealValue);
496                 break;
497             default:
498                  PGAError(ctx, "PGARealMutation: Invalid value of "
499                           "ga.MutationType:", PGA_FATAL, PGA_INT,
500                           (void *) &(ctx->ga.MutationType));
501                  break;
502             }
503
504             /* apply multiplier calculated in switch above */
505             if ( (ctx->ga.MutationType == PGA_MUTATION_CONSTANT) ||
506                  (ctx->ga.MutationType == PGA_MUTATION_UNIFORM)  ||
507                  (ctx->ga.MutationType == PGA_MUTATION_GAUSSIAN)
508                ) {
509                 /* add/subtract from allele */
510                 if ( PGARandomFlip(ctx, .5) )
511                     c[i] += val*c[i];
512                 else
513                     c[i] -= val*c[i];
514             }
515
516             /* reset to min/max if bounded flag true and outside range */
517             if( ctx->ga.MutateBoundedFlag == PGA_TRUE ) {
518                if( c[i] < ctx->init.RealMin[i])
519                    c[i] = ctx->init.RealMin[i];
520                if( c[i] > ctx->init.RealMax[i])
521                    c[i] = ctx->init.RealMax[i];
522             }
523
524             /* increment mutation count */
525             count++;
526         }
527     }
528
529     PGADebugExited("PGARealMutation");
530
531     return(count);
532}
533
534/*I****************************************************************************
535   PGARealOneptCrossover - this routine performs one point crossover on two
536   parent strings, producing (via side effect) the crossed children child1 and
537   child2
538
539   Inputs:
540      ctx  - context variable
541      p1   - the first parent string
542      p2   - the second parent string
543      pop1 - symbolic constant of the population containing string p1 and p2
544      c1   - the first child string
545      c2   - the second child string
546      pop2 - symbolic constant of the population to contain string c1 and c2
547
548   Outputs:
549      c1 and c2 in population pop2 are modified by side-effect.
550
551   Example:
552      Performs crossover on the two parent strings m and d, producing
553      children s and b.
554
555      PGAContext *ctx;
556      int m, d, s, b;
557      :
558      PGARealOneptCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP );
559
560****************************************************************************I*/
561void PGARealOneptCrossover( PGAContext *ctx, int p1, int p2, int pop1,
562                           int c1, int c2, int pop2)
563{
564     PGAReal *parent1 = (PGAReal *)PGAGetIndividual(ctx, p1,
565                                                    pop1)->chrom;
566     PGAReal *parent2 = (PGAReal *)PGAGetIndividual(ctx, p2,
567                                                    pop1)->chrom;
568     PGAReal *child1  = (PGAReal *)PGAGetIndividual(ctx, c1,
569                                                    pop2)->chrom;
570     PGAReal *child2  = (PGAReal *)PGAGetIndividual(ctx, c2,
571                                                    pop2)->chrom;
572     int i, xsite;
573
574    PGADebugEntered("PGARealOneptCrossover");
575
576    xsite = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1);
577
578    for(i=0;i<xsite;i++) {
579        child1[i] = parent1[i];
580        child2[i] = parent2[i];
581    }
582
583    for(i=xsite;i<ctx->ga.StringLen;i++) {
584        child1[i] = parent2[i];
585        child2[i] = parent1[i];
586    }
587
588    PGADebugExited("PGARealOneptCrossover");
589}
590
591
592/*I****************************************************************************
593   PGARealTwoptCrossover - performs two-point crossover on two parent strings
594   producing two children via side-effect
595
596   Inputs:
597      ctx  - context variable
598      p1   - the first parent string
599      p2   - the second parent string
600      pop1 - symbolic constant of the population containing string p1 and p2
601      c1   - the first child string
602      c2   - the second child string
603      pop2 - symbolic constant of the population to contain string c1 and c2
604
605   Outputs:
606      c1 and c2 in population pop2 are modified by side-effect.
607
608   Example:
609      Performs crossover on the two parent strings m and d, producing
610      children s and b.
611
612      PGAContext *ctx;
613      int m, d, s, b;
614      :
615      PGARealTwoptCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP );
616
617****************************************************************************I*/
618void PGARealTwoptCrossover( PGAContext *ctx, int p1, int p2, int pop1,
619                           int c1, int c2, int pop2)
620{
621     PGAReal *parent1 = (PGAReal *)PGAGetIndividual(ctx, p1,
622                                                    pop1)->chrom;
623     PGAReal *parent2 = (PGAReal *)PGAGetIndividual(ctx, p2,
624                                                    pop1)->chrom;
625     PGAReal *child1  = (PGAReal *)PGAGetIndividual(ctx, c1,
626                                                    pop2)->chrom;
627     PGAReal *child2  = (PGAReal *)PGAGetIndividual(ctx, c2,
628                                                    pop2)->chrom;
629     int i, temp, xsite1, xsite2;
630
631    PGADebugEntered("PGARealTwoptCrossover");
632
633    /* pick two cross sites such that xsite2 > xsite1 */
634    xsite1 = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1);
635    xsite2 = xsite1;
636    while ( xsite2 == xsite1 )
637        xsite2 = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1);
638    if ( xsite1 > xsite2 ) {
639        temp   = xsite1;
640        xsite1 = xsite2;
641        xsite2 = temp;
642    }
643
644    for(i=0;i<xsite1;i++) {
645        child1[i] = parent1[i];
646        child2[i] = parent2[i];
647    }
648
649    for(i=xsite1;i<xsite2;i++) {
650        child1[i] = parent2[i];
651        child2[i] = parent1[i];
652    }
653
654    for(i=xsite2;i<ctx->ga.StringLen;i++) {
655        child1[i] = parent1[i];
656        child2[i] = parent2[i];
657    }
658
659    PGADebugExited("PGARealTwoptCrossover");
660}
661
662
663/*I****************************************************************************
664   PGARealUniformCrossover - performs uniform crossover on two parent strings
665   producing two children via side-effect
666
667   Inputs:
668      ctx  - context variable
669      p1   - the first parent string
670      p2   - the second parent string
671      pop1 - symbolic constant of the population containing string p1 and p2
672      c1   - the first child string
673      c2   - the second child string
674      pop2 - symbolic constant of the population to contain string c1 and c2
675
676   Outputs:
677      c1 and c2 in population pop2 are modified by side-effect.
678
679   Example:
680      Performs crossover on the two parent strings m and d, producing
681      children s and b.
682
683      PGAContext *ctx;
684      int m, d, s, b;
685      :
686      PGARealUniformCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP );
687
688****************************************************************************I*/
689void PGARealUniformCrossover( PGAContext *ctx, int p1, int p2, int pop1,
690                             int c1, int c2, int pop2)
691{
692     PGAReal *parent1 = (PGAReal *)PGAGetIndividual(ctx, p1,
693                                                    pop1)->chrom;
694     PGAReal *parent2 = (PGAReal *)PGAGetIndividual(ctx, p2,
695                                                    pop1)->chrom;
696     PGAReal *child1  = (PGAReal *)PGAGetIndividual(ctx, c1,
697                                                    pop2)->chrom;
698     PGAReal *child2  = (PGAReal *)PGAGetIndividual(ctx, c2,
699                                                    pop2)->chrom;
700     int i;
701
702    PGADebugEntered("PGARealUniformCrossover");
703
704    for(i=0;i<ctx->ga.StringLen;i++) {
705        if ( parent1[i] == parent2[i] ) {
706            child1[i] = parent1[i];
707            child2[i] = parent2[i];
708        }
709        else {
710            if(PGARandomFlip(ctx, ctx->ga.UniformCrossProb)) {
711                child1[i] = parent1[i];
712                child2[i] = parent2[i];
713            }
714            else {
715                child1[i] = parent2[i];
716                child2[i] = parent1[i];
717            }
718        }
719    }
720
721    PGADebugExited("PGARealUniformCrossover");
722}
723
724/*I****************************************************************************
725   PGARealPrintString - writes a real-valued string to a file.  This routine
726   casts the void string pointer it is passed as the second argument.
727
728   Inputs:
729      ctx - context variable
730      fp  - file pointer to file to write the string to
731      p   - index of the string to write out
732      pop - symbolic constant of the population string p is in
733
734   Outputs:
735
736   Example:
737      Write string s to stdout.
738
739      PGAContext *ctx;
740      int s;
741      :
742      PGARealPrintString( ctx, stdout, s, PGA_NEWPOP );
743
744****************************************************************************I*/
745void PGARealPrintString (PGAContext *ctx, FILE *fp, int p, int pop)
746{
747    PGAReal *c = (PGAReal *)PGAGetIndividual(ctx, p, pop)->chrom;
748    int i;
749
750    PGADebugEntered("PGARealPrintString");
751
752    for(i = 0; i < ctx->ga.StringLen; i++)
753    {
754        switch ( i % 5 )
755        {
756        case 0:
757            fprintf ( fp, "#%4d: [%11.7g]",i,c[i]);
758            break;
759        case 1:
760        case 2:
761        case 3:
762            fprintf ( fp, ", [%11.7g]",c[i]);
763            break;
764        case 4:
765            fprintf ( fp, ", [%11.7g]",c[i]);
766            if (i+1 < ctx->ga.StringLen)
767                fprintf ( fp, "\n");
768            break;
769        }
770    }
771    fprintf ( fp, "\n" );
772
773    PGADebugExited("PGARealPrintString");
774}
775
776
777/*I****************************************************************************
778   PGARealCopyString - Copy one real-valued string string to another
779
780   Inputs:
781      ctx - context variable
782      p1   - string to copy
783      pop1 - symbolic constant of population containing string p1
784      p2   - string to copy p1 to
785      pop2 - symbolic constant of population containing string p2
786
787   Outputs:
788      String p2 in population pop2 is modified to be a copy of string
789      p1 in population pop1.
790
791   Example:
792      Copy string x to y.
793
794      PGAContext *ctx;
795      int x, y;
796      :
797      PGARealCopyString (ctx, x, PGA_OLDPOP, y, PGA_NEWPOP);
798
799****************************************************************************I*/
800void PGARealCopyString ( PGAContext *ctx, int p1, int pop1, int p2, int pop2)
801{
802    PGAReal *source = (PGAReal *)PGAGetIndividual(ctx, p1, pop1)->chrom;
803    PGAReal *dest   = (PGAReal *)PGAGetIndividual(ctx, p2, pop2)->chrom;
804    int i;
805
806    PGADebugEntered("PGARealCopyString");
807
808    for (i=ctx->ga.StringLen-1; i>=0; i--)
809        *(dest++) = *(source++);
810
811    PGADebugExited("PGARealCopyString");
812}
813
814
815/*I****************************************************************************
816   PGARealDuplicate - Returns true if real-valued string a is a duplicate of
817   real-valued string b, else returns false.
818
819   Inputs:
820      ctx - context variable
821      p1   - string index of the first string to compare
822      pop1 - symbolic constant of the population string p1 is in
823      p2   - string index of the second string to compare
824      pop2 - symbolic constant of the population string p2 is in
825
826   Outputs:
827      Returns true/false if strings are duplicates
828
829   Example:
830      Compare strings x with y to see if they are duplicates
831
832      PGAContext *ctx;
833      int x, y;
834      :
835      if ( PGARealDuplicate( ctx, x, PGA_OLDPOP, y, PGA_OLDPOP ) )
836          printf("strings are duplicates\n");
837
838****************************************************************************I*/
839int PGARealDuplicate( PGAContext *ctx, int p1, int pop1, int p2, int pop2)
840{
841     PGAReal *a = (PGAReal *)PGAGetIndividual(ctx, p1, pop1)->chrom;
842     PGAReal *b = (PGAReal *)PGAGetIndividual(ctx, p2, pop2)->chrom;
843     int i;
844
845    PGADebugEntered("PGARealDuplicate");
846
847     i = ctx->ga.StringLen-1;
848     if (a[0] == b[0])
849       for(; (i>0) && (a[i] == b[i]); i--);
850
851    PGADebugExited("PGARealDuplicate");
852
853     return((i==0) ? PGA_TRUE : PGA_FALSE);
854}
855
856/*I****************************************************************************
857   PGARealInitString - randomly initialize a string of type PGAReal
858
859   Inputs:
860      ctx - context variable
861      p   - index of string to randomly initialize
862      pop - symbolic constant of the population string p is in
863
864   Outputs:
865      String p in population pop is randomly initialized by side-effect.
866
867   Example:
868      PGAContext *ctx;
869      int p;
870      :
871      PGARealInitString (ctx, p, PGA_NEWPOP);
872
873****************************************************************************I*/
874void PGARealInitString ( PGAContext *ctx, int p, int pop)
875{
876     int i;
877     PGAReal *c = (PGAReal *)PGAGetIndividual(ctx, p, pop)->chrom;
878
879     PGADebugEntered("PGARealInitString");
880
881     for (i = 0; i < ctx->ga.StringLen; i++)
882          c[i] = PGARandomUniform(ctx, ctx->init.RealMin[i],
883                                  ctx->init.RealMax[i]);
884
885     PGADebugExited("PGARealInitString");
886}
887
888/*I****************************************************************************
889  PGARealBuildDatatype - Build an MPI datatype for a string.
890
891  Inputs:
892     ctx   - context variable
893     p     - index of string
894     pop   - symbolic constant of population string p is in
895
896  Outputs:
897     An MPI_Datatype.
898
899  Example:
900     PGAContext   *ctx;
901     int           p;
902     MPI_Datatype  dt;
903     :
904     dt = PGARealBuildDatatype(ctx, p, pop);
905
906****************************************************************************I*/
907MPI_Datatype PGARealBuildDatatype(PGAContext *ctx, int p, int pop)
908{
909
910     int            counts[4];      /* Number of elements in each
911                                       block (array of integer) */
912     MPI_Aint       displs[4];      /* byte displacement of each
913                                       block (array of integer) */
914     MPI_Datatype   types[4];       /* type of elements in each block (array
915                                       of handles to datatype objects) */
916     MPI_Datatype   individualtype; /* new datatype (handle) */
917     PGAIndividual *traveller;      /* address of individual in question */
918
919    PGADebugEntered("PGARealBuildDatatype");
920
921     traveller = PGAGetIndividual(ctx, p, pop);
922     MPI_Address(&traveller->evalfunc, &displs[0]);
923     counts[0] = 1;
924     types[0]  = MPI_DOUBLE;
925
926     MPI_Address(&traveller->fitness, &displs[1]);
927     counts[1] = 1;
928     types[1]  = MPI_DOUBLE;
929
930     MPI_Address(&traveller->evaluptodate, &displs[2]);
931     counts[2] = 1;
932     types[2]  = MPI_INT;
933
934     MPI_Address(traveller->chrom, &displs[3]);
935     counts[3] = ctx->ga.StringLen;
936     types[3]  = MPI_DOUBLE;
937
938     MPI_Type_struct(4, counts, displs, types, &individualtype);
939     MPI_Type_commit(&individualtype);
940
941    PGADebugExited("PGARealBuildDatatype");
942
943     return (individualtype);
944}
Note: See TracBrowser for help on using the repository browser.