1

遺伝的アルゴリズム内の突然変異機能に問題があります。自分が間違っていることもよくわかりません。私はこのコードをしばらく見てきましたが、ロジックは正しいと思います。それは私が望む結果を生み出していないだけです。

問題子構造体にあるバイナリ配列を出力するときに、いずれかのビットでミューテーションが発生した場合、本来あるべきものではなく、乱数が変更されます。

例えば

  • 0000000はバイナリ文字列です
  • 2番目のビットで突然変異が発生しました
  • 0001000が結果になります

このセクションはメイン内にあります。

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

これが突然変異関数です

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';
        }
    }
}

このようにメインに出力されています

    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;
     }
4

2 に答える 2

3

この方法では、繁殖率を抑えることはできません。変異したビットを、変異が発生する確率から分離する必要があります。

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

ビットを反転するさらに簡単な方法:

Child1.binary_code[z] ^= 1;
于 2011-01-18T15:19:48.140 に答える
1

これを試して:

void mutation(Parent& Child1,int childnumber)
于 2011-01-18T15:32:17.437 に答える