File: hamming.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 (146 lines) | stat: -rw-r--r-- 4,814 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
/*
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: hamming.c: This file contains the routines that have to do with
*                      Hamming distances.
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle
*****************************************************************************/

#include "pgapack.h"

/*U****************************************************************************
  PGAHammingDistance - Calculates the mean Hamming distance for a population
  of binary strings.  For all other data types returns a value of 0.0 and
  prints a warning message.

  Category: Utility

  Inputs:
      ctx      - context variable
      popindex - symbolic constant of the population for which the
                 Hamming distance is to be calculated
  Output:
      The mean Hamming distance in the population

  Example:
      PGAContext *ctx;
      double hd;
      :
      hd = PGAHammingDistance(ctx, PGA_NEWPOP);

****************************************************************************U*/
double PGAHammingDistance( PGAContext *ctx, int popindex)
{
    int i, j, hd, count=0;
    double avg_hd = 0.;
    PGAIndividual *pop;      /* pointer to appropriate population          */

    PGADebugEntered("PGAHammingDistance");

    switch (popindex) {
    case PGA_OLDPOP:
        pop = ctx->ga.oldpop;
        break;
    case PGA_NEWPOP:
        pop = ctx->ga.newpop;
        break;
    default:
        PGAError( ctx, "PGAHammingDistance: Invalid value of popindex:",
                  PGA_FATAL, PGA_INT, (void *) &popindex );
        break;
    }

    switch (ctx->ga.datatype) {
    case PGA_DATATYPE_BINARY:
        for(i=0; i<ctx->ga.PopSize-1; ++i)
            for ( j = i+1; j<ctx->ga.PopSize; ++j ) {
                count++;
                hd = PGABinaryHammingDistance( ctx,
                                            (pop+i)->chrom, (pop+j)->chrom );
                avg_hd += (double) hd;
            }
        avg_hd /= (double) count;
        break;
    case PGA_DATATYPE_INTEGER:
        avg_hd = 0.0;
        PGAError( ctx,
        "PGAHammingDistance: No Hamming Distance for PGA_DATATYPE_INTEGER ",
                  PGA_WARNING,
                  PGA_DOUBLE,
                  (void *) &avg_hd );
        break;
    case PGA_DATATYPE_REAL:
        avg_hd = 0;
        PGAError( ctx,
        "PGAHammingDistance: No Hamming Distance for PGA_DATATYPE_REAL ",
                  PGA_WARNING,
                  PGA_DOUBLE,
                  (void *) &avg_hd );
        break;
    case PGA_DATATYPE_CHARACTER:
        avg_hd = 0;
        PGAError( ctx,
        "PGAHammingDistance: No Hamming Distance for PGA_DATATYPE_CHARACTER ",
                  PGA_WARNING,
                  PGA_DOUBLE,
                  (void *) &avg_hd );
        break;
    case PGA_DATATYPE_USER:
        avg_hd = 0;
        PGAError( ctx,
        "PGAHammingDistance: No Hamming Distance for PGA_DATATYPE_USER ",
                  PGA_WARNING,
                  PGA_DOUBLE,
                  (void *) &avg_hd );
        break;
    default:
        PGAError( ctx,
                 "PGAHammingDistance: Invalid value of datatype:",
                  PGA_FATAL,
                  PGA_INT,
                  (void *) &(ctx->ga.datatype) );
        break;
    }

    PGADebugExited("PGAHammingDistance");

    return(avg_hd);
}