Advertisement
Guest User

ox2

a guest
Jan 22nd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. public void crossover()
  2.     {
  3.         if(ThreadLocalRandom.current().nextInt(1, 101)<86)
  4.         {
  5.             for(int i=0; i<pop.size(); i++)
  6.             {
  7.                 if(i%2==0)
  8.                 {
  9.                     Individual tmp1=ordered_crossover(pop.get(i), pop.get(i+1));
  10.                     Individual tmp2=ordered_crossover(pop.get(i+1), pop.get(i));
  11.                     pop.set(i, tmp1);
  12.                     pop.set(i+1, tmp2);
  13.                 }
  14.             }
  15.         }
  16.     }
  17.    
  18.     public static Individual ordered_crossover(Individual a, Individual b)
  19.     {
  20.         //cutpoints
  21.         int c1=0;
  22.         int c2=0;
  23.        
  24.         int gen_size=a.getGenotype().size();
  25.        
  26.         int r1=ThreadLocalRandom.current().nextInt(0, gen_size);
  27.         int r2=ThreadLocalRandom.current().nextInt(0, gen_size);
  28.        
  29.         while(r2==r1)
  30.         {
  31.             r2=ThreadLocalRandom.current().nextInt(0, gen_size);
  32.         }
  33.        
  34.         c1=Math.min(r1, r2);
  35.         c2=Math.max(r1, r2);
  36.    
  37.        
  38.         List<Integer> child_a=new ArrayList<>(a.getGenotype());
  39.                
  40.         for(int i=0; i<gen_size;i++)
  41.         {
  42.             if(i<c1)
  43.             {
  44.                 child_a.set(i, gen_size);
  45.                
  46.             }
  47.             if((i>c1-1)&&(i<c2+1))
  48.             {
  49.                 child_a.set(i, a.getGenotype().get(i));
  50.            
  51.             }
  52.             if(i>c2)
  53.             {
  54.                 child_a.set(i, gen_size);
  55.                
  56.             }
  57.         }
  58.        
  59.            
  60.        
  61.         for(int i=0; i<gen_size;i++)
  62.         {
  63.             if(child_a.get(i)==gen_size)
  64.             {
  65.                 for(int j=0; j<gen_size;j++)
  66.                 {
  67.                     if(child_a.contains(b.getGenotype().get(j))==false)
  68.                     {
  69.                         child_a.set(i, b.getGenotype().get(j));
  70.                         break;
  71.                     }
  72.                 }
  73.             }  
  74.         }
  75.        
  76.        
  77.         while(child_a.contains(52))
  78.         {
  79.             for(int i : b.genotype)
  80.             {
  81.                 if(!child_a.contains(i))
  82.                 {
  83.                     child_a.set(child_a.indexOf(52), i);
  84.                 }
  85.             }
  86.         }
  87.        
  88.         Individual child = new Individual(a, child_a);
  89.         return child;
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement