1

I Have a problem with the mutation function within my genetic Algorithm. I can't quite see what I am doing wrong either. I've looked at this code for a while and I think the logic is correct, it's just not producing the results i want.

The problem When i output the Binary array located in the Child Struct, If mutation has occured on any of the bits, then a random number will be changed, and not the one that should be.

for example

  • 0000000 is the binary string
  • mutation has occured on the second bit
  • 0001000 would be the result

This section is located within the main.

for (int Child = 0; Child < ParentNumberInit; Child++)
{
    cout << endl;
    mutation(child[Child],Child);
}

This is the mutation function

void mutation(struct Parent Child1,int childnumber)
{
    int mutation; // will be the random number generated

    cout << endl << "Child " << (childnumber+1) << endl;

    //loop through every bit in the binary string
    for (int z = 0; z < Binscale; z++)
    {
        mutation = 0;   // set mutation at 0 at the start of every loop
        mutation = rand()%100;      //create a random number

        cout << "Generated number = " << mutation << endl;

        //if variable mutation is smaller, mutation occurs
        if (mutation < MutationRate)
        {
            if(Child1.binary_code[z] == '0')
                Child1.binary_code[z] = '1';
            else if(Child1.binary_code[z] == '1')
                Child1.binary_code[z] = '0';
        }
    }
}

It's being outputted in the main like this

    for (int childnumber = 0; childnumber < ParentNumberInit; childnumber++)
    {
        cout<<"Child "<<(childnumber+1)<<" Binary code = ";
        for (int z = 0; z < Binscale; z ++)
        {
        cout<<child[childnumber].binary_code[z];
        }
        cout<<endl;
     }
  • 2
    What result are you expecting? What result are you getting? – Oliver Charlesworth Jan 18 '11 at 14:58
  • 1
    You should really be using an `std::vector` instead of a string. Also, `mutation = 0` does nothing since you're setting it to a different value in the next line. You're also not modifying anything because you're passing a copy of the `struct Parent` to `mutation`. – Fred Foo Jan 18 '11 at 15:06
  • The mutation rate is around 25( just to make sure it works) I am expecting to see, when i output the array, a change in the bits that matches perfectly with the ones that have been altered in the function. – William Markey Jan 18 '11 at 15:06
  • Where are you outputting the array, within `mutation` or in its caller? – Fred Foo Jan 18 '11 at 15:08
  • 1
    To repeat Oli Charlesworth's question: which bit do you expect to change? I find it hard to believe that anything changes at all, since you're passing `Child1` by value. – Fred Foo Jan 18 '11 at 15:13
  • The bit changed will all depend on the random number generated. As the number is generated for the amount of bits in the chromosome. If the loop is on 3, and the random number generated is less than 25, i want the third bit to change, then when the loop goes to 4, if the random number that is generated is below 25, i want that bit to change too. do you understand? or am i making a hash of this – William Markey Jan 18 '11 at 15:18
  • @William, you aren't, the point is that when you pass in the `Parent` structure to the `mutate()` function, you are passing by value - which means you modify a copy of the object rather than what you have in your array (`child`). Hence it could be happily modifying an instance, but not the one you want. Change, `mutate()` to accept by reference and it should do the trick. – Nim Jan 18 '11 at 15:23
  • thank you all, for the help. :D – William Markey Jan 18 '11 at 15:33

2 Answers2

3

You can't throttle the multation rate this way. You need to separate the mutated bit from the probability of the mutation occuring.

for (int z = 0; z < Binscale; z++)     
{         
    if (rand() % 100 < MutationRate)        
    {
        // flip bit             
        Child1.binary_code[z] += 1; 
        Child1.binary_code[z] %= 2;
    }
} 

Even simpler way to flip bit:

Child1.binary_code[z] ^= 1;
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
1

try this:

void mutation(Parent& Child1,int childnumber)
Nim
  • 33,299
  • 2
  • 62
  • 101