Hi everybody, I'm working with Genetic Algorithms and I'm using well-known David Goldberg code, written in C.
However, because of my poor background in programming i'm a bit confused about some statements of this code. I'll copy it here and if anyone can tell me what does it mean i'll appreciate any advice. If you need more information to help me, dont hesitate to ask me for it.
My doubt is in initpop() function with what is defined as /*A fair coin toss*/ . Flip is a function which flips a biased coin with p probability (flip(p)).
There is popsize variable (Size of population) which says the number of individuals of the population. I leave definition of individual structure as well, and what is lchrom and chromsize.
Code:
initpop()
/* Initialize a population at random */
{
int j, j1, k, stop;
unsigned mask = 1;
for(j = 0; j < popsize; j++)
{
for(k = 0; k < chromsize; k++)
{
oldpop[j].chrom[k] = 0;
if(k == (chromsize-1))
stop = lchrom - (k*UINTSIZE);
else
stop = UINTSIZE;
/* A fair coin toss */
for(j1 = 1; j1 <= stop; j1++)
{
oldpop[j].chrom[k] = oldpop[j].chrom[k]<<1;
if(flip(0.5))
oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;
}
}
}
}
------EXTRA DATA-----
Code:
struct individual
{
unsigned *chrom; /* chromosome string for the individual */
double fitness; /* fitness of the individual */
int xsite; /* crossover site at mating */
int parent[2]; /* who the parents of offspring were */
int *utility; /* utility field can be used as pointer to a */
/* dynamically allocated, application-specific data structure */
};
/* define chromosome size in terms of machine bytes, ie */
/* length of chromosome in bits (lchrom)/(bits-per-byte) */
/* chromsize must be known for malloc() of chrom pointer */
chromsize = (lchrom/UINTSIZE);
if(lchrom%UINTSIZE) chromsize++;
#define UINTSIZE (BITS_PER_BYTE*sizeof(unsigned));
/*where BITS_PER_BYTE is 8.*/