C ++遺伝的アルゴリズム変異誤差
-
12-10-2019 - |
質問
遺伝的アルゴリズム内の突然変異機能に問題があります。自分が間違っていることもよくわかりません。私はしばらくこのコードを見ましたが、論理は正しいと思います。それは私が望む結果を生み出していません。
問題は、子供の構造体にあるバイナリ配列を出力すると、突然変異がビットのいずれかで発生した場合、乱数が変更され、そうではないものが変更されます。
例えば
- 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;
}
解決
この方法でマルチレートを絞ることはできません。突然変異の確率から変異ビットを分離する必要があります。
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;
他のヒント
これを試して:
void mutation(Parent& Child1,int childnumber)
所属していません StackOverflow