File: evaluate.c

package info (click to toggle)
pgapack 1.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,344 kB
  • ctags: 1,786
  • sloc: ansic: 10,331; fortran: 2,985; sh: 486; makefile: 462; perl: 105
file content (859 lines) | stat: -rw-r--r-- 30,896 bytes parent folder | download | duplicates (3)
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
/*
COPYRIGHT

The following is a notice of limited availability of the code, and disclaimer
which must be included in the prologue of the code and in all source listings
of the code.

(C) COPYRIGHT 2008 University of Chicago

Permission is hereby granted to use, reproduce, prepare derivative works, and
to redistribute to others. This software was authored by:

D. Levine
Mathematics and Computer Science Division 
Argonne National Laboratory Group

with programming assistance of participants in Argonne National 
Laboratory's SERS program.

GOVERNMENT LICENSE

Portions of this material resulted from work developed under a
U.S. Government Contract and are subject to the following license: the
Government is granted for itself and others acting on its behalf a paid-up,
nonexclusive, irrevocable worldwide license in this computer software to
reproduce, prepare derivative works, and perform publicly and display
publicly.

DISCLAIMER

This computer code material was prepared, in part, as an account of work
sponsored by an agency of the United States Government. Neither the United
States, nor the University of Chicago, nor any of their employees, makes any
warranty express or implied, or assumes any legal liability or responsibility
for the accuracy, completeness, or usefulness of any information, apparatus,
product, or process disclosed, or represents that its use would not infringe
privately owned rights.
*/

/*****************************************************************************
*     FILE: evaluate.c: This file contains routines specific to the evaluation
*                       of the strings.
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
*              Brian P. Walenz
*****************************************************************************/

#include "pgapack.h"

/*U****************************************************************************
   PGASetEvaluation - Set the evaluation function value for a string to a
   specified value.  Also sets the evaulation up to date flag to PGA_TRUE.

   Category: Fitness & Evaluation

   Inputs:
      ctx  - context variable
      p    - string index
      pop  - symbolic constant of the population string p is in
      val  - the (user) evaluation value to assign to string p

   Outputs:
      Sets the evaluation function value of string p and the EvalUpToDate
      flag (to PGA_TRUE) via side effect

   Example:
      Set the evaluation function value of string p in population PGA_NEWPOP
      to 123.456.

      PGAContext *ctx;
      int p;
      :
      PGASetEvaluation(ctx, p, PGA_NEWPOP, 123.456);

****************************************************************************U*/
void PGASetEvaluation ( PGAContext *ctx, int p, int pop, double val )
{
    PGAIndividual *ind;

    PGADebugEntered("PGASetEvaluation");
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGASetEvaluation", "p = ",
                   PGA_INT, (void *) &p );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGASetEvaluation", "pop = ",
                   PGA_INT, (void *) &pop );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGASetEvaluation", "val = ",
                   PGA_DOUBLE, (void *) &val );

    ind               = PGAGetIndividual ( ctx, p, pop );
    ind->evalfunc     = val;
    ind->evaluptodate = PGA_TRUE;

    PGADebugExited("PGASetEvaluation");
}

/*U***************************************************************************
   PGAGetEvaluation - returns the evaluation function value for
   string p in population pop

   Category: Fitness & Evaluation

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

   Outputs:
      The evaluation function value for string p in population pop

   Example:
      PGAContext *ctx;
      int p;
      double eval;
      :
      eval = PGAGetEvaluation(ctx, p, PGA_NEWPOP);

***************************************************************************U*/
double PGAGetEvaluation ( PGAContext *ctx, int p, int pop )
{
    PGAIndividual *ind;

    PGADebugEntered("PGAGetEvaluation");
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetEvaluation", "p = ",
                   PGA_INT, (void *) &p );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetEvaluation", "pop = ",
                   PGA_INT, (void *) &pop );

    ind               = PGAGetIndividual ( ctx, p, pop );

#ifndef OPTIMIZE
    if (ind->evaluptodate != PGA_TRUE)
	PGAError(ctx, "Evaluation not up to date.  Returning old evaluation.",
                 PGA_WARNING, PGA_VOID, NULL);
#endif

    PGADebugExited("PGAGetEvaluation");
    return(ind->evalfunc);
}

/*U****************************************************************************
  PGASetEvaluationUpToDateFlag - sets the flag associated with a
  string to PGA_TRUE or PGA_FLASE to indicate whether the evaluate
  function value is out-of-date or not.  Note that this flag is always
  set to PGA_TRUE when PGASetEvaluation is called.

    Category: Fitness & Evaluation

    Inputs:
      ctx  - context variable
      p    - string index
      pop  - symbolic constant of the population string p is in
      status - boolean for whether up-to-date

   Outputs:
      Sets the EvalUpToDate associated with the evaluation function value of
      string p via side effect

   Example:
      Set the evaluation function flag for string p in population PGA_NEWPOP
      to PGA_FALSE (as might happen after, for example, calling a hill-climbing
      routine that modified this string).

      PGAContext *ctx;
      int p;
      :
      PGASetEvaluationUpToDateFlag(ctx, p, PGA_NEWPOP, PGA_FALSE);

****************************************************************************U*/
void PGASetEvaluationUpToDateFlag ( PGAContext *ctx, int p, int pop,
                                   int status )
{
    PGAIndividual *ind;

    PGADebugEntered("PGASetEvaluationUpToDateFlag");
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGASetEvaluationUpToDateFlag",
                  "p = ", PGA_INT, (void *) &p );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGASetEvaluationUpToDateFlag",
                  "pop = ", PGA_INT, (void *) &pop );

    ind = PGAGetIndividual ( ctx, p, pop );

    switch(status) {
    case PGA_TRUE:
    case PGA_FALSE:
      ind->evaluptodate = status;
      break;
    default:
      PGAError(ctx, "PGASetEvaluationUpToDateFlag: Invalid value of status:",
               PGA_FATAL, PGA_INT, (void *) &status);
      break;
    }

    PGADebugExited("PGASetEvaluationUpToDateFlag");
}

/*U***************************************************************************
  PGAGetEvaluationUpToDateFlag - returns true/false to indicate
  whether the evaluate function value is up to date

   Category: Fitness & Evaluation

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

   Outputs:
      Returns PGA_TRUE if the evaluate function value is up to date.
      Otherwise, returns PGA_FALSE

   Example:
      PGAContext *ctx;
      int uptodate;
      :
      uptodate = PGAGetEvaluationUpToDateFlag(ctx);
      switch (uptodate) {
      case PGA_TRUE:
          printf("Evaluation function value current\n");
          break;
      case PGA_FALSE:
          printf("Evaluation function value out-of-date\n");
          break;
      }

***************************************************************************U*/
int PGAGetEvaluationUpToDateFlag ( PGAContext *ctx, int p, int pop )
{
    PGAIndividual *ind;

    PGADebugEntered("PGAGetEvaluationUpToDateFlag");
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetEvaluationUpToDateFlag",
                  "p = ", PGA_INT, (void *) &p );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetEvaluationUpToDateFlag",
                   "p = ", PGA_INT, (void *) &pop );

    ind = PGAGetIndividual ( ctx, p, pop );

    PGADebugExited("PGAGetEvaluationUpToDateFlag");
    return(ind->evaluptodate);
}

/*U****************************************************************************
  PGAGetRealFromBinary - Interpets a binary string as encoding a real value
  and returns the real value it represents.

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in the binary representation
      end   - ending bit position in the binary representation
      lower - lower bound of the interval the real number is defined on
      upper - lower bound of the interval the real number is defined on

  Outputs:
      The real value encoded by the binary string

  Example:
      Decode a real value from the string p in population PGA_NEWPOP.  The
      value to decode lies on the interval [-10,20] and is represented
      using the 20 bits in bit positions 10--29.

      double x;
      :
      x = PGAGetRealFromBinary(ctx, p, PGA_NEWPOP, 10, 29, -10.0, 20.0);

****************************************************************************U*/
double PGAGetRealFromBinary(PGAContext *ctx, int p, int pop, int start,
                            int end, double lower, double upper)
{
     int length, sum;
     double value;

    PGADebugEntered("PGAGetRealFromBinary");
     PGACheckDataType("PGAGetRealFromBinary", PGA_DATATYPE_BINARY);

     length = end - start + 1;

     if (start < 0)
          PGAError(ctx, "PGAGetRealFromBinary: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
	 PGAError(ctx, "PGAGetRealFromBinary: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAGetRealFromBinary: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (lower >= upper)
          PGAError(ctx, "PGAGetRealFromBinary: lower exceeds upper:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &lower);

     sum = PGAGetIntegerFromBinary(ctx, p, pop, start, end);
     value = PGAMapIntegerToReal(ctx, sum, 0,
                                 (length == sizeof(unsigned) * 8 - 1)
                                 ? INT_MAX : (1u << length) - 1, lower, upper);

    PGADebugExited("PGAGetRealFromBinary");

     return(value);
}

/*U****************************************************************************
  PGAGetRealFromGrayCode - interpets a binary reflected Gray code sequence in
  a binary string as encoding a real value and returns the real value it
  represents.

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in the binary representation
      end   - ending bit position in the binary representation
      lower - lower bound of the interval the real number is defined on
      upper - lower bound of the interval the real number is defined on

  Outputs:
      The real value encoded by the binary reflected Gray code sequence

  Example:
      Decode a real value from the string p in population PGA_NEWPOP.  The
      value to decode lies on the interval [-10,20] and is represented
      using the 20 bits in bit positions 10--29.

      double x;
      :
      x = PGAGetRealFromGrayCode(ctx, p, PGA_NEWPOP, 10, 29, -10.0, 20.0);

****************************************************************************U*/
double PGAGetRealFromGrayCode(PGAContext *ctx, int p, int pop, int start,
                                  int end, double lower, double upper)
{
     int length, sum;
     double value;

    PGADebugEntered("PGAGetRealFromGrayCode");
     PGACheckDataType("PGAGetRealFromGrayCode", PGA_DATATYPE_BINARY);

     length = end - start + 1;

     if (start < 0)
          PGAError(ctx, "PGAGetRealFromGrayCode: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAGetRealFromGrayCode: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAGetRealFromGrayCode: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (lower >= upper)
          PGAError(ctx, "PGAGetRealFromGrayCode: lower exceeds upper:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &lower);

     sum = PGAGetIntegerFromGrayCode(ctx, p, pop, start, end);
     value = PGAMapIntegerToReal(ctx, sum, 0,
                                 (length == sizeof(unsigned) * 8 - 1)
                                 ? INT_MAX : (1u << length) - 1, lower, upper);

    PGADebugExited("PGAGetRealFromGrayCode");

     return(value);
}

/*U****************************************************************************
  PGAEncodeRealAsBinary - encodes a real value as a binary string

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in p to encode val in
      end   - ending bit position in p to encode val in
      low   - lower bound of the interval the val is defined on
      high  - lower bound of the interval the val is defined on
      val   - the real number to be represented as a binary string

  Outputs:
      The string is modified by side-effect.

  Example:
      Encode 3.14 from the interval [0,10] in 30 bits in bit positions
      0--29 in string p in population PGA_NEWPOP.

      PGAContext *ctx;
      int p;
      :
      PGAEncodeRealAsBinary(ctx, p, PGA_NEWPOP, 0, 29, 0.0, 10.0, 3.14);

****************************************************************************U*/
void PGAEncodeRealAsBinary(PGAContext *ctx, int p, int pop, int start,
                               int end, double low, double high, double val)
{
     int length, d;

    PGADebugEntered("PGAEncodeRealAsBinary");
     PGACheckDataType("PGAEncodeRealAsBinary", PGA_DATATYPE_BINARY);

     length = end - start + 1;
     if (start < 0)
          PGAError(ctx, "PGAEncodeRealAsBinary: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAEncodeRealAsBinary: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAEncodeRealAsBinary: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (low >= high)
          PGAError(ctx, "PGAEncodeRealAsBinary: low exceeds high:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &low);
     if (val < low || val > high)
          PGAError(ctx, "PGAEncodeRealAsBinary: val outside of bounds:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &val);

     d = PGAMapRealToInteger(ctx, val, low, high, 0,
                             (length == sizeof(unsigned) * 8 - 1)
                             ? INT_MAX : (1u << length) - 1);
     PGAEncodeIntegerAsBinary(ctx, p, pop, start, end, d);

    PGADebugExited("PGAEncodeRealAsBinary");
}

/*U****************************************************************************
  PGAEncodeRealAsGrayCode - encodes a real value as a binary reflected Gray
  code sequence

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in p to encode val in
      end   - ending bit position in p to encode val in
      low   - lower bound of the interval the val is defined on
      high  - lower bound of the interval the val is defined on
      val   - the real number to be represented as a binary string

  Outputs:
      The string is modified by side-effect.

  Example:
      Encode 3.14 from the interval [0,10] in 30 bits in bit positions
      0--29 in string p in population PGA_NEWPOP as a binary reflected Gray
      code sequence.

      PGAContext *ctx;
      int p;
      :
      PGAEncodeRealAsGrayCode(ctx, p, PGA_NEWPOP, 0, 29, 0.0, 10.0, 3.14);

****************************************************************************U*/
void PGAEncodeRealAsGrayCode(PGAContext *ctx, int p, int pop, int start,
                              int end, double low, double high, double val)
{
     int length, d;

    PGADebugEntered("PGAEncodeRealAsGrayCode");
     PGACheckDataType("PGAEncodeRealAsGrayCode", PGA_DATATYPE_BINARY);

     length = end - start + 1;
     if (start < 0)
          PGAError(ctx, "PGAEncodeRealAsGrayCode: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAEncodeRealAsGrayCode: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAEncodeRealAsGrayCode: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (low >= high)
          PGAError(ctx, "PGAEncodeRealAsGrayCode: low exceeds high:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &low);
     if (val < low || val > high)
          PGAError(ctx, "PGAEncodeRealAsGrayCode: val outside of bounds:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &val);

     d = PGAMapRealToInteger(ctx, val, low, high, 0,
                             (length == sizeof(unsigned) * 8 - 1) ? INT_MAX :
                             (1u << length) - 1);
     PGAEncodeIntegerAsGrayCode(ctx, p, pop, start, end, d);

    PGADebugExited("PGAEncodeRealAsGrayCode");
}


/*U****************************************************************************
  PGAGetIntegerFromBinary - interpets a binary string as encoding an integer
  value and returns the integer value it represents.

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in the binary representation
      end   - ending bit position in the binary representation

  Outputs:
      The integer value encoded by the binary string

  Example:
      Get an integer j from bits 10--29 of string p in population PGA_NEWPOP.

      PGAContext *ctx;
      int j, p;
      :
      j = PGAGetIntegerFromBinary(ctx, p, PGA_NEWPOP, 10, 29);

****************************************************************************U*/
int PGAGetIntegerFromBinary(PGAContext *ctx, int p, int pop, int start,
                                 int end)
{
     int length, i, val;
     unsigned power2;

    PGADebugEntered("PGAGetIntegerFromBinary");
     PGACheckDataType("PGAGetIntegerFromBinary", PGA_DATATYPE_BINARY);

     length = end - start + 1;
     if (length > sizeof(int) * 8 - 1)
          PGAError(ctx, "PGAGetIntegerFromBinary: length of bit string "
                   "exceeds sizeof type int:", PGA_FATAL, PGA_INT,
                   (void *) &length);
     if (start < 0)
          PGAError(ctx, "PGAGetIntegerFromBinary: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAGetIntegerFromBinary: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAGetIntegerFromBinary: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);

     val = 0;
     power2 = 1u << (length - 1);
     for (i = start; i <= end; i++)
     {
          if (PGAGetBinaryAllele(ctx, p, pop, i))
               val += power2;
          power2 >>= 1;
     }

    PGADebugExited("PGAGetIntegerFromBinary");

     return(val);
}

/*U****************************************************************************
  PGAGetIntegerFromGrayCode - interpets a binary reflected Gray code sequence
  as encoding an integer value and returns the integer value it represents.

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in the binary representation
      end   - ending bit position in the binary representation

  Outputs:
      The integer value encoded by the binary reflected Gray code sequence

  Example:
      Get an integer j from bits 10--29 of string p in population PGA_NEWPOP.
      The string is encoded in Gray code.

      PGAContext *ctx;
      int j, p;
      :
      j = PGAGetIntegerFromGrayCode(ctx, p, PGA_NEWPOP, 10, 29);

****************************************************************************U*/
int PGAGetIntegerFromGrayCode(PGAContext *ctx, int p, int pop, int start,
                                   int end)
{
     int length, *BitString, i, val;
     unsigned power2;

    PGADebugEntered("PGAGetIntegerFromGrayCode");
     PGACheckDataType("PGAGetIntegerFromGrayCode", PGA_DATATYPE_BINARY);

     length = end - start + 1;
     if (length > sizeof(int) * 8 - 1)
          PGAError(ctx, "PGAGetIntegerFromGrayCode: length of binary string "
                   "exceeds size of type int:", PGA_FATAL, PGA_INT,
                   (void *) &length);
     if (start < 0)
          PGAError(ctx, "PGAGetIntegerFromGrayCode: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAGetIntegerFromGrayCode: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAGetIntegerFromGrayCode: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);

     BitString = (int *) malloc(length * sizeof(int));
     if (!BitString)
          PGAError(ctx, "PGAGetIntegerFromGrayCode: No room for BitString",
                   PGA_FATAL, PGA_VOID, NULL);
     BitString[0] = PGAGetBinaryAllele(ctx, p, pop, start);

     for(i = 1; i < length; i++)
          BitString[i] = BitString[i-1] ^ PGAGetBinaryAllele(ctx, p, pop,
                                                             start + i);
     val = 0;
     power2 = 1u << (length - 1);
     for (i = 0; i < length; i++)
     {
          if (BitString[i])
               val += power2;
          power2 >>= 1;
     }
     free(BitString);

    PGADebugExited("PGAGetIntegerFromGrayCode");
     return(val);
}

/*U****************************************************************************
  PGAEncodeIntegerAsBinary - encodes an integer value as a binary string

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in p to encode val in
      end   - ending bit position in p to encode val in
      val   - the integer value to be represented as a binary string

  Outputs:
      The string is modified by side-effect.

  Example:
      Encode an integer v in 20 bits in bit positions 0--19 in string p
      in population PGA_NEWPOP.

      PGAContext *ctx;
      int v, p;
      :
      PGAEncodeIntegerAsBinary(ctx, p, PGA_NEWPOP, 0, 19, v);

****************************************************************************U*/
void PGAEncodeIntegerAsBinary(PGAContext *ctx, int p, int pop, int start,
                              int end, int val)
{
     int length, i;
     unsigned power2;

    PGADebugEntered("PGAEncodeIntegerAsBinary");
     PGACheckDataType("PGAEncodeIntegerAsBinary", PGA_DATATYPE_BINARY);

     length = end - start + 1;

     if (length > sizeof(int) * 8 - 1)
          PGAError(ctx, "PGAEncodeIntegerAsBinary: length of bit string "
                   "exceeds size of type int:", PGA_FATAL, PGA_INT,
                   (void *) &length);
     if (start < 0)
          PGAError(ctx, "PGAEncodeIntegerAsBinary: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAEncodeIntegerAsBinary: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAEncodeIntegerAsBinary: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if ((val > (1u << length) - 1) && (length != sizeof(int) * 8) - 1)
          PGAError(ctx, "PGAEncodeIntegerAsBinary: Integer too big for string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &val);
     if (val < 0)
          PGAError(ctx, "PGAEncodeIntegerAsBinary: Integer less than zero:",
                   PGA_FATAL, PGA_INT, (void *) & val);

     power2 = 1u << (length - 1);
     for (i = 0; i < length; i++)
     {
          if (val >= power2)
          {
               PGASetBinaryAllele(ctx, p, pop, start + i, 1);
               val -= power2;
          }
          else
               PGASetBinaryAllele(ctx, p, pop, start + i, 0);
          power2 >>= 1;
     }

    PGADebugExited("PGAEncodeIntegerAsBinary");
}

/*U****************************************************************************
  PGAEncodeIntegerAsGrayCode - encodes a real value as a binary reflected
  Gray code sequence

  Category: Fitness & Evaluation

  Inputs:
      ctx   - context variable
      p     - string index
      pop   - symbolic constant of the population the string is in
      start - starting bit position in p to encode val in
      end   - ending bit position in p to encode val in
      val   - the integer value to be represented as a binary reflected
              Gray code sequence

  Outputs:
      The string is modified by side-effect.

  Example:
      Encode an integer v in 20 bits in bit positions  0--19 in string p in
      population PGA_NEWPOP using Gray code.

      PGAContext *ctx;
      int v, p;
      :
      PGAEncodeIntegerAsGrayCode(ctx, p, PGA_NEWPOP, 0, 19, 7);

****************************************************************************U*/
void PGAEncodeIntegerAsGrayCode(PGAContext *ctx, int p, int pop, int start,
                                int end, int val)
{
     int i, *bit, length;
     unsigned power2;

    PGADebugEntered("PGAEncodeIntegerAsGrayCode");
     PGACheckDataType("PGAEncodeIntegerAsGrayCode", PGA_DATATYPE_BINARY);

     length = end - start + 1;

     if (length > sizeof(int) * 8 - 1)
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: length of bit string"
                   "exceeds size of type int:", PGA_FATAL, PGA_INT,
                   (void *) &length);
     if (start < 0)
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: start less than 0:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if (end >= PGAGetStringLength(ctx))
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: end greater than string "
                   "length:", PGA_FATAL, PGA_INT, (void *) &end);
     if (start >= end)
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: start exceeds end:",
                   PGA_FATAL, PGA_INT, (void *) &start);
     if ((val > (1u << length) - 1) && (length != sizeof(int) * 8 - 1))
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: Integer too big for "
                   "string length:", PGA_FATAL, PGA_INT, (void *) &val);
     if (val < 0)
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: Integer less than zero:",
                   PGA_FATAL, PGA_INT, (void *) &val);

     bit = (int *) malloc(length * sizeof(int));
     if (bit == NULL)
          PGAError(ctx, "PGAEncodeIntegerAsGrayCode: No room to allocate bit",
                   PGA_FATAL, PGA_VOID, NULL);
     power2 = 1u << (length - 1);
     for (i = 0; i < length; i++)
     {
          if (val >= power2)
          {
               bit[i] = 1;
               val -= power2;
          }
          else
               bit[i] = 0;
          power2 >>= 1;
     }
     PGASetBinaryAllele(ctx, p, pop, start, bit[0]);
     for(i = 1; i < length; i++)
          PGASetBinaryAllele(ctx, p, pop, start + i, bit[i-1] ^ bit[i]);
     free(bit);

    PGADebugExited("PGAEncodeIntegerAsGrayCode");
}


/*I****************************************************************************
   PGAMapIntegerToReal - Maps the value v defined on [a,b] to r defined on
   [l,u].  In the context of PGAPack [a,b] is the discrete interval
   [0,2^nbits-1] (i.e., the number of bits in a binary string) and [l,u]
   represent the range of possible values of the real number r.

   Inputs:
      ctx      - context variable
      v        - value from original interval (usually the decoded bit string)
      a        - lower bound of integer interval (usually 0)
      b        - upper bound of integer interval (usually 2^nbits-1)
      l        - lower bound of real interval
      u        - upper bound of real interval

   Outputs:
      Scaled value of v defined on [l,u]

   Example:
       Map a five bit (that is, an integer with a range of [0, 31]) integer v
       to a real in the range [0, 3.14].

       PGAContext *ctx;
       double x;
       int v;
       :
       x = PGAMapIntegerToReal(ctx, v, 0, 31, 0.0, 3.14);

****************************************************************************I*/
double PGAMapIntegerToReal (PGAContext *ctx, int v, int a, int b, double l,
                            double u)
{
    PGADebugEntered("PGAMapIntegerToReal");

    PGADebugExited("PGAMapIntegerToReal");

     return((v-a) * (u-l) / (b-a) + l);
}

/*I****************************************************************************
   PGAMapRealToInteger - Maps the value r defined on [l,u] to v defined on
   [a,b].  In the context of PGAPack [a,b] is the discrete interval
   [0,2^nbits-1] (i.e., the number of bits in a binary string) and [l,u]
   represent the range of possible values of the real number r.

   Inputs:
      ctx      - context variable
      r        - real value defined on [l,u]
      l        - lower bound of real interval
      u        - upper bound of real interval
      a        - lower bound of integer interval (usually 0)
      b        - upper bound of integer interval (usually 2^nbits-1)

   Outputs:
      Scaled value of r defined on [a,b]

   Example:
     Map the value r on the interval [0, 3.14] to a five bit integer v.

     PGAContext *ctx;
     double r;
     int v;
     :
     v = PGAMapRealToInteger(ctx, r, 0.0, 3.14, 0, 31);

****************************************************************************I*/
int PGAMapRealToInteger(PGAContext *ctx, double r, double l, double u, int a,
                        int b)
{
    PGADebugEntered("PGAMapRealToInteger");

    PGADebugExited("PGAMapRealToInteger");

     return PGARound(ctx, (b - a) * (r - l) / (u - l) + a);
}