Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class Util {
  5.     public Util() {
  6.         //DO NOT USE!!!
  7.     }
  8.     /**
  9.      * Uses heapsort and sort a population of Individuals by highest fitness.
  10.      * @param population
  11.      */
  12.     public static void sort(ArrayList<Individual> population) {
  13.         int N = population.size();
  14.        
  15.         for (int k = N/2; k > 0; k--)
  16.         downheap(population, k, N);
  17.  
  18.         do {
  19.             Individual T = population.get(0);
  20.             population.set(0,population.get(N - 1));
  21.             population.set(N - 1,T);
  22.            
  23.             N--;
  24.             downheap(population, 1, N);
  25.         }while (N > 1);
  26.     }
  27.    
  28.     private static void downheap(ArrayList<Individual> population, int k, int N) {
  29.         Individual T = population.get(k - 1);
  30.        
  31.         while (k <= N/2) {
  32.             int j = k + k;
  33.             if ((j < N) && (population.get(j - 1).getFitness() > population.get(j).getFitness()))
  34.             j++;
  35.  
  36.             if (T.getFitness() <= population.get(j - 1).getFitness())
  37.             break;
  38.  
  39.             else
  40.             {
  41.                 population.set(k - 1, population.get(j - 1));
  42.                 k = j;
  43.             }
  44.         }
  45.         population.set(k - 1,T);
  46.     }
  47.    
  48.     public static String alphabet() {
  49.         return "AZERTYUIOPQSDFGHJKLMWXCVBNazertyuiopqsdfghjklmwxcvbn ";
  50.     }
  51.    
  52.     public static ArrayList<Individual> initialise(int numberOfIndividuals) {
  53.         Random rand = new Random();
  54.         ArrayList<Individual> population = new ArrayList<Individual>();
  55.        
  56.         for(int i = 0; i< numberOfIndividuals; i++) {
  57.             String chromosome = "";
  58.             for(int j = 0; j < 11; j++) {
  59.                 chromosome += Util.alphabet().charAt(rand.nextInt(Util.alphabet().length()));
  60.             }
  61.             Individual individual = new Individual(chromosome);
  62.             population.add(individual);
  63.         }
  64.         return population;
  65.        
  66.     }
  67.    
  68.     public static ArrayList<Individual> mate (ArrayList<Individual> population, double mutation_rate) {
  69.         Random rand = new Random();
  70.        
  71.         Util.sort(population);
  72.         Individual parent1 = null;
  73.         Individual parent2 = null;
  74.         ArrayList<Individual> newPopulation = new ArrayList<Individual>();
  75.         double highest_fitness = 0;
  76.         for(int i = 0; i< population.size(); i++) {
  77.             if(population.get(i).getFitness() > highest_fitness) {
  78.                 parent1 = population.get(i);
  79.                 parent2 = parent1;
  80.             }
  81.         }
  82.        
  83.         //Random rand2 = new Random();
  84.         for(int i = 0; i < population.size();i++) {
  85.             //Generate a random split point.
  86.             int split_point = rand.nextInt(parent1.getChromosome().length());
  87.             //Merging the two
  88.             Individual newIndividual = new Individual(parent1.getChromosome().substring(0,split_point)+parent2.getChromosome().substring(split_point));
  89.             if(mutation_rate > rand.nextInt(101)) {
  90.                 char[] temp = newIndividual.getChromosome().toCharArray();
  91.                 temp[rand.nextInt(temp.length)] = Util.alphabet().charAt(rand.nextInt(Util.alphabet().length()));
  92.                 String newString = "";
  93.                 for(int j = 0; j <temp.length; j++) {
  94.                     newString += temp[j];
  95.                 }
  96.                 newIndividual = new Individual(newString);
  97.                 newPopulation.add(newIndividual);
  98.             }else {
  99.                 newPopulation.add(newIndividual);
  100.             }
  101.            
  102.         }
  103.         return newPopulation;
  104.        
  105.     }
  106.    
  107.     public static void display(ArrayList<Individual> population) {
  108.         for(int i = 0; i<population.size(); i++) {
  109.             System.out.println(population.get(i).getChromosome() + " -> " + population.get(i).getFitness() + "%");
  110.         }
  111.     }
  112.    
  113.     public static boolean check(ArrayList<Individual> population) {
  114.         for(int i = 0; i<population.size(); i++) {
  115.             if(population.get(i).getChromosome().equals("Hello World")) {
  116.                 return true;
  117.             }
  118.         }
  119.         return false;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement