Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void crossover(Point parent1 , Point parent2,Population newpopulation)
- {
- byte [] array_x1 = toByteArray(parent1.x);
- byte [] array_x2 = toByteArray(parent2.x);
- byte [] array_y1 = toByteArray(parent1.y);
- byte [] array_y2 = toByteArray(parent2.y);
- Random rand = new Random();
- int cutpoint = rand.nextInt(array_x1.length)+1;
- byte[] arraytmp = new byte[8];
- for(int i = 0; i<array_x1.length;i++)
- {
- if(i<cutpoint) arraytmp[i] = array_x1[i];
- else arraytmp[i] = array_x2[i];
- }
- double result_x1 = toDouble(arraytmp);
- for(int i = 0; i<array_x1.length;i++)
- {
- if(i<cutpoint) arraytmp[i] = array_x2[i];
- else arraytmp[i] = array_x1[i];
- }
- double result_x2 = toDouble(arraytmp);
- for(int i = 0; i<array_x1.length;i++)
- {
- if(i<cutpoint) arraytmp[i] = array_y1[i];
- else arraytmp[i] = array_y2[i];
- }
- double result_y1 = toDouble(arraytmp);
- for(int i = 0; i<array_x1.length;i++)
- {
- if(i<cutpoint) arraytmp[i] = array_y2[i];
- else arraytmp[i] = array_y1[i];
- }
- double result_y2 = toDouble(arraytmp);
- newpopulation.points[point_count].x=result_x1;
- newpopulation.points[point_count].y=result_y1;
- point_count+=1;
- newpopulation.points[point_count].x=result_x2;
- newpopulation.points[point_count].y=result_y2;
- point_count+=1;
- }
- public Point tournament(Population population)
- {
- Population tournament = new Population(this.tournament_size);
- for(int i = 0 ; i< this.tournament_size ; i++)
- {
- int index = (int) (Math.random() * this.population_size);
- tournament.points[i] = population.points[index];
- }
- Point parent = tournament.getFittest();
- return parent;
- }
- public void mutation()
- {
- Random ran = new Random();
- double xd = Math.random() * (1);
- if(xd <=0.5) {
- int x = ran.nextInt(this.population_size*2) ;
- double y = -5 + Math.random() * (5 +5);
- if(x%2==0) {
- this.points[x / 2].x = y;
- }else
- {
- this.points[x / 2].y = y;
- }
- }
- }
- public static byte[] toByteArray(double value) {
- byte[] bytes = new byte[8];
- ByteBuffer.wrap(bytes).putDouble(value);
- return bytes;
- }
- public static double toDouble(byte[] bytes) {
- return ByteBuffer.wrap(bytes).getDouble();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement