Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,978 people online right now. Registration is fast and FREE... Join Now!




Genetic Algorithm

 
Reply to this topicStart new topic

Genetic Algorithm

M.A.H
15 Jan, 2008 - 06:01 PM
Post #1

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 2

Hi,
I am making a GA program where i have to include encoding, selection, crossover & mutation. I am on the last part which is the crossover & mutation and I am stuck. I need to implement a single point crossover on each pair of parents in the parent population. So at a random point in the chromosome swap the trailing binary bits. The parent were selected by using a roullete wheel.

How would you do a crossover if you got binarys for example:

Chromosome A: 0010011
Chromosome B: 0110101

And I want to crossover from ][

Chromosome A: 0010][011
Chromosome B: 0110][101

So that I get:

New Chromosome A: 0010101
New Chromosome B: 0110011

And also I need to implement a mutation operator on the post crossover population. With a probability of 1 in 100 to examine each bit and decide if it swaps its state and change the state where necessary.

Heres my code for the roulette wheel and crossover & mutation.
CODE

//------------------------------------------------------------------------------
//Roulette_Wheel
void roulette_wheel(int fitness [population], char chromosome[population][chromelength],
char parent[population][chromelength])
{
  int i;
  int j;
  int k;
  int max_fitness;
  max_fitness=0;
  cout<<endl;
  int random;
  int running_total[population];

    for (i=0; i<population; i++)
     {
       max_fitness = fitness[i] + max_fitness;
        running_total[i] = max_fitness;
       cout<<"Running Total ["<<i<<"]  "<<running_total[i]<<endl;
     }

          cout<<endl;
       cout<<"Max Fitness = "<<max_fitness<<endl;
       cout<<endl;

       for (i=0; i<population; i++)
          {
           cout<<endl;
           random = My_rand_no(max_fitness);
           cout<<"Random Number = "<<random<<endl;
                for (j=0; j < population; j++)
                {
                  if (random < running_total[j])
                   {
                       cout<<"Chromosome    = "<<running_total[j]<<endl;
                        for (k=0; k<chromelength; k++)
                      {
                              parent[i][k]= chromosome[j][k];
                              cout<<"Parent ["<<i<<"]         = "<<parent[i][k]<<endl;
                      }
                          j = population + 1;
                  }
                   }
              }
}
//------------------------------------------------------------------------------
//Cross Over & Mutation
/*Loop1
    get 2 chromosomes from parent[i]
    chromsome a = 10010  18
    chromosme b = 10111  23

    randomly select crossover point
    chromsome a = 100!10
    chromosme b = 101!11
Loop2
    then crossover the existing chromsome into the new chromosomes
    New Chromosome A = 10011   19
    New Chromosome B = 10110   22
*/
void crossover (char chromosome[population][chromelength],
char parent[population][chromelength]);

int parent[population][chromelength];
{
cout<<parent[i][k]<<endl;
}



I havent started coding it yet. How would I start coding it?

Thanks
User is offlineProfile CardPM
+Quote Post

mmakrzem
RE: Genetic Algorithm
17 Jan, 2008 - 06:24 AM
Post #2

New D.I.C Head
*

Joined: 11 Jan, 2008
Posts: 27


My Contributions
Hey! I created a genetic algorithm that predicts friction in a mechanical system for my masters degree. This is right up my alley!

What I did was the following. Encode all the chromosomes as plain integer numbers. If you want to know the value of each bit you can use bitwise operations to get their values.

when you want to do a cross over, mask your integer. For example:

CODE

int iChrom1 = 101010
int iChrom2 = 110110

//pick cross over point
int iChosenPoint = 3;
int iMask = 1; // 000001
iMask = iMask<<iChosenPoint; //  001000
iMask = iMask - 1; //mask is now 000111

//get tail end
int iChrom1_end = iChrom1 & iMask; //000010
int iChrom2_end = iChrom2 & iMask; //000110

//clear old tails
int iMask2 = ~iMask; // 111000
iChrom1 = iChrom1 & iMask2; //101000
iChrom2 = iChrom2 & iMask2; //110000

//swap tails
iChrom1 = iChrom1 | iChrom2_end; //101110
iChrom2 = iChrom2 | iChrom1_end; //110010



Let me know how that works for you

User is offlineProfile CardPM
+Quote Post

M.A.H
RE: Genetic Algorithm
25 Jan, 2008 - 12:38 PM
Post #3

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 2

Thanks for your reply. I sorted out the crossover. For crossover I implemented a single point crossover on each pair of parents in the parent population. So at a random point the chromosomes swaped the trailing binary bits.

I now need to implement a mutation operator on the post crossover population. How would I start coding this?

If the post is unclear, please say so and I will try elaborate abit more.

Thanks

This post has been edited by M.A.H: 26 Jan, 2008 - 07:45 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month