Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class CycleCrossoverDemo
  5. {
  6.     static int searchParent(char [] p, char c)
  7.     {
  8.         for(int i = 0; i < p.length; i++)
  9.             if(p[i] == c)
  10.                 return i;
  11.         return 0; //won't get here
  12.     }
  13.     static void printArray(char [] a)
  14.     {
  15.         for(int i = 0; i < a.length; i++)
  16.             System.out.print(a[i] + " ");
  17.         System.out.println("");
  18.     }
  19.     static char [] cycleCrossover(char [] p1, char [] p2, int flag)
  20.     {
  21.         char city;
  22.         int index;
  23.         boolean cycle = true;
  24.         char [] c1 = new char [p1.length];
  25.         for(int i = 0; i < c1.length; i++) //step 1: initialise child with x's
  26.             c1[i] = 'X';
  27.            
  28.         //start by taking the first city from parent 1.
  29.         c1[0] = p1[0];
  30.        
  31.         //take next city from parent 2 and put it in the child at that position (i.e. '4' in position 4 in child)
  32.         city = p2[0];
  33.         index = Integer.parseInt(Character.toString(p2[0]))-1;
  34.         c1[index] = city;
  35.        
  36.         //set up cycle loop
  37.         while(cycle == true)
  38.         {
  39.             index = searchParent(p1, city); //search parent 1 for that char and get index
  40.             city = p2[index]; //look at same index in parent 2, get char
  41.             index = searchParent(p1, city); //search parent 1 for that char, get index
  42.             if(c1[index] == 'X') //if not filled in yet - i.e. haven't had a cycle yet, fill it in
  43.                 c1[index] = city;
  44.             else
  45.                 cycle = false; //else, have gone in a cycle and exit while loop
  46.         }
  47.         //fill in rest of child path with path of parent 2
  48.         for(int i = 0; i < c1.length; i++)
  49.             if(flag%2==0)
  50.             {
  51.                 if(c1[i] == 'X' && c1[i] != p2[i])
  52.                     c1[i] = p2[i];         
  53.             }
  54.             else
  55.             {
  56.                 if(c1[i] == 'X' && c1[i] != p1[i])
  57.                     c1[i] = p1[i];     
  58.             }
  59.         return c1;
  60.     }
  61.     public static void main(String [] args)
  62.     {  
  63.         int flag = 1;
  64.         for(int i = 0; i < 100; i++)
  65.         {
  66.             long start = System.currentTimeMillis();
  67.             char [] parent1  = {'1','2','3','4','5','6','7','8','9'};
  68.             char [] parent2 = {'4','1','2','8','7','6','9','3','5'};
  69.             char [] child1;
  70.             child1 = cycleCrossover(parent1, parent2, flag);
  71.             flag++;
  72.             //printArray(child1);
  73.             long end = System.currentTimeMillis();
  74.             long total = end - start;
  75.             System.out.println(total);
  76.         }
  77.         try
  78.         {
  79.             Scanner scan = new Scanner(new File("cctimes.txt"));
  80.             int tot = 0;
  81.             while(scan.hasNextInt())
  82.             {
  83.                 tot = tot + scan.nextInt();
  84.             }
  85.             System.out.println("Total is: " + tot);
  86.         }
  87.         catch(IOException e)
  88.         {}
  89.        
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement