0

遺伝的アルゴリズムで 2 点交差演算のコードを書こうとしています。最初に 2 つのランダムな遺伝子位置が選択されます。その後、2 つの染色体は遺伝子位置 1 と遺伝子位置 2 と呼ばれる乱数の間にある遺伝子を交換します。

for example  First Gene [0.3,0.2,0.4,0,0.1,0.5,0.7]
             Second Gene [0.25,0.6,0.45,0.15,0.80,0.9,0.85]
        rndm    genelocation1=3
           rdnm  gnelocation2 =5
child Gene1 [0.3,0.2,0.4,0.15,0.80,0.5,0.7]
      Gene2 [0.25, 0.6, 0.45, 0, 0.1,0.9,0.85]

私の問題はこれです: 2 つの数値がランダムに生成されるため、array[genelocation2-genelocation1] のような配列を定義できませんでした..どうすれば問題を解決できますか? これが2点交差に関する私のコード全体です。ポインターはおそらく解決策ですが、私はポインターが苦手です。

コードは次のとおりです。

void Xover (int mother,int father)
{
    int tempo;
    int Rndmgenelocation1=(rand()%ActivityNumber);
    int Rndmgenelocation2=(rand()%ActivityNumber);

    if (Rndmgenelocation1>Rndmgenelocation2)//sure that 2>1
    {
        tempo=Rndmgenelocation1;
        Rndmgenelocation1=Rndmgenelocation2;
        Rndmgenelocation2=tempo;
    }

    int size=(Rndmgenelocation2-Rndmgenelocation1);
    int Temp1[size];//this makes an error

    int ppp=Rndmgenelocation1;
    for (int pp=Rndmgenelocation1;pp<Rndmgenelocation2;pp++)
    {
        Temp1[pp]=Sol_list[father].Chromosome[ppp];
        ppp++;
    }
    int pppx=Rndmgenelocation1;
    for (int ppx=Rndmgenelocation1;ppx<Rndmgenelocation2;ppx++)
    {
        Sol_list[father].Chromosome[ppx]=Sol_list[mother].Chromosome[pppx];
        pppx++;
    }
    int ppplx=Rndmgenelocation1;
    for (int pplx=Rndmgenelocation1;pplx<Rndmgenelocation2;pplx++)
    {
        Sol_list[father].Chromosome[pplx]=Temp1[ppplx];
        ppplx++;
    }

    return;
}
4

2 に答える 2

3

スタックに可変サイズの配列を定義することはできません。あなたが使用することができます

int *Temp1=new int[size]

それからあなたは電話することを忘れてはなりません

delete[] Temp1;

あなたの機能の最後に!

編集:

私は以下のコードをテストしませんでしたが、以下はより効率的な (そしてより理解しやすい) 方法であなたが望むことを行うはずです:

#include <algorithm>
void Xover (int mother,int father)
{
    int Rndmgenelocation1=(rand()%ActivityNumber);
    int Rndmgenelocation2=(rand()%ActivityNumber);

    if (Rndmgenelocation1>Rndmgenelocation2)//sure that 2>1
    {
        std::swap(Rndmgenelocation1,Rndmgenelocation2);
    }

    for (int pp=Rndmgenelocation1;pp<Rndmgenelocation2;pp++)
    {
        std::swap(Sol_list[father].Chromosome[pp],Sol_list[mother].Chromosome[pp]);
    }
    return;
}

編集2:

ここで、さらに優れた別の方法を見つけました。STL は、すぐに使用できるクロス オーバー アルゴリズムを実装しています。使用する:

#include <algorithm>
void Xover (int mother,int father)
{
    int Rndmgenelocation1=(rand()%ActivityNumber);
    int Rndmgenelocation2=(rand()%ActivityNumber);

    if (Rndmgenelocation1>Rndmgenelocation2)//sure that 2>1
    {
        std::swap(Rndmgenelocation1,Rndmgenelocation2);
    }

    std::swap_ranges(
        Sol_list[father].Chromosome[Rndmgenelocation1],
        Sol_list[father].Chromosome[Rndmgenelocation2],
        Sol_list[mother].Chromosome[Rndmgenelocation1]
    );

    return;
}
于 2011-08-22T09:59:02.557 に答える
0

g++コンパイラとして使用してはいけないと思います。その場合std::vectorは、配列ではなく a を使用できます。単純に

std::vector<int> array(size);

これで、構文を使用して「通常の」配列のように扱うことができoperator[]ます。deleteまた、ポインタの呼び出し忘れによるメモリ リークの心配もありません。

于 2011-08-22T10:09:54.417 に答える