遺伝的アルゴリズム内の突然変異機能に問題があります。自分が間違っていることもよくわかりません。私はこのコードをしばらく見てきましたが、ロジックは正しいと思います。それは私が望む結果を生み出していないだけです。
問題子構造体にあるバイナリ配列を出力するときに、いずれかのビットでミューテーションが発生した場合、本来あるべきものではなく、乱数が変更されます。
例えば
- 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;
}