2017-03-26 9 views
0

나는 안드로이드 애플 리케이션에 대한 TSP 문제에 대한 작업.자바 크로스 오버 알고리즘

나는 크로스 오버 알고리즘을 가지고 있으며 더 빠른 알고리즘을 위해 루프 수를 최소화하고 싶습니다. 어떻게 할 수 있습니까? 내가 제대로 GA 크로스 오버를 이해한다면

public static Path crossover(Path dad, Path mom) { 
    //Create new child path 
    Path child = new Path(); 

    //Get start and sub path positions for dads path 
    double startPos = (double) (Math.random() * dad.pathSize()); 
    double endPos = (double) (Math.random() * dad.pathSize()); 

    //Loop and add the sub path from dad to our child 
    for (int i = 0; i < child.pathSize(); i++) { 
     //If our start position is less than the end position 
     if (startPos < endPos && i > startPos && i < endPos) { 
      child.setDestination(i, dad.getDestination(i)); 
     } // if our start position is larger 
     else if (startPos > endPos) { 
      if (!(i < startPos && i > endPos)) { 
       child.setDestination(i, dad.getDestination(i)); 
      } 
     } 
    } 


    // Loop through mom destination path 
    for (int i = 0; i < mom.pathSize(); i++){ 
     // If child doesn't have the destination add it 
     if (!child.containsDestination(mom.getDestination(i))) { 
      // Loop to find a spare position in the child's path 
      for (int j = 0; j < child.pathSize(); j++) { 
       //Spare position found, add destination 
       if (child.getDestination(j) == null) { 
        child.setDestination(j, mom.getDestination(i)); 
        break; 
       } 
      } 
     } 
    } 
    return child; 
} 

답변

0

, 당신은 루프 하나 가 부모로부터 아이를 돌려 사용할 수있다 :

는 코드입니다.

public Chromosomes crossoverChrom(Chromosomes inpChrom1, Chromosomes inpChrom2){ 
     // offspring chromosome has the same size as the target chromosome 
     Chromosomes offspring = new Chromosomes(inpChrom1.getGenes().length); 

     for (int i = 0; i < offspring.getGenes().length;i++){ 
      double randOffspring = Math.random(); 
      //   System.out.println("i_offspring [" + i + "] , randOffspring = " + randOffspring); 
      if(randOffspring <= crossoverRate){ 
       //    System.out.println("gene from chrom 1"); 
       offspring.setGenes(i, inpChrom1.getGenes()[i]); 
      } else { 
       //    System.out.println("gene from chrom 2"); 
       offspring.setGenes(i, inpChrom2.getGenes()[i]); 
      } 
     } 
     //  System.out.println("Offspring  = " + offspring + " | Fitness = " + offspring.getFitness()); 
     //  System.out.println("--------------------------------------------------"); 
     return offspring; 
    } 
:

나의 샘플 코드에서 참조하시기 바랍니다