Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. class Evolution {
  2.     private int n;
  3.     private int numberOfFFE = 0;
  4.     private Data data;
  5.     private Utils ut;
  6.     private int bestScore = 10000;
  7.     private int popsize;
  8.     private float crossprob, mutationprob;
  9.     private HashMap<int[], Integer> bufferedScores = new HashMap<>(10000);
  10.     private ArrayList<int[]> generation = new ArrayList<>();
  11.     private int crossIndex;
  12.     private PrintWriter pw;
  13.     private int number;
  14.  
  15.     public Evolution(Data data, int pop_size, float cross_prob, float mutation_prob, int crossIndex){
  16.         this.data = data;
  17.         n = data.getN();
  18.         popsize = pop_size;
  19.         crossprob = cross_prob;
  20.         mutationprob = mutation_prob;
  21.         ut = new Utils();
  22.         this.crossIndex = crossIndex;
  23.     }
  24.  
  25.     void getFirstGeneration(){
  26.         generation = new ArrayList<>();
  27.         int count = popsize;
  28.         while (count > 0) {
  29.             int [] perm = ut.randomPerm(n);
  30.             generation.add(perm);
  31.             int fitness = data.scoreFunction(perm);
  32.             if(fitness < bestScore) bestScore = fitness;
  33.             bufferedScores.put(perm, fitness);
  34.             numberOfFFE++;
  35.             count--;
  36.         }
  37.         printGeneration();
  38.         System.out.println("First generation created");
  39.  
  40.         System.out.println("FFE calls: "+numberOfFFE+" , best score: "+bestScore);
  41.     }
  42.  
  43.     private void evolve(){
  44.         bestScore = Collections.min(bufferedScores.values());
  45.         numberOfFFE = 0;
  46.         for(int i=0; i<popsize/2; i++){
  47.             cross(generation.get(i), generation.get(popsize/2+i));
  48.         }
  49.         pw.print(++number +","+numberOfFFE+","+bestScore+"\n");
  50.  
  51.         System.out.println("Liczba FFE: "+numberOfFFE+", cache size: " + bufferedScores.size()+", best fitness: "+bestScore);
  52.         printGeneration();
  53.     }
  54.  
  55.     private void cross(int[] parent1, int[] parent2) {
  56.  
  57.         HashMap<Integer, int[]> candidates = new HashMap<>();
  58.         candidates.put(data.scoreFunction(parent1), parent1);
  59.         candidates.put(data.scoreFunction(parent2), parent2);
  60.  
  61.         if(Main.n.random(1) < crossprob) {
  62.             StringBuilder sb = new StringBuilder();
  63.             crossIndex = new Random().nextInt(n);
  64.             int[] child1 = new int[parent1.length];
  65.             System.arraycopy(parent1, 0, child1, 0, crossIndex);
  66.             System.arraycopy(parent2, crossIndex, child1, crossIndex, child1.length - crossIndex);
  67.             validate(child1);
  68.             mutate(child1);
  69.             candidates.put(data.scoreFunction(child1), child1);
  70.             sb.append(data.scoreFunction(parent1)).append(" ").append(data.scoreFunction(parent2)).append(" ").append(data.scoreFunction(child1)).append(" ");
  71.             if (!bufferedScores.containsKey(child1)) {
  72.                 int fitness = data.scoreFunction(child1);
  73.                 if(fitness < bestScore) bestScore = fitness;
  74.                 bufferedScores.put(child1, fitness);
  75.                 numberOfFFE++;
  76.             }
  77.  
  78.             int[] child2 = new int[parent1.length];
  79.             System.arraycopy(parent2, 0, child2, 0, crossIndex);
  80.             System.arraycopy(parent1, crossIndex, child2, crossIndex, child2.length - crossIndex);
  81.             validate(child2);
  82.             mutate(child2);
  83.             sb.append(data.scoreFunction(child2));
  84.             if (!bufferedScores.containsKey(child2)) {
  85.                 int fitness = data.scoreFunction(child2);
  86.                 if(fitness < bestScore) bestScore = fitness;
  87.                 bufferedScores.put(child2, data.scoreFunction(child2));
  88.                 numberOfFFE++;
  89.             }
  90.             candidates.put(data.scoreFunction(child2), child2);
  91.  
  92.  
  93.             int bestOfFour = Collections.min(candidates.keySet());
  94.             generation.set(generation.indexOf(parent1), candidates.get(bestOfFour));
  95.             candidates.remove(bestOfFour);
  96.             bestOfFour = Collections.min(candidates.keySet());
  97.             generation.set(generation.indexOf(parent2), candidates.get(bestOfFour));
  98.             System.out.println(sb);
  99.         }
  100.     }
  101.  
  102.     private void printGeneration(){
  103.         for(int[] x : generation){
  104.             for(int z : x) {
  105.                 System.out.print(z+" ");
  106.             }
  107.             System.out.println(" : "+data.scoreFunction(x));
  108.         }
  109.     }
  110.  
  111.     private void mutate(int[] perm) {
  112.         if (Main.n.random(1) < mutationprob) {
  113.             int index1 = Main.n.random(perm.length);
  114.             int index2 = Main.n.random(perm.length);
  115.             int temp = perm[index1];
  116.             perm[index1] = perm[index2];
  117.             perm[index2] = temp;
  118.         }
  119.     }
  120.  
  121.     private void validate(int[] perm){
  122.         ArrayList<Integer> list = new ArrayList<>(ut.getSet(n));
  123.         for (int i=0; i<perm.length; i++) {
  124.             if(list.contains(perm[i])) list.remove(new Integer(perm[i]));
  125.             else {
  126.                 perm[i] = list.get(0);
  127.                 list.remove(0);
  128.             }
  129.         }
  130.     }
  131.  
  132.     void startEvolution(int iterations) {
  133.         try {
  134.             pw = new PrintWriter(new FileWriter("res.txt"));
  135.             pw.print("Generation,FFE calls,Generation best\n");
  136.             while (iterations>0) {
  137.                 evolve();
  138.                 iterations--;
  139.             }
  140.             pw.close();
  141.         } catch (IOException e) {
  142.             e.printStackTrace();
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement