File: integer.c

package info (click to toggle)
pgapack 1.0.0.1-2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,348 kB
  • ctags: 1,780
  • sloc: ansic: 10,331; fortran: 2,985; sh: 574; makefile: 431; perl: 105
file content (907 lines) | stat: -rw-r--r-- 30,150 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*
 *  
 *  ********************************************************************* 
 *  (C) COPYRIGHT 1995 UNIVERSITY OF CHICAGO 
 *  *********************************************************************
 *  
 *  This software was authored by
 *  
 *  D. Levine
 *  Mathematics and Computer Science Division Argonne National Laboratory
 *  Argonne IL 60439
 *  levine@mcs.anl.gov
 *  (708) 252-6735
 *  (708) 252-5986 (FAX)
 *  
 *  with programming assistance of participants in Argonne National 
 *  Laboratory's SERS program.
 *  
 *  This program contains material protectable under copyright laws of the 
 *  United States.  Permission is hereby granted to use it, reproduce it, 
 *  to translate it into another language, and to redistribute it to 
 *  others at no charge except a fee for transferring a copy, provided 
 *  that you conspicuously and appropriately publish on each copy the 
 *  University of Chicago's copyright notice, and the disclaimer of 
 *  warranty and Government license included below.  Further, permission 
 *  is hereby granted, subject to the same provisions, to modify a copy or 
 *  copies or any portion of it, and to distribute to others at no charge 
 *  materials containing or derived from the material.
 *  
 *  The developers of the software ask that you acknowledge its use in any 
 *  document referencing work based on the  program, such as published 
 *  research.  Also, they ask that you supply to Argonne National 
 *  Laboratory a copy of any published research referencing work based on 
 *  the software.
 *  
 *  Any entity desiring permission for further use must contact:
 *  
 *  J. Gleeson
 *  Industrial Technology Development Center Argonne National Laboratory
 *  Argonne IL 60439
 *  gleesonj@smtplink.eid.anl.gov
 *  (708) 252-6055
 *  
 *  ******************************************************************** 
 *  DISCLAIMER
 *  
 *  THIS PROGRAM WAS PREPARED AS AN ACCOUNT OF WORK SPONSORED BY AN AGENCY 
 *  OF THE UNITED STATES GOVERNMENT.  NEITHER THE UNIVERSITY OF CHICAGO, 
 *  THE UNITED STATES GOVERNMENT NOR ANY OF THEIR EMPLOYEES MAKE ANY 
 *  WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR 
 *  RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY 
 *  INFORMATION OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT 
 *  INFRINGE PRIVATELY OWNED RIGHTS.
 *  
 *  ********************************************************************** 
 *  GOVERNMENT LICENSE
 *  
 *  The Government is granted for itself and others acting on its behalf a 
 *  paid-up, non-exclusive, irrevocable worldwide license in this computer 
 *  software to reproduce, prepare derivative works, and perform publicly 
 *  and display publicly.
 */

/*****************************************************************************
*     FILE: integer.c: This file contains the routines specific to the integer
*                      data structure
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
*              Brian P. Walenz
*****************************************************************************/

#include "pgapack.h"

/*U****************************************************************************
   PGASetIntegerAllele - sets the value of a (integer) allele.

   Category: Fitness & Evaluation

   Inputs:
      ctx - context variable
      p   - string index
      pop - symbolic constant of the population the string is in
      i   - allele index
      val - integer value to set the allele to

   Outputs:

   Example:
      Set the value of the ith allele of string p in population PGA_NEWPOP
      to 64.

      PGAContext *ctx;
      int p, i;
      :
      PGASetIntegerAllele (ctx, p, PGA_NEWPOP, i, 64)

****************************************************************************U*/
void PGASetIntegerAllele (PGAContext *ctx, int p, int pop, int i, int value)
{
    PGAIndividual *ind;
    PGAInteger     *chrom;

    PGADebugEntered("PGASetIntegerAllele");
    PGACheckDataType("PGASetIntegerAllele", PGA_DATATYPE_INTEGER);

    ind = PGAGetIndividual ( ctx, p, pop );
    chrom = (PGAInteger *)ind->chrom;
    chrom[i] = value;

    PGADebugExited("PGASetIntegerAllele");
}

/*U****************************************************************************
   PGAGetIntegerAllele - Returns the value of allele i of member p in
   population pop.  Assumes the data type is PGA_DATATYPE_INTEGER.

   Category: Fitness & Evaluation

   Inputs:
      ctx - context variable
      p   - string index
      pop - symbolic constant of the population the string is in
      i   - allele index

   Outputs:

   Example:
      Returns the value of the ith integer allele of string p
      in population PGA_NEWPOP.

      PGAContext *ctx;
      int p, i, k;
      :
      k =  PGAGetIntegerAllele ( ctx, p, PGA_NEWPOP, i )

****************************************************************************U*/
int PGAGetIntegerAllele (PGAContext *ctx, int p, int pop, int i)
{
    PGAIndividual *ind;
    PGAInteger     *chrom;

    PGADebugEntered("PGAGetIntegerAllele");

    PGACheckDataType("PGAGetIntegerAllele", PGA_DATATYPE_INTEGER);

    ind = PGAGetIndividual ( ctx, p, pop );
    chrom = (PGAInteger *)ind->chrom;

    PGADebugExited("PGAGetIntegerAllele");

    return( (int) chrom[i] );
}

/*U****************************************************************************
  PGASetIntegerInitPermute - sets a flag to tell the initialization routines
  to set each integer-valued gene to a random permutation of the values given
  by an upper and lower bound.  The length of the interval must be the same
  as the string length.  This is the default strategy for initializing
  integer-valued strings. The default interval is [0,L-1] where L is the
  string length.  No string initialization is done by this call.

  Category: Initialization

  Inputs:
     ctx - context variable
     min - the lower bound of numbers used in the permutation
     max - the upper bound of numbers used in the permutation

  Outputs:

  Example:
      Set the initialization routines to set each gene to a random and
      unique value from the interval $[500,599]$.

      PGAContext *ctx;
      :
      PGASetIntegerInitPermute(ctx, 500, 599)}

****************************************************************************U*/
void PGASetIntegerInitPermute ( PGAContext *ctx, int min, int max)
{
     int i, range;

    PGADebugEntered("PGASetIntegerInitPermute");
    PGAFailIfSetUp("PGASetIntegerInitPermute");
    PGACheckDataType("PGASetIntegerInitPermute", PGA_DATATYPE_INTEGER);

     range = max - min + 1;
     if (max <= min)
          PGAError(ctx, "PGASetIntegerInitPermute: max does not exceed min:",
                   PGA_FATAL, PGA_INT, (void *) &max);
     else if (range != ctx->ga.StringLen) {
          PGAError(ctx, "PGASetIntegerInitPermute: range of:",
                   PGA_FATAL, PGA_INT, (void *) &range);
          PGAError(ctx, "PGASetIntegerInitPermute: does not equal "
                   "string length:", PGA_FATAL, PGA_INT,
                    (void *) &(ctx->ga.StringLen));
     }
     else
     {
          ctx->init.IntegerType = PGA_IINIT_PERMUTE;
          for (i = 0; i < ctx->ga.StringLen; i++)
          {
               ctx->init.IntegerMin[i] = min;
               ctx->init.IntegerMax[i] = max;
          }
     }

    PGADebugExited("PGASetIntegerInitPermute");
}

/*U****************************************************************************
  PGASetIntegerInitRange - sets a flag to tell the initialization routines to
  set each integer-valued gene to a value chosen randomly from the interval
  given by an upper and lower bound.  No string initialization is done by
  this call.

  Category: Initialization

  Inputs:
     ctx - context variable
     min - array of lower bounds that define the interval the gene is
           initialized from
     max - array of upper bounds that define the interval the gene is
           initialized from

  Outputs:

  Example:
      Set the initialization routines to select a value for gene i
      uniformly randomly from the interval [0,i].  Assumes all strings
      are of the same length.

      PGAContext *ctx;
      int *low, *high, stringlen, i;
      :
      stringlen = PGAGetStringLength(ctx);
      low  = (int *) malloc(stringlen*sizeof(int));
      high = (int *) malloc(stringlen*sizeof(int));
      for(i=0;i<stringlen;i++) {
          low[i]  = 0;
          high[i] = i
      }
      PGASetIntegerInitRange(ctx, low, high);

****************************************************************************U*/
void PGASetIntegerInitRange (PGAContext *ctx, int *min, int *max)
{
     int i;

     PGADebugEntered("PGASetIntegerInitRange");
     PGAFailIfSetUp("PGASetIntegerInitRange");
     PGACheckDataType("PGASetIntegerInitRange", PGA_DATATYPE_INTEGER);

     for (i = 0; i < ctx->ga.StringLen; i++)
     {
        if (max[i] < min[i])
            PGAError(ctx, "PGASetIntegerInitRange: Lower bound exceeds upper "
                    "bound for allele #", PGA_FATAL, PGA_INT, (void *) &i);
        else {
            ctx->init.IntegerMin[i] = min[i];
            ctx->init.IntegerMax[i] = max[i];
        }
     }
     ctx->init.IntegerType = PGA_IINIT_RANGE;

     PGADebugExited("PGASetIntegerInitRange");
}

/*U***************************************************************************
  PGAGetIntegerInitType - returns the type of scheme used to randomly
  initialize strings of data type PGA_DATATYPE_INTEGER.

   Category: Initialization

   Inputs:
      ctx - context variable

   Outputs:
      Returns the integer corresponding to the symbolic constant
      used to specify the scheme used to initialize integer strings

   Example:
      PGAContext *ctx;
      int inittype;
      :
      inittype = PGAGetIntegerInitType(ctx);
      switch (inittype) {
      case PGA_IINIT_PERMUTE:
          printf("Data Type = PGA_IINIT_PERMUTE\n");
          break;
      case PGA_IINIT_RANGE:
          printf("Data Type = PGA_IINIT_RANGE\n");
          break;
      }

***************************************************************************U*/
int PGAGetIntegerInitType (PGAContext *ctx)
{
    PGADebugEntered("PGAGetIntegerInitType");
    PGAFailIfNotSetUp("PGAGetIntegerInitType");
    PGACheckDataType("PGAGetIntegerInitType", PGA_DATATYPE_INTEGER);

    PGADebugExited("PGAGetIntegerInitType");

    return(ctx->init.IntegerType);
}

/*U***************************************************************************
   PGAGetMinIntegerInitValue - returns the minimum of the range of integers
   used to randomly initialize integer strings.

   Category: Initialization

   Inputs:
      ctx - context variable

   Outputs:
      The minimum of the range of integers used to randomly initialize
      integer strings

   Example:
      PGAContext *ctx;
      int min;
      :
      min = PGAGetMinIntegerInitValue(ctx);

***************************************************************************U*/
int PGAGetMinIntegerInitValue (PGAContext *ctx, int i)
{
    PGADebugEntered("PGAGetMinIntegerInitValue");
    PGAFailIfNotSetUp("PGAGetMinIntegerInitValue");
    PGACheckDataType("PGASetIntegerAllele", PGA_DATATYPE_INTEGER);

    if (i < 0 || i >= ctx->ga.StringLen)
         PGAError(ctx, "PGAGetMinIntegerInitValue: Index out of range:",
                  PGA_FATAL, PGA_INT, (int *) &i);

    PGADebugExited("PGAGetMinIntegerInitValue");

    return(ctx->init.IntegerMin[i]);
}

/*U***************************************************************************
   PGAGetMaxIntegerInitValue - returns the maximum of the range of integers
   used to randomly initialize integer strings.

   Category: Initialization

   Inputs:
      ctx - context variable

   Outputs:
      The maximum of the range of integers used to randomly initialize
      integer strings.

   Example:
      PGAContext *ctx;
      int max;
      :
      max = PGAGetMaxIntegerInitValue(ctx);

***************************************************************************U*/
int PGAGetMaxIntegerInitValue (PGAContext *ctx, int i)
{
    PGADebugEntered("PGAGetMaxIntegerInitValue");
    PGAFailIfNotSetUp("PGAGetMaxIntegerInitValue");
    PGACheckDataType("PGAGetMaxIntegerInitValue", PGA_DATATYPE_INTEGER);

    if (i < 0 || i >= ctx->ga.StringLen)
         PGAError(ctx, "PGAGetMaxIntegerInitValue: Index out of range:",
                  PGA_FATAL, PGA_INT, (int *) &i);

    PGADebugExited("PGAGetMaxIntegerInitValue");

    return(ctx->init.IntegerMax[i]);
}


/*I****************************************************************************
   PGAIntegerCreateString - Allocate memory for a string of type PGAInteger,
   and initializes or clears the string according to initflag.

   Inputs:
      ctx      - context variable
      p        - string index
      pop      - symbolic constant of the population string p is in
      initflag - A true/false flag used in conjunction with ctx->ga.RandomInit
                 to initialize the string either randomly or set to zero

   Outputs:
      new      - a pointer set to the address of the allocated memory

   Example:
      Allocates and clears memory and assigns the address of the allocated
      memory to the string field (ind->chrom) of the individual.

      PGAContext *ctx;
      PGAIndividual *ind;
      :
      PGAIntegerCreateString( ctx, ind, PGA_FALSE );

****************************************************************************I*/
void PGAIntegerCreateString (PGAContext *ctx, int p, int pop, int InitFlag)
{
    int i, fp;
    PGAInteger *c;
    PGAIndividual *new = PGAGetIndividual(ctx, p, pop);

    PGADebugEntered("PGAIntegerCreateString");

    new->chrom = (void *)malloc(ctx->ga.StringLen * sizeof(PGAInteger));
    if (new->chrom == NULL)
	PGAError(ctx, "PGAIntegerCreateString: No room to allocate "
		 "new->chrom", PGA_FATAL, PGA_VOID, NULL);
    c = (PGAInteger *)new->chrom;
    if (InitFlag)
	if (ctx->fops.InitString) {
	    fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1;
	    (*ctx->fops.InitString)(&ctx, &fp, &pop);
	} else {
	    (*ctx->cops.InitString)(ctx, p, pop);
	}
    else
	for (i=0; i<ctx->ga.StringLen; i++)
	    c[i] = 0;
    
    PGADebugExited("PGAIntegerCreateString");
}

/*I****************************************************************************
   PGAIntegerMutation - randomly mutates an integer-valued gene with a
   specified probability. This routine is called from PGAMutation and must
   cast the void string pointer it is passed as the second argument.

   Inputs:
      ctx      - context variable
      p        - string index
      pop      - symbolic constant of the population string p is in
      mr       - probability of mutating an integer-valued gene

   Outputs:
      Returns the number of mutations

   Example:

****************************************************************************I*/
int PGAIntegerMutation( PGAContext *ctx, int p, int pop, double mr )
{
     PGAInteger *c;
     int i, j, temp;
     int count = 0;

     PGADebugEntered("PGAIntegerMutation");

     c = (PGAInteger *)PGAGetIndividual(ctx, p, pop)->chrom;
     for(i=0; i<ctx->ga.StringLen; i++) {

         /* randomly choose an allele   */
         if ( PGARandomFlip(ctx, mr) ) {

             /* apply appropriate mutation operator */
             switch (ctx->ga.MutationType) {
             case PGA_MUTATION_CONSTANT:
                 /* add or subtract from allele */             
                 if ( PGARandomFlip(ctx, .5) )
                      c[i] += ctx->ga.MutateIntegerValue;
                 else
                      c[i] -= ctx->ga.MutateIntegerValue;
                 break;
             case PGA_MUTATION_PERMUTE:
                 /* could check for j == i if we were noble */
                 j = PGARandomInterval(ctx, 0, ctx->ga.StringLen);
                 temp = c[i];                 
                 c[i] = c[j];
                 c[j] = temp;                 
                 break;
             case PGA_MUTATION_RANGE:
                 c[i] = PGARandomInterval(ctx, ctx->init.IntegerMin[i],
                                               ctx->init.IntegerMax[i]);
                 break;
             default:
                  PGAError(ctx, "PGAIntegerMutation: Invalid value of "
                           "ga.MutationType:", PGA_FATAL, PGA_INT,
                           (void *) &(ctx->ga.MutationType));
                  break;
             }

             /* reset to min/max if bounded flag true and outside range */
             if( ctx->ga.MutateBoundedFlag == PGA_TRUE ) {
                 if( c[i] < ctx->init.IntegerMin[i])
                     c[i] = ctx->init.IntegerMin[i];
                 if( c[i] > ctx->init.IntegerMax[i])
                     c[i] = ctx->init.IntegerMax[i];
             }

             count++;
         }
     }
     PGADebugExited("PGAIntegerMutation");
     return(count);
}

/*I****************************************************************************
   PGAIntegerOneptCrossover - performs one-point crossover on two parent
   strings producing two children via side-effect

   Inputs:
      ctx  - context variable
      p1   - the first parent string
      p2   - the second parent string
      pop1 - symbolic constant of the population containing string p1 and p2
      c1   - the first child string
      c2   - the second child string
      pop2 - symbolic constant of the population to contain string c1 and c2

   Outputs:

   Example:
      Performs crossover on the two parent strings m and d, producing
      children s and b.

      PGAContext *ctx;
      int m, d, s, b;
      :
      PGAIntegerOneptCrossover(ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP);

****************************************************************************I*/
void PGAIntegerOneptCrossover(PGAContext *ctx, int p1, int p2, int pop1,
                              int c1, int c2, int pop2)
{
     PGAInteger *parent1 = (PGAInteger *)PGAGetIndividual(ctx, p1,
                                                          pop1)->chrom;
     PGAInteger *parent2 = (PGAInteger *)PGAGetIndividual(ctx, p2,
                                                          pop1)->chrom;
     PGAInteger *child1  = (PGAInteger *)PGAGetIndividual(ctx, c1,
                                                          pop2)->chrom;
     PGAInteger *child2  = (PGAInteger *)PGAGetIndividual(ctx, c2,
                                                          pop2)->chrom;
     int i, xsite;

    PGADebugEntered("PGAIntegerOneptCrossover");

    xsite = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1);

    for(i=0;i<xsite;i++) {
        child1[i] = parent1[i];
        child2[i] = parent2[i];
    }

    for(i=xsite;i<ctx->ga.StringLen;i++) {
        child1[i] = parent2[i];
        child2[i] = parent1[i];
    }

    PGADebugExited("PGAIntegerOneptCrossover");
}


/*I****************************************************************************
   PGAIntegerTwoptCrossover - performs two-point crossover on two parent
   strings producing two children via side-effect

   Inputs:
      ctx  - context variable
      p1   - the first parent string
      p2   - the second parent string
      pop1 - symbolic constant of the population containing string p1 and p2
      c1   - the first child string
      c2   - the second child string
      pop2 - symbolic constant of the population to contain string c1 and c2

   Outputs:

   Example:
      Performs crossover on the two parent strings m and d, producing
      children s and b.

      PGAContext *ctx;
      int m, d, s, b;
      :
      PGAIntegerTwoptCrossover(ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP);

****************************************************************************I*/
void PGAIntegerTwoptCrossover( PGAContext *ctx, int p1, int p2, int pop1,
                              int c1, int c2, int pop2)
{
     PGAInteger *parent1 = (PGAInteger *)PGAGetIndividual(ctx, p1,
                                                          pop1)->chrom;
     PGAInteger *parent2 = (PGAInteger *)PGAGetIndividual(ctx, p2,
                                                          pop1)->chrom;
     PGAInteger *child1  = (PGAInteger *)PGAGetIndividual(ctx, c1,
                                                          pop2)->chrom;
     PGAInteger *child2  = (PGAInteger *)PGAGetIndividual(ctx, c2,
                                                          pop2)->chrom;
     int i, temp, xsite1, xsite2;

    PGADebugEntered("PGAIntegerTwoptCrossover");

    /* pick two cross sites such that xsite2 > xsite1 */
    xsite1 = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1);
    xsite2 = xsite1;
    while ( xsite2 == xsite1 )
        xsite2 = PGARandomInterval(ctx, 1,ctx->ga.StringLen-1);
    if ( xsite1 > xsite2 ) {
        temp   = xsite1;
        xsite1 = xsite2;
        xsite2 = temp;
    }

    for(i=0;i<xsite1;i++) {
        child1[i] = parent1[i];
        child2[i] = parent2[i];
    }

    for(i=xsite1;i<xsite2;i++) {
        child1[i] = parent2[i];
        child2[i] = parent1[i];
    }

    for(i=xsite2;i<ctx->ga.StringLen;i++) {
        child1[i] = parent1[i];
        child2[i] = parent2[i];
    }

    PGADebugExited("PGAIntegerTwoptCrossover");
}


/*I****************************************************************************
   PGAIntegerUniformCrossover - performs uniform crossover on two parent
   strings producing two children via side-effect

   Inputs:
      ctx  - context variable
      p1   - the first parent string
      p2   - the second parent string
      pop1 - symbolic constant of the population containing string p1 and p2
      c1   - the first child string
      c2   - the second child string
      pop2 - symbolic constant of the population to contain string c1 and c2

   Outputs:

   Example:
      Performs crossover on the two parent strings m and d, producing
      children s and b.

      PGAContext *ctx;
      int m, d, s, b;
      :
      PGAIntegerUniformCrossover( ctx, m, d, PGA_OLDPOP, s, b, PGA_NEWPOP);

****************************************************************************I*/
void PGAIntegerUniformCrossover(PGAContext *ctx, int p1, int p2, int pop1,
                                int c1, int c2, int pop2)
{
     PGAInteger *parent1 = (PGAInteger *)PGAGetIndividual(ctx, p1,
                                                          pop1)->chrom;
     PGAInteger *parent2 = (PGAInteger *)PGAGetIndividual(ctx, p2,
                                                          pop1)->chrom;
     PGAInteger *child1  = (PGAInteger *)PGAGetIndividual(ctx, c1,
                                                          pop2)->chrom;
     PGAInteger *child2  = (PGAInteger *)PGAGetIndividual(ctx, c2,
                                                          pop2)->chrom;
     int i;

    PGADebugEntered("PGAIntegerUniformCrossover");

    for(i=0;i<ctx->ga.StringLen;i++) {
        if ( parent1[i] == parent2[i] ) {
            child1[i] = parent1[i];
            child2[i] = parent2[i];
        }
        else {
            if(PGARandomFlip(ctx, ctx->ga.UniformCrossProb)) {
                child1[i] = parent1[i];
                child2[i] = parent2[i];
            }
            else {
                child1[i] = parent2[i];
                child2[i] = parent1[i];
            }
        }
    }

    PGADebugExited("PGAIntegerUniformCrossover");
}

/*I****************************************************************************
   PGAIntegerPrintString - writes an integer-valued string to a file.

   Inputs:
      ctx - context variable
      fp  - file pointer to file to write the string to
      p   - index of the string to write out
      pop - symbolic constant of the population string p is in

   Outputs:

   Example:
      Write member p in population PGA_NEWPOP to stdout.

      PGAContext *ctx;
      int  p;
      :
      PGAIntegerPrintString(ctx, stdout, p, PGA_NEWPOP);

****************************************************************************I*/
void PGAIntegerPrintString ( PGAContext *ctx, FILE *fp, int p, int pop)
{
    PGAInteger *c = (PGAInteger *)PGAGetIndividual(ctx, p, pop)->chrom;
    int i;

    PGADebugEntered("PGAIntegerPrintString");

    for(i = 0; i < ctx->ga.StringLen; i++)
    {
        switch ( i % 6 )
        {
        case 0:
            fprintf ( fp, "#%5d: [%8ld]",i,c[i]);
            break;
        case 1:
        case 2:
        case 3:
        case 4:
            fprintf ( fp, ", [%8ld]",c[i]);
            break;
        case 5:
            fprintf ( fp, ", [%8ld]",c[i]);
            if (i+1 < ctx->ga.StringLen)
                fprintf ( fp, "\n");
            break;
        }
    }
    fprintf ( fp, "\n" );

    PGADebugExited("PGAIntegerPrintString");
}

/*I****************************************************************************
   PGAIntegerCopyString - Copy one integer-valued string to another.

   Inputs:
      ctx - context variable
      p1   - string to copy
      pop1 - symbolic constant of population containing string p1
      p2   - string to copy p1 to
      pop2 - symbolic constant of population containing string p2

   Outputs:

   Example:

****************************************************************************I*/
void PGAIntegerCopyString (PGAContext *ctx, int p1, int pop1, int p2, int pop2)
{
    PGAInteger *source = (PGAInteger *)PGAGetIndividual(ctx, p1, pop1)->chrom;
    PGAInteger *dest   = (PGAInteger *)PGAGetIndividual(ctx, p2, pop2)->chrom;
    int i;

    PGADebugEntered("PGAIntegerCopyString");

    for (i = 0; i < ctx->ga.StringLen; i++)
        dest[i] = source[i];

    PGADebugExited("PGAIntegerCopyString");
}

/*I****************************************************************************
   PGAIntegerDuplicate - Returns true if string a is a duplicate of
   string b, else returns false.

   Inputs:
      ctx - context variable
      p1   - string index of the first string to compare
      pop1 - symbolic constant of the population string p1 is in
      p2   - string index of the second string to compare
      pop2 - symbolic constant of the population string p2 is in

   Outputs:
      Returns true/false if strings are duplicates

   Example:

****************************************************************************I*/
int PGAIntegerDuplicate( PGAContext *ctx, int p1, int pop1, int p2, int pop2)
{
     PGAInteger *a = (PGAInteger *)PGAGetIndividual(ctx, p1, pop1)->chrom;
     PGAInteger *b = (PGAInteger *)PGAGetIndividual(ctx, p2, pop2)->chrom;
     int i;

    PGADebugEntered("PGAIntegerDuplicate");

     i = ctx->ga.StringLen-1;
     if (a[0] == b[0])
       for(; (i>0) && (a[i] == b[i]); i--);

    PGADebugExited("PGAIntegerDuplicate");

     return((i==0) ? PGA_TRUE : PGA_FALSE);
}

/*I****************************************************************************
   PGAIntegerInitString - randomly initialize a string of type PGAInteger

   Inputs:
      ctx - context variable
      p   - index of string to randomly initialize
      pop - symbolic constant of the population string p is in

   Outputs:

   Example:

****************************************************************************I*/
void PGAIntegerInitString(PGAContext *ctx, int p, int pop)
{
     int *list;
     int len, i, j;
     PGAInteger *c = (PGAInteger *)PGAGetIndividual(ctx, p, pop)->chrom;

     PGADebugEntered("PGAIntegerInitString");

     len = ctx->ga.StringLen;

     switch (ctx->init.IntegerType)
     {
     case PGA_IINIT_PERMUTE:
          list = (int *)malloc(sizeof(int) * len);
          if (list == NULL)
               PGAError(ctx, "PGAIntegerInitString: No room to allocate list",
                        PGA_FATAL, PGA_VOID, NULL);
          j = ctx->init.IntegerMin[0];
          for (i = 0; i < len; i++)
               list[i] = j++;
          for (i = 0; i < len; i++)
          {
               j = PGARandomInterval ( ctx, 0, len - i - 1 );
               c[i] = list[j];
               list[j] = list[len - i - 1];
          }
          free(list);
          break;
     case PGA_IINIT_RANGE:
          for (i = 0; i < len; i++)
               c[i] = PGARandomInterval(ctx, ctx->init.IntegerMin[i],
                                        ctx->init.IntegerMax[i]);
          break;
     }

     PGADebugExited("PGAIntegerInitString");
}

/*I****************************************************************************
  PGAIntegerBuildDatatype - Build an MPI datatype for a string of type
  PGA_DATATYPE_INTEGER.

  Inputs:
      ctx - context variable
      p   - index of string to randomly initialize
      pop - symbolic constant of the population string p is in

  Outputs:

  Example:

****************************************************************************I*/
MPI_Datatype PGAIntegerBuildDatatype(PGAContext *ctx, int p, int pop)
{

     int            counts[4];      /* Number of elements in each
                                       block (array of integer) */
     MPI_Aint       displs[4];      /* byte displacement of each
                                       block (array of integer) */
     MPI_Datatype   types[4];       /* type of elements in each block (array
                                       of handles to datatype objects) */
     MPI_Datatype   individualtype; /* new datatype (handle) */
     PGAIndividual *traveller;      /* address of individual in question */

    PGADebugEntered("PGAIntegerBuildDatatype");

     traveller = PGAGetIndividual(ctx, p, pop);
     MPI_Address(&traveller->evalfunc, &displs[0]);
     counts[0] = 1;
     types[0]  = MPI_DOUBLE;

     MPI_Address(&traveller->fitness, &displs[1]);
     counts[1] = 1;
     types[1]  = MPI_DOUBLE;

     MPI_Address(&traveller->evaluptodate, &displs[2]);
     counts[2] = 1;
     types[2]  = MPI_INT;

     MPI_Address(traveller->chrom, &displs[3]);
     counts[3] = ctx->ga.StringLen;
     types[3]  = MPI_LONG;

     MPI_Type_struct(4, counts, displs, types, &individualtype);
     MPI_Type_commit(&individualtype);

    PGADebugExited("PGAIntegerBuildDatatype");

     return (individualtype);
}