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