Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1.  public void crossover(Point parent1 , Point parent2,Population newpopulation)
  2.     {
  3.  
  4.         byte [] array_x1 = toByteArray(parent1.x);
  5.         byte [] array_x2 = toByteArray(parent2.x);
  6.         byte [] array_y1 = toByteArray(parent1.y);
  7.         byte [] array_y2 = toByteArray(parent2.y);
  8.  
  9.         Random rand = new Random();
  10.         int cutpoint = rand.nextInt(array_x1.length)+1;
  11.  
  12.         byte[] arraytmp = new byte[8];
  13.         for(int i = 0; i<array_x1.length;i++)
  14.         {
  15.             if(i<cutpoint) arraytmp[i] = array_x1[i];
  16.             else arraytmp[i] = array_x2[i];
  17.         }
  18.  
  19.         double result_x1 = toDouble(arraytmp);
  20.  
  21.         for(int i = 0; i<array_x1.length;i++)
  22.         {
  23.             if(i<cutpoint) arraytmp[i] = array_x2[i];
  24.             else arraytmp[i] = array_x1[i];
  25.         }
  26.  
  27.         double result_x2 = toDouble(arraytmp);
  28.  
  29.  
  30.  
  31.         for(int i = 0; i<array_x1.length;i++)
  32.         {
  33.             if(i<cutpoint) arraytmp[i] = array_y1[i];
  34.             else arraytmp[i] = array_y2[i];
  35.         }
  36.  
  37.         double result_y1 = toDouble(arraytmp);
  38.  
  39.         for(int i = 0; i<array_x1.length;i++)
  40.         {
  41.             if(i<cutpoint) arraytmp[i] = array_y2[i];
  42.             else arraytmp[i] = array_y1[i];
  43.         }
  44.  
  45.         double result_y2 = toDouble(arraytmp);
  46.  
  47.         newpopulation.points[point_count].x=result_x1;
  48.         newpopulation.points[point_count].y=result_y1;
  49.  
  50.         point_count+=1;
  51.  
  52.         newpopulation.points[point_count].x=result_x2;
  53.         newpopulation.points[point_count].y=result_y2;
  54.  
  55.         point_count+=1;
  56.     }
  57.  
  58.     public Point tournament(Population population)
  59.     {
  60.         Population tournament = new Population(this.tournament_size);
  61.  
  62.         for(int i = 0 ; i< this.tournament_size ; i++)
  63.         {
  64.             int index = (int) (Math.random() * this.population_size);
  65.             tournament.points[i] = population.points[index];
  66.         }
  67.  
  68.  
  69.         Point parent = tournament.getFittest();
  70.  
  71.         return parent;
  72.     }
  73.  
  74.     public void mutation()
  75.     {
  76.         Random ran = new Random();
  77.         double xd = Math.random() * (1);
  78.         if(xd <=0.5) {
  79.             int x = ran.nextInt(this.population_size*2) ;
  80.             double y = -5 + Math.random() * (5 +5);
  81.  
  82.             if(x%2==0) {
  83.                 this.points[x / 2].x = y;
  84.             }else
  85.             {
  86.                 this.points[x / 2].y = y;
  87.             }
  88.         }
  89.     }
  90.  
  91.  
  92.  
  93.     public static byte[] toByteArray(double value) {
  94.         byte[] bytes = new byte[8];
  95.         ByteBuffer.wrap(bytes).putDouble(value);
  96.         return bytes;
  97.     }
  98.  
  99.     public static double toDouble(byte[] bytes) {
  100.         return ByteBuffer.wrap(bytes).getDouble();
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement