0

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;
}
manlio
  • 18,345
  • 14
  • 76
  • 126
furkan
  • 21
  • 1
  • 5
  • Can you explain the meaning of your variables? Is mother/father the value itself or an index to some array? What is ActivityNumber? What's the structure of Sol_list? – Heinzi Aug 22 '11 at 09:47
  • mother and father are chromosomes which i stated in the beginning as first gene and second gene.I wrote "gene" wrongly it should be "chromosome". Anyway, Activity number is the lenght of gene (in this example 7). Sol_list[].Chromosome[] structure is the arrays which i store previous population. (Example: Sol_list[1].Chromosome is a solution from previous population, like first gene, second gene) – furkan Aug 22 '11 at 09:54
  • my code maybe strange to you. I accept your own codes but ıt should include same operation which i stated in the beginning. two point crossover.. thanks – furkan Aug 22 '11 at 09:57

2 Answers2

3

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;
}
Heinzi
  • 5,793
  • 4
  • 40
  • 69
  • Thanks it give no error but doesnot solve my problem. Can i use Temp1[0] or Temp1[1] to store values if i define Temp1 as a pointer? or what should i use – furkan Aug 22 '11 at 10:10
  • Variable array sizes are allowed as an extension in `gcc` and `g++`. – Jason Aug 22 '11 at 10:12
  • You have the same access via Temp1[0] or Temp[1] as in your code. Just the declaration and the deletion of Temp1 are different. If you want to use your approach with the temp array, you could also consider using std::vector, like Jason states in his answer. But I would recommend to you to not use a temp array and directly swap the elements (see one of the edits of my answer) – Heinzi Aug 22 '11 at 10:14
0

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.

Jason
  • 31,834
  • 7
  • 59
  • 78