File: maxbit.c

package info (click to toggle)
pgapack 1.1.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,556 kB
  • ctags: 1,829
  • sloc: ansic: 10,331; fortran: 2,985; sh: 503; makefile: 466; perl: 105
file content (45 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download | duplicates (8)
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
/*
 *  This is a test program for PGAPack.  The objective is to maximize the
 *  number of 1-bits in a chromosome.
 */

#include <pgapack.h>

double NumberOfSetBits(PGAContext *, int, int);

/*******************************************************************
*                   user main program                              *
*******************************************************************/
int main( int argc, char **argv ) {
    PGAContext *ctx;
    
    ctx = PGACreate(&argc, argv, PGA_DATATYPE_BINARY, 256, PGA_MAXIMIZE);
    PGASetRandomSeed(ctx, 1);
    
    PGASetUp(ctx);
    PGARun(ctx, NumberOfSetBits);
    PGADestroy(ctx);
    
    return(0);
}


/*******************************************************************
*               user defined evaluation function                   *
*   ctx - contex variable                                          *
*   p   - chromosome index in population                           *
*   pop - which population to refer to                             *
*******************************************************************/
double NumberOfSetBits(PGAContext *ctx, int p, int pop) {
    int i, nbits, stringlen;

    stringlen = PGAGetStringLength(ctx);
    
    nbits = 0;
    for ( i=0; i<stringlen; i++ )
	if ( PGAGetBinaryAllele(ctx, p, pop, i) )
	    nbits++;
    
    return((double) nbits);
}