Question

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;
}
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top