2011-08-22 8 views
0

유전자 알고리즘에서 2 포인트 크로스 오버 연산을위한 코드를 작성하려고했습니다. 처음에는 두 개의 무작위 유전자 위치가 선택됩니다. 그 후에, 두 염색체는 genelocation1과 genelocatıon2라고 불리는 난수에 btw 위치하는 유전자를 교환합니다.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] 

내 문제점이있다 : 두 개의 번호가 랜덤하게 생성되어 있기 때문에, 전 [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; 
} 
+0

변수의 의미를 설명 할 수 있습니까? 어머니/아버지는 값 자체인가, 배열의 인덱스인가? ActivityNumber 란 무엇입니까? Sol_list의 구조는 무엇입니까? – Heinzi

+0

어머니와 아버지는 내가 처음에 첫 번째 유전자와 두 번째 유전자로 언급 한 염색체입니다. 나는 "유전자"를 잘못 작성했는데 "염색체"여야합니다. 어쨌든, 활동 수는 유전자의 길이입니다 (이 예제에서 7). Sol_list [] .Chromosome [] 구조체는 이전에 채집 한 것을 저장하는 배열입니다. (예 : Sol_list [1]. 염색체는 첫 번째 유전자, 두 번째 유전자와 같은 이전 집단의 해답입니다) – furkan

+0

내 코드가 이상 할 수도 있습니다. 나는 당신의 코드를 받아들이지 만 처음부터 언급 한 것과 같은 작업을 포함해야합니다. 두 점 크로스 오버 .. 감사합니다 – furkan

답변

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

EDIT2 :

방금 ​​더 나은 방법으로 here을 발견했습니다. 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; 
} 
+0

고마워요,하지만 아무 문제가 내 문제를 해결하지 않습니다. Temp1을 포인터로 정의하면 Temp1 [0] 또는 Temp1 [1]을 사용하여 값을 저장할 수 있습니까? 또는 무엇을 사용해야합니까? – furkan

+0

가변 배열 크기는'gcc'와'g ++'의 확장자로 허용됩니다. – Jason

+0

코드에서와 같이 Temp1 [0] 또는 Temp [1]을 통해 동일한 액세스 권한을가집니다. Temp1의 선언과 삭제 만 다릅니다. temp 배열을 사용하여 접근법을 사용하려면 Jason 상태와 마찬가지로 std :: vector를 사용하는 것이 좋습니다.하지만 임시 배열을 사용하지 않고 직접 요소를 바꾸는 것이 좋습니다. (내 대답의 편집 내용 중 하나 참조) – Heinzi

0

나는 컴파일러로 g++을 사용하면 안됩니다. 그렇다면 배열이 아닌 std::vector을 사용할 수 있습니다. 간단히

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

는 이제 operator[] 구문하지만 "정상적인"배열처럼 처리 할 수 ​​있습니다 않습니다. 또한 포인터에서 delete을 호출하는 것을 잊어 버려서 메모리 누수가 발생할 염려가 없습니다.