Thread: Understanding Genetic Algorithm Code!!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Understanding Genetic Algorithm Code!!

    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.*/

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you referring to the << and | operations?

    If so, << is shifting the bits to the left by the number of positions indicated after(in your case 1).

    | is the bitwise OR operation.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thanks claudiu for your fast reply. I've already understood how << and | work. The point is to know why shifting the bits to the left or changing bits to 1. But while writing this post reply I realized somehow of the solution to my problem. When flip is true (in the code I guess that being written if (flip(0.5)) is enough to say if flip is true) the coin toss is 1, and in this way u put a 1 in the string (which was initiated with 0 all over the places). However I dont understand how shifting the bits to the left 1 position can help to define a binary string when it was initated as 0-row.

    Then I have 2 more questions.

    1- If the coin toss for loop is function of j1 control variable, why doesnt it appear inside??
    2- According to how it was defined, what is k??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  2. Understanding a 40,000 lines C source code
    By senglaxo in forum C Programming
    Replies: 13
    Last Post: 03-17-2010, 08:40 AM
  3. code understanding
    By elwad in forum C Programming
    Replies: 5
    Last Post: 04-18-2009, 06:57 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM