两点交叉运算

发布于 2024-11-30 21:36:44 字数 1560 浏览 0 评论 0原文

我一直在尝试编写遗传算法中两点交叉操作的代码。首先选择两个随机基因位置。之后,两条染色体交换位于随机数之间的基因,称为基因定位 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]

我的问题是这样的:由于两个数字是随机生成的,我无法定义像 array[genelocation2-genelocation1] 这样的数组。我该如何解决这个问题。这是我关于两点交叉的完整代码。指针也许是一个解决方案,但我不擅长指针。

这是代码:

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

I've been trying to write a code for two point crossover operation in a genetic algorithm. At first two random gene location is selected. After that, two chromosomes swap their genes which are located btw random numbers called genelocation1 and genelocatıon2.

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]

my problem is this: since two numbers are generated randomly, i could not define an array like array[genelocation2-genelocation1].. How can i solve the problem. here is my whole code about two point crossover. pointers maybe a solution but i am not good at pointers.

Here is the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2024-12-07 21:36:44

您不能在堆栈上定义可变大小的数组。
您可以使用

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

edit2:

我刚刚发现 这里另一种更好的方法 - 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;
}

You can't define an array of variable size on the stack.
You could use

int *Temp1=new int[size]

You then must not forget to call

delete[] Temp1;

at the end of your function!

edit:

I didn't test my code below, but the following should do what you want in a more efficient (and more understandable) way:

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

edit2:

I just found here another even better way - the STL implements a ready-to-use cross over algorithm. Use:

#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;
}
倾其所爱 2024-12-07 21:36:44

我猜您一定没有使用 g++ 作为编译器。如果是这样,您可以使用 std::vector 而不是数组。只需执行

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

以下操作即可通过 operator[] 语法将其视为“普通”数组。也不用担心由于忘记对指针调用delete而导致内存泄漏。

I'm guessing you must not be using g++ as your compiler. If so, you can use a std::vector rather than an array. Simply do

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

Now you can treat it like a "normal" array though the operator[] syntax. There's also no concern about memory leaks from forgetting to call delete on a pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文